Skip to content

Commit

Permalink
add fix for protected parent
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 30, 2024
1 parent 2774024 commit eb46e12
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Command/PrivatizeConstantsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ private function runPHPStanAnalyse(array $paths): array
private function replacePrivateConstWith(ClassConstMatch $publicClassConstMatch, string $replaceString): void
{
$classFileContents = FileSystem::read($publicClassConstMatch->getClassFileName());

// replace "private const NAME" with "private const NAME"
$classFileContents = str_replace(
'private const ' . $publicClassConstMatch->getConstantName(),
Expand All @@ -142,5 +143,9 @@ private function replacePrivateConstWith(ClassConstMatch $publicClassConstMatch,
);

FileSystem::write($publicClassConstMatch->getClassFileName(), $classFileContents);

// @todo handle case when "AppBundle\Rpc\BEItem\BeItemPackage::ITEM_TYPE_NAME_PACKAGE" constant is in parent class
$parentClassConstMatch = $publicClassConstMatch->getParentClassConstMatch();
$this->replacePrivateConstWith($parentClassConstMatch, $replaceString);

Check failure on line 149 in src/Command/PrivatizeConstantsCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $publicClassConstMatch of method Rector\SwissKnife\Command\PrivatizeConstantsCommand::replacePrivateConstWith() expects Rector\SwissKnife\ValueObject\ClassConstMatch, Rector\SwissKnife\ValueObject\ClassConstMatch|null given.
}
}
10 changes: 10 additions & 0 deletions src/ValueObject/ClassConstMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@ public function getClassFileName(): string
$classReflection = new ReflectionClass($this->className);
return (string) $classReflection->getFileName();
}

public function getParentClassConstMatch(): ?self
{
$classReflection = new ReflectionClass($this->className);
if ($classReflection->getParentClass() === false) {
return null;
}

return new self($classReflection->getParentClass()->getName(), $this->constantName);
}
}

0 comments on commit eb46e12

Please sign in to comment.