Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves #8 - @api aware comparison #181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/PHPSemVerChecker/Analyzer/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class Analyzer
{
/** @var array */
protected $args = ['apiOnly' => null];
/**
* Compare with a destination registry (what the new source code is like).
*
Expand All @@ -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(),
];
Expand All @@ -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;
}
}
12 changes: 8 additions & 4 deletions src/PHPSemVerChecker/Analyzer/ClassAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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),
];

Expand Down
39 changes: 20 additions & 19 deletions src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int,mixed> $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;
}

/**
Expand All @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions src/PHPSemVerChecker/Console/Command/CompareCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CompareCommand extends BaseCommand
/**
* @return void
*/
protected function configure()
protected function configure(): void
{
$this
->setName('compare')
Expand All @@ -34,7 +34,8 @@ protected function configure()
new InputOption('exclude-after', null, InputOption::VALUE_REQUIRED, 'List of paths to exclude <info>(comma separated)</info>'),
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')
]);
}

Expand Down Expand Up @@ -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);
Expand Down