Skip to content

Commit

Permalink
[Core] Improve performance: remove repetitive currentFileProvider->se…
Browse files Browse the repository at this point in the history
…tFile() call on PhpFileProcessor (#3213)

* [Core] Improve performance: remove repetitive currentFileProvider->setFile() call on PhpFileProcessor

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 17, 2022
1 parent 21c9e59 commit e895bce
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -112,12 +113,13 @@ private function isUsingStatic(ClassConstFetch $classConstFetch): bool
return $classConstFetch->class->toString() === 'static';
}

private function isPrivateConstant(ClassConstFetch $constant, Class_ $class): bool
private function isPrivateConstant(ClassConstFetch $classConstFetch, Class_ $class): bool
{
$constantName = $this->getConstantName($constant);
$constantName = $this->getConstantName($classConstFetch);
if ($constantName === null) {
return false;
}

foreach ($class->getConstants() as $classConst) {
if (! $this->nodeNameResolver->isName($classConst, $constantName)) {
continue;
Expand All @@ -129,11 +131,11 @@ private function isPrivateConstant(ClassConstFetch $constant, Class_ $class): bo
return false;
}

private function isUsedInPrivateMethod(ClassConstFetch $node): bool
private function isUsedInPrivateMethod(ClassConstFetch $classConstFetch): bool
{
$method = $this->betterNodeFinder->findParentType($node, Node\Stmt\ClassMethod::class);
$method = $this->betterNodeFinder->findParentType($classConstFetch, ClassMethod::class);

if (! $method instanceof Node\Stmt\ClassMethod) {
if (! $method instanceof ClassMethod) {
return false;
}

Expand All @@ -145,9 +147,11 @@ private function shouldBeSkipped(Class_ $class, ClassConstFetch $classConstFetch
if (! $this->isUsingStatic($classConstFetch)) {
return true;
}

if (! $this->isPrivateConstant($classConstFetch, $class)) {
return true;
}

if ($this->isUsedInPrivateMethod($classConstFetch)) {
return false;
}
Expand All @@ -170,10 +174,11 @@ private function isOverwrittenInChildClass(ClassConstFetch $classConstFetch): bo
if (! $classReflection instanceof ClassReflection) {
return false;
}

$childrenClassReflections = $this->familyRelationsAnalyzer->getChildrenOfClassReflection($classReflection);

foreach ($childrenClassReflections as $childrenClassReflection) {
if ($childrenClassReflection->hasConstant($constantName)) {
foreach ($childrenClassReflections as $childClassReflection) {
if ($childClassReflection->hasConstant($constantName)) {
return true;
}
}
Expand Down
10 changes: 1 addition & 9 deletions src/Application/FileProcessor/PhpFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,10 @@ public function process(File $file, Configuration $configuration): array
return $systemErrorsAndFileDiffs;
}

$this->currentFileProvider->setFile($file);

// 2. change nodes with Rectors
do {
$file->changeHasChanged(false);
$this->refactorNodesWithRectors($file, $configuration);
$this->fileProcessor->refactor($file, $configuration);

// 3. apply post rectors
$newStmts = $this->postFileProcessor->traverse($file->getNewStmts());
Expand Down Expand Up @@ -99,12 +97,6 @@ public function getSupportedFileExtensions(): array
return ['php'];
}

private function refactorNodesWithRectors(File $file, Configuration $configuration): void
{
$this->currentFileProvider->setFile($file);
$this->fileProcessor->refactor($file, $configuration);
}

/**
* @return SystemError[]
*/
Expand Down

0 comments on commit e895bce

Please sign in to comment.