| Total Complexity | 42 |
| Total Lines | 265 |
| 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) |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param $tv_name |
||
| 142 | * @return $this |
||
| 143 | */ |
||
| 144 | public function detachTemplateVariable($tv_name) |
||
| 145 | { |
||
| 146 | if (!isset($this->related_data['detach'])) { |
||
| 147 | $this->related_data['detach'] = []; |
||
| 148 | } |
||
| 149 | $this->related_data['detach'][] = $tv_name; |
||
| 150 | return $this; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @deprecated use detachTemplateVariable |
||
| 155 | * @param $tv_name |
||
| 156 | * @return $this |
||
| 157 | */ |
||
| 158 | public function detachTV($tv_name) |
||
| 159 | { |
||
| 160 | return $this->detachTemplateVariable($tv_name); |
||
| 161 | } |
||
| 162 | |||
| 163 | protected function attachRelatedPieces() |
||
| 164 | { |
||
| 165 | if (isset($this->related_data['seeds'])) { |
||
| 166 | foreach ($this->related_data['seeds'] as $tv) { |
||
| 167 | // blend the the TV from seed: |
||
| 168 | $tvSeed = new TemplateVariable($this->modx, $this->blender, $tv['name']); |
||
| 169 | $tvSeed |
||
| 170 | ->setSeedsDir($this->getSeedsDir()) |
||
| 171 | ->blendFromSeed($tv['seed_key'], true); |
||
| 172 | |||
| 173 | $this->attachTemplateVariable($tv['name'], $tv['rank']); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | if (isset($this->related_data['attach']) && count($this->related_data['attach']) > 0) { |
||
| 178 | $tvs = []; |
||
| 179 | foreach ($this->related_data['attach'] as $tv_name_data) { |
||
| 180 | // get the TV: |
||
| 181 | $tv = $this->modx->getObject('modTemplateVar', ['name' => $tv_name_data['name']]); |
||
| 182 | if ($tv) { |
||
| 183 | $tvt = $this->modx->getObject('modTemplateVarTemplate', ['tmplvarid' => $tv->get('id'), 'templateid' => $this->xPDOSimpleObject->getPrimaryKey()]); |
||
|
|
|||
| 184 | |||
| 185 | if (!$tvt) { |
||
| 186 | $tvt = $this->modx->newObject('modTemplateVarTemplate'); |
||
| 187 | } |
||
| 188 | $tvt->set('tmplvarid', $tv->get('id')); |
||
| 189 | $tvt->set('rand', $tv_name_data['rank']); |
||
| 190 | |||
| 191 | $tvs[] = $tvt; |
||
| 192 | } else { |
||
| 193 | $this->error = true; |
||
| 194 | |||
| 195 | } |
||
| 196 | |||
| 197 | } |
||
| 198 | $this->xPDOSimpleObject->addMany($tvs, 'TemplateVarTemplates'); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | protected function attachRelatedPiecesAfterSave() |
||
| 203 | { |
||
| 204 | if (isset($this->related_data['detach']) && count($this->related_data['detach']) > 0) { |
||
| 205 | foreach ($this->related_data['detach'] as $tv_name) { |
||
| 206 | // get the TV: |
||
| 207 | $tv = $this->modx->getObject('modTemplateVar', ['name' => $tv_name]); |
||
| 208 | if ($tv) { |
||
| 209 | $templateVarTemplate = $this->modx->getObject('modTemplateVarTemplate', array( |
||
| 210 | 'tmplvarid' => $tv->get('id'), |
||
| 211 | 'templateid' => $this->xPDOSimpleObject->get('id'), |
||
| 212 | )); |
||
| 213 | if ($templateVarTemplate && $templateVarTemplate instanceof \modTemplateVarTemplate) { |
||
| 214 | $templateVarTemplate->remove(); |
||
| 215 | } |
||
| 216 | } |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * |
||
| 223 | */ |
||
| 224 | protected function onDeleteRevertRelatedPieces() |
||
| 225 | { |
||
| 226 | if (!isset($this->related_data['seeds']) || empty($this->related_data['seeds'])) { |
||
| 227 | // If this is a seed then the TVs are not in the revert file but the seed file |
||
| 228 | $name = $this->getFieldTemplateName(); |
||
| 229 | if (empty ($name) && isset($this->current_xpdo_simple_object_data['templatename'])) { |
||
| 230 | $name = $this->current_xpdo_simple_object_data['templatename']; |
||
| 231 | } |
||
| 232 | $this->loadObjectDataFromSeed($this->blender->getSeedKeyFromName($name)); |
||
| 233 | } |
||
| 234 | |||
| 235 | if (isset($this->related_data['seeds'])) { |
||
| 236 | |||
| 237 | foreach ($this->related_data['seeds'] as $tv) { |
||
| 238 | // seed the TV: |
||
| 239 | $tvSeed = new TemplateVariable($this->modx, $this->blender, $tv['name']); |
||
| 240 | $tvSeed |
||
| 241 | ->setSeedsDir($this->getSeedsDir()) |
||
| 242 | ->revertBlend(); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @var string $type blend or revert |
||
| 249 | */ |
||
| 250 | protected function seedRelated($type = 'blend') |
||
| 279 | } |
||
| 280 | |||
| 281 | } |
||
| 282 | } |
||
| 283 |
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.