| Conditions | 1 |
| Total Lines | 117 |
| 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 |
||
| 93 | public function dataValidationFailed(): array |
||
| 94 | { |
||
| 95 | $lessThanMinmessage = 'This value must contain at least 3 items.'; |
||
| 96 | $greaterThanMaxMessage = 'This value must contain at most 3 items.'; |
||
| 97 | |||
| 98 | return [ |
||
| 99 | 'incorrect input' => [ |
||
| 100 | 1, |
||
| 101 | [new Count(min: 3)], |
||
| 102 | ['' => ['This value must be an array or implement \Countable interface.']], |
||
| 103 | ], |
||
| 104 | 'custom incorrect input message' => [ |
||
| 105 | 1, |
||
| 106 | [new Count(min: 3, incorrectInputMessage: 'Custom incorrect input message.')], |
||
| 107 | ['' => ['Custom incorrect input message.']], |
||
| 108 | ], |
||
| 109 | 'custom incorrect input message with parameters' => [ |
||
| 110 | 1, |
||
| 111 | [new Count(min: 3, incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], |
||
| 112 | ['' => ['Attribute - , type - int.']], |
||
| 113 | ], |
||
| 114 | 'custom incorrect input message, attribute set' => [ |
||
| 115 | ['data' => 1], |
||
| 116 | ['data' => new Count(min: 3, incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], |
||
| 117 | ['data' => ['Attribute - data, type - int.']], |
||
| 118 | ], |
||
| 119 | |||
| 120 | [[1], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
||
| 121 | [[], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
||
| 122 | [[0, 0], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
||
| 123 | [[1.1], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
||
| 124 | [[''], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
||
| 125 | [['some string'], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
||
| 126 | [[new stdClass()], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
||
| 127 | // https://www.php.net/manual/ru/class.countable.php |
||
| 128 | [ |
||
| 129 | [ |
||
| 130 | new class () { |
||
| 131 | protected int $myCount = 3; |
||
| 132 | |||
| 133 | public function count(): int |
||
| 134 | { |
||
| 135 | return $this->myCount; |
||
| 136 | } |
||
| 137 | }, |
||
| 138 | ], |
||
| 139 | [new Count(min: 3)], |
||
| 140 | ['' => [$lessThanMinmessage]], |
||
| 141 | ], |
||
| 142 | [[0, 0, 0, 0], [new Count(max: 3)], ['' => [$greaterThanMaxMessage]]], |
||
| 143 | |||
| 144 | 'custom less than min message' => [ |
||
| 145 | [0, 0], |
||
| 146 | [new Count(min: 3, lessThanMinMessage: 'Custom less than min message.')], |
||
| 147 | ['' => ['Custom less than min message.']], |
||
| 148 | ], |
||
| 149 | 'custom less than min message with parameters' => [ |
||
| 150 | [0, 0], |
||
| 151 | [new Count(min: 3, lessThanMinMessage: 'Min - {min}, attribute - {attribute}, number - {number}.')], |
||
| 152 | ['' => ['Min - 3, attribute - , number - 2.']], |
||
| 153 | ], |
||
| 154 | 'custom less than min message with parameters, attribute set' => [ |
||
| 155 | ['data' => [0, 0]], |
||
| 156 | [ |
||
| 157 | 'data' => new Count( |
||
| 158 | min: 3, |
||
| 159 | lessThanMinMessage: 'Min - {min}, attribute - {attribute}, number - {number}.', |
||
| 160 | ), |
||
| 161 | ], |
||
| 162 | ['data' => ['Min - 3, attribute - data, number - 2.']], |
||
| 163 | ], |
||
| 164 | |||
| 165 | 'custom greater than max message' => [ |
||
| 166 | [0, 0, 0, 0], |
||
| 167 | [new Count(max: 3, greaterThanMaxMessage: 'Custom greater than max message.')], |
||
| 168 | ['' => ['Custom greater than max message.']], |
||
| 169 | ], |
||
| 170 | 'custom greater than max message with parameters' => [ |
||
| 171 | [0, 0, 0, 0], |
||
| 172 | [new Count(max: 3, greaterThanMaxMessage: 'Max - {max}, attribute - {attribute}, number - {number}.')], |
||
| 173 | ['' => ['Max - 3, attribute - , number - 4.']], |
||
| 174 | ], |
||
| 175 | 'custom greater than max message with parameters, attribute set' => [ |
||
| 176 | ['data' => [0, 0, 0, 0]], |
||
| 177 | [ |
||
| 178 | 'data' => new Count( |
||
| 179 | max: 3, |
||
| 180 | greaterThanMaxMessage: 'Max - {max}, attribute - {attribute}, number - {number}.', |
||
| 181 | ), |
||
| 182 | ], |
||
| 183 | ['data' => ['Max - 3, attribute - data, number - 4.']], |
||
| 184 | ], |
||
| 185 | |||
| 186 | 'custom not exactly message' => [ |
||
| 187 | [0, 0, 0, 0], |
||
| 188 | [new Count(exactly: 3, notExactlyMessage: 'Custom not exactly message.')], |
||
| 189 | ['' => ['Custom not exactly message.']], |
||
| 190 | ], |
||
| 191 | 'custom not exactly message with parameters' => [ |
||
| 192 | [0, 0, 0, 0], |
||
| 193 | [ |
||
| 194 | new Count( |
||
| 195 | exactly: 3, |
||
| 196 | notExactlyMessage: 'Exactly - {exactly}, attribute - {attribute}, number - {number}.', |
||
| 197 | ), |
||
| 198 | ], |
||
| 199 | ['' => ['Exactly - 3, attribute - , number - 4.']], |
||
| 200 | ], |
||
| 201 | 'custom not exactly message with parameters, attribute set' => [ |
||
| 202 | ['data' => [0, 0, 0, 0]], |
||
| 203 | [ |
||
| 204 | 'data' => new Count( |
||
| 205 | exactly: 3, |
||
| 206 | notExactlyMessage: 'Exactly - {exactly}, attribute - {attribute}, number - {number}.', |
||
| 207 | ), |
||
| 208 | ], |
||
| 209 | ['data' => ['Exactly - 3, attribute - data, number - 4.']], |
||
| 210 | ], |
||
| 235 |