| Total Complexity | 42 |
| Total Lines | 275 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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() |
||
| 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) |
||
| 84 | } |
||
| 85 | /** |
||
| 86 | * @param string $name |
||
| 87 | * @return $this |
||
| 88 | * @throws \LCI\Blend\Exception\BlendableKeyLengthException |
||
| 89 | */ |
||
| 90 | public function setFieldName($name) |
||
| 91 | { |
||
| 92 | return $this->setFieldTemplateName($name); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $name |
||
| 97 | * @return $this |
||
| 98 | * @throws \LCI\Blend\Exception\BlendableKeyLengthException |
||
| 99 | */ |
||
| 100 | public function setFieldTemplateName($name) |
||
| 101 | { |
||
| 102 | $this->setUniqueCriteria($name); |
||
| 103 | return $this; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $name |
||
| 108 | * @return $this |
||
| 109 | */ |
||
| 110 | public function setFieldTemplateType($name) |
||
| 111 | { |
||
| 112 | $this->blendable_xpdo_simple_object_data['template_type'] = $name; |
||
| 113 | return $this; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param array $tvs |
||
| 118 | */ |
||
| 119 | public function setTvs($tvs) |
||
| 120 | { |
||
| 121 | $this->tv_seeds = $tvs; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $tv_name |
||
| 126 | * @param int $rank |
||
| 127 | * |
||
| 128 | * @return $this |
||
| 129 | */ |
||
| 130 | public function attachTemplateVariable($tv_name, $rank = 0) |
||
| 131 | { |
||
| 132 | if (!isset($this->related_data['attach'])) { |
||
| 133 | $this->related_data['attach'] = []; |
||
| 134 | } |
||
| 135 | $this->related_data['attach'][] = [ |
||
| 136 | 'name' => $tv_name, |
||
| 137 | 'rank' => $rank |
||
| 138 | ]; |
||
| 139 | return $this; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param $tv_name |
||
| 144 | * @return $this |
||
| 145 | */ |
||
| 146 | public function detachTemplateVariable($tv_name) |
||
| 147 | { |
||
| 148 | if (!isset($this->related_data['detach'])) { |
||
| 149 | $this->related_data['detach'] = []; |
||
| 150 | } |
||
| 151 | $this->related_data['detach'][] = $tv_name; |
||
| 152 | return $this; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @deprecated use detachTemplateVariable |
||
| 157 | * @param $tv_name |
||
| 158 | * @return $this |
||
| 159 | */ |
||
| 160 | public function detachTV($tv_name) |
||
| 161 | { |
||
| 162 | return $this->detachTemplateVariable($tv_name); |
||
| 163 | } |
||
| 164 | |||
| 165 | protected function attachRelatedPieces() |
||
| 166 | { |
||
| 167 | if (isset($this->related_data['seeds'])) { |
||
| 168 | foreach ($this->related_data['seeds'] as $tv) { |
||
| 169 | // blend the the TV from seed: |
||
| 170 | $tvSeed = new TemplateVariable($this->modx, $this->blender, $tv['name']); |
||
| 171 | $tvSeed |
||
| 172 | ->setSeedsDir($this->getSeedsDir()) |
||
| 173 | ->blendFromSeed($tv['seed_key'], true); |
||
| 174 | |||
| 175 | $this->attachTemplateVariable($tv['name'], $tv['rank']); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | if (isset($this->related_data['attach']) && count($this->related_data['attach']) > 0) { |
||
| 180 | $tvs = []; |
||
| 181 | foreach ($this->related_data['attach'] as $tv_name_data) { |
||
| 182 | // get the TV: |
||
| 183 | $tv = $this->modx->getObject('modTemplateVar', ['name' => $tv_name_data['name']]); |
||
| 184 | if ($tv) { |
||
| 185 | $tvt = $this->modx->getObject('modTemplateVarTemplate', ['tmplvarid' => $tv->get('id'), 'templateid' => $this->xPDOSimpleObject->getPrimaryKey()]); |
||
|
|
|||
| 186 | |||
| 187 | if (!$tvt) { |
||
| 188 | $tvt = $this->modx->newObject('modTemplateVarTemplate'); |
||
| 189 | } |
||
| 190 | $tvt->set('tmplvarid', $tv->get('id')); |
||
| 191 | $tvt->set('rand', $tv_name_data['rank']); |
||
| 192 | |||
| 193 | $tvs[] = $tvt; |
||
| 194 | } else { |
||
| 195 | $this->error = true; |
||
| 196 | |||
| 197 | } |
||
| 198 | |||
| 199 | } |
||
| 200 | $this->xPDOSimpleObject->addMany($tvs, 'TemplateVarTemplates'); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | protected function attachRelatedPiecesAfterSave() |
||
| 205 | { |
||
| 206 | if (isset($this->related_data['detach']) && count($this->related_data['detach']) > 0) { |
||
| 207 | foreach ($this->related_data['detach'] as $tv_name) { |
||
| 208 | // get the TV: |
||
| 209 | $tv = $this->modx->getObject('modTemplateVar', ['name' => $tv_name]); |
||
| 210 | if ($tv) { |
||
| 211 | $templateVarTemplate = $this->modx->getObject('modTemplateVarTemplate', array( |
||
| 212 | 'tmplvarid' => $tv->get('id'), |
||
| 213 | 'templateid' => $this->xPDOSimpleObject->get('id'), |
||
| 214 | )); |
||
| 215 | if ($templateVarTemplate && $templateVarTemplate instanceof \modTemplateVarTemplate) { |
||
| 216 | $templateVarTemplate->remove(); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * |
||
| 225 | */ |
||
| 226 | protected function onDeleteRevertRelatedPieces() |
||
| 227 | { |
||
| 228 | if (!isset($this->related_data['seeds']) || empty($this->related_data['seeds'])) { |
||
| 229 | // If this is a seed then the TVs are not in the revert file but the seed file |
||
| 230 | $name = $this->getFieldTemplateName(); |
||
| 231 | if (empty ($name) && isset($this->current_xpdo_simple_object_data['templatename'])) { |
||
| 232 | $name = $this->current_xpdo_simple_object_data['templatename']; |
||
| 233 | } |
||
| 234 | $this->loadObjectDataFromSeed($this->blender->getSeedKeyFromName($name)); |
||
| 235 | } |
||
| 236 | |||
| 237 | if (isset($this->related_data['seeds'])) { |
||
| 238 | |||
| 239 | foreach ($this->related_data['seeds'] as $tv) { |
||
| 240 | // seed the TV: |
||
| 241 | $tvSeed = new TemplateVariable($this->modx, $this->blender, $tv['name']); |
||
| 242 | $tvSeed |
||
| 243 | ->setSeedsDir($this->getSeedsDir()) |
||
| 244 | ->revertBlend(); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @var string $type blend or revert |
||
| 251 | */ |
||
| 252 | protected function seedRelated($type = 'blend') |
||
| 289 | } |
||
| 290 | |||
| 291 | } |
||
| 292 | } |
||
| 293 |
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.