Skip to content

Commit

Permalink
[TypeDeclaration] Handle multiple methods define __construct later on…
Browse files Browse the repository at this point in the history
… TypedPropertyFromStrictConstructorRector (#2009)
  • Loading branch information
samsonasik committed Apr 5, 2022
1 parent 85ff416 commit e470787
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector\Fixture;

final class MultiMethodsWithDefaultValue
{
/**
* @var string
*/
private $rootView = 'admin.index';

public function run()
{
$this->rootView = 'super_admin.index';
}

public function __construct()
{
$this->rootView = 'super_admin.index';
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector\Fixture;

final class MultiMethodsWithDefaultValue
{
private string $rootView;

public function run()
{
$this->rootView = 'super_admin.index';
}

public function __construct()
{
$this->rootView = 'super_admin.index';
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Rector\Core\NodeManipulator\PropertyManipulator;
use Rector\Core\PhpParser\NodeFinder\PropertyFetchFinder;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
Expand Down Expand Up @@ -138,6 +139,14 @@ private function isDefaultToBeNull(Property $property, Class_ $class): bool

foreach ($propertyFetches as $propertyFetch) {
$classMethod = $this->betterNodeFinder->findParentType($propertyFetch, ClassMethod::class);
if (! $classMethod instanceof ClassMethod) {
continue;
}

if (! $this->nodeNameResolver->isName($classMethod, MethodName::CONSTRUCT)) {
continue;
}

if (! $this->propertyManipulator->isInlineStmtWithConstructMethod($propertyFetch, $classMethod)) {
return false;
}
Expand Down

0 comments on commit e470787

Please sign in to comment.