| Total Complexity | 63 |
| Total Lines | 425 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SSViewer_DataPresenter 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 SSViewer_DataPresenter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class SSViewer_DataPresenter extends SSViewer_Scope |
||
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * List of global property providers |
||
| 20 | * |
||
| 21 | * @internal |
||
| 22 | * @var TemplateGlobalProvider[]|null |
||
| 23 | */ |
||
| 24 | private static $globalProperties = null; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * List of global iterator providers |
||
| 28 | * |
||
| 29 | * @internal |
||
| 30 | * @var TemplateIteratorProvider[]|null |
||
| 31 | */ |
||
| 32 | private static $iteratorProperties = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Overlay variables. Take precedence over anything from the current scope |
||
| 36 | * |
||
| 37 | * @var array|null |
||
| 38 | */ |
||
| 39 | protected $overlay; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Flag for whether overlay should be preserved when pushing a new scope |
||
| 43 | * |
||
| 44 | * @see SSViewer_DataPresenter::pushScope() |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | protected $preserveOverlay = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Underlay variables. Concede precedence to overlay variables or anything from the current scope |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $underlay; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var object $item |
||
| 58 | * @var array $overlay |
||
| 59 | * @var array $underlay |
||
| 60 | * @var SSViewer_Scope $inheritedScope |
||
| 61 | */ |
||
| 62 | public function __construct( |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Build cache of global properties |
||
| 79 | */ |
||
| 80 | protected function cacheGlobalProperties() |
||
| 81 | { |
||
| 82 | if (self::$globalProperties !== null) { |
||
| 83 | return; |
||
| 84 | } |
||
| 85 | |||
| 86 | self::$globalProperties = $this->getPropertiesFromProvider( |
||
| 87 | TemplateGlobalProvider::class, |
||
| 88 | 'get_template_global_variables' |
||
| 89 | ); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Build cache of global iterator properties |
||
| 94 | */ |
||
| 95 | protected function cacheIteratorProperties() |
||
| 96 | { |
||
| 97 | if (self::$iteratorProperties !== null) { |
||
| 98 | return; |
||
| 99 | } |
||
| 100 | |||
| 101 | self::$iteratorProperties = $this->getPropertiesFromProvider( |
||
| 102 | TemplateIteratorProvider::class, |
||
| 103 | 'get_template_iterator_variables', |
||
| 104 | true // Call non-statically |
||
| 105 | ); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var string $interfaceToQuery |
||
| 110 | * @var string $variableMethod |
||
| 111 | * @var boolean $createObject |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | protected function getPropertiesFromProvider($interfaceToQuery, $variableMethod, $createObject = false) |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Look up injected value - it may be part of an "overlay" (arguments passed to <% include %>), |
||
| 163 | * set on the current item, part of an "underlay" ($Layout or $Content), or an iterator/global property |
||
| 164 | * |
||
| 165 | * @param string $property Name of property |
||
| 166 | * @param array $params |
||
| 167 | * @param bool $cast If true, an object is always returned even if not an object. |
||
| 168 | * @return array|null |
||
| 169 | */ |
||
| 170 | public function getInjectedValue($property, array $params, $cast = true) |
||
| 171 | { |
||
| 172 | // Get source for this value |
||
| 173 | $result = $this->getValueSource($property); |
||
| 174 | if (!array_key_exists('source', $result)) { |
||
| 175 | return null; |
||
| 176 | } |
||
| 177 | |||
| 178 | // Look up the value - either from a callable, or from a directly provided value |
||
| 179 | $source = $result['source']; |
||
| 180 | $res = []; |
||
| 181 | if (isset($source['callable'])) { |
||
| 182 | $res['value'] = $source['callable'](...$params); |
||
| 183 | } elseif (array_key_exists('value', $source)) { |
||
| 184 | $res['value'] = $source['value']; |
||
| 185 | } else { |
||
| 186 | throw new InvalidArgumentException( |
||
| 187 | "Injected property $property does't have a value or callable value source provided" |
||
| 188 | ); |
||
| 189 | } |
||
| 190 | |||
| 191 | // If we want to provide a casted object, look up what type object to use |
||
| 192 | if ($cast) { |
||
| 193 | $res['obj'] = $this->castValue($res['value'], $source); |
||
| 194 | } |
||
| 195 | |||
| 196 | return $res; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Store the current overlay (as it doesn't directly apply to the new scope |
||
| 201 | * that's being pushed). We want to store the overlay against the next item |
||
| 202 | * "up" in the stack (hence upIndex), rather than the current item, because |
||
| 203 | * SSViewer_Scope::obj() has already been called and pushed the new item to |
||
| 204 | * the stack by this point |
||
| 205 | * |
||
| 206 | * @return SSViewer_Scope |
||
| 207 | */ |
||
| 208 | public function pushScope() |
||
| 209 | { |
||
| 210 | $scope = parent::pushScope(); |
||
| 211 | $upIndex = $this->getUpIndex() ?: 0; |
||
| 212 | |||
| 213 | $itemStack = $this->getItemStack(); |
||
| 214 | $itemStack[$upIndex][SSViewer_Scope::ITEM_OVERLAY] = $this->overlay; |
||
| 215 | $this->setItemStack($itemStack); |
||
| 216 | |||
| 217 | // Remove the overlay when we're changing to a new scope, as values in |
||
| 218 | // that scope take priority. The exceptions that set this flag are $Up |
||
| 219 | // and $Top as they require that the new scope inherits the overlay |
||
| 220 | if (!$this->preserveOverlay) { |
||
| 221 | $this->overlay = []; |
||
| 222 | } |
||
| 223 | |||
| 224 | return $scope; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Now that we're going to jump up an item in the item stack, we need to |
||
| 229 | * restore the overlay that was previously stored against the next item "up" |
||
| 230 | * in the stack from the current one |
||
| 231 | * |
||
| 232 | * @return SSViewer_Scope |
||
| 233 | */ |
||
| 234 | public function popScope() |
||
| 235 | { |
||
| 236 | $upIndex = $this->getUpIndex(); |
||
| 237 | |||
| 238 | if ($upIndex !== null) { |
||
| 239 | $itemStack = $this->getItemStack(); |
||
| 240 | $this->overlay = $itemStack[$upIndex][SSViewer_Scope::ITEM_OVERLAY]; |
||
| 241 | } |
||
| 242 | |||
| 243 | return parent::popScope(); |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * $Up and $Top need to restore the overlay from the parent and top-level |
||
| 248 | * scope respectively. |
||
| 249 | * |
||
| 250 | * @param string $name |
||
| 251 | * @param array $arguments |
||
| 252 | * @param bool $cache |
||
| 253 | * @param string $cacheName |
||
| 254 | * @return $this |
||
| 255 | */ |
||
| 256 | public function obj($name, $arguments = [], $cache = false, $cacheName = null) |
||
| 257 | { |
||
| 258 | $overlayIndex = false; |
||
| 259 | |||
| 260 | switch ($name) { |
||
| 261 | case 'Up': |
||
| 262 | $upIndex = $this->getUpIndex(); |
||
| 263 | if ($upIndex === null) { |
||
| 264 | throw new \LogicException('Up called when we\'re already at the top of the scope'); |
||
| 265 | } |
||
| 266 | $overlayIndex = $upIndex; // Parent scope |
||
| 267 | $this->preserveOverlay = true; // Preserve overlay |
||
| 268 | break; |
||
| 269 | case 'Top': |
||
| 270 | $overlayIndex = 0; // Top-level scope |
||
| 271 | $this->preserveOverlay = true; // Preserve overlay |
||
| 272 | break; |
||
| 273 | default: |
||
| 274 | $this->preserveOverlay = false; |
||
| 275 | break; |
||
| 276 | } |
||
| 277 | |||
| 278 | if ($overlayIndex !== false) { |
||
| 279 | $itemStack = $this->getItemStack(); |
||
| 280 | if (!$this->overlay && isset($itemStack[$overlayIndex][SSViewer_Scope::ITEM_OVERLAY])) { |
||
| 281 | $this->overlay = $itemStack[$overlayIndex][SSViewer_Scope::ITEM_OVERLAY]; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | parent::obj($name, $arguments, $cache, $cacheName); |
||
| 286 | return $this; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * {@inheritdoc} |
||
| 291 | */ |
||
| 292 | public function getObj($name, $arguments = [], $cache = false, $cacheName = null) |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * {@inheritdoc} |
||
| 303 | */ |
||
| 304 | public function __call($name, $arguments) |
||
| 305 | { |
||
| 306 | // Extract the method name and parameters |
||
| 307 | $property = $arguments[0]; // The name of the public function being called |
||
| 308 | |||
| 309 | // The public function parameters in an array |
||
| 310 | $params = (isset($arguments[1])) ? (array)$arguments[1] : []; |
||
| 311 | |||
| 312 | $val = $this->getInjectedValue($property, $params); |
||
| 313 | if ($val) { |
||
| 314 | $obj = $val['obj']; |
||
| 315 | if ($name === 'hasValue') { |
||
| 316 | $result = ($obj instanceof ViewableData) ? $obj->exists() : (bool)$obj; |
||
| 317 | } elseif (is_null($obj) || (is_scalar($obj) && !is_string($obj))) { |
||
| 318 | $result = $obj; // Nulls and non-string scalars don't need casting |
||
| 319 | } else { |
||
| 320 | $result = $obj->forTemplate(); // XML_val |
||
| 321 | } |
||
| 322 | |||
| 323 | $this->resetLocalScope(); |
||
| 324 | return $result; |
||
| 325 | } |
||
| 326 | |||
| 327 | return parent::__call($name, $arguments); |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Evaluate a template override. Returns an array where the presence of |
||
| 332 | * a 'value' key indiciates whether an override was successfully found, |
||
| 333 | * as null is a valid override value |
||
| 334 | * |
||
| 335 | * @param string $property Name of override requested |
||
| 336 | * @param array $overrides List of overrides available |
||
| 337 | * @return array An array with a 'value' key if a value has been found, or empty if not |
||
| 338 | */ |
||
| 339 | protected function processTemplateOverride($property, $overrides) |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Determine source to use for getInjectedValue. Returns an array where the presence of |
||
| 363 | * a 'source' key indiciates whether a value source was successfully found, as a source |
||
| 364 | * may be a null value returned from an override |
||
| 365 | * |
||
| 366 | * @param string $property |
||
| 367 | * @return array An array with a 'source' key if a value source has been found, or empty if not |
||
| 368 | */ |
||
| 369 | protected function getValueSource($property) |
||
| 370 | { |
||
| 371 | // Check for a presenter-specific override |
||
| 372 | $result = $this->processTemplateOverride($property, $this->overlay); |
||
| 373 | if (array_key_exists('value', $result)) { |
||
| 374 | return ['source' => $result]; |
||
| 375 | } |
||
| 376 | |||
| 377 | // Check if the method to-be-called exists on the target object - if so, don't check any further |
||
| 378 | // injection locations |
||
| 379 | $on = $this->itemIterator ? $this->itemIterator->current() : $this->item; |
||
| 380 | if (isset($on->$property) || method_exists($on, $property)) { |
||
| 381 | return []; |
||
| 382 | } |
||
| 383 | |||
| 384 | // Check for a presenter-specific override |
||
| 385 | $result = $this->processTemplateOverride($property, $this->underlay); |
||
| 386 | if (array_key_exists('value', $result)) { |
||
| 387 | return ['source' => $result]; |
||
| 388 | } |
||
| 389 | |||
| 390 | // Then for iterator-specific overrides |
||
| 391 | if (array_key_exists($property, self::$iteratorProperties)) { |
||
| 392 | $source = self::$iteratorProperties[$property]; |
||
| 393 | /** @var TemplateIteratorProvider $implementor */ |
||
| 394 | $implementor = $source['implementor']; |
||
| 395 | if ($this->itemIterator) { |
||
| 396 | // Set the current iterator position and total (the object instance is the first item in |
||
| 397 | // the callable array) |
||
| 398 | $implementor->iteratorProperties( |
||
| 399 | $this->itemIterator->key(), |
||
| 400 | $this->itemIteratorTotal |
||
| 401 | ); |
||
| 402 | } else { |
||
| 403 | // If we don't actually have an iterator at the moment, act like a list of length 1 |
||
| 404 | $implementor->iteratorProperties(0, 1); |
||
| 405 | } |
||
| 406 | |||
| 407 | return ($source) ? ['source' => $source] : []; |
||
| 408 | } |
||
| 409 | |||
| 410 | // And finally for global overrides |
||
| 411 | if (array_key_exists($property, self::$globalProperties)) { |
||
| 412 | return [ |
||
| 413 | 'source' => self::$globalProperties[$property] // get the method call |
||
| 414 | ]; |
||
| 415 | } |
||
| 416 | |||
| 417 | // No value |
||
| 418 | return []; |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Ensure the value is cast safely |
||
| 423 | * |
||
| 424 | * @param mixed $value |
||
| 425 | * @param array $source |
||
| 426 | * @return DBField |
||
| 427 | */ |
||
| 428 | protected function castValue($value, $source) |
||
| 441 | } |
||
| 442 | } |
||
| 443 |
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