| Conditions | 1 |
| Total Lines | 86 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 84 | public function dataValidationFailed(): array |
||
| 85 | { |
||
| 86 | $rule = new StringValue(); |
||
| 87 | $message = 'The value must be a string.'; |
||
| 88 | |||
| 89 | return [ |
||
| 90 | 'value: null' => [null, [$rule], ['' => [$message]]], |
||
| 91 | 'value: integer' => [1, [$rule], ['' => [$message]]], |
||
| 92 | 'value: float' => [1.5, [$rule], ['' => [$message]]], |
||
| 93 | 'value: boolean' => [false, [$rule], ['' => [$message]]], |
||
| 94 | 'value: array' => [['test'], [$rule], ['' => [$message]]], |
||
| 95 | 'value: object' => [new stdClass(), [$rule], ['' => [$message]]], |
||
| 96 | 'value: null, multiple rules' => [ |
||
| 97 | null, |
||
| 98 | [ |
||
| 99 | new StringValue(), |
||
| 100 | new StringValue(), |
||
| 101 | ], |
||
| 102 | ['' => [$message, $message]], |
||
| 103 | ], |
||
| 104 | 'value: null, multiple rules, skipOnError: true' => [ |
||
| 105 | null, |
||
| 106 | [ |
||
| 107 | new StringValue(), |
||
| 108 | new StringValue(skipOnError: true), |
||
| 109 | ], |
||
| 110 | ['' => [$message]], |
||
| 111 | ], |
||
| 112 | 'value: integer, when: custom callable allowing everything except null' => [ |
||
| 113 | 1, |
||
| 114 | [new StringValue(when: static fn (mixed $value): bool => $value !== null)], |
||
| 115 | ['' => [$message]], |
||
| 116 | ], |
||
| 117 | 'value: object providing rules and wrong data' => [ |
||
| 118 | new class () { |
||
| 119 | #[StringValue] |
||
| 120 | private ?string $name = null; |
||
| 121 | }, |
||
| 122 | null, |
||
| 123 | ['name' => [$message]], |
||
| 124 | ], |
||
| 125 | 'value: boolean, message: custom' => [ |
||
| 126 | false, |
||
| 127 | [new StringValue(message: 'Custom message.')], |
||
| 128 | ['' => ['Custom message.']], |
||
| 129 | ], |
||
| 130 | 'value: boolean, message: custom, with parameters' => [ |
||
| 131 | false, |
||
| 132 | [new StringValue(message: 'Attribute - {attribute}, type - {type}.')], |
||
| 133 | ['' => ['Attribute - , type - bool.']], |
||
| 134 | ], |
||
| 135 | 'value: boolean, message: custom, with parameters, attribute set' => [ |
||
| 136 | ['data' => false], |
||
| 137 | ['data' => new StringValue(message: 'Attribute - {attribute}, type - {type}.')], |
||
| 138 | ['data' => ['Attribute - data, type - bool.']], |
||
| 139 | ], |
||
| 140 | 'value: object providing rules, attribute labels and wrong data' => [ |
||
| 141 | new class () implements RulesProviderInterface, AttributeTranslatorProviderInterface { |
||
| 142 | public function __construct( |
||
| 143 | public ?string $name = null, |
||
| 144 | ) { |
||
| 145 | } |
||
| 146 | |||
| 147 | public function getAttributeLabels(): array |
||
| 148 | { |
||
| 149 | return [ |
||
| 150 | 'name' => 'Имя', |
||
| 151 | ]; |
||
| 152 | } |
||
| 153 | |||
| 154 | public function getAttributeTranslator(): ?AttributeTranslatorInterface |
||
| 155 | { |
||
| 156 | return new ArrayAttributeTranslator($this->getAttributeLabels()); |
||
| 157 | } |
||
| 158 | |||
| 159 | public function getRules(): array |
||
| 160 | { |
||
| 161 | return [ |
||
| 162 | 'name' => [ |
||
| 163 | new StringValue(message: '{attribute} плохое.'), |
||
| 164 | ], |
||
| 165 | ]; |
||
| 166 | } |
||
| 167 | }, |
||
| 168 | null, |
||
| 169 | ['name' => ['Имя плохое.']], |
||
| 170 | ], |
||
| 195 |