Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/Command/PrivatizeConstantsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}

Expand All @@ -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())
);
Expand All @@ -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();

Expand All @@ -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(),
Expand All @@ -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;
Expand Down
Loading