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 AbstractTestGenerator 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 AbstractTestGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | abstract class AbstractTestGenerator |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var \Twig_Environment |
||
| 11 | */ |
||
| 12 | protected $twig; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var KernelInterface |
||
| 16 | */ |
||
| 17 | protected $kernel; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $usedClasses; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param \Twig_Environment $twig |
||
| 26 | * @param KernelInterface $kernelInterface |
||
| 27 | */ |
||
| 28 | public function __construct( |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param string $className |
||
| 39 | */ |
||
| 40 | abstract public function generate($className); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param \ReflectionClass $class |
||
| 44 | * @return array |
||
| 45 | */ |
||
| 46 | protected function getMethodsData(\ReflectionClass $class) |
||
| 68 | |||
| 69 | |||
| 70 | /** |
||
| 71 | * @param \ReflectionClass $class |
||
| 72 | * @return \ReflectionMethod[] |
||
| 73 | */ |
||
| 74 | protected function getPublicMethods(\ReflectionClass $class) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param array $dependencies |
||
| 88 | * @return array |
||
| 89 | */ |
||
| 90 | protected function getDependenciesData($dependencies) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param string[] $classes |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | protected function getOrderedUses(array $classes) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $className |
||
| 141 | * @param string $testType |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | protected function getNamespaceForTest($className, $testType) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $namespace |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | protected function getTestPath($namespace) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param string $path |
||
| 191 | * @param string $content |
||
| 192 | */ |
||
| 193 | protected function createFile($path, $content) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param \ReflectionMethod $constructor |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | protected function getDependencies(\ReflectionMethod $constructor) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param \ReflectionParameter $param |
||
| 226 | * @param array $arguments |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | protected function fillArguments(\ReflectionParameter $param, $arguments) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param \ReflectionClass|string $class |
||
| 262 | */ |
||
| 263 | protected function addClassToUses($class) |
||
| 273 | } |
||
| 274 |