Total Complexity | 40 |
Total Lines | 249 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Template 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.
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 Template, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Template extends Element |
||
15 | { |
||
16 | /** @var string */ |
||
17 | protected $opt_cache_key = 'elements/templates'; |
||
18 | |||
19 | /** @var string ~ the xPDO class name */ |
||
20 | protected $xpdo_simple_object_class = 'modTemplate'; |
||
21 | |||
22 | /** @var string */ |
||
23 | protected $unique_key_column = 'templatename'; |
||
24 | |||
25 | /** @var array */ |
||
26 | protected $tv_names = []; |
||
27 | |||
28 | /** @var array */ |
||
29 | protected $detach_tvs = []; |
||
30 | |||
31 | /** @var array */ |
||
32 | protected $tv_seeds = []; |
||
33 | |||
34 | /** |
||
35 | * @return \LCI\Blend\Blendable\Template |
||
36 | */ |
||
37 | public function getCurrentVersion() |
||
38 | { |
||
39 | /** @var \LCI\Blend\Blendable\Template $element */ |
||
40 | $element = new self($this->modx, $this->blender, $this->getFieldName()); |
||
41 | return $element->setSeedsDir($this->getSeedsDir()); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @return string |
||
46 | */ |
||
47 | public function getFieldIcon() |
||
48 | { |
||
49 | return $this->blendable_xpdo_simple_object_data['icon']; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @return string |
||
54 | */ |
||
55 | public function getFieldName() |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @return string |
||
62 | */ |
||
63 | public function getFieldTemplateName() |
||
64 | { |
||
65 | return $this->blendable_xpdo_simple_object_data['templatename']; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @return string |
||
70 | */ |
||
71 | public function getFieldTemplateType() |
||
72 | { |
||
73 | return $this->blendable_xpdo_simple_object_data['template_type']; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param string $icon |
||
78 | * @return $this |
||
79 | */ |
||
80 | public function setFieldIcon($icon) |
||
81 | { |
||
82 | $this->blendable_xpdo_simple_object_data['icon'] = $icon; |
||
83 | return $this; |
||
84 | } |
||
85 | /** |
||
86 | * @param string $name |
||
87 | * @return $this |
||
88 | */ |
||
89 | public function setFieldName($name) |
||
90 | { |
||
91 | return $this->setFieldTemplateName($name); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param string $name |
||
96 | * @return $this |
||
97 | */ |
||
98 | public function setFieldTemplateName($name) |
||
99 | { |
||
100 | $this->blendable_xpdo_simple_object_data['templatename'] = $name; |
||
101 | return $this; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param string $name |
||
106 | * @return $this |
||
107 | */ |
||
108 | public function setFieldTemplateType($name) |
||
109 | { |
||
110 | $this->blendable_xpdo_simple_object_data['template_type'] = $name; |
||
111 | return $this; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param array $tvs |
||
116 | */ |
||
117 | public function setTvs($tvs) |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param string $tv_name |
||
124 | * @param int $rank |
||
125 | * |
||
126 | * @return $this |
||
127 | */ |
||
128 | public function attachTemplateVariable($tv_name, $rank = 0) |
||
129 | { |
||
130 | if (!isset($this->related_data['attach'])) { |
||
131 | $this->related_data['attach'] = []; |
||
132 | } |
||
133 | $this->related_data['attach'][] = [ |
||
134 | 'name' => $tv_name, |
||
135 | 'rank' => $rank |
||
136 | ]; |
||
137 | return $this; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param $tv_name |
||
142 | * @return $this |
||
143 | */ |
||
144 | public function detachTV($tv_name) |
||
151 | } |
||
152 | |||
153 | protected function attachRelatedPieces() |
||
185 | } |
||
186 | } |
||
187 | |||
188 | protected function attachRelatedPiecesAfterSave() |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * |
||
209 | */ |
||
210 | protected function onDeleteRevertRelatedPieces() |
||
211 | { |
||
212 | if (!isset($this->related_data['seeds']) || empty($this->related_data['seeds'])) { |
||
213 | // If this is a seed then the TVs are not in the revert file but the seed file |
||
214 | $name = $this->getFieldTemplateName(); |
||
215 | if (empty ($name) && isset($this->current_xpdo_simple_object_data['templatename'])) { |
||
216 | $name = $this->current_xpdo_simple_object_data['templatename']; |
||
217 | } |
||
218 | $this->loadObjectDataFromSeed($this->blender->getSeedKeyFromName($name)); |
||
219 | } |
||
220 | |||
221 | if (isset($this->related_data['seeds'])) { |
||
222 | |||
223 | foreach ($this->related_data['seeds'] as $tv) { |
||
224 | // seed the TV: |
||
225 | $tvSeed = new TemplateVariable($this->modx, $this->blender, $tv['name']); |
||
226 | $tvSeed |
||
227 | ->setSeedsDir($this->getSeedsDir()) |
||
228 | ->revertBlend(); |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @var string $type blend or revert |
||
235 | */ |
||
236 | protected function seedRelated($type = 'blend') |
||
263 | } |
||
264 | |||
267 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.