From 5cabd210d1dba767b4bfb8ea7aefc061925efe9b Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 17 Dec 2023 17:25:36 +0100 Subject: [PATCH] Implemented reference time parameter (#10) --- README.md | 25 +++++++++++++++++++++++++ extension.neon | 3 +++ src/TodoByRule.php | 11 +++++++++-- tests/TodoByRuleTest.php | 24 +++++++++++++++++++++++- tests/data/referenceTime.php | 6 ++++++ 5 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 tests/data/referenceTime.php diff --git a/README.md b/README.md index a4a3b7c..1275b91 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ examples supported as of version 0.1.5: ## Configuration +### Non-ignorable errors + Errors emitted by the extension are non-ignorable by default, so they cannot accidentally be put into the baseline. You can change this behaviour with a configuration option within your `phpstan.neon`: @@ -58,6 +60,29 @@ parameters: nonIgnorable: false # default is true ``` +### Reference time + +By default comments are checked against todays date. + +You might be interested, which comments will expire e.g. within the next 7 days, which can be configured with the `referenceTime` option. +You need to configure a date parsable by `strtotime`. + +```neon +parameters: + todo_by: + referenceTime: "now+7days" +``` + +It can be especially handy to use a env variable for it, so you can pass the reference date e.g. via the CLI: + +```neon +parameters: + todo_by: + referenceTime: %env.TODOBY_REF_TIME% +``` + +`TODOBY_REF_TIME="now+7days" vendor/bin/phpstan analyze` + ## Installation To use this extension, require it in [Composer](https://getcomposer.org/): diff --git a/extension.neon b/extension.neon index 41d2fea..f1b65d1 100644 --- a/extension.neon +++ b/extension.neon @@ -1,12 +1,14 @@ parametersSchema: todo_by: structure([ nonIgnorable: bool() + referenceTime: string() ]) # default parameters parameters: todo_by: nonIgnorable: true + referenceTime: "now" services: - @@ -14,3 +16,4 @@ services: tags: [phpstan.rules.rule] arguments: - %todo_by.nonIgnorable% + - %todo_by.referenceTime% diff --git a/src/TodoByRule.php b/src/TodoByRule.php index baef15c..0b49a30 100644 --- a/src/TodoByRule.php +++ b/src/TodoByRule.php @@ -7,6 +7,7 @@ use PHPStan\Node\VirtualNode; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleErrorBuilder; +use PHPStan\ShouldNotHappenException; use function preg_match_all; use function strtotime; use function substr_count; @@ -34,9 +35,15 @@ final class TodoByRule implements Rule private int $now; private bool $nonIgnorable; - public function __construct(bool $nonIgnorable) + public function __construct(bool $nonIgnorable, string $referenceTime) { - $this->now = time(); + $time = strtotime($referenceTime); + + if ($time === false) { + throw new \RuntimeException('Unable to parse reference time "' . $referenceTime . '"'); + } + + $this->now = $time; $this->nonIgnorable = $nonIgnorable; } diff --git a/tests/TodoByRuleTest.php b/tests/TodoByRuleTest.php index 76c779d..d202a0b 100644 --- a/tests/TodoByRuleTest.php +++ b/tests/TodoByRuleTest.php @@ -11,13 +11,16 @@ */ final class TodoByRuleTest extends RuleTestCase { + private string $referenceTime; protected function getRule(): Rule { - return new TodoByRule(true); + return new TodoByRule(true, $this->referenceTime); } public function testRule(): void { + $this->referenceTime = "now"; + $this->analyse([__DIR__ . '/data/example.php'], [ [ 'Expired on 2023-12-14: Expired comment1', @@ -105,4 +108,23 @@ public function testRule(): void ], ]); } + + public function testReferenceTime(): void + { + $this->referenceTime = "1st january 2023"; + + $this->analyse([__DIR__ . '/data/referenceTime.php'], []); + } + + public function testReferenceTime2(): void + { + $this->referenceTime = "18th january 2023"; + + $this->analyse([__DIR__ . '/data/referenceTime.php'], [ + [ + 'Expired on 2023-01-14: fix it', + 5, + ], + ]); + } } diff --git a/tests/data/referenceTime.php b/tests/data/referenceTime.php new file mode 100644 index 0000000..a8ebdf0 --- /dev/null +++ b/tests/data/referenceTime.php @@ -0,0 +1,6 @@ +