Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeQuality][TypeDeclaration] Handle default value from constructor removed on InlineConstructorDefaultToPropertyRector+TypedPropertyFromStrictConstructorRector #3225

Merged
merged 3 commits into from
Dec 20, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
use PHPStan\Type\ObjectType;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\ValueObject\MethodName;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PostRector\Collector\NodesToRemoveCollector;
use Rector\TypeDeclaration\Matcher\PropertyAssignMatcher;
use Rector\TypeDeclaration\NodeAnalyzer\AutowiredClassMethodOrPropertyAnalyzer;

Expand All @@ -31,7 +33,8 @@ public function __construct(
private readonly PropertyAssignMatcher $propertyAssignMatcher,
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private readonly AutowiredClassMethodOrPropertyAnalyzer $autowiredClassMethodOrPropertyAnalyzer,
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer,
private readonly NodesToRemoveCollector $nodesToRemoveCollector
) {
}

Expand Down Expand Up @@ -64,6 +67,11 @@ public function isPropertyAssigned(ClassLike $classLike, string $propertyName):
return null;
}

$parentNode = $assign->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof Expression && $this->nodesToRemoveCollector->isNodeRemoved($parentNode)) {
return null;
}

$isAssignedInConstructor = true;

return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\InlineTypedFromConstructorDefaultValue\Fixture;

final class Fixture
{
private $url;

public function __construct()
{
$this->url = 'https://website.tld';
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\InlineTypedFromConstructorDefaultValue\Fixture;

final class Fixture
{
private string $url = 'https://website.tld';

public function __construct()
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\InlineTypedFromConstructorDefaultValue;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class InlineTypedFromConstructorDefaultValueTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<array<string>>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
InlineConstructorDefaultToPropertyRector::class,
TypedPropertyFromStrictConstructorRector::class,
]);
};