Skip to content

Commit 772320f

Browse files
authored
Extend NoTimeRule to forbid microtime() calls (#76)
1 parent 15a8db7 commit 772320f

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ These rules forbid:
6363
1. `new DateTimeImmutable()`
6464
1. `new DateTime('yesterday')`
6565
1. `date('Y-m-d')`
66-
1. `time()`
66+
1. `time()`, `microtime()`
6767
1. `strtotime('noon')`
6868

6969
You should instead rely on a clock abstraction like [`lcobucci/clock`](https://github.com/lcobucci/clock).

lib/NotNow/NoTimeRule.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,16 @@ public function processNode(Node $node, Scope $scope): array
3636
return [];
3737
}
3838

39-
if ('time' !== $this->reflectionProvider->resolveFunctionName($node->name, $scope)) {
39+
$functionName = $this->reflectionProvider->resolveFunctionName($node->name, $scope);
40+
41+
if (! \in_array($functionName, ['time', 'microtime'], true)) {
4042
return [];
4143
}
4244

43-
return [RuleErrorBuilder::message('Calling time() directly is forbidden, rely on a clock abstraction like lcobucci/clock')->identifier('timecall.forbidden')->build()];
45+
return [
46+
RuleErrorBuilder::message(
47+
\sprintf('Calling %s() directly is forbidden, rely on a clock abstraction like lcobucci/clock', $functionName)
48+
)
49+
->identifier('timecall.forbidden')->build()];
4450
}
4551
}

tests/NotNow/NoTimeRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public function testClassConstant(): void
3535
'Calling time() directly is forbidden, rely on a clock abstraction like lcobucci/clock',
3636
7,
3737
],
38+
[
39+
'Calling microtime() directly is forbidden, rely on a clock abstraction like lcobucci/clock',
40+
17,
41+
],
42+
[
43+
'Calling microtime() directly is forbidden, rely on a clock abstraction like lcobucci/clock',
44+
18,
45+
],
3846
]
3947
);
4048
}

tests/NotNow/TestAsset/NoTimeRule/fixture.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,14 @@
1212

1313
$string = 'time';
1414
$a5 = $string();
15+
16+
// Not OK
17+
$a1 = \microtime();
18+
$a2 = microtime();
19+
20+
// OK
21+
$a3 = \MyNamespace\microtime();
22+
$a4 = MySubNamespace\microtime();
23+
24+
$string = 'microtime';
25+
$a5 = $string();

0 commit comments

Comments
 (0)