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
13 changes: 12 additions & 1 deletion packages/Php74/src/Rector/Property/TypedPropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer;
use Rector\TypeDeclaration\VendorLock\VendorLockResolver;
use Rector\ValueObject\PhpVersionFeature;

/**
Expand All @@ -25,9 +26,15 @@ final class TypedPropertyRector extends AbstractRector
*/
private $propertyTypeInferer;

public function __construct(PropertyTypeInferer $propertyTypeInferer)
/**
* @var VendorLockResolver
*/
private $vendorLockResolver;

public function __construct(PropertyTypeInferer $propertyTypeInferer, VendorLockResolver $vendorLockResolver)
{
$this->propertyTypeInferer = $propertyTypeInferer;
$this->vendorLockResolver = $vendorLockResolver;
}

public function getDefinition(): RectorDefinition
Expand Down Expand Up @@ -92,6 +99,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->vendorLockResolver->isPropertyChangeVendorLockedIn($node)) {
return null;
}
Comment thread
ruudk marked this conversation as resolved.

$node->type = $propertyTypeNode;

return $node;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector\Fixture;

use Rector\Php74\Tests\Rector\Property\TypedPropertyRector\Source\SomeParent;

final class Child extends SomeParent
{
/**
* @var string
*/
protected $name = 'child';

/**
* @var string
*/
protected string $typedName = 'child';
}

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

namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector\Fixture;

use Rector\Php74\Tests\Rector\Property\TypedPropertyRector\Source\SomeParent;

abstract class Middle extends SomeParent
{
}

final class Child2 extends Middle
{
/**
* @var string
*/
protected $name = 'child';

/**
* @var string
*/
protected string $typedName = 'child';
}

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

declare(strict_types=1);

namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector\Source;

abstract class SomeParent
{
/**
* @var string
*/
protected $name;

/**
* @var string
*/
protected string $typedName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
final class TypedPropertyRectorTest extends AbstractRectorTestCase
{
/**
* @requires PHP >= 7.4
* @dataProvider provideDataForTest()
*/
public function test(string $file): void
Expand Down
75 changes: 71 additions & 4 deletions packages/TypeDeclaration/src/VendorLock/VendorLockResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

namespace Rector\TypeDeclaration\VendorLock;

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use Rector\NodeContainer\ParsedNodesByType;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\Manipulator\ClassManipulator;
Expand Down Expand Up @@ -41,7 +45,12 @@ public function __construct(

public function isParameterChangeVendorLockedIn(ClassMethod $classMethod, int $paramPosition): bool
{
if (! $this->hasParentClassOrImplementsInterface($classMethod)) {
$classNode = $classMethod->getAttribute(AttributeKey::CLASS_NODE);
if ($classNode === null) {
return false;
}

if (! $this->hasParentClassOrImplementsInterface($classNode)) {
return false;
}

Expand Down Expand Up @@ -102,7 +111,12 @@ public function isParameterChangeVendorLockedIn(ClassMethod $classMethod, int $p

public function isReturnChangeVendorLockedIn(ClassMethod $classMethod): bool
{
if (! $this->hasParentClassOrImplementsInterface($classMethod)) {
$classNode = $classMethod->getAttribute(AttributeKey::CLASS_NODE);
if ($classNode === null) {
return false;
}

if (! $this->hasParentClassOrImplementsInterface($classNode)) {
return false;
}

Expand Down Expand Up @@ -160,13 +174,66 @@ public function isReturnChangeVendorLockedIn(ClassMethod $classMethod): bool
return false;
}

private function hasParentClassOrImplementsInterface(ClassMethod $classMethod): bool
public function isPropertyChangeVendorLockedIn(Property $property): bool
{
$classNode = $classMethod->getAttribute(AttributeKey::CLASS_NODE);
$classNode = $property->getAttribute(AttributeKey::CLASS_NODE);
if ($classNode === null) {
return false;
}

if (! $this->hasParentClassOrImplementsInterface($classNode)) {
return false;
}

$propertyName = $this->nameResolver->getName($property);

// @todo extract to some "inherited parent method" service
/** @var string|null $parentClassName */
$parentClassName = $classNode->getAttribute(AttributeKey::PARENT_CLASS_NAME);

if ($parentClassName !== null) {
$parentClassNode = $this->parsedNodesByType->findClass($parentClassName);
if ($parentClassNode !== null) {
$parentPropertyNode = $this->getProperty($parentClassNode, $propertyName);
// @todo validate type is conflicting
// parent class property in local scope → it's ok
if ($parentPropertyNode !== null) {
return $parentPropertyNode->type !== null;
}

// if not, look for it's parent parent - @todo recursion
}

if (property_exists($parentClassName, $propertyName)) {
// @todo validate type is conflicting
// parent class property in external scope → it's not ok
return true;

// if not, look for it's parent parent - @todo recursion
}
}

return false;
}

// Until we have getProperty (https://github.com/nikic/PHP-Parser/pull/646)
Comment thread
ruudk marked this conversation as resolved.
private function getProperty(ClassLike $classLike, string $name)
{
$lowerName = strtolower($name);
foreach ($classLike->stmts as $stmt) {
if ($stmt instanceof Property) {
foreach ($stmt->props as $prop) {
if ($prop instanceof PropertyProperty && $lowerName === $prop->name->toLowerString()) {
return $stmt;
}
}
}
}
return null;
}

private function hasParentClassOrImplementsInterface(Node $classNode): bool
{
if ($classNode instanceof Class_ || $classNode instanceof Interface_) {
if ($classNode->extends) {
return true;
Expand Down