Skip to content

Commit 3ef76ab

Browse files
Add scenario
1 parent 21bcb53 commit 3ef76ab

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/Rules/Methods/CallToConstructorStatementWithoutSideEffectsRule.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Rules\Rule;
1111
use PHPStan\Rules\RuleErrorBuilder;
1212
use PHPStan\Type\NeverType;
13+
use function count;
1314
use function sprintf;
1415

1516
/**
@@ -57,6 +58,10 @@ public function processNode(Node $node, Scope $scope): array
5758
}
5859

5960
$constructor = $classReflection->getConstructor();
61+
if (count($constructor->getAsserts()->getAsserts()) > 0) {
62+
return [];
63+
}
64+
6065
$methodResult = $scope->getType($instantiation);
6166
if ($methodResult instanceof NeverType && $methodResult->isExplicit()) {
6267
return [];

tests/PHPStan/Rules/Methods/CallToConstructorStatementWithoutSideEffectsRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@ public function testBug4455(): void
5151
$this->analyse([__DIR__ . '/data/bug-4455-constructor.php'], []);
5252
}
5353

54+
public function testBug12224(): void
55+
{
56+
$this->analyse([__DIR__ . '/data/bug-12224.php'], []);
57+
}
58+
5459
}

tests/PHPStan/Rules/Methods/data/bug-12224.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,22 @@ public function string(mixed $value): string
2929
}
3030
}
3131

32+
class AssertConstructor
33+
{
34+
/**
35+
* @phpstan-pure
36+
* @phpstan-assert string $value
37+
*/
38+
public function __construct(mixed $value)
39+
{
40+
if (!\is_string($value)) {
41+
throw new \RuntimeException();
42+
}
43+
}
44+
}
45+
3246
/** @var string|null $a */
3347
$a = '';
3448
Assert::staticString($a);
3549
(new Assert())->string($a);
50+
new AssertConstructor($a);

0 commit comments

Comments
 (0)