Complex classes like DependencyGraph 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 DependencyGraph, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
66 | class DependencyGraph |
||
67 | { |
||
68 | /** |
||
69 | * Stores the names of all modules (vertices) as keys. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | private $moduleNames = array(); |
||
74 | |||
75 | /** |
||
76 | * Stores the edges in the keys of a multi-dimensional array. |
||
77 | * |
||
78 | * The first dimension stores the module names, the second dimension the |
||
79 | * names of the dependencies. |
||
80 | * |
||
81 | * @var array |
||
82 | */ |
||
83 | private $dependencies = array(); |
||
84 | |||
85 | /** |
||
86 | * Creates an override graph for the given modules. |
||
87 | * |
||
88 | * @param ModuleList $modules The modules to load. |
||
89 | * |
||
90 | * @return static The created override graph. |
||
91 | */ |
||
92 | 93 | public static function forModules(ModuleList $modules) |
|
128 | |||
129 | /** |
||
130 | * Creates a new graph. |
||
131 | * |
||
132 | * @param string[] $moduleNames The module names stored in the nodes of |
||
133 | * the graph. |
||
134 | */ |
||
135 | 132 | public function __construct(array $moduleNames = array()) |
|
139 | |||
140 | /** |
||
141 | * Adds a module name to the graph. |
||
142 | * |
||
143 | * @param string $moduleName The module name. |
||
144 | * |
||
145 | * @throws RuntimeException If the module name already exists. |
||
146 | */ |
||
147 | 131 | public function addModuleName($moduleName) |
|
159 | |||
160 | /** |
||
161 | * Adds a list of module names to the graph. |
||
162 | * |
||
163 | * @param string[] $moduleNames The module names. |
||
164 | * |
||
165 | * @throws RuntimeException If a module name already exists. |
||
166 | */ |
||
167 | 132 | public function addModuleNames(array $moduleNames) |
|
173 | |||
174 | /** |
||
175 | * Returns whether a module name exists in the graph. |
||
176 | * |
||
177 | * @param string $moduleName The module name. |
||
178 | * |
||
179 | * @return bool Whether the module name exists. |
||
180 | */ |
||
181 | 40 | public function hasModuleName($moduleName) |
|
185 | |||
186 | /** |
||
187 | * Returns all module names in the graph. |
||
188 | * |
||
189 | * @return array All module names in the graph. |
||
190 | */ |
||
191 | public function getModuleNames() |
||
195 | |||
196 | /** |
||
197 | * Returns the sorted module names. |
||
198 | * |
||
199 | * The names are sorted such that if a module m1 depends on a module m2, |
||
200 | * then m2 comes before m1 in the sorted set. |
||
201 | * |
||
202 | * If module names are passed, only those module names are sorted. Otherwise |
||
203 | * all module names are sorted. |
||
204 | * |
||
205 | * @param string[] $namesToSort The module names which should be sorted. |
||
206 | * |
||
207 | * @return string[] The sorted module names. |
||
208 | * |
||
209 | * @throws RuntimeException If any of the passed module names does not |
||
210 | * exist in the graph. |
||
211 | */ |
||
212 | 83 | public function getSortedModuleNames(array $namesToSort = array()) |
|
239 | |||
240 | /** |
||
241 | * Adds a dependency from one to another module. |
||
242 | * |
||
243 | * @param string $moduleName The module name. |
||
244 | * @param string $dependency The name of the dependency. |
||
245 | */ |
||
246 | 67 | public function addDependency($moduleName, $dependency) |
|
276 | |||
277 | /** |
||
278 | * Removes a dependency from one to another module. |
||
279 | * |
||
280 | * @param string $moduleName The module name. |
||
281 | * @param string $dependency The name of the dependency. |
||
282 | */ |
||
283 | 2 | public function removeDependency($moduleName, $dependency) |
|
287 | |||
288 | /** |
||
289 | * Returns whether a module directly depends on another module. |
||
290 | * |
||
291 | * @param string $moduleName The module name. |
||
292 | * @param string $dependency The name of the dependency. |
||
293 | * @param bool $recursive Whether to take recursive dependencies into |
||
294 | * account. |
||
295 | * |
||
296 | * @return bool Whether an edge exists from the origin to the target module. |
||
297 | */ |
||
298 | 40 | public function hasDependency($moduleName, $dependency, $recursive = true) |
|
306 | |||
307 | /** |
||
308 | * Returns whether a path exists from a module to a dependency. |
||
309 | * |
||
310 | * @param string $moduleName The module name. |
||
311 | * @param string $dependency The name of the dependency. |
||
312 | * |
||
313 | * @return bool Whether a path exists from the origin to the target module. |
||
314 | */ |
||
315 | 18 | public function hasPath($moduleName, $dependency) |
|
336 | |||
337 | /** |
||
338 | * Returns the path from a module name to a dependency. |
||
339 | * |
||
340 | * @param string $moduleName The module name. |
||
341 | * @param string $dependency The name of the dependency. |
||
342 | * |
||
343 | * @return null|string[] The sorted module names on the path or `null` if no |
||
|
|||
344 | * path was found. |
||
345 | */ |
||
346 | 65 | public function getPath($moduleName, $dependency) |
|
354 | |||
355 | /** |
||
356 | * Finds a path between modules using Depth-First Search. |
||
357 | * |
||
358 | * @param string $moduleName The end module name. |
||
359 | * @param string $dependency The start module name. |
||
360 | * @param array $reversePath The path in reverse order. |
||
361 | * |
||
362 | * @return bool Whether a path was found. |
||
363 | */ |
||
364 | 65 | private function getPathDFS($moduleName, $dependency, &$reversePath = array()) |
|
389 | |||
390 | /** |
||
391 | * Topologically sorts the given module name into the output array. |
||
392 | * |
||
393 | * The resulting array is sorted such that all predecessors of the module |
||
394 | * come before the module (and their predecessors before them, and so on). |
||
395 | * |
||
396 | * @param string $currentName The current module name to sort. |
||
397 | * @param array $namesToSort The module names yet to be sorted. |
||
398 | * @param array $output The output array. |
||
399 | */ |
||
400 | 82 | private function sortModulesDFS($currentName, array &$namesToSort, array &$output) |
|
418 | } |
||
419 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.If the return type contains the type array, this check recommends the use of a more specific type like
String[]
orarray<String>
.