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

Commit f9e2b5f

Browse files
committed
cli: create OptimizeUseCommand, add OptimizeUseCommand to CLI
OptimizeUse: uses the new features from NameScanner to implement
1 parent 3f43413 commit f9e2b5f

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

src/main/QafooLabs/Refactoring/Adapters/Symfony/CliApplication.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ protected function getDefaultCommands()
4242
$commands[] = new Commands\RenameLocalVariableCommand();
4343
$commands[] = new Commands\ConvertLocalToInstanceVariableCommand();
4444
$commands[] = new Commands\FixClassNamesCommand();
45+
$commands[] = new Commands\OptimizeUseCommand();
4546

4647
return $commands;
4748
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\Application;
15+
16+
use QafooLabs\Refactoring\Domain\Model\Directory;
17+
use QafooLabs\Refactoring\Domain\Model\File;
18+
use QafooLabs\Refactoring\Domain\Model\PhpClassName;
19+
use QafooLabs\Refactoring\Domain\Model\PhpName;
20+
21+
class OptimizeUse
22+
{
23+
private $codeAnalysis;
24+
private $editor;
25+
private $phpNameScanner;
26+
27+
public function __construct($codeAnalysis, $editor, $phpNameScanner)
28+
{
29+
$this->codeAnalysis = $codeAnalysis;
30+
$this->editor = $editor;
31+
$this->phpNameScanner = $phpNameScanner;
32+
}
33+
34+
public function refactor(File $file)
35+
{
36+
$classes = $this->codeAnalysis->findClasses($file);
37+
$names = $this->phpNameScanner->findNames($file);
38+
$class = $classes[0];
39+
40+
$lastUseStatement = null;
41+
$usedNames = array();
42+
$fqcns = array();
43+
foreach ($names as $name) {
44+
if ($name->isUse()) {
45+
$lastUseStatement = $name->parent();
46+
$usedNames[] = $name->fullyQualifiedName();
47+
} elseif ($name->isFullyQualified()) {
48+
$fqcns[] = $name;
49+
}
50+
}
51+
$lastUseStatementLine = null !== $lastUseStatement ? $lastUseStatement->getEndLine() : $class->getNamespaceDeclarationLine()+2;
52+
53+
if (count($fqcns) > 0) {
54+
55+
$buffer = $this->editor->openBuffer($file);
56+
foreach ($fqcns as $name) {
57+
$buffer->replaceString($name->declaredLine(), '\\'.$name->fullyQualifiedName(), $name->shortname());
58+
59+
if (!in_array($name->fullyQualifiedName(), $usedNames)) {
60+
$buffer->append($lastUseStatementLine, array(sprintf('use %s;', $name->fullyQualifiedName())));
61+
$lastUseStatementLine++;
62+
}
63+
}
64+
65+
$this->editor->save();
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)