| Total Complexity | 73 |
| Total Lines | 373 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 0 | Features | 0 |
Complex classes like FormModel 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 FormModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | abstract class FormModel implements FormModelInterface, PostValidationHookInterface, RulesProviderInterface |
||
| 31 | { |
||
| 32 | private array $attributes; |
||
| 33 | private array $attributesLabels; |
||
| 34 | private array $attributesErrors = []; |
||
| 35 | private ?Inflector $inflector = null; |
||
| 36 | private bool $validated = false; |
||
| 37 | |||
| 38 | public function __construct() |
||
| 39 | { |
||
| 40 | $this->attributes = $this->collectAttributes(); |
||
| 41 | $this->attributesLabels = $this->getAttributeLabels(); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function isAttributeRequired(string $attribute): bool |
||
| 45 | { |
||
| 46 | $validators = $this->getRules()[$attribute] ?? []; |
||
| 47 | |||
| 48 | foreach ($validators as $validator) { |
||
| 49 | if ($validator instanceof Required) { |
||
| 50 | return true; |
||
| 51 | } |
||
| 52 | if ($validator instanceof HtmlOptionsProvider && (bool)($validator->getHtmlOptions()['required'] ?? false)) { |
||
| 53 | return true; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | return false; |
||
| 58 | } |
||
| 59 | |||
| 60 | public function getAttributeValue(string $attribute) |
||
| 63 | } |
||
| 64 | |||
| 65 | public function getAttributeLabels(): array |
||
| 66 | { |
||
| 67 | return []; |
||
| 68 | } |
||
| 69 | |||
| 70 | public function getAttributeHint(string $attribute): string |
||
| 71 | { |
||
| 72 | [$attribute, $nested] = $this->getNestedAttribute($attribute); |
||
| 73 | if ($nested !== null) { |
||
| 74 | return $this->readProperty($attribute)->getAttributeHint($nested); |
||
| 75 | } |
||
| 76 | |||
| 77 | $hints = $this->getAttributeHints(); |
||
| 78 | |||
| 79 | return $hints[$attribute] ?? ''; |
||
| 80 | } |
||
| 81 | |||
| 82 | public function getAttributeHints(): array |
||
| 83 | { |
||
| 84 | return []; |
||
| 85 | } |
||
| 86 | |||
| 87 | public function getAttributeLabel(string $attribute): string |
||
| 88 | { |
||
| 89 | if (array_key_exists($attribute, $this->attributesLabels)) { |
||
| 90 | return $this->attributesLabels[$attribute]; |
||
| 91 | } |
||
| 92 | |||
| 93 | [$attribute, $nested] = $this->getNestedAttribute($attribute); |
||
| 94 | |||
| 95 | return $nested !== null |
||
| 96 | ? $this->readProperty($attribute)->getAttributeLabel($nested) |
||
| 97 | : $this->generateAttributeLabel($attribute); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return string Returns classname without a namespace part or empty string when class is anonymous |
||
| 102 | */ |
||
| 103 | public function getFormName(): string |
||
| 104 | { |
||
| 105 | if (strpos(static::class, '@anonymous') !== false) { |
||
| 106 | return ''; |
||
| 107 | } |
||
| 108 | |||
| 109 | $className = strrchr(static::class, '\\'); |
||
| 110 | if ($className === false) { |
||
| 111 | return static::class; |
||
| 112 | } |
||
| 113 | |||
| 114 | return substr($className, 1); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function hasAttribute(string $attribute): bool |
||
| 118 | { |
||
| 119 | return array_key_exists($attribute, $this->attributes); |
||
| 120 | } |
||
| 121 | |||
| 122 | public function getError(string $attribute): array |
||
| 123 | { |
||
| 124 | return $this->attributesErrors[$attribute] ?? []; |
||
| 125 | } |
||
| 126 | |||
| 127 | public function getErrors(): array |
||
| 128 | { |
||
| 129 | return $this->attributesErrors; |
||
| 130 | } |
||
| 131 | |||
| 132 | public function getErrorSummary(bool $showAllErrors): array |
||
| 133 | { |
||
| 134 | $lines = []; |
||
| 135 | $errors = $showAllErrors ? $this->getErrors() : [$this->getFirstErrors()]; |
||
| 136 | |||
| 137 | foreach ($errors as $error) { |
||
| 138 | $lines = array_merge($lines, $error); |
||
| 139 | } |
||
| 140 | |||
| 141 | return $lines; |
||
| 142 | } |
||
| 143 | |||
| 144 | public function getFirstError(string $attribute): string |
||
| 145 | { |
||
| 146 | if (empty($this->attributesErrors[$attribute])) { |
||
| 147 | return ''; |
||
| 148 | } |
||
| 149 | |||
| 150 | return reset($this->attributesErrors[$attribute]); |
||
| 151 | } |
||
| 152 | |||
| 153 | public function getFirstErrors(): array |
||
| 154 | { |
||
| 155 | if (empty($this->attributesErrors)) { |
||
| 156 | return []; |
||
| 157 | } |
||
| 158 | |||
| 159 | $errors = []; |
||
| 160 | |||
| 161 | foreach ($this->attributesErrors as $name => $es) { |
||
| 162 | if (!empty($es)) { |
||
| 163 | $errors[$name] = reset($es); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | return $errors; |
||
| 168 | } |
||
| 169 | |||
| 170 | public function hasErrors(?string $attribute = null): bool |
||
| 171 | { |
||
| 172 | return $attribute === null ? !empty($this->attributesErrors) : isset($this->attributesErrors[$attribute]); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param array $data |
||
| 177 | * @param string|null $formName |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | public function load(array $data, ?string $formName = null): bool |
||
| 182 | { |
||
| 183 | $scope = $formName ?? $this->getFormName(); |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @psalm-var array<string,mixed> |
||
| 187 | */ |
||
| 188 | $values = []; |
||
| 189 | |||
| 190 | if ($scope === '' && !empty($data)) { |
||
| 191 | $values = $data; |
||
| 192 | } elseif (isset($data[$scope])) { |
||
| 193 | $values = $data[$scope]; |
||
| 194 | } |
||
| 195 | |||
| 196 | foreach ($values as $name => $value) { |
||
| 197 | $this->setAttribute($name, $value); |
||
| 198 | } |
||
| 199 | |||
| 200 | return $values !== []; |
||
| 201 | } |
||
| 202 | |||
| 203 | public function setAttribute(string $name, $value): void |
||
| 204 | { |
||
| 205 | [$realName] = $this->getNestedAttribute($name); |
||
| 206 | if (isset($this->attributes[$realName])) { |
||
| 207 | switch ($this->attributes[$realName]) { |
||
| 208 | case 'bool': |
||
| 209 | $this->writeProperty($name, (bool) $value); |
||
| 210 | break; |
||
| 211 | case 'float': |
||
| 212 | $this->writeProperty($name, (float) $value); |
||
| 213 | break; |
||
| 214 | case 'int': |
||
| 215 | $this->writeProperty($name, (int) $value); |
||
| 216 | break; |
||
| 217 | case 'string': |
||
| 218 | $this->writeProperty($name, (string) $value); |
||
| 219 | break; |
||
| 220 | default: |
||
| 221 | $this->writeProperty($name, $value); |
||
| 222 | break; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | public function processValidationResult(ResultSet $resultSet): void |
||
| 228 | { |
||
| 229 | $this->clearErrors(); |
||
| 230 | foreach ($resultSet as $attribute => $result) { |
||
| 231 | if ($result->isValid() === false) { |
||
| 232 | $this->addErrors([$attribute => $result->getErrors()]); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | $this->validated = true; |
||
| 236 | } |
||
| 237 | |||
| 238 | public function addError(string $attribute, string $error): void |
||
| 239 | { |
||
| 240 | $this->attributesErrors[$attribute][] = $error; |
||
| 241 | } |
||
| 242 | |||
| 243 | public function getRules(): array |
||
| 244 | { |
||
| 245 | return []; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param string[][] $items |
||
| 250 | */ |
||
| 251 | private function addErrors(array $items): void |
||
| 252 | { |
||
| 253 | foreach ($items as $attribute => $errors) { |
||
| 254 | foreach ($errors as $error) { |
||
| 255 | $this->attributesErrors[$attribute][] = $error; |
||
| 256 | } |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Returns the list of attribute types indexed by attribute names. |
||
| 262 | * |
||
| 263 | * By default, this method returns all non-static properties of the class. |
||
| 264 | * |
||
| 265 | * @throws \ReflectionException |
||
| 266 | * |
||
| 267 | * @return array list of attribute types indexed by attribute names. |
||
| 268 | */ |
||
| 269 | private function collectAttributes(): array |
||
| 270 | { |
||
| 271 | $class = new ReflectionClass($this); |
||
| 272 | $attributes = []; |
||
| 273 | |||
| 274 | foreach ($class->getProperties() as $property) { |
||
| 275 | if ($property->isStatic()) { |
||
| 276 | continue; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** @var ReflectionNamedType $type */ |
||
| 280 | $type = $property->getType(); |
||
| 281 | if ($type === null) { |
||
| 282 | throw new InvalidArgumentException(sprintf( |
||
| 283 | 'You must specify the type hint for "%s" property in "%s" class.', |
||
| 284 | $property->getName(), |
||
| 285 | $property->getDeclaringClass()->getName(), |
||
| 286 | )); |
||
| 287 | } |
||
| 288 | |||
| 289 | $attributes[$property->getName()] = $type->getName(); |
||
| 290 | } |
||
| 291 | |||
| 292 | return $attributes; |
||
| 293 | } |
||
| 294 | |||
| 295 | private function clearErrors(?string $attribute = null): void |
||
| 296 | { |
||
| 297 | if ($attribute === null) { |
||
| 298 | $this->attributesErrors = []; |
||
| 299 | } else { |
||
| 300 | unset($this->attributesErrors[$attribute]); |
||
| 301 | } |
||
| 302 | |||
| 303 | $this->validated = false; |
||
| 304 | } |
||
| 305 | |||
| 306 | private function getInflector(): Inflector |
||
| 307 | { |
||
| 308 | if ($this->inflector === null) { |
||
| 309 | $this->inflector = new Inflector(); |
||
| 310 | } |
||
| 311 | return $this->inflector; |
||
|
|
|||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Generates a user friendly attribute label based on the give attribute name. |
||
| 316 | * |
||
| 317 | * This is done by replacing underscores, dashes and dots with blanks and changing the first letter of each word to |
||
| 318 | * upper case. |
||
| 319 | * |
||
| 320 | * For example, 'department_name' or 'DepartmentName' will generate 'Department Name'. |
||
| 321 | * |
||
| 322 | * @param string $name the column name. |
||
| 323 | * |
||
| 324 | * @return string the attribute label. |
||
| 325 | */ |
||
| 326 | private function generateAttributeLabel(string $name): string |
||
| 327 | { |
||
| 328 | return StringHelper::uppercaseFirstCharacterInEachWord( |
||
| 329 | $this->getInflector()->toWords($name) |
||
| 330 | ); |
||
| 331 | } |
||
| 332 | |||
| 333 | private function readProperty(string $attribute) |
||
| 334 | { |
||
| 335 | $class = static::class; |
||
| 336 | |||
| 337 | [$attribute, $nested] = $this->getNestedAttribute($attribute); |
||
| 338 | |||
| 339 | if (!property_exists($class, $attribute)) { |
||
| 340 | throw new InvalidArgumentException("Undefined property: \"$class::$attribute\"."); |
||
| 341 | } |
||
| 342 | |||
| 343 | if ($this->isPublicAttribute($attribute)) { |
||
| 344 | return $nested === null ? $this->$attribute : $this->$attribute->getAttributeValue($nested); |
||
| 345 | } |
||
| 346 | |||
| 347 | $getter = fn (FormModel $class, $attribute) => $nested === null |
||
| 348 | ? $class->$attribute |
||
| 349 | : $class->$attribute->getAttributeValue($nested); |
||
| 350 | $getter = Closure::bind($getter, null, $this); |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @psalm-var Closure $getter |
||
| 354 | */ |
||
| 355 | return $getter($this, $attribute); |
||
| 356 | } |
||
| 357 | |||
| 358 | private function writeProperty(string $attribute, $value): void |
||
| 377 | } |
||
| 378 | } |
||
| 379 | |||
| 380 | private function isPublicAttribute(string $attribute): bool |
||
| 383 | } |
||
| 384 | |||
| 385 | private function getNestedAttribute(string $attribute): array |
||
| 386 | { |
||
| 387 | if (strpos($attribute, '.') === false) { |
||
| 388 | return [$attribute, null]; |
||
| 389 | } |
||
| 390 | |||
| 391 | [$attribute, $nested] = explode('.', $attribute, 2); |
||
| 392 | |||
| 393 | if (!is_subclass_of($this->attributes[$attribute], self::class)) { |
||
| 394 | throw new InvalidArgumentException('Nested attribute can only be of ' . self::class . ' type.'); |
||
| 395 | } |
||
| 396 | |||
| 397 | return [$attribute, $nested]; |
||
| 398 | } |
||
| 399 | |||
| 400 | public function isValidated(): bool |
||
| 403 | } |
||
| 404 | } |
||
| 405 |