Complex classes like BaseElement 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 BaseElement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | abstract class BaseElement implements Htmlable, HtmlElement |
||
| 13 | { |
||
| 14 | /** @var string */ |
||
| 15 | protected $tag; |
||
| 16 | |||
| 17 | /** @var \Spatie\Html\Attributes */ |
||
| 18 | protected $attributes; |
||
| 19 | |||
| 20 | /** @var \Illuminate\Support\Collection */ |
||
| 21 | protected $children; |
||
| 22 | |||
| 23 | public function __construct() |
||
| 32 | |||
| 33 | public static function create() |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param string $attribute |
||
| 40 | * @param string $value |
||
| 41 | * |
||
| 42 | * @return static |
||
| 43 | */ |
||
| 44 | public function attribute(string $attribute, ?string $value = '') |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param bool $condition |
||
| 55 | * @param string $attribute |
||
| 56 | * @param string $value |
||
| 57 | * |
||
| 58 | * @return static |
||
| 59 | */ |
||
| 60 | public function attributeIf(bool $condition, string $attribute, ?string $value = '') |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param iterable $attributes |
||
| 69 | * |
||
| 70 | * @return static |
||
| 71 | */ |
||
| 72 | public function attributes(iterable $attributes) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string $attribute |
||
| 83 | * @param string $value |
||
|
|
|||
| 84 | * |
||
| 85 | * @return static |
||
| 86 | */ |
||
| 87 | public function forgetAttribute(string $attribute) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $attribute |
||
| 98 | * @param mixed $fallback |
||
| 99 | * |
||
| 100 | * @return mixed |
||
| 101 | */ |
||
| 102 | public function getAttribute(string $attribute, ?string $fallback = '') |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $attribute |
||
| 109 | * |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | public function hasAttribute(string $attribute): bool |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param iterable|string $class |
||
| 119 | * |
||
| 120 | * @return static |
||
| 121 | */ |
||
| 122 | public function class($class) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Alias for `class`. |
||
| 129 | * |
||
| 130 | * @param iterable|string $class |
||
| 131 | * |
||
| 132 | * @return static |
||
| 133 | */ |
||
| 134 | public function addClass($class) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param string $id |
||
| 145 | * |
||
| 146 | * @return static |
||
| 147 | */ |
||
| 148 | public function id(string $id) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param \Spatie\Html\HtmlElement|string|iterable|null $children |
||
| 155 | * @param ?callable $mapper |
||
| 156 | * |
||
| 157 | * @return static |
||
| 158 | */ |
||
| 159 | public function addChildren($children, callable $mapper = null) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Alias for `addChildren`. |
||
| 176 | * |
||
| 177 | * @param \Spatie\Html\HtmlElement|string|iterable|null $children |
||
| 178 | * @param ?callable $mapper |
||
| 179 | * |
||
| 180 | * @return static |
||
| 181 | */ |
||
| 182 | public function addChild($child, callable $mapper = null) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Alias for `addChildren`. |
||
| 189 | * |
||
| 190 | * @param \Spatie\Html\HtmlElement|string|iterable|null $children |
||
| 191 | * @param ?callable $mapper |
||
| 192 | * |
||
| 193 | * @return static |
||
| 194 | */ |
||
| 195 | public function child($child, callable $mapper = null) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Alias for `addChildren`. |
||
| 202 | * |
||
| 203 | * @param \Spatie\Html\HtmlElement|string|iterable|null $children |
||
| 204 | * @param ?callable $mapper |
||
| 205 | * |
||
| 206 | * @return static |
||
| 207 | */ |
||
| 208 | public function children($children, callable $mapper = null) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param \Spatie\Html\HtmlElement|string|iterable|null $children |
||
| 215 | * @param ?callable $mapper |
||
| 216 | * |
||
| 217 | * @return static |
||
| 218 | */ |
||
| 219 | public function prependChildren($children, callable $mapper = null) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Alias for `prependChildren`. |
||
| 232 | * |
||
| 233 | * @param \Spatie\Html\HtmlElement|string|iterable|null $children |
||
| 234 | * @param ?callable $mapper |
||
| 235 | * |
||
| 236 | * @return static |
||
| 237 | */ |
||
| 238 | public function prependChild($children, callable $mapper = null) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param string $text |
||
| 245 | * |
||
| 246 | * @return static |
||
| 247 | */ |
||
| 248 | public function text(?string $text) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $html |
||
| 255 | * |
||
| 256 | * @return static |
||
| 257 | */ |
||
| 258 | public function html(?string $html) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Condintionally transform the element. Note that since elements are |
||
| 273 | * immutable, you'll need to return a new instance from the callback. |
||
| 274 | * |
||
| 275 | * @param bool $condition |
||
| 276 | * @param callable $callback |
||
| 277 | */ |
||
| 278 | public function if(bool $condition, callable $callback) |
||
| 286 | |||
| 287 | public function open(): Htmlable |
||
| 311 | |||
| 312 | public function close(): Htmlable |
||
| 320 | |||
| 321 | public function render(): Htmlable |
||
| 327 | |||
| 328 | public function isVoidElement(): bool |
||
| 336 | |||
| 337 | public function __clone() |
||
| 342 | |||
| 343 | public function __toString(): string |
||
| 347 | |||
| 348 | public function toHtml(): string |
||
| 352 | |||
| 353 | protected function parseChildren($children, callable $mapper = null): Collection |
||
| 371 | |||
| 372 | protected function guardAgainstInvalidChildren(Collection $children) |
||
| 380 | } |
||
| 381 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.