From 829088bc640d58a61ab630769ea8a0aeb7b0b12a Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 22 Dec 2023 16:39:09 +0100 Subject: [PATCH] Fix backtrack limit issues with composer package names (#46) --- src/TodoByPackageVersionRule.php | 3 ++- tests/IntegrationTest.php | 8 ++++---- tests/TodoByPackageVersionRuleTest.php | 5 +++++ tests/data/bug44.php | 6 ++++++ 4 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 tests/data/bug44.php diff --git a/src/TodoByPackageVersionRule.php b/src/TodoByPackageVersionRule.php index de06bf8..8607161 100644 --- a/src/TodoByPackageVersionRule.php +++ b/src/TodoByPackageVersionRule.php @@ -29,13 +29,14 @@ final class TodoByPackageVersionRule implements Rule private const COMPARATORS = ['<', '>', '=']; // composer package-name pattern from https://getcomposer.org/doc/04-schema.md#name + // adjusted because of backtrack limit issues https://github.com/staabm/phpstan-todo-by/issues/44 private const PATTERN = <<<'REGEXP' { @?TODO # possible @ prefix @?[a-zA-Z0-9_-]* # optional username \s*[:-]?\s* # optional colon or hyphen \s+ # keyword/version separator - (?:(?P(php|[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]|-{1,2})?[a-z0-9]+)*)):) # "php" or a composer package name, followed by ":" + (?:(?P(php|[a-z0-9]([_.-]?[a-z0-9]++)*+/[a-z0-9](([_.]|-{1,2})?[a-z0-9]++)*+)):) # "php" or a composer package name, followed by ":" (?P[<>=]?[^\s:\-]+) # version \s*[:-]?\s* # optional colon or hyphen (?P.*) # rest of line as comment text diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index 171e465..97f79cc 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -18,8 +18,8 @@ public function testE2E(): void $errors = $this->runAnalyse(__DIR__ . '/data/e2e.php'); static::assertCount(2, $errors); - $this->assertSame('Expired on 2023-12-14: fix it.', $errors[0]->getMessage()); - $this->assertSame('"php" version requirement ">=7" satisfied.', $errors[1]->getMessage()); + static::assertSame('Expired on 2023-12-14: fix it.', $errors[0]->getMessage()); + static::assertSame('"php" version requirement ">=7" satisfied.', $errors[1]->getMessage()); } public static function getAdditionalConfigFiles(): array @@ -33,7 +33,7 @@ public static function getAdditionalConfigFiles(): array * @param string[]|null $allAnalysedFiles * @return Error[] */ - private function runAnalyse(string $file, ?array $allAnalysedFiles = null): array + private function runAnalyse(string $file, array $allAnalysedFiles = null): array { $file = $this->getFileHelper()->normalizePath($file); /** @var Analyser $analyser */ @@ -43,7 +43,7 @@ private function runAnalyse(string $file, ?array $allAnalysedFiles = null): arra /** @phpstan-ignore-next-line missing bc promise */ $errors = $analyser->analyse([$file], null, null, true, $allAnalysedFiles)->getErrors(); foreach ($errors as $error) { - $this->assertSame($fileHelper->normalizePath($file), $error->getFilePath()); + static::assertSame($fileHelper->normalizePath($file), $error->getFilePath()); } return $errors; diff --git a/tests/TodoByPackageVersionRuleTest.php b/tests/TodoByPackageVersionRuleTest.php index 79e965a..2f1299f 100644 --- a/tests/TodoByPackageVersionRuleTest.php +++ b/tests/TodoByPackageVersionRuleTest.php @@ -78,4 +78,9 @@ public static function provideErrors(): iterable ], ]; } + + public function testBug44(): void + { + $this->analyse([__DIR__ . '/data/bug44.php'], []); + } } diff --git a/tests/data/bug44.php b/tests/data/bug44.php new file mode 100644 index 0000000..f02a75c --- /dev/null +++ b/tests/data/bug44.php @@ -0,0 +1,6 @@ +