Skip to content

Commit

Permalink
Dont report unsafe static if constructor is in both parent and interface
Browse files Browse the repository at this point in the history
  • Loading branch information
canvural authored and ondrejmirtes committed Jun 3, 2022
1 parent 164e35e commit 3a7a22d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Rules/Classes/NewStaticRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

foreach ($classReflection->getImmediateInterfaces() as $interface) {
if ($interface->hasConstructor()) {
return [];
}
}

if ($constructor instanceof PhpMethodReflection) {
if ($constructor->isFinal()->yes()) {
return [];
Expand Down
30 changes: 30 additions & 0 deletions tests/PHPStan/Rules/Classes/data/new-static.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,33 @@ public function doBar()
}

}

interface FooInterface
{
public function __construct();
}

interface BarInterface extends FooInterface {}

class VendorFoo
{
public function __construct()
{
}
}

class Foo extends VendorFoo implements FooInterface
{
static function build()
{
return new static();
}
}

class Bar extends VendorFoo implements BarInterface
{
static function build()
{
return new static();
}
}

0 comments on commit 3a7a22d

Please sign in to comment.