diff --git a/src/Command/PrivatizeConstantsCommand.php b/src/Command/PrivatizeConstantsCommand.php index ab2128081..18ba8bdeb 100644 --- a/src/Command/PrivatizeConstantsCommand.php +++ b/src/Command/PrivatizeConstantsCommand.php @@ -108,21 +108,7 @@ private function processFileInfo(SplFileInfo $phpFileInfo, array $classConstantF } foreach ($findNonPrivateClassConstNodeVisitor->getClassConstants() as $classConstant) { - $isPublic = false; - foreach ($classConstantFetches as $classConstantFetch) { - if (! $classConstantFetch->isClassConstantMatch($classConstant)) { - continue; - } - - if ($classConstantFetch instanceof CurrentClassConstantFetch) { - continue; - } - - // used externally, make public - $isPublic = true; - } - - if ($isPublic) { + if ($this->isClassConstantUsedPublicly($classConstantFetches, $classConstant)) { $changedFileContents = Strings::replace( $phpFileInfo->getContents(), '#(public\s+)?const\s+' . $classConstant->getConstantName() . '#', @@ -138,7 +124,7 @@ private function processFileInfo(SplFileInfo $phpFileInfo, array $classConstantF // make private $changedFileContents = Strings::replace( $phpFileInfo->getContents(), - '#(public\s+)?const\s+' . $classConstant->getConstantName() . '#', + '#((public|protected)\s+)?const\s+' . $classConstant->getConstantName() . '#', 'private const ' . $classConstant->getConstantName() ); FileSystem::write($phpFileInfo->getRealPath(), $changedFileContents); @@ -146,4 +132,24 @@ private function processFileInfo(SplFileInfo $phpFileInfo, array $classConstantF $this->symfonyStyle->note(sprintf('Constant %s changed to private', $classConstant->getConstantName())); } } + + private function isClassConstantUsedPublicly( + array $classConstantFetches, + \Rector\SwissKnife\ValueObject\ClassConstant $classConstant + ): bool { + foreach ($classConstantFetches as $classConstantFetch) { + if (! $classConstantFetch->isClassConstantMatch($classConstant)) { + continue; + } + + if ($classConstantFetch instanceof CurrentClassConstantFetch) { + continue; + } + + // used externally, make public + return true; + } + + return false; + } }