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
2 changes: 0 additions & 2 deletions config/sets/symfony/symfony-constructor-injection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Rector\Symfony\DependencyInjection\Rector\Class_\CommandGetByTypeToConstructorInjectionRector;
use Rector\Symfony\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector;
use Rector\Symfony\DependencyInjection\Rector\Class_\GetBySymfonyStringToConstructorInjectionRector;
use Rector\Symfony\DependencyInjection\Rector\Trait_\TraitGetByTypeToInjectRector;
use Rector\Symfony\Symfony28\Rector\MethodCall\GetToConstructorInjectionRector;
use Rector\Symfony\Symfony34\Rector\Closure\ContainerGetNameToTypeInTestsRector;

Expand All @@ -16,7 +15,6 @@
ControllerGetByTypeToConstructorInjectionRector::class,
CommandGetByTypeToConstructorInjectionRector::class,
GetBySymfonyStringToConstructorInjectionRector::class,
TraitGetByTypeToInjectRector::class,

// legacy rules that require container fetch
ContainerGetNameToTypeInTestsRector::class,
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,19 @@

namespace Rector\Symfony\DependencyInjection\Rector\Trait_;

use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\PropertyItem;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Trait_;
use PHPStan\Type\ObjectType;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Naming\Naming\PropertyNaming;
use Rector\PostRector\ValueObject\PropertyMetadata;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\Symfony\DependencyInjection\NodeFactory\AutowireClassMethodFactory;
use Rector\Symfony\DependencyInjection\ThisGetTypeMatcher;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Symfony\Tests\DependencyInjection\Rector\Trait_\TraitGetByTypeToInjectRector\TraitGetByTypeToInjectRectorTest
* @deprecated A trait has no context about the class it is used in, so the `$this->get()` call cannot be safely resolved. This rule was made for a single custom project and does not generalize.
*/
final class TraitGetByTypeToInjectRector extends AbstractRector
final class TraitGetByTypeToInjectRector extends AbstractRector implements DeprecatedInterface
{
public function __construct(
private readonly PropertyNaming $propertyNaming,
private readonly ThisGetTypeMatcher $thisGetTypeMatcher,
private readonly AutowireClassMethodFactory $autowireClassMethodFactory,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -88,62 +71,9 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$propertyMetadatas = [];

$this->traverseNodesWithCallable($node, function (Node $node) use (&$propertyMetadatas): ?Node {
if (! $node instanceof MethodCall) {
return null;
}

$className = $this->thisGetTypeMatcher->match($node);
if (! is_string($className)) {
return null;
}

$propertyName = $this->propertyNaming->fqnToVariableName($className);
$propertyMetadata = new PropertyMetadata($propertyName, new FullyQualifiedObjectType($className));

$propertyMetadatas[] = $propertyMetadata;
return $this->nodeFactory->createPropertyFetch('this', $propertyMetadata->getName());
});

if ($propertyMetadatas === []) {
return null;
}

// create local properties

$autowiredProperties = $this->createAutowiredProperties($propertyMetadatas);
$autowireClassMethod = $this->autowireClassMethodFactory->create($node, $propertyMetadatas);

$node->stmts = array_merge($autowiredProperties, [$autowireClassMethod], $node->stmts);

return $node;
}

/**
* @param PropertyMetadata[] $propertyMetadatas
* @return Property[]
*/
private function createAutowiredProperties(array $propertyMetadatas): array
{
$autowiredProperties = [];

foreach ($propertyMetadatas as $propertyMetadata) {
$propertyType = $propertyMetadata->getType();
if (! $propertyType instanceof ObjectType) {
throw new ShouldNotHappenException();
}

// create property
$autowiredProperties[] = new Property(
Modifiers::PRIVATE,
[new PropertyItem($propertyMetadata->getName())],
[],
new FullyQualified($propertyType->getClassName())
);
}

return $autowiredProperties;
throw new ShouldNotHappenException(sprintf(
'"%s" is deprecated, as a trait has no context about the class it is used in. Handle the `$this->get()` calls in the using class instead.',
self::class
));
}
}
Loading