Skip to content

Commit 9cc0922

Browse files
Consider assert as side effect
1 parent 745ae5a commit 9cc0922

8 files changed

+80
-0
lines changed

src/Rules/Functions/CallToFunctionStatementWithoutSideEffectsRule.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public function processNode(Node $node, Scope $scope): array
6565
}
6666

6767
$function = $this->reflectionProvider->getFunction($funcCall->name, $scope);
68+
if (count($function->getAsserts()->getAsserts()) > 0) {
69+
return [];
70+
}
71+
6872
$functionName = $function->getName();
6973
$functionHasSideEffects = !$function->hasSideEffects()->no();
7074

src/Rules/Methods/CallToMethodStatementWithoutSideEffectsRule.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public function processNode(Node $node, Scope $scope): array
6969
}
7070

7171
$method = $calledOnType->getMethod($methodName, $scope);
72+
if (count($method->getAsserts()->getAsserts()) > 0) {
73+
return [];
74+
}
7275

7376
return [
7477
RuleErrorBuilder::message(sprintf(

src/Rules/Methods/CallToStaticMethodStatementWithoutSideEffectsRule.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public function processNode(Node $node, Scope $scope): array
8787
return [];
8888
}
8989

90+
if (count($method->getAsserts()->getAsserts()) > 0) {
91+
return [];
92+
}
93+
9094
$methodResult = $scope->getType($staticCall);
9195
if ($methodResult instanceof NeverType && $methodResult->isExplicit()) {
9296
return [];

tests/PHPStan/Rules/Functions/CallToFunctionStatementWithoutSideEffectsRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ public function testPhpDoc(): void
8585
]);
8686
}
8787

88+
public function testBug12224(): void
89+
{
90+
$this->analyse([__DIR__ . '/data/bug-12224.php'], []);
91+
}
92+
8893
public function testBug4455(): void
8994
{
9095
require_once __DIR__ . '/data/bug-4455.php';
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12224Function;
4+
5+
/**
6+
* @phpstan-pure
7+
* @phpstan-assert string $value
8+
*/
9+
function string(mixed $value): string
10+
{
11+
if (\is_string($value)) {
12+
return $value;
13+
}
14+
throw new \RuntimeException();
15+
}
16+
17+
/** @var string|null $a */
18+
$a = '';
19+
string($a);

tests/PHPStan/Rules/Methods/CallToMethodStatementWithoutSideEffectsRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ public function testBug11503(): void
111111
$this->analyse([__DIR__ . '/data/bug-11503.php'], $errors);
112112
}
113113

114+
public function testBug12224(): void
115+
{
116+
$this->analyse([__DIR__ . '/data/bug-12224.php'], []);
117+
}
118+
114119
public function testFirstClassCallables(): void
115120
{
116121
$this->analyse([__DIR__ . '/data/first-class-callable-method-without-side-effect.php'], [

tests/PHPStan/Rules/Methods/CallToStaticMethodStatementWithoutSideEffectsRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public function testBug4455(): void
7070
$this->analyse([__DIR__ . '/data/bug-4455-static.php'], []);
7171
}
7272

73+
public function testBug12224(): void
74+
{
75+
$this->analyse([__DIR__ . '/data/bug-12224.php'], []);
76+
}
77+
7378
public function testFirstClassCallables(): void
7479
{
7580
$this->analyse([__DIR__ . '/data/first-class-callable-static-method-without-side-effect.php'], [
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12224Method;
4+
5+
class Assert
6+
{
7+
/**
8+
* @phpstan-pure
9+
* @phpstan-assert string $value
10+
*/
11+
public static function staticString(mixed $value): string
12+
{
13+
if (\is_string($value)) {
14+
return $value;
15+
}
16+
throw new \RuntimeException();
17+
}
18+
19+
/**
20+
* @phpstan-pure
21+
* @phpstan-assert string $value
22+
*/
23+
public function string(mixed $value): string
24+
{
25+
if (\is_string($value)) {
26+
return $value;
27+
}
28+
throw new \RuntimeException();
29+
}
30+
}
31+
32+
/** @var string|null $a */
33+
$a = '';
34+
Assert::staticString($a);
35+
(new Assert())->string($a);

0 commit comments

Comments
 (0)