1414namespace Dunglas \PhpDocToTypeHint ;
1515
1616use phpDocumentor \Reflection \Php \ProjectFactory ;
17+ use SebastianBergmann \Diff \Differ ;
1718use Symfony \Component \Console \Command \Command ;
1819use Symfony \Component \Console \Helper \ProgressBar ;
1920use Symfony \Component \Console \Input \InputArgument ;
2930 */
3031class 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}
0 commit comments