Skip to content
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ These rules forbid:
1. `new DateTimeImmutable()`
1. `new DateTime('yesterday')`
1. `date('Y-m-d')`
1. `time()`
1. `time()`, `microtime()`
1. `strtotime('noon')`

You should instead rely on a clock abstraction like [`lcobucci/clock`](https://github.com/lcobucci/clock).
Expand Down
10 changes: 8 additions & 2 deletions lib/NotNow/NoTimeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if ('time' !== $this->reflectionProvider->resolveFunctionName($node->name, $scope)) {
$functionName = $this->reflectionProvider->resolveFunctionName($node->name, $scope);

if (! \in_array($functionName, ['time', 'microtime'], true)) {
return [];
}

return [RuleErrorBuilder::message('Calling time() directly is forbidden, rely on a clock abstraction like lcobucci/clock')->identifier('timecall.forbidden')->build()];
return [
RuleErrorBuilder::message(
\sprintf('Calling %s() directly is forbidden, rely on a clock abstraction like lcobucci/clock', $functionName)
)
->identifier('timecall.forbidden')->build()];
}
}
8 changes: 8 additions & 0 deletions tests/NotNow/NoTimeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public function testClassConstant(): void
'Calling time() directly is forbidden, rely on a clock abstraction like lcobucci/clock',
7,
],
[
'Calling microtime() directly is forbidden, rely on a clock abstraction like lcobucci/clock',
17,
],
[
'Calling microtime() directly is forbidden, rely on a clock abstraction like lcobucci/clock',
18,
],
]
);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/NotNow/TestAsset/NoTimeRule/fixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@

$string = 'time';
$a5 = $string();

// Not OK
$a1 = \microtime();
$a2 = microtime();

// OK
$a3 = \MyNamespace\microtime();
$a4 = MySubNamespace\microtime();

$string = 'microtime';
$a5 = $string();
Loading