diff --git a/README.md b/README.md index 7e9c506..4950109 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/lib/NotNow/NoTimeRule.php b/lib/NotNow/NoTimeRule.php index 8f1d3ca..d866ef5 100644 --- a/lib/NotNow/NoTimeRule.php +++ b/lib/NotNow/NoTimeRule.php @@ -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()]; } } diff --git a/tests/NotNow/NoTimeRuleTest.php b/tests/NotNow/NoTimeRuleTest.php index 1bc57f8..41cf77c 100644 --- a/tests/NotNow/NoTimeRuleTest.php +++ b/tests/NotNow/NoTimeRuleTest.php @@ -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, + ], ] ); } diff --git a/tests/NotNow/TestAsset/NoTimeRule/fixture.php b/tests/NotNow/TestAsset/NoTimeRule/fixture.php index 0c18ce4..5013214 100644 --- a/tests/NotNow/TestAsset/NoTimeRule/fixture.php +++ b/tests/NotNow/TestAsset/NoTimeRule/fixture.php @@ -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();