Skip to content

Commit

Permalink
Fix Rector apply on Source locators (#1053)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Oct 24, 2021
1 parent a504819 commit 06ea31c
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/packages_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:

# test with current commit in a pull-request
-
run: composer require rector/rector-src dev-main#${{github.event.pull_request.head.sha}} --no-install
run: composer require rector/rector-src dev-main#${{github.event.pull_request.head.sha}} --no-update
if: ${{ github.event_name == 'pull_request' }}

- run: composer install --ansi
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:

# with --runner=WrapperRunner, it is faster, only ideal with 4 processes (p4)
# @see https://github.com/rectorphp/rector-src/pull/551#issuecomment-889990905
- run: vendor/bin/paratest -p4 --runner=WrapperRunner ${{ matrix.path }}
- run: vendor/bin/paratest -p4 --runner=WrapperRunner ${{ matrix.path }} --colors
if: ${{ matrix.path == 'rules-tests' }}

- run: vendor/bin/phpunit ${{ matrix.path }}
Expand Down
27 changes: 1 addition & 26 deletions packages/Caching/FileSystem/DependencyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Dependency\DependencyResolver as PHPStanDependencyResolver;
use PHPStan\File\FileHelper;
use Symplify\PackageBuilder\Reflection\PrivatesAccessor;

final class DependencyResolver
{
public function __construct(
private NodeScopeResolver $nodeScopeResolver,
private PHPStanDependencyResolver $phpStanDependencyResolver,
private FileHelper $fileHelper,
private PrivatesAccessor $privatesAccessor
) {
}
Expand All @@ -31,30 +29,7 @@ public function resolveDependencies(Node $node, MutatingScope $mutatingScope): a
'analysedFiles'
);

$dependencyFiles = [];

$nodeDependencies = $this->phpStanDependencyResolver->resolveDependencies($node, $mutatingScope);
foreach ($nodeDependencies as $nodeDependency) {
$dependencyFile = $nodeDependency->getFileName();
if (! $dependencyFile) {
continue;
}

$dependencyFile = $this->fileHelper->normalizePath($dependencyFile);
if ($mutatingScope->getFile() === $dependencyFile) {
continue;
}

// only work with files that we've analysed
if (! in_array($dependencyFile, $analysedFileAbsolutesPaths, true)) {
continue;
}

$dependencyFiles[] = $dependencyFile;
}

$dependencyFiles = array_unique($dependencyFiles, SORT_STRING);

return array_values($dependencyFiles);
return $nodeDependencies->getFileDependencies($mutatingScope->getFile(), $analysedFileAbsolutesPaths);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,12 @@

final class IntermediateSourceLocator implements SourceLocator
{
/**
* @var SourceLocatorProviderInterface[]
*/
private $sourceLocatorProviders = [];

/**
* @param SourceLocatorProviderInterface[] $sourceLocatorProviders
*/
public function __construct(array $sourceLocatorProviders)
{
$this->sourceLocatorProviders = $sourceLocatorProviders;
public function __construct(
private array $sourceLocatorProviders
) {
}

public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,18 @@ final class DynamicSourceLocatorProvider implements SourceLocatorProviderInterfa
/**
* @var string[]
*/
private $files = [];
private array $files = [];

/**
* @var array<string, string[]>
*/
private $filesByDirectory = [];
private array $filesByDirectory = [];

/**
* @var FileNodesFetcher
*/
private $fileNodesFetcher;

/**
* @var SourceLocator|null
*/
private $cachedSourceLocator;
private ?AggregateSourceLocator $aggregateSourceLocator = null;

public function __construct(FileNodesFetcher $fileNodesFetcher)
{
$this->fileNodesFetcher = $fileNodesFetcher;
public function __construct(
private FileNodesFetcher $fileNodesFetcher
) {
}

public function setFileInfo(SmartFileInfo $fileInfo): void
Expand All @@ -58,8 +50,8 @@ public function provide(): SourceLocator
// do not cache for PHPUnit, as in test every fixture is different
$isPHPUnitRun = StaticPHPUnitEnvironment::isPHPUnitRun();

if ($this->cachedSourceLocator && $isPHPUnitRun === false) {
return $this->cachedSourceLocator;
if ($this->aggregateSourceLocator && ! $isPHPUnitRun) {
return $this->aggregateSourceLocator;
}

$sourceLocators = [];
Expand All @@ -71,9 +63,9 @@ public function provide(): SourceLocator
$sourceLocators[] = new OptimizedDirectorySourceLocator($this->fileNodesFetcher, $files);
}

$this->cachedSourceLocator = new AggregateSourceLocator($sourceLocators);
$this->aggregateSourceLocator = new AggregateSourceLocator($sourceLocators);

return $this->cachedSourceLocator;
return $this->aggregateSourceLocator;
}

/**
Expand Down
17 changes: 13 additions & 4 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,19 @@
SplitStringClassConstantToClassConstFetchRector::class,

// test paths
'*/Fixture/*',
'*/Fixture*/*',
'*/Source/*',
'*/Source*/*',
'*/tests/**/Fixture/*',
'*/rules-tests/**/Fixture/*',
'*/packages-tests/**/Fixture/*',
'*/tests/**/Fixture*/*',
'*/rules-tests/**/Fixture*/*',
'*/packages-tests/**/Fixture*/*',
// source
'*/tests/**/Source/*',
'*/rules-tests/**/Source/*',
'*/packages-tests/**/Source/*',
'*/tests/**/Source*/*',
'*/rules-tests/**/Source*/*',
'*/packages-tests/**/Source*/*',
'*/Expected/*',
'*/Expected*/*',

Expand Down
41 changes: 20 additions & 21 deletions src/StaticReflection/SourceLocator/ParentAttributeSourceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,34 @@ public function autowireParentAttributeSourceLocator(AstResolver $astResolver):

public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection
{
if ($identifier->getName() === 'Symfony\Component\DependencyInjection\Attribute\Autoconfigure') {
if ($this->reflectionProvider->hasClass($identifier->getName())) {
$classReflection = $this->reflectionProvider->getClass($identifier->getName());
if ($identifier->getName() === 'Symfony\Component\DependencyInjection\Attribute\Autoconfigure' && $this->reflectionProvider->hasClass(
$identifier->getName()
)) {
$classReflection = $this->reflectionProvider->getClass($identifier->getName());

$class = $this->astResolver->resolveClassFromClassReflection(
$classReflection,
$identifier->getName()
);
if ($class === null) {
return null;
}

$class->namespacedName = new FullyQualified($identifier->getName());
$class = $this->astResolver->resolveClassFromClassReflection($classReflection, $identifier->getName());
if ($class === null) {
return null;
}

$fakeLocatedSource = new LocatedSource('virtual', null);
$class->namespacedName = new FullyQualified($identifier->getName());
$fakeLocatedSource = new LocatedSource('virtual', null);
$classReflector = new ClassReflector($this);

$classReflector = new ClassReflector($this);
return ReflectionClass::createFromNode(
$classReflector,
$class,
$fakeLocatedSource,
new Namespace_(new Name('Symfony\Component\DependencyInjection\Attribute'))
);
}
return ReflectionClass::createFromNode(
$classReflector,
$class,
$fakeLocatedSource,
new Namespace_(new Name('Symfony\Component\DependencyInjection\Attribute'))
);
}

return null;
}

/**
* @return Reflection[]
*/
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array
{
return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(

public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection
{
foreach ($this->renamedClassesDataCollector->getOldToNewClasses() as $oldClass => $newClass) {
foreach ($this->renamedClassesDataCollector->getOldClasses() as $oldClass) {
if ($identifier->getName() !== $oldClass) {
continue;
}
Expand All @@ -39,6 +39,9 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
return null;
}

/**
* @return Reflection[]
*/
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array
{
return [];
Expand Down

0 comments on commit 06ea31c

Please sign in to comment.