Skip to content

Commit c54717e

Browse files
Invalidate native expression types after clearstatcache call
1 parent bab0af7 commit c54717e

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/Analyser/MutatingScope.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ public function afterExtractCall(): self
477477
public function afterClearstatcacheCall(): self
478478
{
479479
$expressionTypes = $this->expressionTypes;
480+
$nativeExpressionTypes = $this->nativeExpressionTypes;
480481
foreach (array_keys($expressionTypes) as $exprString) {
481482
// list from https://www.php.net/manual/en/function.clearstatcache.php
482483

@@ -507,16 +508,18 @@ public function afterClearstatcacheCall(): self
507508
}
508509

509510
unset($expressionTypes[$exprString]);
511+
unset($nativeExpressionTypes[$exprString]);
510512
continue 2;
511513
}
512514
}
515+
513516
return $this->scopeFactory->create(
514517
$this->context,
515518
$this->isDeclareStrictTypes(),
516519
$this->getFunction(),
517520
$this->getNamespace(),
518521
$expressionTypes,
519-
$this->nativeExpressionTypes,
522+
$nativeExpressionTypes,
520523
$this->conditionalExpressions,
521524
$this->inClosureBindScopeClasses,
522525
$this->anonymousFunctionReflection,
@@ -533,6 +536,7 @@ public function afterClearstatcacheCall(): self
533536
public function afterOpenSslCall(string $openSslFunctionName): self
534537
{
535538
$expressionTypes = $this->expressionTypes;
539+
$nativeExpressionTypes = $this->nativeExpressionTypes;
536540

537541
if (in_array($openSslFunctionName, [
538542
'openssl_cipher_iv_length',
@@ -590,6 +594,7 @@ public function afterOpenSslCall(string $openSslFunctionName): self
590594
'openssl_x509_verify',
591595
], true)) {
592596
unset($expressionTypes['\openssl_error_string()']);
597+
unset($nativeExpressionTypes['\openssl_error_string()']);
593598
}
594599

595600
return $this->scopeFactory->create(
@@ -598,7 +603,7 @@ public function afterOpenSslCall(string $openSslFunctionName): self
598603
$this->getFunction(),
599604
$this->getNamespace(),
600605
$expressionTypes,
601-
$this->nativeExpressionTypes,
606+
$nativeExpressionTypes,
602607
$this->conditionalExpressions,
603608
$this->inClosureBindScopeClasses,
604609
$this->anonymousFunctionReflection,

tests/PHPStan/Rules/Comparison/NumberComparisonOperatorsConstantConditionRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ public function testBug6467(): void
233233
$this->analyse([__DIR__ . '/data/bug-6467.php'], []);
234234
}
235235

236+
public function testBug11484(): void
237+
{
238+
$this->treatPhpDocTypesAsCertain = false;
239+
$this->analyse([__DIR__ . '/data/bug-11484.php'], []);
240+
}
241+
236242
public function testBug6642(): void
237243
{
238244
$this->treatPhpDocTypesAsCertain = true;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug11484;
4+
5+
class HelloWorld
6+
{
7+
public function sayHello(): void
8+
{
9+
if (filesize("file.txt") > 100) {
10+
file_put_contents("file.txt", str_repeat('aaaaaaa', rand(1,100)));
11+
clearstatcache();
12+
if (filesize("file.txt") > 50) {
13+
14+
}
15+
}
16+
17+
}
18+
}

0 commit comments

Comments
 (0)