diff --git a/src/PHPSemVerChecker/Analyzer/Analyzer.php b/src/PHPSemVerChecker/Analyzer/Analyzer.php index 34e8c3a..fde9112 100644 --- a/src/PHPSemVerChecker/Analyzer/Analyzer.php +++ b/src/PHPSemVerChecker/Analyzer/Analyzer.php @@ -8,6 +8,8 @@ class Analyzer { + /** @var array */ + protected $args = ['apiOnly' => null]; /** * Compare with a destination registry (what the new source code is like). * @@ -21,7 +23,7 @@ public function analyze(Registry $registryBefore, Registry $registryAfter): Repo $analyzers = [ new FunctionAnalyzer(), - new ClassAnalyzer(), + new ClassAnalyzer($this->args), new InterfaceAnalyzer(), new TraitAnalyzer(), ]; @@ -33,4 +35,13 @@ public function analyze(Registry $registryBefore, Registry $registryAfter): Repo return $finalReport; } + + /** + * @param array $args + * @return void + */ + public function setArgs(array $args): void + { + $this->args = $args; + } } diff --git a/src/PHPSemVerChecker/Analyzer/ClassAnalyzer.php b/src/PHPSemVerChecker/Analyzer/ClassAnalyzer.php index ea073c7..c6cfefc 100644 --- a/src/PHPSemVerChecker/Analyzer/ClassAnalyzer.php +++ b/src/PHPSemVerChecker/Analyzer/ClassAnalyzer.php @@ -12,9 +12,13 @@ class ClassAnalyzer { /** - * @var string + * @param string $context + * @param array $args + * @return void */ - protected $context = 'class'; + public function __construct(protected array $args, protected string $context = 'class',) + { + } /** * @param \PHPSemVerChecker\Registry\Registry $registryBefore @@ -68,7 +72,7 @@ public function analyze(Registry $registryBefore, Registry $registryAfter): Repo if ($classBefore != $classAfter) { // Check for case change of class name. // If we entered this section then the normalized names (lowercase) were equal. - if ($classBefore->name !== $classAfter->name) { + if ($classBefore->name->toString() !== $classAfter->name->toString()) { $report->add( $this->context, new ClassCaseChanged( @@ -81,7 +85,7 @@ public function analyze(Registry $registryBefore, Registry $registryAfter): Repo } $analyzers = [ - new ClassMethodAnalyzer('class', $fileBefore, $fileAfter), + new ClassMethodAnalyzer('class', $fileBefore, $fileAfter, $this->args), new PropertyAnalyzer('class', $fileBefore, $fileAfter), ]; diff --git a/src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php b/src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php index 4c0b81d..d4b243d 100644 --- a/src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php +++ b/src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php @@ -23,29 +23,14 @@ class ClassMethodAnalyzer { - /** - * @var string - */ - protected $context; - /** - * @var null|string - */ - protected $fileBefore; - /** - * @var null|string - */ - protected $fileAfter; - /** * @param string $context * @param string|null $fileBefore * @param string|null $fileAfter + * @param array $args */ - public function __construct(string $context, string $fileBefore = null, string $fileAfter = null) + public function __construct(protected string $context, protected ?string $fileBefore, protected ?string $fileAfter, protected array $args) { - $this->context = $context; - $this->fileBefore = $fileBefore; - $this->fileAfter = $fileAfter; } /** @@ -62,12 +47,28 @@ public function analyze(Stmt $contextBefore, Stmt $contextAfter): Report $methodsBeforeKeyed = []; foreach ($methodsBefore as $method) { - $methodsBeforeKeyed[$method->name->toLowerString()] = $method; + if (!$this->args['apiOnly']) { + $methodsBeforeKeyed[$method->name->toLowerString()] = $method; + continue; + } + /** @var \PhpParser\Comment\Doc $comment */ + $comment = $method->getDocComment(); + if ($comment !== null && str_contains($comment->getText(),'@api')) { + $methodsBeforeKeyed[$method->name->toLowerString()] = $method; + } } $methodsAfterKeyed = []; foreach ($methodsAfter as $method) { - $methodsAfterKeyed[$method->name->toLowerString()] = $method; + if (!$this->args['apiOnly']) { + $methodsAfterKeyed[$method->name->toLowerString()] = $method; + continue; + } + /** @var \PhpParser\Comment\Doc $comment */ + $comment = $method->getDocComment(); + if ($comment !== null && str_contains($comment->getText(),'@api')) { + $methodsAfterKeyed[$method->name->toLowerString()] = $method; + } } $methodNamesBefore = array_keys($methodsBeforeKeyed); diff --git a/src/PHPSemVerChecker/Console/Command/CompareCommand.php b/src/PHPSemVerChecker/Console/Command/CompareCommand.php index 0c408ac..22a0a16 100644 --- a/src/PHPSemVerChecker/Console/Command/CompareCommand.php +++ b/src/PHPSemVerChecker/Console/Command/CompareCommand.php @@ -20,7 +20,7 @@ class CompareCommand extends BaseCommand /** * @return void */ - protected function configure() + protected function configure(): void { $this ->setName('compare') @@ -34,7 +34,8 @@ protected function configure() new InputOption('exclude-after', null, InputOption::VALUE_REQUIRED, 'List of paths to exclude (comma separated)'), new InputOption('full-path', null, InputOption::VALUE_NONE, 'Display the full path to the file instead of the relative path'), new InputOption('config', null, InputOption::VALUE_REQUIRED, 'A configuration file to configure php-semver-checker'), - new InputOption('to-json', null, InputOption::VALUE_REQUIRED, 'Output the result to a JSON file') + new InputOption('to-json', null, InputOption::VALUE_REQUIRED, 'Output the result to a JSON file'), + new InputOption('api-only', null, InputOption::VALUE_REQUIRED, 'If true the tool only checks methods annotated with @api') ]); } @@ -72,7 +73,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int $registryBefore = $scannerBefore->getRegistry(); $registryAfter = $scannerAfter->getRegistry(); + $args = ['apiOnly' => $this->config->get('api-only')]; + $analyzer = new Analyzer(); + $analyzer->setArgs($args); $report = $analyzer->analyze($registryBefore, $registryAfter); $reporter = new Reporter($report);