|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Qafoo PHP Refactoring Browser |
| 4 | + * |
| 5 | + * LICENSE |
| 6 | + * |
| 7 | + * This source file is subject to the MIT license that is bundled |
| 8 | + * with this package in the file LICENSE.txt. |
| 9 | + * If you did not receive a copy of the license and are unable to |
| 10 | + * obtain it through the world-wide-web, please send an email |
| 11 | + * to [email protected] so I can send you a copy immediately. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace QafooLabs\Refactoring\Adapters\Symfony\Commands; |
| 15 | + |
| 16 | +use Symfony\Component\Console\Input\InputArgument; |
| 17 | +use Symfony\Component\Console\Input\InputOption; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | +use Symfony\Component\Console\Command\Command; |
| 21 | + |
| 22 | +use QafooLabs\Refactoring\Application\OptimizeUse; |
| 23 | +use QafooLabs\Refactoring\Adapters\PHPParser\ParserVariableScanner; |
| 24 | +use QafooLabs\Refactoring\Adapters\PHPParser\ParserPhpNameScanner; |
| 25 | +use QafooLabs\Refactoring\Adapters\TokenReflection\StaticCodeAnalysis; |
| 26 | +use QafooLabs\Refactoring\Adapters\Patches\PatchEditor; |
| 27 | +use QafooLabs\Refactoring\Adapters\Symfony\OutputPatchCommand; |
| 28 | + |
| 29 | +use QafooLabs\Refactoring\Domain\Model\LineRange; |
| 30 | +use QafooLabs\Refactoring\Domain\Model\File; |
| 31 | + |
| 32 | +/** |
| 33 | + * Symfony Adapter to execute the Optimize Use Refactoring |
| 34 | + */ |
| 35 | +class OptimizeUseCommand extends Command |
| 36 | +{ |
| 37 | + protected function configure() |
| 38 | + { |
| 39 | + $this |
| 40 | + ->setName('optimize-use') |
| 41 | + ->setDescription('Optimize use statements of a file. Replace FQNs with imported aliases.') |
| 42 | + ->addArgument('file', InputArgument::REQUIRED, 'File that contains the use statements to optimize') |
| 43 | + ->setHelp(<<<HELP |
| 44 | +Optimizes the use of Fully qualified names in a file so that FQN is imported with |
| 45 | +"use" at the top of the file and the FQN is replaced with its classname. |
| 46 | +
|
| 47 | +All other use statements will be untouched, only new ones will be added. |
| 48 | +
|
| 49 | +<comment>Operations:</comment> |
| 50 | +
|
| 51 | +1. import found FQNs |
| 52 | +2. replace FQNs with the imported classname |
| 53 | +
|
| 54 | +<comment>Pre-Conditions:</comment> |
| 55 | +
|
| 56 | +1. File has a single namespace defined |
| 57 | +
|
| 58 | +<comment>Known issues:</comment> |
| 59 | +
|
| 60 | +1. a FQN might be renamed with an conflicting name when the className of the renamend full qualified name is already in use |
| 61 | +2. if there is no use statement in the whole file, new ones will be appended after the namespace |
| 62 | +
|
| 63 | +<comment>Usage:</comment> |
| 64 | +
|
| 65 | + <info>php refactor.phar optimize-use file.php</info> |
| 66 | +
|
| 67 | +Will optimize the use statements in <info>file.php</info>. |
| 68 | +HELP |
| 69 | + ); |
| 70 | + ; |
| 71 | + } |
| 72 | + |
| 73 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 74 | + { |
| 75 | + $file = File::createFromPath($input->getArgument('file'), getcwd()); |
| 76 | + |
| 77 | + $codeAnalysis = new StaticCodeAnalysis(); |
| 78 | + $editor = new PatchEditor(new OutputPatchCommand($output)); |
| 79 | + $phpNameScanner = new ParserPhpNameScanner(); |
| 80 | + |
| 81 | + $optimizeUse = new OptimizeUse($codeAnalysis, $editor, $phpNameScanner); |
| 82 | + $optimizeUse->refactor($file); |
| 83 | + } |
| 84 | +} |
0 commit comments