Complex classes like SchemaModel 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 SchemaModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class SchemaModel implements ContainerAwareInterface |
||
19 | { |
||
20 | /** |
||
21 | * object |
||
22 | */ |
||
23 | private $schema; |
||
24 | |||
25 | /** |
||
26 | * @var ContainerInterface |
||
27 | */ |
||
28 | private $container; |
||
29 | |||
30 | /** |
||
31 | * load some schema info for the model |
||
32 | */ |
||
33 | public function __construct() |
||
61 | |||
62 | /** |
||
63 | * inject container |
||
64 | * |
||
65 | * @param ContainerInterface $container service container |
||
|
|||
66 | * |
||
67 | * @return self |
||
68 | */ |
||
69 | public function setContainer(ContainerInterface $container = null) |
||
75 | |||
76 | /** |
||
77 | * get Title |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | public function getTitle() |
||
85 | |||
86 | /** |
||
87 | * get description |
||
88 | * |
||
89 | * @return string Description |
||
90 | */ |
||
91 | public function getDescription() |
||
95 | |||
96 | /** |
||
97 | * get recordOriginModifiable |
||
98 | * |
||
99 | * @return bool |
||
100 | */ |
||
101 | public function getRecordOriginModifiable() |
||
108 | |||
109 | /** |
||
110 | * get isVersioning |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | public function isVersioning() |
||
122 | |||
123 | /** |
||
124 | * Returns the bare schema |
||
125 | * |
||
126 | * @return \stdClass Schema |
||
127 | */ |
||
128 | public function getSchema() |
||
132 | |||
133 | /** |
||
134 | * get title for a given field |
||
135 | * |
||
136 | * @param string $field field name |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | public function getTitleOfField($field) |
||
144 | |||
145 | /** |
||
146 | * get description for a given field |
||
147 | * |
||
148 | * @param string $field field name |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | public function getDescriptionOfField($field) |
||
156 | |||
157 | /** |
||
158 | * get groups for a given field |
||
159 | * |
||
160 | * @param string $field field name |
||
161 | * |
||
162 | * @return array<string> group names |
||
163 | */ |
||
164 | public function getGroupsOfField($field) |
||
168 | |||
169 | /** |
||
170 | * get property model for embedded field |
||
171 | * |
||
172 | * @param string $mapping name of mapping class |
||
173 | * |
||
174 | * @return $this |
||
175 | */ |
||
176 | public function manyPropertyModelForTarget($mapping) |
||
188 | |||
189 | /** |
||
190 | * get required fields for this object |
||
191 | * |
||
192 | * @param string $variationName a variation that can alter which fields are required |
||
193 | * |
||
194 | * @return string[] |
||
195 | */ |
||
196 | public function getRequiredFields($variationName = null) |
||
230 | |||
231 | /** |
||
232 | * get a collection of service names that can extref refer to |
||
233 | * |
||
234 | * @param string $field field name |
||
235 | * |
||
236 | * @return array |
||
237 | */ |
||
238 | public function getRefCollectionOfField($field) |
||
242 | |||
243 | /** |
||
244 | * get readOnly flag for a given field |
||
245 | * |
||
246 | * @param string $field field name |
||
247 | * |
||
248 | * @return boolean the readOnly flag |
||
249 | */ |
||
250 | public function getReadOnlyOfField($field) |
||
254 | |||
255 | /** |
||
256 | * get readOnly flag for a given field |
||
257 | * |
||
258 | * @param string $field field name |
||
259 | * |
||
260 | * @return boolean the readOnly flag |
||
261 | */ |
||
262 | public function getRecordOriginExceptionOfField($field) |
||
266 | |||
267 | /** |
||
268 | * get searchable flag for a given field, weight based. |
||
269 | * |
||
270 | * @param string $field field name |
||
271 | * |
||
272 | * @return integer the searchable flag |
||
273 | */ |
||
274 | public function getSearchableOfField($field) |
||
278 | |||
279 | /** |
||
280 | * tell us if a model what to be exposed using a key as field |
||
281 | * |
||
282 | * @param string $field field that we check for dynamic-key spec |
||
283 | * |
||
284 | * @return boolean |
||
285 | */ |
||
286 | public function hasDynamicKey($field) |
||
290 | |||
291 | /** |
||
292 | * Get field used for setting stringy key value |
||
293 | * |
||
294 | * @param string $field field that we get dynamic-key spec from |
||
295 | * |
||
296 | * @return object |
||
297 | */ |
||
298 | public function getDynamicKeySpec($field) |
||
302 | |||
303 | /** |
||
304 | * Gets the defined document class in shortform from schema |
||
305 | * |
||
306 | * @return string|false either the document class or false it not given |
||
307 | */ |
||
308 | public function getDocumentClass() |
||
316 | |||
317 | /** |
||
318 | * Get defined constraints on this field (if any) |
||
319 | * |
||
320 | * @param string $field field that we get constraints spec from |
||
321 | * |
||
322 | * @return object |
||
323 | */ |
||
324 | public function getConstraints($field) |
||
328 | |||
329 | /** |
||
330 | * Get defined onVariation on this field (if any) |
||
331 | * |
||
332 | * @param string $field field that we get constraints spec from |
||
333 | * |
||
334 | * @return object |
||
335 | */ |
||
336 | public function getOnVariaton($field) |
||
340 | |||
341 | /** |
||
342 | * get variations |
||
343 | * |
||
344 | * @return array variations |
||
345 | */ |
||
346 | public function getVariations() |
||
353 | |||
354 | /** |
||
355 | * Tells us if in this model, the ID can be given on a POST request or not (in the payload). |
||
356 | * This basically depends on if the "id" property is given in the JSON definition or not. |
||
357 | * |
||
358 | * @return bool true if yes, false otherwise |
||
359 | */ |
||
360 | public function isIdInPostAllowed() |
||
368 | |||
369 | /** |
||
370 | * get schema field value |
||
371 | * |
||
372 | * @param string $field field name |
||
373 | * @param string $property property name |
||
374 | * @param mixed $fallbackValue fallback value if property isn't set |
||
375 | * |
||
376 | * @return mixed |
||
377 | */ |
||
378 | private function getSchemaField($field, $property, $fallbackValue = '') |
||
386 | |||
387 | /** |
||
388 | * get searchable fields for this object |
||
389 | * |
||
390 | * @return string[] |
||
391 | */ |
||
392 | public function getSearchableFields() |
||
396 | } |
||
397 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.