Complex classes like ProxyGeneratorService 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 ProxyGeneratorService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class ProxyGeneratorService |
||
| 15 | { |
||
| 16 | const REGEX_CLASS = '#class\s(?<class>\w+)(\sextends\s(?<parentClass>\w+))?(\simplements\s(?<interface>\w+))?#'; |
||
| 17 | const REGEX_NAMESPACE = '#namespace\s(?<namespace>\w+.*)\;#'; |
||
| 18 | const PROCESS_INTERFACES = 'implements'; |
||
| 19 | const PROCESS_CHILD_CLASSES = 'extends'; |
||
| 20 | |||
| 21 | const TEMPLATE = <<<HERE |
||
| 22 | <?php |
||
| 23 | |||
| 24 | namespace REPLACE_NAMESPACE; |
||
| 25 | |||
| 26 | REPLACE_USE |
||
| 27 | |||
| 28 | REPLACE_ABSTRACTclass REPLACE_CLASS REPLACE_EXTENDS REPLACE_IMPLEMENTS |
||
| 29 | { |
||
| 30 | } |
||
| 31 | HERE; |
||
| 32 | |||
| 33 | |||
| 34 | /** @var Filesystem */ |
||
| 35 | private $fileSystem; |
||
| 36 | |||
| 37 | /** @var string $scanPath */ |
||
| 38 | private $scanPath; |
||
| 39 | |||
| 40 | /** @var string $scanPath */ |
||
| 41 | private $scanInterface; |
||
| 42 | |||
| 43 | /** @var string $replaceInterface */ |
||
| 44 | private $replaceInterface; |
||
| 45 | |||
| 46 | /** @var string $targetPath */ |
||
| 47 | private $targetPath; |
||
| 48 | |||
| 49 | /** @var string $targetNamespace */ |
||
| 50 | private $targetNamespace; |
||
| 51 | |||
| 52 | /** @var $replaceNamespace */ |
||
| 53 | private $replaceNamespace; |
||
| 54 | |||
| 55 | /** @var array $implementingClasses */ |
||
| 56 | private $implementingClasses = []; |
||
| 57 | |||
| 58 | /** @var array $extendingClasses */ |
||
| 59 | private $extendingClasses = []; |
||
| 60 | |||
| 61 | private $type = self::PROCESS_INTERFACES; |
||
| 62 | |||
| 63 | /** @var array $currentFile */ |
||
| 64 | private $currentFile; |
||
| 65 | |||
| 66 | /** @var string $currentNamespace */ |
||
| 67 | private $currentNamespace; |
||
| 68 | |||
| 69 | /** @var string $checkClass */ |
||
| 70 | private $checkClass; |
||
| 71 | |||
| 72 | private $newMatches = false; |
||
| 73 | |||
| 74 | private $baseNamespace; |
||
| 75 | |||
| 76 | 2 | public function __construct($projectRootPath = '.') |
|
| 82 | |||
| 83 | /** |
||
| 84 | * @param string $scanPath |
||
| 85 | */ |
||
| 86 | 1 | public function setScanPath($scanPath) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @param string $scanInterface |
||
| 93 | */ |
||
| 94 | 1 | public function setScanInterface($scanInterface) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @param string $replaceInterface |
||
| 101 | */ |
||
| 102 | 1 | public function setReplaceInterface($replaceInterface) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $targetPath |
||
| 109 | * @return ProxyGeneratorService |
||
| 110 | */ |
||
| 111 | 1 | public function setTargetPath($targetPath) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $targetNamespace |
||
| 119 | * @return ProxyGeneratorService |
||
| 120 | */ |
||
| 121 | 1 | public function setTargetNamespace($targetNamespace) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @param mixed $replaceNamespace |
||
| 129 | * @return ProxyGeneratorService |
||
| 130 | */ |
||
| 131 | 1 | public function setReplaceNamespace($replaceNamespace) |
|
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | 2 | public function generate() |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @param $interface |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | 1 | private function getClasses() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @param $contents |
||
| 169 | * @param $interfaceName |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | 1 | private function processContents($contents) |
|
| 189 | |||
| 190 | 1 | private function processClasses($classes, $contents) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @param $file |
||
| 202 | * @return bool |
||
| 203 | */ |
||
| 204 | 1 | private function processFile($file) |
|
| 217 | |||
| 218 | 1 | private function processLine($line) |
|
| 230 | |||
| 231 | 1 | private function processMatch(array $match) |
|
| 243 | |||
| 244 | 1 | private function processImplementingClass(array $match) |
|
| 257 | |||
| 258 | 1 | private function processExtendingClass(array $match) |
|
| 272 | |||
| 273 | |||
| 274 | 1 | private function generateFiles($classes) |
|
| 308 | } |