| Total Complexity | 49 |
| Total Lines | 325 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Context 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 Context, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Context extends Blendable |
||
| 11 | { |
||
| 12 | use DescriptionGetterAndSetter; |
||
| 13 | |||
| 14 | /** @var string */ |
||
| 15 | protected $opt_cache_key = 'contexts'; |
||
| 16 | |||
| 17 | /** @var string ex: modContext */ |
||
| 18 | protected $xpdo_simple_object_class = 'modContext'; |
||
| 19 | |||
| 20 | /** @var string */ |
||
| 21 | protected $unique_key_column = 'key'; |
||
| 22 | |||
| 23 | /** @var array ~ this should match data to be inserted via xPDO, ex [column_name => value, ...] */ |
||
| 24 | protected $blendable_xpdo_simple_object_data = [ |
||
| 25 | 'description' => '', |
||
| 26 | 'key' => '', |
||
| 27 | 'name' => '', |
||
| 28 | 'rank' => 0, |
||
| 29 | ]; |
||
| 30 | |||
| 31 | /** @var array */ |
||
| 32 | protected $portable_settings = []; |
||
| 33 | |||
| 34 | protected $remove_settings = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Resource constructor. |
||
| 38 | * |
||
| 39 | * @param \modx $modx |
||
| 40 | * @param Blender $blender |
||
| 41 | * @param string $key ~ the context key |
||
| 42 | */ |
||
| 43 | public function __construct(\modx $modx, Blender $blender, $key = '') |
||
|
|
|||
| 44 | { |
||
| 45 | $this->setFieldKey($key); |
||
| 46 | parent::__construct($modx, $blender, $key); |
||
| 47 | |||
| 48 | $additional = explode(',', $this->modx->getOption('blend.portable.templateVariables.mediaSources')); |
||
| 49 | if (count($additional) > 0) { |
||
| 50 | foreach ($additional as $tv_name) { |
||
| 51 | $this->portable_settings[$tv_name] = 'media_source'; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | $additional = explode(',', $this->modx->getOption('blend.portable.templateVariables.resources')); |
||
| 56 | if (count($additional) > 0) { |
||
| 57 | foreach ($additional as $tv_name) { |
||
| 58 | $this->portable_settings[$tv_name] = 'resource'; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | $additional = explode(',', $this->modx->getOption('blend.portable.templateVariables.templates')); |
||
| 63 | if (count($additional) > 0) { |
||
| 64 | foreach ($additional as $tv_name) { |
||
| 65 | $this->portable_settings[$tv_name] = 'template'; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param string $type ~ seed or revert |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | public function getSeedKey($type = 'seed') |
||
| 91 | } |
||
| 92 | |||
| 93 | // Column Getters: |
||
| 94 | /** |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function getFieldKey() |
||
| 98 | { |
||
| 99 | return $this->blendable_xpdo_simple_object_data['key']; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function getFieldName() |
||
| 106 | { |
||
| 107 | return $this->blendable_xpdo_simple_object_data['name']; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return int |
||
| 112 | */ |
||
| 113 | public function getFieldRank() |
||
| 114 | { |
||
| 115 | return $this->blendable_xpdo_simple_object_data['rank']; |
||
| 116 | } |
||
| 117 | |||
| 118 | // Column Setters: |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $value max characters: 100 |
||
| 122 | * @return $this |
||
| 123 | */ |
||
| 124 | public function setFieldKey($value) |
||
| 125 | { |
||
| 126 | $this->blendable_xpdo_simple_object_data['key'] = $value; |
||
| 127 | return $this; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param string $value max characters: 191 |
||
| 132 | * @return $this |
||
| 133 | */ |
||
| 134 | public function setFieldName($value) |
||
| 135 | { |
||
| 136 | $this->blendable_xpdo_simple_object_data['name'] = $value; |
||
| 137 | return $this; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param int $value |
||
| 142 | * @return $this |
||
| 143 | */ |
||
| 144 | public function setFieldRank($value) |
||
| 145 | { |
||
| 146 | $this->blendable_xpdo_simple_object_data['rank'] = $value; |
||
| 147 | return $this; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param string $key |
||
| 152 | * @param mixed $value |
||
| 153 | * @param string $xtype |
||
| 154 | * @param string $area |
||
| 155 | * @param string $namespace |
||
| 156 | * @return $this |
||
| 157 | */ |
||
| 158 | public function addSetting($key, $value, $xtype = 'textfield', $area = '', $namespace = 'core') |
||
| 159 | { |
||
| 160 | $this->related_data['settings'][] = [ |
||
| 161 | 'area' => $area, |
||
| 162 | 'namespace' => $namespace, |
||
| 163 | 'key' => $key, |
||
| 164 | 'value' => $value, |
||
| 165 | 'xtype' => $xtype, |
||
| 166 | ]; |
||
| 167 | return $this; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $key |
||
| 172 | * @return $this |
||
| 173 | */ |
||
| 174 | public function removeSetting($key) |
||
| 175 | { |
||
| 176 | $this->remove_settings[] = $key; |
||
| 177 | return $this; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @return Blendable |
||
| 182 | */ |
||
| 183 | public function getCurrentVersion() |
||
| 184 | { |
||
| 185 | /** @var \LCI\Blend\Blendable\Resource $resource */ |
||
| 186 | $resource = new self($this->modx, $this->blender, $this->getFieldKey()); |
||
| 187 | return $resource |
||
| 188 | ->setSeedsDir($this->getSeedsDir()); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Override in child classes |
||
| 193 | */ |
||
| 194 | protected function loadRelatedData() |
||
| 195 | { |
||
| 196 | $settings = []; |
||
| 197 | $contextSettings = []; |
||
| 198 | if (is_object($this->xPDOSimpleObject)) { |
||
| 199 | /** @var array of \modContextSetting $contextSettings */ |
||
| 200 | $contextSettings = $this->xPDOSimpleObject->getMany('ContextSettings'); |
||
| 201 | } |
||
| 202 | /** @var \modContextSetting $setting */ |
||
| 203 | foreach ($contextSettings as $setting) { |
||
| 204 | $settings[] = $this->makePortableData($setting->toArray()); |
||
| 205 | } |
||
| 206 | |||
| 207 | $this->related_data = [ |
||
| 208 | 'settings' => $settings |
||
| 209 | ]; |
||
| 210 | |||
| 211 | // Calls on the event: OnBlendLoadRelatedData |
||
| 212 | parent::loadRelatedData(); |
||
| 213 | } |
||
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * @param array $setting |
||
| 218 | * @return bool|string |
||
| 219 | */ |
||
| 220 | protected function getPortableType($setting) |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param array $setting |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | protected function makePortableData($setting) |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Create convert methods for any portable data column that needs to be converted to an int for a related primary key |
||
| 276 | */ |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param array $setting |
||
| 280 | * @return string|int|mixed $value |
||
| 281 | */ |
||
| 282 | protected function convertToLocalData($setting) |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * This method is called just after a successful blend/save() |
||
| 312 | */ |
||
| 313 | protected function attachRelatedPiecesAfterSave() |
||
| 335 | } |
||
| 336 | } |
||
| 337 | } |
||
| 338 | } |
||
| 339 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths