Skip to content

Commit 4274cfb

Browse files
committed
Check new static() arguments when paired with @phpstan-consistent-constructor
1 parent b6ad10a commit 4274cfb

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/Rules/Classes/InstantiationRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ private function checkClassName(string $class, bool $isName, Node $node, Scope $
9494
&& $constructor instanceof PhpMethodReflection
9595
&& !$constructor->isFinal()->yes()
9696
&& !$constructor->getPrototype()->isAbstract()
97+
&& !$constructor->getDeclaringClass()->hasConsistentConstructor()
9798
) {
9899
return [];
99100
}

tests/PHPStan/Rules/Classes/InstantiationRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,4 +562,14 @@ public function testNamedArgumentsPhpversion(): void
562562
$this->analyse([__DIR__ . '/data/named-arguments-phpversion.php'], []);
563563
}
564564

565+
public function testNewStaticWithConsistentConstructor(): void
566+
{
567+
$this->analyse([__DIR__ . '/data/instantiation-new-static-consistent-constructor.php'], [
568+
[
569+
'Parameter #1 $i of class InstantiationNewStaticConsistentConstructor\Foo constructor expects int, string given.',
570+
18,
571+
],
572+
]);
573+
}
574+
565575
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace InstantiationNewStaticConsistentConstructor;
4+
5+
/**
6+
* @phpstan-consistent-constructor
7+
*/
8+
class Foo
9+
{
10+
11+
public function __construct(int $i)
12+
{
13+
14+
}
15+
16+
public function doFoo(): void
17+
{
18+
$a = new static('s');
19+
}
20+
21+
}

0 commit comments

Comments
 (0)