From 1d42cda3891901f2f1b7899c66e91cc6c5196715 Mon Sep 17 00:00:00 2001 From: Jarek Jakubowski Date: Tue, 3 Mar 2026 13:39:52 +0100 Subject: [PATCH] Add dry-run mode to privatize-constants command --- src/Command/PrivatizeConstantsCommand.php | 32 +++++++++++++++++++---- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/Command/PrivatizeConstantsCommand.php b/src/Command/PrivatizeConstantsCommand.php index 2afb7d8c6..55d946b54 100644 --- a/src/Command/PrivatizeConstantsCommand.php +++ b/src/Command/PrivatizeConstantsCommand.php @@ -45,10 +45,15 @@ public function getDescription(): string * @param string[] $sources One or more paths to check, include tests directory as well * @param string[] $excludedPaths Paths to exclude * @param bool $isDebug Debug output + * @param bool $dryRun Do no change anything, only list constants able to be privatized. If there are constants to privatize, it will exit with code 1. Useful for CI. * @return ExitCode::* */ - public function run(array $sources, array $excludedPaths = [], bool $isDebug = false): int - { + public function run( + array $sources, + array $excludedPaths = [], + bool $isDebug = false, + bool $dryRun = false + ): int { $phpFileInfos = PhpFilesFinder::find($sources, $excludedPaths); if ($phpFileInfos === []) { $this->symfonyStyle->warning('No PHP files found in provided paths'); @@ -84,7 +89,7 @@ public function run(array $sources, array $excludedPaths = [], bool $isDebug = f // go file by file and deal with public + protected constants foreach ($phpFileInfos as $phpFileInfo) { - $currentVisibilityChangeStats = $this->processFileInfo($phpFileInfo, $classConstantFetches); + $currentVisibilityChangeStats = $this->processFileInfo($phpFileInfo, $classConstantFetches, $dryRun); $visibilityChangeStats->merge($currentVisibilityChangeStats); } @@ -96,6 +101,15 @@ public function run(array $sources, array $excludedPaths = [], bool $isDebug = f $this->symfonyStyle->newLine(2); + // to make it fail in CI + if ($dryRun) { + $this->symfonyStyle->error( + sprintf('%d constants can be privatized', $visibilityChangeStats->getPrivateCount()) + ); + + return ExitCode::ERROR; + } + $this->symfonyStyle->success( sprintf('Totally %d constants were made private', $visibilityChangeStats->getPrivateCount()) ); @@ -106,7 +120,7 @@ public function run(array $sources, array $excludedPaths = [], bool $isDebug = f /** * @param ClassConstantFetchInterface[] $classConstantFetches */ - private function processFileInfo(SplFileInfo $phpFileInfo, array $classConstantFetches): VisibilityChangeStats + private function processFileInfo(SplFileInfo $phpFileInfo, array $classConstantFetches, bool $dryRun): VisibilityChangeStats { $visibilityChangeStats = new VisibilityChangeStats(); @@ -121,6 +135,15 @@ private function processFileInfo(SplFileInfo $phpFileInfo, array $classConstantF continue; } + $visibilityChangeStats->countPrivate(); + + if ($dryRun) { + $this->symfonyStyle->writeln( + sprintf('Constant "%s" could be changed to private', $classConstant->getConstantName()) + ); + continue; + } + // make private $changedFileContents = Strings::replace( $phpFileInfo->getContents(), @@ -132,7 +155,6 @@ private function processFileInfo(SplFileInfo $phpFileInfo, array $classConstantF $this->symfonyStyle->writeln( sprintf('Constant "%s" changed to private', $classConstant->getConstantName()) ); - $visibilityChangeStats->countPrivate(); } return $visibilityChangeStats;