1 | <?php |
||
23 | class CheckCommand extends Command |
||
24 | { |
||
25 | 3 | protected function configure() |
|
43 | |||
44 | 2 | protected function execute(InputInterface $input, OutputInterface $output): int |
|
45 | { |
||
46 | |||
47 | 2 | if (!$output->isQuiet()) { |
|
48 | 2 | $output->writeln($this->getApplication()->getLongVersion()); |
|
49 | } |
||
50 | |||
51 | 2 | $composerJson = realpath($input->getArgument('composer-json')); |
|
52 | 2 | if (false === $composerJson) { |
|
53 | 1 | throw new \InvalidArgumentException('file not found: [' . $input->getArgument('composer-json') . ']'); |
|
54 | } |
||
55 | 1 | $this->checkJsonFile($composerJson); |
|
56 | |||
57 | 1 | $options = $this->getCheckOptions($input); |
|
58 | |||
59 | 1 | $getPackageSourceFiles = new LocateComposerPackageSourceFiles(); |
|
60 | |||
61 | 1 | $sourcesASTs = new LocateASTFromFiles((new ParserFactory())->create(ParserFactory::PREFER_PHP7)); |
|
62 | |||
63 | 1 | $definedVendorSymbols = (new LocateDefinedSymbolsFromASTRoots())->__invoke($sourcesASTs( |
|
64 | 1 | (new ComposeGenerators())->__invoke( |
|
65 | 1 | $getPackageSourceFiles($composerJson), |
|
66 | 1 | (new LocateComposerPackageDirectDependenciesSourceFiles())->__invoke($composerJson) |
|
67 | ) |
||
68 | )); |
||
69 | |||
70 | 1 | $definedExtensionSymbols = (new LocateDefinedSymbolsFromExtensions())->__invoke( |
|
71 | 1 | (new DefinedExtensionsResolver())->__invoke($composerJson, $options->getPhpCoreExtensions()) |
|
72 | ); |
||
73 | |||
74 | 1 | $usedSymbols = (new LocateUsedSymbolsFromASTRoots()) |
|
75 | 1 | ->__invoke($sourcesASTs($getPackageSourceFiles($composerJson))); |
|
76 | |||
77 | 1 | if (!count($usedSymbols)) { |
|
78 | throw new \LogicException('There were no symbols found, please check your configuration.'); |
||
79 | } |
||
80 | |||
81 | 1 | $unknownSymbols = array_diff( |
|
82 | 1 | $usedSymbols, |
|
83 | 1 | $definedVendorSymbols, |
|
84 | 1 | $definedExtensionSymbols, |
|
85 | 1 | $options->getSymbolWhitelist() |
|
86 | ); |
||
87 | |||
88 | 1 | if (!$unknownSymbols) { |
|
|
|||
89 | 1 | $output->writeln("There were no unknown symbols found."); |
|
90 | 1 | return 0; |
|
91 | } |
||
92 | |||
93 | $output->writeln("The following unknown symbols were found:"); |
||
94 | $table = new Table($output); |
||
95 | $table->setHeaders(['unknown symbol', 'guessed dependency']); |
||
96 | $guesser = new DependencyGuesser(); |
||
97 | foreach ($unknownSymbols as $unknownSymbol) { |
||
98 | $guessedDependencies = []; |
||
99 | foreach ($guesser($unknownSymbol) as $guessedDependency) { |
||
100 | $guessedDependencies[] = $guessedDependency; |
||
101 | } |
||
102 | $table->addRow([$unknownSymbol, implode("\n", $guessedDependencies)]); |
||
103 | } |
||
104 | $table->render(); |
||
105 | |||
106 | return ((int)(bool)$unknownSymbols); |
||
107 | } |
||
108 | |||
109 | 1 | private function getCheckOptions(InputInterface $input): Options |
|
117 | |||
118 | /** |
||
119 | * @param string $jsonFile |
||
120 | * @throws \ComposerRequireChecker\Exception\InvalidJsonException |
||
121 | * @throws \ComposerRequireChecker\Exception\NotReadableException |
||
122 | * @internal param string $composerJson the path to composer.json |
||
123 | */ |
||
124 | 1 | private function checkJsonFile(string $jsonFile) |
|
129 | } |
||
130 |
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.