Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like LibraryChanges often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LibraryChanges, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class LibraryChanges extends LanguageFeatureAnalyser implements AnalyserAwareInterface, LibraryInformationAwareInterface |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var LibraryInformation |
||
| 39 | */ |
||
| 40 | private $information; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @inheritdoc |
||
| 44 | * @param array $options |
||
| 45 | * @param \Pvra\Analyser $analyser |
||
| 46 | * @param \Pvra\InformationProvider\LibraryInformation $information |
||
| 47 | */ |
||
| 48 | 72 | public function __construct(array $options = [], Analyser $analyser = null, LibraryInformation $information = null) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @param \Pvra\InformationProvider\LibraryInformationInterface $libInfo |
||
| 59 | * @return $this |
||
| 60 | */ |
||
| 61 | 40 | public function setLibraryInformation(LibraryInformationInterface $libInfo) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * @param \Pvra\InformationProvider\LibraryInformationInterface $libInfo |
||
| 70 | * @return $this |
||
| 71 | */ |
||
| 72 | 2 | public function addLibraryInformation(LibraryInformationInterface $libInfo) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @return LibraryInformationInterface |
||
| 81 | */ |
||
| 82 | 54 | public function getLibraryInformation() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Prepare a name |
||
| 96 | * |
||
| 97 | * If the first argument is an instance of `PhpParser\Node\Name` its string representation |
||
| 98 | * will be returned as first element of the result array. The value retrieved via `PhpParser\Node\Name::getLine()` |
||
| 99 | * will be used as second element. |
||
| 100 | * If the first parameter is not an instance of `PhpParser\Node\Name` it will be casted to string and returned alongside |
||
| 101 | * with the value given for the second parameter as line |
||
| 102 | * |
||
| 103 | * @param \PhpParser\Node\Name|string $name |
||
| 104 | * @param int $line Only used if the $name parameter is not an instance of `PhpParser\Node\Name` |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | 50 | private function prepareNameAndLine($name, $line = -1) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @param \PhpParser\Node\Name|string $name |
||
| 121 | * @param int $line |
||
| 122 | */ |
||
| 123 | 34 | View Code Duplication | private function handleClassName($name, $line = -1) |
| 155 | |||
| 156 | /** |
||
| 157 | * @param \PhpParser\Node\Name|string $name |
||
| 158 | * @param int $line |
||
| 159 | */ |
||
| 160 | 36 | View Code Duplication | private function handleFunctionName($name, $line = -1) |
| 192 | |||
| 193 | /** |
||
| 194 | * @param \PhpParser\Node\Name|string $name |
||
| 195 | * @param int $line |
||
| 196 | */ |
||
| 197 | 24 | View Code Duplication | private function handleConstantName($name, $line = -1) |
| 229 | |||
| 230 | /** |
||
| 231 | * @inheritdoc |
||
| 232 | * @param \PhpParser\Node $node |
||
| 233 | * @return null|\PhpParser\Node|void |
||
| 234 | */ |
||
| 235 | 62 | public function enterNode(Node $node) |
|
| 279 | } |
||
| 280 |