| Conditions | 12 |
| Paths | 51 |
| Total Lines | 70 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 44 | private function handleAttributes(ReflectionClass $classMeta): array |
||
| 45 | { |
||
| 46 | $rules = []; |
||
| 47 | foreach ($classMeta->getProperties() as $property) { |
||
| 48 | if ($property->isStatic()) { |
||
| 49 | continue; |
||
| 50 | } |
||
| 51 | |||
| 52 | foreach ([HasMany::class, HasOne::class] as $className) { |
||
| 53 | $attributes = $property->getAttributes($className); |
||
| 54 | if (empty($attributes)) { |
||
| 55 | continue; |
||
| 56 | } |
||
| 57 | |||
| 58 | $relatedClassName = $attributes[0]->getArguments()[0]; |
||
| 59 | |||
| 60 | try { |
||
| 61 | $relatedClassMeta = new ReflectionClass($relatedClassName); |
||
| 62 | } catch (ReflectionException) { |
||
| 63 | throw new InvalidArgumentException("Class \"$relatedClassName\" not found."); |
||
| 64 | } |
||
| 65 | |||
| 66 | $nestedRule = new Nested( |
||
| 67 | $this->handleAttributes($relatedClassMeta), |
||
| 68 | ...(($property->getAttributes(Nested::class)[0] ?? null)?->getArguments() ?? []) |
||
|
|
|||
| 69 | ); |
||
| 70 | |||
| 71 | if ($className !== HasMany::class) { |
||
| 72 | $rules[$property->getName()] = $nestedRule; |
||
| 73 | } else { |
||
| 74 | /** @psalm-suppress UndefinedMethod */ |
||
| 75 | $rules[$property->getName()][] = new Each( |
||
| 76 | [$nestedRule], |
||
| 77 | ...(($property->getAttributes(Each::class)[0] ?? null)?->getArguments() ?? []) |
||
| 78 | ); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | $flatRules = []; |
||
| 83 | $attributes = $property->getAttributes(); |
||
| 84 | foreach ($attributes as $attribute) { |
||
| 85 | if (!is_subclass_of($attribute->getName(), RuleInterface::class)) { |
||
| 86 | continue; |
||
| 87 | } |
||
| 88 | |||
| 89 | if (in_array($attribute->getName(), [Each::class, Nested::class])) { |
||
| 90 | continue; |
||
| 91 | } |
||
| 92 | |||
| 93 | $flatRules[] = $attribute->newInstance(); |
||
| 94 | } |
||
| 95 | |||
| 96 | if (!$flatRules) { |
||
| 97 | continue; |
||
| 98 | } |
||
| 99 | |||
| 100 | if ((string) $property->getType() !== 'array') { |
||
| 101 | $rules[$property->getName()] = $flatRules; |
||
| 102 | |||
| 103 | continue; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** @psalm-suppress UndefinedMethod */ |
||
| 107 | $rules[$property->getName()][] = new Each( |
||
| 108 | $flatRules, |
||
| 109 | ...(($property->getAttributes(Each::class)[0] ?? null)?->getArguments() ?? []) |
||
| 110 | ); |
||
| 111 | } |
||
| 112 | |||
| 113 | return $rules; |
||
| 114 | } |
||
| 116 |