Skip to content

Commit

Permalink
Remove SourceLocatorProviderInterface as unused (#4126)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Jun 8, 2023
1 parent ade49b3 commit ae874ef
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 84 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,22 @@
use PHPStan\BetterReflection\Reflection\Reflection;
use PHPStan\BetterReflection\Reflector\Reflector;
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
use Rector\NodeTypeResolver\Contract\SourceLocatorProviderInterface;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;

final class IntermediateSourceLocator implements SourceLocator
{
/**
* @param SourceLocatorProviderInterface[] $sourceLocatorProviders
*/
public function __construct(
private readonly array $sourceLocatorProviders
private readonly DynamicSourceLocatorProvider $dynamicSourceLocatorProvider
) {
}

public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection
{
foreach ($this->sourceLocatorProviders as $sourceLocatorProvider) {
$sourceLocator = $sourceLocatorProvider->provide();
$sourceLocator = $this->dynamicSourceLocatorProvider->provide();

$reflection = $sourceLocator->locateIdentifier($reflector, $identifier);
if ($reflection instanceof Reflection) {
return $reflection;
}
$reflection = $sourceLocator->locateIdentifier($reflector, $identifier);
if ($reflection instanceof Reflection) {
return $reflection;
}

return null;
Expand All @@ -41,13 +36,11 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
*/
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array
{
foreach ($this->sourceLocatorProviders as $sourceLocatorProvider) {
$sourceLocator = $sourceLocatorProvider->provide();
$sourceLocator = $this->dynamicSourceLocatorProvider->provide();

$reflections = $sourceLocator->locateIdentifiersByType($reflector, $identifierType);
if ($reflections !== []) {
return $reflections;
}
$reflections = $sourceLocator->locateIdentifiersByType($reflector, $identifierType);
if ($reflections !== []) {
return $reflections;
}

return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
use PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher;
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocator;
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocator;
use Rector\NodeTypeResolver\Contract\SourceLocatorProviderInterface;
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
use Webmozart\Assert\Assert;

/**
* @api phpstan external
*/
final class DynamicSourceLocatorProvider implements SourceLocatorProviderInterface
final class DynamicSourceLocatorProvider
{
/**
* @var string[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use Rector\Tests\Transform\Rector\Class_\ParentClassToTraitsRector\Source\Anothe

class ClassWithMultipleTraits
{
use \Rector\Tests\Transform\Rector\Class_\ParentClassToTraitsRector\Source\SomeTrait;
use \Rector\Tests\Transform\Rector\Class_\ParentClassToTraitsRector\Source\SecondTrait;
use \Rector\Tests\Transform\Rector\Class_\ParentClassToTraitsRector\Source\SomeTrait;
}

?>
16 changes: 11 additions & 5 deletions rules/Transform/Rector/Class_/ParentClassToTraitsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\TraitUse;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\NodeManipulator\ClassInsertManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Transform\ValueObject\ParentClassToTraits;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
Expand All @@ -31,7 +32,6 @@ final class ParentClassToTraitsRector extends AbstractRector implements Configur
private array $parentClassToTraits = [];

public function __construct(
private readonly ClassInsertManipulator $classInsertManipulator,
private readonly ClassAnalyzer $classAnalyzer
) {
}
Expand Down Expand Up @@ -80,21 +80,27 @@ public function refactor(Node $node): ?Node
return null;
}

$traitUses = [];

foreach ($this->parentClassToTraits as $parentClassToTrait) {
if (! $this->isName($parentExtends, $parentClassToTrait->getParentType())) {
continue;
}

foreach ($parentClassToTrait->getTraitNames() as $traitName) {
$this->classInsertManipulator->addAsFirstTrait($node, $traitName);
$traitUses[] = new TraitUse([new FullyQualified($traitName)]);
}

$this->removeParentClass($node);
}

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

return null;
$node->stmts = array_merge($traitUses, $node->stmts);

return $node;
}

/**
Expand Down
3 changes: 0 additions & 3 deletions rules/TypeDeclaration/PHPStan/ObjectTypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ public function narrowToFullyQualifiedOrAliasedObjectType(
ObjectType $objectType,
Scope|null $scope
): TypeWithClassName | NonExistingObjectType | UnionType | MixedType {
// $nameScope = $this->nameScopeFactory->createNameScopeFromNodeWithoutTemplateTypes($node);
// @todo reuse name scope

if ($scope instanceof Scope) {
foreach ($this->typeWithClassTypeSpecifiers as $typeWithClassTypeSpecifier) {
if ($typeWithClassTypeSpecifier->match($objectType, $scope)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v75';
private const CACHE_KEY = 'v76';

private ContainerInterface|null $container = null;

Expand Down
43 changes: 0 additions & 43 deletions src/NodeManipulator/ClassInsertManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,17 @@

namespace Rector\Core\NodeManipulator;

use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\TraitUse;
use PHPStan\Type\Type;
use Rector\Core\PhpParser\Node\NodeFactory;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class ClassInsertManipulator
{
/**
* @var array<class-string<Stmt>>
*/
private const BEFORE_TRAIT_TYPES = [TraitUse::class, Property::class, ClassMethod::class];

public function __construct(
private readonly NodeFactory $nodeFactory,
) {
Expand All @@ -46,19 +39,6 @@ public function addAsFirstMethod(Class_ $class, Property | ClassConst | ClassMet
$class->stmts[] = $stmt;
}

/**
* @api
*
* @deprecated Add properties directly instead
* @param Property[] $properties
*/
public function addPropertiesToClass(Class_ $class, array $properties): void
{
foreach ($properties as $property) {
$this->addAsFirstMethod($class, $property);
}
}

/**
* @internal Use PropertyAdder service instead
*/
Expand All @@ -73,12 +53,6 @@ public function addPropertyToClass(Class_ $class, string $name, ?Type $type): vo
$this->addAsFirstMethod($class, $property);
}

public function addAsFirstTrait(Class_ $class, string $traitName): void
{
$traitUse = new TraitUse([new FullyQualified($traitName)]);
$this->addTraitUse($class, $traitUse);
}

/**
* @param Stmt[] $stmts
* @return Stmt[]
Expand Down Expand Up @@ -120,21 +94,4 @@ private function isSuccessToInsertAfterLastProperty(Class_ $class, ClassConst|Cl

return false;
}

private function addTraitUse(Class_ $class, TraitUse $traitUse): void
{
foreach (self::BEFORE_TRAIT_TYPES as $type) {
foreach ($class->stmts as $key => $classStmt) {
if (! $classStmt instanceof $type) {
continue;
}

$class->stmts = $this->insertBefore($class->stmts, $traitUse, $key);

return;
}
}

$class->stmts[] = $traitUse;
}
}

0 comments on commit ae874ef

Please sign in to comment.