Skip to content

Commit

Permalink
[config] Move from privatize-constants
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 5, 2024
1 parent 66f8a2f commit 5d5bc5d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 69 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ PHPStan can report unused private class constants, but it skips all the public o
Do you have lots of class constants, all of them public but want to narrow scope to privates?
```bash
vendor/bin/swiss-knife privatize-constants src
vendor/bin/swiss-knife privatize-constants src test
```
This command will:
Expand Down
5 changes: 0 additions & 5 deletions config/privatize-constants-phpstan-ruleset.neon

This file was deleted.

67 changes: 4 additions & 63 deletions src/Command/PrivatizeConstantsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\SwissKnife\Command;

use Nette\Utils\FileSystem;
use Nette\Utils\Strings;
use Rector\SwissKnife\Finder\PhpFilesFinder;
use Rector\SwissKnife\Helpers\ClassNameResolver;
use Rector\SwissKnife\PHPStan\ClassConstantResultAnalyser;
Expand Down Expand Up @@ -76,7 +75,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return self::SUCCESS;
}

$this->privatizeClassConstants($phpFileInfos);
$this->symfonyStyle->success('1. Finding all class constants...');

dump('testing');
die;

// special case of self::NAME, that should be protected - their children too
$staticClassConstMatches = $this->staticClassConstResolver->resolve($phpFileInfos);
Expand Down Expand Up @@ -121,67 +123,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return self::SUCCESS;
}

/**
* @param SplFileInfo[] $phpFileInfos
*/
private function privatizeClassConstants(array $phpFileInfos): void
{
$this->symfonyStyle->note(sprintf('Found %d PHP files, turning constants to private', count($phpFileInfos)));

$privatizedFileCount = 0;

foreach ($phpFileInfos as $phpFileInfo) {
$originalFileContent = $phpFileInfo->getContents();

$fileContent = $this->makeClassConstantsPrivate($originalFileContent);
if ($originalFileContent === $fileContent) {
continue;
}

FileSystem::write($phpFileInfo->getRealPath(), $fileContent);
++$privatizedFileCount;
}

$this->symfonyStyle->success(sprintf('Constants in %d files turned to private', $privatizedFileCount));
}

private function makeClassConstantsPrivate(string $fileContents): string
{
return Strings::replace($fileContents, self::PUBLIC_CONST_REGEX, '$1private const ');
}

/**
* @param string[] $paths
* @return array<string, mixed>
*/
private function runPHPStanAnalyse(array $paths): array
{
$this->symfonyStyle->note('Running PHPStan to spot false-private class constants');

$commandOptions = [
'vendor/bin/phpstan',
'analyse',
...$paths,
'--configuration',
__DIR__ . '/../../config/privatize-constants-phpstan-ruleset.neon',
'--error-format',
'json',
];

$phpStanAnalyseProcess = new Process($commandOptions, null, null, null, self::TIMEOUT_IN_SECONDS);
$phpStanAnalyseProcess->run();

$this->symfonyStyle->success('PHPStan analysis finished');

// process output message
sleep(1);

$this->symfonyStyle->newLine();

$resultOutput = $phpStanAnalyseProcess->getOutput() ?: $phpStanAnalyseProcess->getErrorOutput();
return json_decode($resultOutput, true);
}

private function replacePrivateConstWith(ClassConstMatch $publicClassConstMatch, string $replaceString): void
{
$classFileContents = FileSystem::read($publicClassConstMatch->getClassFileName());
Expand Down

0 comments on commit 5d5bc5d

Please sign in to comment.