| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ComposerRequireChecker\Cli; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ComposerRequireChecker\ASTLocator\LocateASTFromFiles; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use ComposerRequireChecker\DefinedExtensionsResolver\DefinedExtensionsResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use ComposerRequireChecker\DefinedSymbolsLocator\LocateDefinedSymbolsFromASTRoots; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use ComposerRequireChecker\DefinedSymbolsLocator\LocateDefinedSymbolsFromExtensions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use ComposerRequireChecker\DependencyGuesser\DependencyGuesser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use ComposerRequireChecker\FileLocator\LocateComposerPackageDirectDependenciesSourceFiles; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use ComposerRequireChecker\FileLocator\LocateComposerPackageSourceFiles; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use ComposerRequireChecker\GeneratorUtil\ComposeGenerators; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use ComposerRequireChecker\JsonLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use ComposerRequireChecker\UsedSymbolsLocator\LocateUsedSymbolsFromASTRoots; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use PhpParser\ErrorHandler\Collecting as CollectingErrorHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use PhpParser\ParserFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Console\Helper\Table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class CheckCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 3 |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 3 |  |             ->setName('check') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 3 |  |             ->setDescription('check the defined dependencies against your code') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 3 |  |             ->addOption( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 3 |  |                 'config-file', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 3 |  |                 null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 3 |  |                 InputOption::VALUE_REQUIRED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 3 |  |                 'the config.json file to configure the checking options' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 3 |  |             ->addArgument( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 3 |  |                 'composer-json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 3 |  |                 InputArgument::OPTIONAL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 3 |  |                 'the composer.json of your package, that should be checked', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 3 |  |                 './composer.json' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 3 |  |         ->addOption( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 3 |  |                 'ignore-parse-errors', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 3 |  |                 null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 3 |  |                 InputOption::VALUE_NONE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 'this will cause ComposerRequireChecker to ignore errors when files cannot be parsed, otherwise' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 3 |  |                 . ' errors will be thrown' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 3 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 2 |  |     protected function execute(InputInterface $input, OutputInterface $output): int | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 2 |  |         if (!$output->isQuiet()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 2 |  |             $output->writeln($this->getApplication()->getLongVersion()); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 2 |  |         $composerJson = realpath($input->getArgument('composer-json')); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 2 |  |         if (false === $composerJson) { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 1 |  |             throw new \InvalidArgumentException('file not found: [' . $input->getArgument('composer-json') . ']'); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 1 |  |         $this->checkJsonFile($composerJson); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 1 |  |         $options = $this->getCheckOptions($input); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 1 |  |         $getPackageSourceFiles = new LocateComposerPackageSourceFiles(); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 1 |  |         $sourcesASTs = $this->getASTFromFilesLocator($input); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 1 |  |         list($definedVendorSymbols, $additionalFiles) = (new LocateDefinedSymbolsFromASTRoots())->__invoke($sourcesASTs( | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 1 |  |             (new ComposeGenerators())->__invoke( | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 1 |  |                 $getPackageSourceFiles($composerJson), | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 1 |  |                 (new LocateComposerPackageDirectDependenciesSourceFiles())->__invoke($composerJson) | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         )); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         while ($additionalFiles && count($additionalFiles)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 1 |  |             list($vendorSymbols, $additionalFiles) = (new LocateDefinedSymbolsFromASTRoots())->__invoke($sourcesASTs($additionalFiles)); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 1 |  |             $definedVendorSymbols = array_unique(array_merge($vendorSymbols, $definedVendorSymbols)); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 1 |  |         $definedExtensionSymbols = (new LocateDefinedSymbolsFromExtensions())->__invoke( | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 1 |  |             (new DefinedExtensionsResolver())->__invoke($composerJson, $options->getPhpCoreExtensions()) | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         $usedSymbols = (new LocateUsedSymbolsFromASTRoots()) | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |             ->__invoke($sourcesASTs($getPackageSourceFiles($composerJson))); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 1 |  |         if (!count($usedSymbols)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 1 |  |             throw new \LogicException('There were no symbols found, please check your configuration.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 1 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 1 |  |         $unknownSymbols = array_diff( | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |             $usedSymbols, | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |             $definedVendorSymbols, | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 1 |  |             $definedExtensionSymbols, | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 1 |  |             $options->getSymbolWhitelist() | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 1 |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         if (!$unknownSymbols) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             $output->writeln("There were no unknown symbols found."); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             return 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         $output->writeln("The following unknown symbols were found:"); | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         $table = new Table($output); | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         $table->setHeaders(['unknown symbol', 'guessed dependency']); | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |         $guesser = new DependencyGuesser(); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         foreach ($unknownSymbols as $unknownSymbol) { | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |             $guessedDependencies = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |             foreach ($guesser($unknownSymbol) as $guessedDependency) { | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |                 $guessedDependencies[] = $guessedDependency; | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |             $table->addRow([$unknownSymbol, implode("\n", $guessedDependencies)]); | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |         $table->render(); | 
            
                                                                        
                            
            
                                    
            
            
                | 116 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         return ((int)(bool)$unknownSymbols); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 1 |  |     private function getCheckOptions(InputInterface $input): Options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $fileName = $input->getOption('config-file'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         if (!$fileName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             return new Options(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         return new Options((new JsonLoader($fileName))->getData()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @param string $jsonFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 1 |  |      * @throws \ComposerRequireChecker\Exception\InvalidJsonException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @throws \ComposerRequireChecker\Exception\NotReadableException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @internal param string $composerJson the path to composer.json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 1 |  |     private function checkJsonFile(string $jsonFile) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         // JsonLoader throws an exception if it cannot load the file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         new JsonLoader($jsonFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 1 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @param InputInterface $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 1 |  |      * @return LocateASTFromFiles | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 1 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 1 |  |     private function getASTFromFilesLocator(InputInterface $input): LocateASTFromFiles | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $errorHandler = $input->getOption('ignore-parse-errors') ? new CollectingErrorHandler() : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $sourcesASTs = new LocateASTFromFiles((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $errorHandler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         return $sourcesASTs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 152 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 153 |  |  |  | 
            
                        
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.