|
5 | 5 | use PHPStan\Analyser\CollectedDataReuse\DescribedClassRule; |
6 | 6 | use PHPStan\Analyser\CollectedDataReuse\EarlyReturnRule; |
7 | 7 | use PHPStan\Analyser\CollectedDataReuse\FirstCollector; |
| 8 | +use PHPStan\Analyser\CollectedDataReuse\NoCollectedDataRule; |
8 | 9 | use PHPStan\Analyser\CollectedDataReuse\OnlyFilesRule; |
9 | 10 | use PHPStan\Analyser\CollectedDataReuse\SecondCollector; |
10 | 11 | use PHPStan\Analyser\CollectedDataReuse\ThrowingRule; |
@@ -118,6 +119,29 @@ public function testOnlyFilesAnalysis(): void |
118 | 119 | $this->assertSame([], $this->getMessages($result)); |
119 | 120 | } |
120 | 121 |
|
| 122 | + public function testRuleThatDoesNotAskForCollectedDataRunsEveryTime(): void |
| 123 | + { |
| 124 | + $file = sys_get_temp_dir() . '/phpstan-collected-data-reuse-' . uniqid() . '.txt'; |
| 125 | + file_put_contents($file, 'one'); |
| 126 | + $collectedData = [ |
| 127 | + self::A => [FirstCollector::class => ['a1']], |
| 128 | + ]; |
| 129 | + |
| 130 | + // tracking a value is not enough, the rule has to ask for collected data |
| 131 | + foreach ([false, true] as $track) { |
| 132 | + $rule = new NoCollectedDataRule($file, $track); |
| 133 | + $result = $this->finalize([$rule], $collectedData, false, []); |
| 134 | + $this->assertSame([], $result->getCollectedDataRuleResults()); |
| 135 | + |
| 136 | + $result = $this->finalize([$rule], $collectedData, false, $result->getCollectedDataRuleResults()); |
| 137 | + $this->assertSame(2, $rule->runs); |
| 138 | + $this->assertSame(0, $result->getReusedRulesCount()); |
| 139 | + $this->assertSame(['Tool says one.'], $this->getMessages($result)); |
| 140 | + } |
| 141 | + |
| 142 | + unlink($file); |
| 143 | + } |
| 144 | + |
121 | 145 | public function testRuleThatThrowsIsNotReused(): void |
122 | 146 | { |
123 | 147 | $rule = new ThrowingRule(); |
|
0 commit comments