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 InMemoryIndex 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 InMemoryIndex, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class InMemoryIndex implements Index |
||
| 14 | { |
||
| 15 | private $files = []; |
||
| 16 | private $fqcns = []; |
||
| 17 | private $classes = []; |
||
| 18 | private $interfaces = []; |
||
| 19 | private $extends = []; |
||
| 20 | private $implements = []; |
||
| 21 | private $functions = []; |
||
| 22 | |||
| 23 | /** @var Index $coreIndex */ |
||
| 24 | private static $coreIndex; |
||
| 25 | |||
| 26 | public function getFQCNs() |
||
| 30 | |||
| 31 | public function getImplements() { |
||
| 37 | |||
| 38 | public function addFile(File $file) |
||
| 57 | |||
| 58 | public function findFileByPath($path) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @return FQCN |
||
| 68 | */ |
||
| 69 | public function findFQCNByFile($file) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return ClassData |
||
| 86 | */ |
||
| 87 | View Code Duplication | public function findClassByFQCN(FQCN $fqcn) { |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @return InterfaceData |
||
| 99 | */ |
||
| 100 | View Code Duplication | public function findInterfaceByFQCN(FQCN $fqcn) { |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @return FunctionData |
||
| 112 | */ |
||
| 113 | public function findFunctionByName($functionName) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return ClassData[] |
||
| 125 | */ |
||
| 126 | View Code Duplication | public function findClassChildren(FQCN $class) { |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @return ClassData[] |
||
| 137 | */ |
||
| 138 | View Code Duplication | public function findInterfaceChildrenClasses(FQCN $interface) { |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @return ClassData[] |
||
| 149 | */ |
||
| 150 | public function getClasses() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return InterfaceData[] |
||
| 161 | */ |
||
| 162 | public function getInterfaces() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return FunctionData[] |
||
| 173 | */ |
||
| 174 | public function getFunctions() |
||
| 182 | |||
| 183 | public function addClass(ClassData $class) { |
||
| 197 | |||
| 198 | public function addInterface(InterfaceData $interface) { |
||
| 209 | |||
| 210 | public function addFunction(FunctionData $function) |
||
| 214 | |||
| 215 | public function addFQCN(FQCN $fqcn) { |
||
| 218 | |||
| 219 | protected function addExtend(ClassData $class, FQCN $parent) { |
||
| 227 | |||
| 228 | protected function addImplement($class, FQCN $fqcn) { |
||
| 236 | |||
| 237 | private function hasCoreIndex() |
||
| 241 | } |
||
| 242 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: