Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private function isConstructorWithStaticFactory(ClassMethod $classMethod, string
continue;
}

$isStaticSelfFactory = $this->isStaticSelfFactory($iteratedClassMethod);
$isStaticSelfFactory = $this->isStaticNamedConstructor($iteratedClassMethod);

if ($isStaticSelfFactory === false) {
continue;
Expand All @@ -192,8 +192,10 @@ private function isConstructorWithStaticFactory(ClassMethod $classMethod, string
/**
* Looks for:
* public static someMethod() { return new self(); }
* or
* public static someMethod() { return new static(); }
*/
private function isStaticSelfFactory(ClassMethod $classMethod): bool
private function isStaticNamedConstructor(ClassMethod $classMethod): bool
{
if (! $classMethod->isPublic()) {
return false;
Expand All @@ -212,7 +214,15 @@ private function isStaticSelfFactory(ClassMethod $classMethod): bool
return false;
}

return $this->isName($node->expr->class, 'self');
if ($this->isName($node->expr->class, 'self')) {
return true;
}

if ($this->isName($node->expr->class, 'static')) {
return true;
}

return false;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\CodingStyle\Tests\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector\Fixture;

use Rector\CodingStyle\Tests\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector\Source\ParentWithPublicConstructor;

final class SkipSelfCtor extends ParentWithPublicConstructor
{
private function __construct()
{
}

public static function create()
{
return new self();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@

namespace Rector\CodingStyle\Tests\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector\Fixture;

class SkipStaticCtor extends ParentWithPublicConstructor
use Rector\CodingStyle\Tests\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector\Source\ParentWithPublicConstructor;

abstract class SkipStaticCtor extends ParentWithPublicConstructor
{
protected function __construct()
{
// do something basic
}

public static function create()
{
return new self();
return new static();
}
}

class ParentWithPublicConstructor
class InheritedSkipCtor extends SkipStaticCtor
{
public function __construct()
protected function __construct()
{
parent::__construct();

// do something more
}
Comment thread
gnutix marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function test(string $file): void

public function provideDataForTest(): Iterator
{
yield [__DIR__ . '/Fixture/skip_self_ctor.php.inc'];
yield [__DIR__ . '/Fixture/skip_static_ctor.php.inc'];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Rector\CodingStyle\Tests\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector\Source;

class ParentWithPublicConstructor
{
public function __construct()
{
}
}