| Total Complexity | 48 | 
| Total Lines | 190 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like Frame 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 Frame, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); | ||
| 5 | class Frame | ||
| 6 | { | ||
| 7 | use PathTrait; | ||
| 8 | |||
| 9 | protected $file; | ||
| 10 | |||
| 11 | protected $line; | ||
| 12 | |||
| 13 | protected $caller; | ||
| 14 | |||
| 15 | protected $args; | ||
| 16 | |||
| 17 | public static function create(array $params): self | ||
| 18 |     { | ||
| 19 | $frame = new self; | ||
| 20 | |||
| 21 |         if (isset($params['file'])) { | ||
| 22 | $frame->setFile($params['file']); | ||
| 23 | } | ||
| 24 | |||
| 25 |         if (isset($params['line'])) { | ||
| 26 | $frame->setLine($params['line']); | ||
| 27 | } | ||
| 28 | |||
| 29 |         if (isset($params['class'])) { | ||
| 30 |             $frame->setCaller(\sprintf('%s%s%s', $params['class'], $params['type'], $params['function'])); | ||
| 31 |         } elseif (isset($params['function'])) { | ||
| 32 | $frame->setCaller($params['function']); | ||
| 33 | } | ||
| 34 | |||
| 35 |         if (isset($params['args'])) { | ||
| 36 | $frame->setArguments($params['args']); | ||
| 37 |         } else { | ||
| 38 | $frame->setArguments([]); | ||
| 39 | } | ||
| 40 | |||
| 41 | return $frame; | ||
| 42 | } | ||
| 43 | |||
| 44 | public function getRelativeFilepath(): string | ||
| 45 |     { | ||
| 46 |         if ($this->hasPath() && \strpos($this->getFile(), $this->getPath()) === 0) { | ||
| 47 | return substr($this->getFile(), \mb_strlen($this->getPath())); | ||
| 48 | } | ||
| 49 | return $this->getFile(); | ||
| 50 | } | ||
| 51 | |||
| 52 | public function getFile(): string | ||
| 53 |     { | ||
| 54 | return $this->file; | ||
| 55 | } | ||
| 56 | |||
| 57 | public function setFile(string $file): void | ||
| 58 |     { | ||
| 59 | $this->file = $file; | ||
| 60 | } | ||
| 61 | |||
| 62 | public function hasFile(): bool | ||
| 63 |     { | ||
| 64 | return $this->file !== null && \is_readable($this->file); | ||
| 65 | } | ||
| 66 | |||
| 67 | public function getLine(): int | ||
| 70 | } | ||
| 71 | |||
| 72 | public function setLine(int $line): void | ||
| 73 |     { | ||
| 74 | $this->line = $line; | ||
| 75 | } | ||
| 76 | |||
| 77 | public function hasLine(): bool | ||
| 78 |     { | ||
| 79 | return $this->line !== null; | ||
| 80 | } | ||
| 81 | |||
| 82 | public function hasCaller(): bool | ||
| 85 | } | ||
| 86 | |||
| 87 | public function getCaller(): string | ||
| 88 |     { | ||
| 89 | return $this->caller; | ||
| 90 | } | ||
| 91 | |||
| 92 | public function setCaller(string $caller): void | ||
| 95 | } | ||
| 96 | |||
| 97 | public function getArguments(): array | ||
| 100 | } | ||
| 101 | |||
| 102 | public function setArguments(array $args): void | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | protected function normaliseArray($value): string | ||
| 112 |     { | ||
| 113 | $count = count($value); | ||
| 114 | |||
| 115 |         if ($count > 100) { | ||
| 116 | return 'Array of length ' . $count; | ||
| 117 | } | ||
| 118 | |||
| 119 | $types = []; | ||
| 120 | |||
| 121 |         foreach ($value as $item) { | ||
| 122 | $type = gettype($item); | ||
| 123 |             if ('object' === $type) { | ||
| 124 | $type = get_class($item); | ||
| 125 | } | ||
| 126 |             if (!in_array($type, $types)) { | ||
| 127 | $types[] = $type; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 |         if (count($types) > 3) { | ||
| 132 | return 'Mixed Array of length ' . $count; | ||
| 133 | } | ||
| 134 | |||
| 135 |         return 'Array<'.implode('|', $types).'> of length ' . $count; | ||
| 136 | } | ||
| 137 | |||
| 138 | protected function normalise($value): string | ||
| 160 | } | ||
| 161 | |||
| 162 | public function hasContext(): bool | ||
| 163 |     { | ||
| 164 | return $this->hasFile() && $this->hasLine(); | ||
| 165 | } | ||
| 166 | |||
| 167 | public function getContext(): Context | ||
| 170 | } | ||
| 171 | |||
| 172 | public function toArray(): array | ||
| 195 | } | ||
| 196 | } | ||
| 197 |