Skip to content
This repository was archived by the owner on Dec 29, 2020. It is now read-only.

Commit 457e7cd

Browse files
committed
Add a dry run option
1 parent b459b73 commit 457e7cd

File tree

4 files changed

+99
-7
lines changed

4 files changed

+99
-7
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"symfony/finder": "^2.7.0",
1717
"phpdocumentor/reflection": "^2.0@dev",
1818
"phpdocumentor/reflection-common": "^1.0@dev",
19-
"phpdocumentor/reflection-docblock": "^3.0@dev"
19+
"phpdocumentor/reflection-docblock": "^3.0@dev",
20+
"sebastian/diff": "^1.3"
2021
}
2122
}

composer.lock

Lines changed: 54 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ConvertCommand.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Dunglas\PhpDocToTypeHint;
1515

1616
use phpDocumentor\Reflection\Php\ProjectFactory;
17+
use SebastianBergmann\Diff\Differ;
1718
use Symfony\Component\Console\Command\Command;
1819
use Symfony\Component\Console\Helper\ProgressBar;
1920
use Symfony\Component\Console\Input\InputArgument;
@@ -29,6 +30,21 @@
2930
*/
3031
class ConvertCommand extends Command
3132
{
33+
/**
34+
* @var Differ
35+
*/
36+
private $differ;
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function __construct($name = null)
42+
{
43+
$this->differ = new Differ();
44+
45+
parent::__construct($name);
46+
}
47+
3248
/**
3349
* {@inheritdoc}
3450
*/
@@ -37,8 +53,9 @@ protected function configure()
3753
$this
3854
->setName('convert')
3955
->setDescription('Convert files')
40-
->addOption('exclude', 'e', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Directories to exclude.', ['vendor'])
41-
->addArgument('input', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Input directories.', ['.'])
56+
->addOption('exclude', 'e', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Directories to exclude', ['vendor'])
57+
->addOption('dry-run', null, InputOption::VALUE_NONE, 'Displays diff instead of modifying files')
58+
->addArgument('input', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Input directories', ['.'])
4259
;
4360
}
4461

@@ -63,17 +80,38 @@ protected function execute(InputInterface $input, OutputInterface $output)
6380
$converter = new Converter();
6481

6582
$output->writeln('<comment>Running the PHPDoc to Type Hint converter. Brought to you by Kévin Dunglas and Les-Tilleuls.coop.</comment>');
83+
$output->writeln('');
6684

6785
$progress = new ProgressBar($output, count($files));
6886

87+
$changed = [];
6988
foreach ($project->getFiles() as $file) {
70-
file_put_contents($file->getPath(), $converter->convert($project, $file));
89+
$old = $file->getSource();
90+
$new = $converter->convert($project, $file);
91+
92+
if ($new !== $old) {
93+
if ($input->getOption('dry-run')) {
94+
$changed[] = ['path' => $file->getPath(), 'diff' => $this->differ->diff($old, $new)];
95+
} else {
96+
file_put_contents($file->getPath(), $new);
97+
}
98+
}
99+
71100
$progress->advance();
72101
}
73102

74103
$progress->finish();
104+
105+
$output->writeln('');
75106
$output->writeln('');
76107

108+
foreach ($changed as $i => $file) {
109+
$output->writeln(sprintf('<fg=blue>%d) %s</>', $i + 1, $file['path']));
110+
$output->writeln('');
111+
$output->writeln($file['diff']);
112+
$output->writeln('');
113+
}
114+
77115
$output->writeln('<info>Conversion done.</info>');
78116
}
79117
}

src/Converter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
declare(strict_types=1);
12+
declare (strict_types = 1);
1313

1414
namespace Dunglas\PhpDocToTypeHint;
1515

@@ -309,6 +309,7 @@ private function getDocBlock(Project $project, int $objectType, string $namespac
309309
private function getNamespace(Fqsen $fqsen): string
310310
{
311311
$value = $fqsen->__toString();
312+
312313
return substr($value, 0, strrpos($value, '\\'));
313314
}
314315

0 commit comments

Comments
 (0)