|
| 1 | +<?php |
| 2 | + |
| 3 | +/** @noinspection TransitiveDependenciesUsageInspection */ |
| 4 | + |
| 5 | +declare(strict_types=1); |
| 6 | + |
| 7 | +namespace voku\PhpDocFixer\CliCommand; |
| 8 | + |
| 9 | +use Symfony\Component\Console\Command\Command; |
| 10 | +use Symfony\Component\Console\Input\InputArgument; |
| 11 | +use Symfony\Component\Console\Input\InputDefinition; |
| 12 | +use Symfony\Component\Console\Input\InputInterface; |
| 13 | +use Symfony\Component\Console\Output\OutputInterface; |
| 14 | + |
| 15 | +final class PhpStanFixerCommand extends Command |
| 16 | +{ |
| 17 | + public const COMMAND_NAME = 'phpstan'; |
| 18 | + |
| 19 | + public function __construct() |
| 20 | + { |
| 21 | + parent::__construct(); |
| 22 | + } |
| 23 | + |
| 24 | + public function configure(): void |
| 25 | + { |
| 26 | + $this |
| 27 | + ->setName(self::COMMAND_NAME) |
| 28 | + ->setDescription('Try to fix types in the phpstan stubs.') |
| 29 | + ->setDefinition( |
| 30 | + new InputDefinition( |
| 31 | + [ |
| 32 | + new InputArgument('path', InputArgument::REQUIRED, 'The path to analyse'), |
| 33 | + ] |
| 34 | + ) |
| 35 | + ) |
| 36 | + ->addOption( |
| 37 | + 'remove-array-value-info', |
| 38 | + null, |
| 39 | + \Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL, |
| 40 | + 'Automatically convert e.g. int[] into array. (false or true)', |
| 41 | + 'false' |
| 42 | + )->addOption( |
| 43 | + 'stubs-path', |
| 44 | + null, |
| 45 | + \Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL, |
| 46 | + 'Overwrite the source of the stubs, by default we use PhpStorm Stubs via composer.', |
| 47 | + '' |
| 48 | + )->addOption( |
| 49 | + 'stubs-file-extension', |
| 50 | + null, |
| 51 | + \Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL, |
| 52 | + 'Overwrite the default file extension for stubs.', |
| 53 | + '.php' |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + public function execute(InputInterface $input, OutputInterface $output): int |
| 58 | + { |
| 59 | + $path = $input->getArgument('path'); |
| 60 | + \assert(\is_string($path)); |
| 61 | + $realPath = \realpath($path); |
| 62 | + \assert(\is_string($realPath)); |
| 63 | + |
| 64 | + $removeArrayValueInfo = $input->getOption('remove-array-value-info') !== 'false'; |
| 65 | + $stubsPath = $input->getOption('stubs-path'); |
| 66 | + $stubsFileExtension = $input->getOption('stubs-file-extension'); |
| 67 | + |
| 68 | + if (!$realPath || !\file_exists($realPath)) { |
| 69 | + $output->writeln('-------------------------------'); |
| 70 | + $output->writeln('The path "' . $path . '" does not exists.'); |
| 71 | + $output->writeln('-------------------------------'); |
| 72 | + |
| 73 | + return 2; |
| 74 | + } |
| 75 | + |
| 76 | + $phpTypesSource = new \voku\PhpDocFixer\PhpStanStubs\PhpStanReader($realPath); |
| 77 | + $phpTypesSourceInfo = $phpTypesSource->parse(); |
| 78 | + |
| 79 | + if (!$stubsPath) { |
| 80 | + $stubsPath = __DIR__ . '/../../../../vendor/jetbrains/phpstorm-stubs/'; |
| 81 | + } |
| 82 | + $phpTypesFromStubs = new \voku\PhpDocFixer\PhpStubs\PhpStubsReader( |
| 83 | + $stubsPath, |
| 84 | + $removeArrayValueInfo, |
| 85 | + $stubsFileExtension |
| 86 | + ); |
| 87 | + $stubsInfo = $phpTypesFromStubs->parse(); |
| 88 | + |
| 89 | + $errors = []; |
| 90 | + foreach ($phpTypesSourceInfo as $functionName_or_classAndMethodName => $types) { |
| 91 | + if (!isset($stubsInfo[$functionName_or_classAndMethodName])) { |
| 92 | + // TODO: error in stubs? |
| 93 | + //\var_dump($functionName_or_classAndMethodName); exit; |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + if ( |
| 98 | + ($stubsInfo[$functionName_or_classAndMethodName]['return'] ?? []) !== ($types['return'] ?? []) |
| 99 | + || |
| 100 | + (array_values($stubsInfo[$functionName_or_classAndMethodName]['params'] ?? [])) !== (array_values($types['params'] ?? [])) |
| 101 | + ) { |
| 102 | + $errors[$functionName_or_classAndMethodName] = [ |
| 103 | + 'phpStubTypes' => $stubsInfo[$functionName_or_classAndMethodName], |
| 104 | + 'phpStanTypes' => $types, |
| 105 | + ]; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + $output->writeln(\count($errors) . ' ' . 'errors found'); |
| 110 | + |
| 111 | + foreach ($errors as $name => $typesArray) { |
| 112 | + $output->writeln('----------------'); |
| 113 | + $output->writeln($name); |
| 114 | + $output->writeln(\print_r($typesArray, true)); |
| 115 | + $output->writeln('----------------'); |
| 116 | + } |
| 117 | + |
| 118 | + return 0; |
| 119 | + } |
| 120 | +} |
0 commit comments