├── .gitignore ├── README.md ├── composer.json ├── LICENSE ├── src └── FqnChecker.php ├── bin └── fqn-check └── composer.lock /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fqn-check 2 | 3 | Checks source trees for fully qualified function calls and constants. 4 | 5 | ## Alternatives 6 | 7 | This tool is no longer maintained, but there are several alternatives you can use: 8 | - Check out [friendsofphp/php-cs-fixer](https://github.com/friendsofphp/php-cs-fixer) with its `native_function_invocation` option. 9 | - Check out the maintained fork at https://github.com/T-Regx/fqn-check if you just want a small tool to check for fully qualified function calls. 10 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "kelunik/fqn-check", 3 | "description": "Checks source trees for not fully qualified function calls.", 4 | "type": "project", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Niklas Keller", 9 | "email": "me@kelunik.com" 10 | } 11 | ], 12 | "autoload": { 13 | "psr-4": { 14 | "Kelunik\\FqnCheck\\": "src" 15 | } 16 | }, 17 | "bin": [ 18 | "bin/fqn-check" 19 | ], 20 | "require": { 21 | "nikic/php-parser": "^2.0|^3.0|^4.0", 22 | "symfony/finder": "^2.3|^3.0|^4.0" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Niklas Keller 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /src/FqnChecker.php: -------------------------------------------------------------------------------- 1 | reset(); 13 | } 14 | 15 | private function reset() { 16 | $this->findings = []; 17 | } 18 | 19 | public function beforeTraverse(array $nodes) { 20 | $this->reset(); 21 | } 22 | 23 | public function enterNode(Node $node) { 24 | if (!($node instanceof Node\Expr\FuncCall || $node instanceof Node\Expr\ConstFetch)) { 25 | return; 26 | } 27 | 28 | if (!$node->name instanceof Node\Name) { 29 | return; 30 | } 31 | 32 | if ($node->name->isFullyQualified()) { 33 | return; 34 | } 35 | 36 | if (in_array(\strtolower($node->name->toString()), ['true', 'false', 'null'], true)) { 37 | return; 38 | } 39 | 40 | $this->findings[] = [ 41 | "line" => $node->getLine(), 42 | "function" => (string) $node->name, 43 | ]; 44 | } 45 | 46 | public function leaveNode(Node $node) { 47 | return $node; 48 | } 49 | 50 | public function afterTraverse(array $nodes) { 51 | return $nodes; 52 | } 53 | 54 | public function getFindings() { 55 | return $this->findings; 56 | } 57 | } -------------------------------------------------------------------------------- /bin/fqn-check: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | files() 28 | ->in($srcDir) 29 | ->name("*.php"); 30 | 31 | $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); 32 | $nodeTraverser = new NodeTraverser; 33 | 34 | $fqnChecker = new FqnChecker; 35 | $nodeTraverser->addVisitor(new NameResolver); 36 | $nodeTraverser->addVisitor($fqnChecker); 37 | 38 | $errors = 0; 39 | $fileCount = count($files); 40 | 41 | print "Checking {$fileCount} file" . ($fileCount === 1 ? "" : "s") . "…" . PHP_EOL . PHP_EOL; 42 | 43 | foreach ($files as $file) { 44 | /** @var SplFileInfo $file */ 45 | $nodes = $parser->parse($file->getContents()); 46 | $nodeTraverser->traverse($nodes); 47 | 48 | $findings = $fqnChecker->getFindings(); 49 | 50 | foreach ($findings as $finding) { 51 | if ($errors === 0) { 52 | print "Found not fully qualified function calls and/or constants in the following files:" . PHP_EOL; 53 | } 54 | 55 | $errors++; 56 | 57 | print " - " . $file->getRelativePathname() . " on line {$finding["line"]} ({$finding["function"]})" . PHP_EOL; 58 | } 59 | } 60 | 61 | if ($errors === 0) { 62 | print "Everything is fine." . PHP_EOL; 63 | exit(0); 64 | } else { 65 | print PHP_EOL; 66 | print "Found {$errors} not fully qualified function calls and/or constants."; 67 | exit(1); 68 | } 69 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "5dc4d007bbd4922aa4225924d47534bf", 8 | "packages": [ 9 | { 10 | "name": "nikic/php-parser", 11 | "version": "v4.10.0", 12 | "source": { 13 | "type": "git", 14 | "url": "https://github.com/nikic/PHP-Parser.git", 15 | "reference": "1c13d05035deff45f1230ca68bd7d74d621762d9" 16 | }, 17 | "dist": { 18 | "type": "zip", 19 | "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/1c13d05035deff45f1230ca68bd7d74d621762d9", 20 | "reference": "1c13d05035deff45f1230ca68bd7d74d621762d9", 21 | "shasum": "" 22 | }, 23 | "require": { 24 | "ext-tokenizer": "*", 25 | "php": ">=7.0" 26 | }, 27 | "require-dev": { 28 | "ircmaxell/php-yacc": "^0.0.7", 29 | "phpunit/phpunit": "^6.5 || ^7.0 || ^8.0 || ^9.0" 30 | }, 31 | "bin": [ 32 | "bin/php-parse" 33 | ], 34 | "type": "library", 35 | "extra": { 36 | "branch-alias": { 37 | "dev-master": "4.9-dev" 38 | } 39 | }, 40 | "autoload": { 41 | "psr-4": { 42 | "PhpParser\\": "lib/PhpParser" 43 | } 44 | }, 45 | "notification-url": "https://packagist.org/downloads/", 46 | "license": [ 47 | "BSD-3-Clause" 48 | ], 49 | "authors": [ 50 | { 51 | "name": "Nikita Popov" 52 | } 53 | ], 54 | "description": "A PHP parser written in PHP", 55 | "keywords": [ 56 | "parser", 57 | "php" 58 | ], 59 | "time": "2020-09-19T14:52:48+00:00" 60 | }, 61 | { 62 | "name": "symfony/finder", 63 | "version": "v4.4.13", 64 | "source": { 65 | "type": "git", 66 | "url": "https://github.com/symfony/finder.git", 67 | "reference": "2a78590b2c7e3de5c429628457c47541c58db9c7" 68 | }, 69 | "dist": { 70 | "type": "zip", 71 | "url": "https://api.github.com/repos/symfony/finder/zipball/2a78590b2c7e3de5c429628457c47541c58db9c7", 72 | "reference": "2a78590b2c7e3de5c429628457c47541c58db9c7", 73 | "shasum": "" 74 | }, 75 | "require": { 76 | "php": ">=7.1.3" 77 | }, 78 | "type": "library", 79 | "extra": { 80 | "branch-alias": { 81 | "dev-master": "4.4-dev" 82 | } 83 | }, 84 | "autoload": { 85 | "psr-4": { 86 | "Symfony\\Component\\Finder\\": "" 87 | }, 88 | "exclude-from-classmap": [ 89 | "/Tests/" 90 | ] 91 | }, 92 | "notification-url": "https://packagist.org/downloads/", 93 | "license": [ 94 | "MIT" 95 | ], 96 | "authors": [ 97 | { 98 | "name": "Fabien Potencier", 99 | "email": "fabien@symfony.com" 100 | }, 101 | { 102 | "name": "Symfony Community", 103 | "homepage": "https://symfony.com/contributors" 104 | } 105 | ], 106 | "description": "Symfony Finder Component", 107 | "homepage": "https://symfony.com", 108 | "time": "2020-08-17T09:56:45+00:00" 109 | } 110 | ], 111 | "packages-dev": [], 112 | "aliases": [], 113 | "minimum-stability": "stable", 114 | "stability-flags": [], 115 | "prefer-stable": false, 116 | "prefer-lowest": false, 117 | "platform": [], 118 | "platform-dev": [] 119 | } 120 | --------------------------------------------------------------------------------