Skip to content

Commit

Permalink
Avoid reprint of json files (#27)
Browse files Browse the repository at this point in the history
* bump

* mics
  • Loading branch information
TomasVotruba committed May 10, 2024
1 parent 64d9bf8 commit c74f67d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 38 deletions.
36 changes: 15 additions & 21 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,19 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_82,
SetList::CODE_QUALITY,
SetList::DEAD_CODE,
SetList::CODING_STYLE,
SetList::TYPE_DECLARATION,
SetList::NAMING,
SetList::PRIVATIZATION,
SetList::EARLY_RETURN,
]);

$rectorConfig->paths([__DIR__ . '/src', __DIR__ . '/tests']);

$rectorConfig->importNames();
$rectorConfig->removeUnusedImports();

$rectorConfig->skip(['*/scoper.php', '*/Source/*', '*/Fixture/*']);
};
return RectorConfig::configure()
->withPaths([__DIR__ . '/src', __DIR__ . '/tests'])
->withPhpSets()
->withPreparedSets(
codeQuality: true,
deadCode: true,
typeDeclarations: true,
privatization: true,
earlyReturn: true,
codingStyle: true,
instanceOf: true,
naming: true
)
->withImportNames(removeUnusedImports: true)
->withSkip(['*/scoper.php', '*/Source/*', '*/Fixture/*']);
46 changes: 35 additions & 11 deletions src/Command/PrettyJsonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@

use Nette\Utils\FileSystem;
use Nette\Utils\Json;
use Rector\SwissKnife\FileSystem\JsonAnalyzer;
use Rector\SwissKnife\Finder\FilesFinder;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

final class PrettyJsonCommand extends Command
{
public function __construct(
private readonly SymfonyStyle $symfonyStyle,
private readonly JsonAnalyzer $jsonAnalyzer,
) {
parent::__construct();
}
Expand All @@ -25,45 +28,66 @@ protected function configure(): void
{
$this->setName('pretty-json');

$this->setDescription('Turns JSON files from 1-line to pretty print format');

$this->addArgument(
'sources',
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
'File or directory to prettify'
'JSON file or directory with JSON files to prettify'
);
$this->setDescription('Turns JSON files from 1-line format to pretty format');

$this->addOption('dry-run', null, InputOption::VALUE_NONE, 'Dry run - no changes will be made');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$sources = (array) $input->getArgument('sources');
$jsonFileInfos = FilesFinder::findJsonFiles($sources);

if (count($jsonFileInfos) === 0) {
if ($jsonFileInfos === []) {
$this->symfonyStyle->error('No *.json files found');
return self::FAILURE;
}

$message = sprintf('Analysing %d *.json files', count($jsonFileInfos));
$this->symfonyStyle->note($message);

$isDryRun = (bool) $input->getOption('dry-run');

$printedFilePaths = [];

// convert file infos from uggly json to pretty json
foreach ($jsonFileInfos as $jsonFileInfo) {
$jsonContent = FileSystem::read($jsonFileInfo->getRealPath());
$prettyJsonContent = Json::encode(Json::decode($jsonContent), JSON_PRETTY_PRINT);

// nothing to convert
if ($prettyJsonContent === $jsonContent) {
$this->symfonyStyle->writeln(sprintf('The "%s" file is already pretty', $jsonFileInfo->getRealPath()));
if ($this->jsonAnalyzer->isPrettyPrinted($jsonContent)) {
$this->symfonyStyle->writeln(
sprintf('File "%s" is already pretty', $jsonFileInfo->getRelativePathname())
);
continue;
}

// notify the file was changed
$this->symfonyStyle->writeln(
sprintf('The "%s" file was changed to pretty json', $jsonFileInfo->getRealPath())
);
$printedFilePaths[] = $jsonFileInfo->getRelativePathname();

// nothing will be changed
if ($isDryRun === true) {
continue;
}

$prettyJsonContent = Json::encode(Json::decode($jsonContent), JSON_PRETTY_PRINT);
FileSystem::write($jsonFileInfo->getRealPath(), $prettyJsonContent);
}

$successMessage = sprintf(
'%d file%s %s',
count($printedFilePaths),
count($printedFilePaths) === 1 ? '' : 's',
$isDryRun ? 'would be changed' : 'changed'
);

$this->symfonyStyle->success($successMessage);
$this->symfonyStyle->listing($printedFilePaths);

return self::SUCCESS;
}
}
2 changes: 1 addition & 1 deletion src/Comments/CommentedCodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function process(string $filePath, int $commentedLinesCountLimit): array
$commentLinesCount = 0;

foreach ($fileLines as $key => $fileLine) {
$isCommentLine = str_starts_with(trim($fileLine), '//');
$isCommentLine = str_starts_with(trim((string) $fileLine), '//');
if ($isCommentLine) {
++$commentLinesCount;
} else {
Expand Down
18 changes: 18 additions & 0 deletions src/FileSystem/JsonAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Rector\SwissKnife\FileSystem;

final class JsonAnalyzer
{
public function isPrettyPrinted(string $json): bool
{
$lines = explode(PHP_EOL, $json);
if (count($lines) >= 3) {
return true;
}

return false;
}
}
9 changes: 4 additions & 5 deletions tests/ParentClassResolver/ParentClassResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Rector\SwissKnife\Finder\PhpFilesFinder;
use Rector\SwissKnife\ParentClassResolver;
use Rector\SwissKnife\Tests\AbstractTestCase;
use Rector\SwissKnife\Tests\ParentClassResolver\Fixture\AbstractParentClass;
use Rector\SwissKnife\Tests\ParentClassResolver\Fixture\ParentClass;
use Rector\SwissKnife\Tests\ParentClassResolver\Fixture\ParentClassInSeparateNamespace;

final class ParentClassResolverTest extends AbstractTestCase
{
Expand All @@ -28,11 +31,7 @@ static function (): void {
);

$this->assertSame(
[
'Rector\SwissKnife\Tests\ParentClassResolver\Fixture\AbstractParentClass',
'Rector\SwissKnife\Tests\ParentClassResolver\Fixture\ParentClass',
'Rector\SwissKnife\Tests\ParentClassResolver\Fixture\ParentClassInSeparateNamespace',
],
[AbstractParentClass::class, ParentClass::class, ParentClassInSeparateNamespace::class],
$parentClasses
);
}
Expand Down

0 comments on commit c74f67d

Please sign in to comment.