Complex classes like GeneratorFactory 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 GeneratorFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class GeneratorFactory { |
||
41 | 2 | ||
42 | 1 | /** @var CommandService */ |
|
43 | private $service; |
||
44 | 2 | ||
45 | 2 | /** @var ActionNameGenerator */ |
|
46 | private $actionNameGenerator; |
||
47 | 1 | ||
48 | 1 | /** @var ActionClassNameGenerator */ |
|
49 | private $actionClassNameGenerator; |
||
50 | 1 | ||
51 | 1 | /** @var ActionTitleGenerator */ |
|
52 | private $actionTitleGenerator; |
||
53 | 1 | ||
54 | 1 | /** @var NamespaceGenerator */ |
|
55 | private $namespaceGenerator; |
||
56 | |||
57 | /** @var ResponderClassNameGenerator */ |
||
58 | private $responderClassNameGenerator; |
||
59 | |||
60 | /** @var RelationshipMethodNameGenerator */ |
||
61 | private $relationshipMethodNameGenerator; |
||
62 | |||
63 | public function __construct(CommandService $service) { |
||
66 | |||
67 | /** |
||
68 | * Creates a new package generator |
||
69 | * |
||
70 | * @param string $type |
||
71 | * @param CommandService $this->serivce |
||
|
|||
72 | * @return AbstractPackageGenerator |
||
73 | */ |
||
74 | public function createPackageGenerator($type) { |
||
83 | |||
84 | /** |
||
85 | * Creates a generator for the given trait type |
||
86 | * |
||
87 | * @param string $type |
||
88 | * @return AbstractModelActionGenerator |
||
89 | */ |
||
90 | public function createModelActionGenerator($type) { |
||
108 | |||
109 | /** |
||
110 | * Creates a generator for a relationship action |
||
111 | * |
||
112 | * @param string $type |
||
113 | * @param Relationship $relationship |
||
114 | * @return AbstractActionGenerator |
||
115 | */ |
||
116 | public function createRelationshipActionGenerator($type, Relationship $relationship) { |
||
141 | |||
142 | /** |
||
143 | * Creates a generator for the given json respose |
||
144 | * |
||
145 | * @param string $type |
||
146 | * @param CommandService $this->service |
||
147 | * @return AbstractModelJsonResponderGenerator |
||
148 | */ |
||
149 | public function createModelJsonResponderGenerator($type) { |
||
167 | |||
168 | /** |
||
169 | * Creates a json generator for a relationship |
||
170 | * |
||
171 | * @param Relationship $relationship |
||
172 | * @return AbstractModelJsonResponderGenerator |
||
173 | */ |
||
174 | public function createRelationshipJsonResponderGenerator(Relationship $relationship) { |
||
179 | |||
180 | /** |
||
181 | * Creates a payload responder for the given format |
||
182 | * |
||
183 | * @param string $format |
||
184 | * @return AbstractResponderGenerator |
||
185 | */ |
||
186 | public function createPayloadResponderGenerator($format) { |
||
195 | |||
196 | /** |
||
197 | * @return ActionNameGenerator |
||
198 | */ |
||
199 | public function getActionNameGenerator() { |
||
206 | |||
207 | /** |
||
208 | * @return ActionClassNameGenerator |
||
209 | */ |
||
210 | public function getActionClassNameGenerator() { |
||
217 | |||
218 | /** |
||
219 | * @return ActionTitleGenerator |
||
220 | */ |
||
221 | public function getActionTitleGenerator() { |
||
228 | |||
229 | /** |
||
230 | * @return NamespaceGenerator |
||
231 | */ |
||
232 | public function getNamespaceGenerator() { |
||
239 | |||
240 | /** |
||
241 | * @return ResponderClassNameGenerator |
||
242 | */ |
||
243 | public function getResponderClassNameGenerator() { |
||
250 | |||
251 | /** |
||
252 | * @return RelationshipMethodNameGenerator |
||
253 | */ |
||
254 | public function getRelationshipMethodNameGenerator() { |
||
261 | } |
||
262 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.