Complex classes like WidgetMap 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 WidgetMap, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class WidgetMap |
||
16 | { |
||
17 | const ACTION_CREATE = 'create'; |
||
18 | const ACTION_OVERWRITE = 'overwrite'; |
||
19 | const ACTION_DELETE = 'delete'; |
||
20 | |||
21 | const POSITION_BEFORE = 'before'; |
||
22 | const POSITION_AFTER = 'after'; |
||
23 | |||
24 | /** |
||
25 | * @var int |
||
26 | * |
||
27 | * @ORM\Column(name="id", type="integer") |
||
28 | * @ORM\Id |
||
29 | * @ORM\GeneratedValue(strategy="AUTO") |
||
30 | */ |
||
31 | protected $id; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | * |
||
36 | * @ORM\Column(name="action", type="string", length=255) |
||
37 | */ |
||
38 | protected $action = null; |
||
39 | |||
40 | /** |
||
41 | * @var View |
||
42 | * |
||
43 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\CoreBundle\Entity\View", inversedBy="widgetMaps") |
||
44 | * @ORM\JoinColumn(name="view_id", referencedColumnName="id", onDelete="cascade") |
||
45 | */ |
||
46 | protected $view; |
||
47 | |||
48 | /** |
||
49 | * A WidgetMap has a View but also a contextualView (not persisted). |
||
50 | * This contextualView is set when WidgetMap is build. |
||
51 | * When getChilds and getSubstitutes are called, we use this contextualView to retrieve |
||
52 | * concerned WidgetMaps in order to avoid useless Doctrine queries. |
||
53 | * |
||
54 | * @var View |
||
55 | */ |
||
56 | protected $contextualView; |
||
57 | |||
58 | /** |
||
59 | * @var [Widget] |
||
60 | * |
||
61 | * @ORM\OneToMany(targetEntity="\Victoire\Bundle\WidgetBundle\Entity\Widget", mappedBy="widgetMap", orphanRemoval=true, cascade={"persist", "remove"}) |
||
62 | */ |
||
63 | protected $widgets; |
||
64 | |||
65 | /** |
||
66 | * @deprecated Remove Doctrine mapping and property |
||
67 | * |
||
68 | * @var Widget |
||
69 | * |
||
70 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\WidgetBundle\Entity\Widget") |
||
71 | * @ORM\JoinColumn(name="widget_id", referencedColumnName="id", onDelete="SET NULL") |
||
72 | */ |
||
73 | protected $widget; |
||
74 | |||
75 | /** |
||
76 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap", inversedBy="substitutes") |
||
77 | * @ORM\JoinColumn(name="replaced_id", referencedColumnName="id") |
||
78 | */ |
||
79 | protected $replaced; |
||
80 | |||
81 | /** |
||
82 | * @var ArrayCollection |
||
83 | * @ORM\OneToMany(targetEntity="\Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap", mappedBy="replaced") |
||
84 | */ |
||
85 | protected $substitutes; |
||
86 | |||
87 | /** |
||
88 | * @var string |
||
89 | * |
||
90 | * @ORM\Column(name="asynchronous", type="boolean") |
||
91 | */ |
||
92 | protected $asynchronous = false; |
||
93 | |||
94 | /** |
||
95 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap", inversedBy="children") |
||
96 | * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL") |
||
97 | */ |
||
98 | protected $parent; |
||
99 | |||
100 | /** |
||
101 | * @var string |
||
102 | * |
||
103 | * @ORM\Column(name="position", type="string", nullable=true) |
||
104 | */ |
||
105 | protected $position; |
||
106 | |||
107 | /** |
||
108 | * @var Collection |
||
109 | * @ORM\OneToMany(targetEntity="\Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap", mappedBy="parent") |
||
110 | */ |
||
111 | protected $children; |
||
112 | |||
113 | /** |
||
114 | * @var string |
||
115 | * |
||
116 | * @ORM\Column(name="slot", type="string", length=255, nullable=true) |
||
117 | */ |
||
118 | protected $slot; |
||
119 | |||
120 | public function __construct() |
||
|
|||
121 | { |
||
122 | $this->children = new ArrayCollection(); |
||
123 | $this->substitutes = new ArrayCollection(); |
||
124 | $this->widgets = new ArrayCollection(); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @return int |
||
129 | */ |
||
130 | public function getId() |
||
134 | |||
135 | public function setId($id) |
||
139 | |||
140 | /** |
||
141 | * @return string |
||
142 | */ |
||
143 | public function isAsynchronous() |
||
147 | |||
148 | /** |
||
149 | * @param bool|string $asynchronous |
||
150 | */ |
||
151 | public function setAsynchronous($asynchronous) |
||
155 | |||
156 | /** |
||
157 | * Set the action. |
||
158 | * |
||
159 | * @param string $action |
||
160 | * |
||
161 | * @throws \Exception The action is not valid |
||
162 | */ |
||
163 | public function setAction($action) |
||
172 | |||
173 | /** |
||
174 | * Get the action. |
||
175 | * |
||
176 | * @return string The action |
||
177 | */ |
||
178 | public function getAction() |
||
182 | |||
183 | /** |
||
184 | * @return [Widget] |
||
185 | */ |
||
186 | public function getWidgets() |
||
190 | |||
191 | /** |
||
192 | * @param Widget $widget |
||
193 | * |
||
194 | * @return $this |
||
195 | */ |
||
196 | public function addWidget(Widget $widget) |
||
197 | { |
||
198 | if (!$this->widgets->contains($widget)) { |
||
199 | $this->widgets->add($widget); |
||
200 | } |
||
201 | |||
202 | return $this; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @param [Widget] $widgets |
||
207 | * |
||
208 | * @return $this |
||
209 | */ |
||
210 | public function setWidgets($widgets) |
||
216 | |||
217 | /** |
||
218 | * @return View |
||
219 | */ |
||
220 | public function getView() |
||
224 | |||
225 | /** |
||
226 | * @param View $view |
||
227 | */ |
||
228 | public function setView(View $view) |
||
232 | |||
233 | /** |
||
234 | * Get the current View context. |
||
235 | * |
||
236 | * @return View |
||
237 | */ |
||
238 | public function getContextualView() |
||
242 | |||
243 | /** |
||
244 | * Store the current View context. |
||
245 | * |
||
246 | * @param View $contextualView |
||
247 | * |
||
248 | * @return $this |
||
249 | */ |
||
250 | public function setContextualView(View $contextualView) |
||
256 | |||
257 | /** |
||
258 | * @return WidgetMap |
||
259 | */ |
||
260 | public function getReplaced() |
||
264 | |||
265 | /** |
||
266 | * @param WidgetMap $replaced |
||
267 | */ |
||
268 | public function setReplaced($replaced) |
||
275 | |||
276 | /** |
||
277 | * @return string |
||
278 | */ |
||
279 | public function getSlot() |
||
283 | |||
284 | /** |
||
285 | * @param string $slot |
||
286 | */ |
||
287 | public function setSlot($slot) |
||
291 | |||
292 | /** |
||
293 | * @return WidgetMap|null |
||
294 | */ |
||
295 | public function getChild($position) |
||
306 | |||
307 | /** |
||
308 | * Return all children from contextual View (already loaded WidgetMaps) |
||
309 | * for a given position. |
||
310 | * |
||
311 | * @return WidgetMap[] |
||
312 | */ |
||
313 | public function getContextualChildren($position) |
||
326 | |||
327 | /** |
||
328 | * @return WidgetMap[] |
||
329 | */ |
||
330 | public function getChildren() |
||
334 | |||
335 | /** |
||
336 | * @param WidgetMap[] $children |
||
337 | */ |
||
338 | public function setChildren($children) |
||
342 | |||
343 | /** |
||
344 | * @param WidgetMap $child |
||
345 | */ |
||
346 | public function addChild($child) |
||
350 | |||
351 | /** |
||
352 | * @param WidgetMap $child |
||
353 | */ |
||
354 | public function removeChild($child) |
||
358 | |||
359 | /** |
||
360 | * @return WidgetMap|null |
||
361 | */ |
||
362 | public function getParent() |
||
366 | |||
367 | /** |
||
368 | * @param null|WidgetMap $parent |
||
369 | */ |
||
370 | public function setParent(WidgetMap $parent = null) |
||
380 | |||
381 | /** |
||
382 | * @return string |
||
383 | */ |
||
384 | public function getPosition() |
||
388 | |||
389 | /** |
||
390 | * @param string $position |
||
391 | */ |
||
392 | public function setPosition($position) |
||
396 | |||
397 | /** |
||
398 | * Return all substitutes from contextual View (already loaded WidgetMaps) |
||
399 | * Ideally must return only one WidgetMap per View. |
||
400 | * |
||
401 | * @return WidgetMap[] |
||
402 | */ |
||
403 | public function getContextualSubstitutes() |
||
416 | |||
417 | /** |
||
418 | * Return substitute if used in View or in one of its inherited Template. |
||
419 | * |
||
420 | * @return WidgetMap|null |
||
421 | */ |
||
422 | public function getSubstituteForView(View $view) |
||
436 | |||
437 | /** |
||
438 | * Return all Substitutes (not based on contextual View). |
||
439 | * |
||
440 | * @return ArrayCollection |
||
441 | */ |
||
442 | public function getAllSubstitutes() |
||
446 | |||
447 | /** |
||
448 | * @param WidgetMap $substitute |
||
449 | */ |
||
450 | public function addSubstitute(WidgetMap $substitute) |
||
454 | |||
455 | /** |
||
456 | * @param [WidgetMap] $substitutes |
||
457 | */ |
||
458 | public function setSubstitutes($substitutes) |
||
462 | |||
463 | /** |
||
464 | * @deprecated |
||
465 | * |
||
466 | * @return Widget |
||
467 | */ |
||
468 | public function getWidget() |
||
472 | |||
473 | /** |
||
474 | * @deprecated |
||
475 | * |
||
476 | * @param Widget $widget |
||
477 | * |
||
478 | * @return WidgetMap |
||
479 | */ |
||
480 | public function setWidget(Widget $widget = null) |
||
486 | } |
||
487 |