Passed
Pull Request — master (#33)
by Matthias
10:53 queued 05:39
created

CheckCommand::checkJsonFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 2
nc 1
nop 1
crap 1
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\ParserFactory;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Helper\Table;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
class CheckCommand extends Command
24
{
25 3
    protected function configure()
26
    {
27
        $this
28 3
            ->setName('check')
29 3
            ->setDescription('check the defined dependencies against your code')
30 3
            ->addOption(
31 3
                'config-file',
32 3
                null,
33 3
                InputOption::VALUE_REQUIRED,
34 3
                'the config.json file to configure the checking options'
35
            )
36 3
            ->addArgument(
37 3
                'composer-json',
38 3
                InputArgument::OPTIONAL,
39 3
                'the composer.json of your package, that should be checked',
40 3
                './composer.json'
41
            );
42 3
    }
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $unknownSymbols of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
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
110
    {
111 1
        $fileName = $input->getOption('config-file');
112 1
        if (!$fileName) {
113 1
            return new Options();
114
        }
115
        return new Options((new JsonLoader($fileName))->getData());
116
    }
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)
125
    {
126
        // JsonLoader throws an exception if it cannot load the file
127 1
        new JsonLoader($jsonFile);
128 1
    }
129
}
130