Skip to content

Commit

Permalink
Implemented reference time parameter (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Dec 17, 2023
1 parent 85949ef commit 5cabd21
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:

Expand All @@ -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/):
Expand Down
3 changes: 3 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
parametersSchema:
todo_by: structure([
nonIgnorable: bool()
referenceTime: string()
])

# default parameters
parameters:
todo_by:
nonIgnorable: true
referenceTime: "now"

services:
-
class: staabm\PHPStanTodoBy\TodoByRule
tags: [phpstan.rules.rule]
arguments:
- %todo_by.nonIgnorable%
- %todo_by.referenceTime%
11 changes: 9 additions & 2 deletions src/TodoByRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
24 changes: 23 additions & 1 deletion tests/TodoByRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
],
]);
}
}
6 changes: 6 additions & 0 deletions tests/data/referenceTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

namespace ReferenceTime;

// todo - 2023-01-14 fix it

0 comments on commit 5cabd21

Please sign in to comment.