| Total Lines | 154 |
| Code Lines | 98 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 36 | public function dataOptions(): array |
||
| 37 | { |
||
| 38 | return [ |
||
| 39 | [ |
||
| 40 | new Composite([ |
||
| 41 | new Number(max: 13, pattern: '/1/'), |
||
| 42 | new Number(max: 14, pattern: '/2/'), |
||
| 43 | ]), |
||
| 44 | [ |
||
| 45 | 'skipOnEmpty' => false, |
||
| 46 | 'skipOnError' => false, |
||
| 47 | 'rules' => [ |
||
| 48 | [ |
||
| 49 | 'number', |
||
| 50 | 'min' => null, |
||
| 51 | 'max' => 13, |
||
| 52 | 'incorrectInputMessage' => [ |
||
| 53 | 'template' => 'The allowed types are integer, float and string.', |
||
| 54 | 'parameters' => [], |
||
| 55 | ], |
||
| 56 | 'notNumberMessage' => [ |
||
| 57 | 'template' => 'Value must be a number.', |
||
| 58 | 'parameters' => [], |
||
| 59 | ], |
||
| 60 | 'lessThanMinMessage' => [ |
||
| 61 | 'template' => 'Value must be no less than {min}.', |
||
| 62 | 'parameters' => ['min' => null], |
||
| 63 | ], |
||
| 64 | 'greaterThanMaxMessage' => [ |
||
| 65 | 'template' => 'Value must be no greater than {max}.', |
||
| 66 | 'parameters' => ['max' => 13], |
||
| 67 | ], |
||
| 68 | 'skipOnEmpty' => false, |
||
| 69 | 'skipOnError' => false, |
||
| 70 | 'pattern' => '/1/', |
||
| 71 | ], |
||
| 72 | [ |
||
| 73 | 'number', |
||
| 74 | 'min' => null, |
||
| 75 | 'max' => 14, |
||
| 76 | 'incorrectInputMessage' => [ |
||
| 77 | 'template' => 'The allowed types are integer, float and string.', |
||
| 78 | 'parameters' => [], |
||
| 79 | ], |
||
| 80 | 'notNumberMessage' => [ |
||
| 81 | 'template' => 'Value must be a number.', |
||
| 82 | 'parameters' => [], |
||
| 83 | ], |
||
| 84 | 'lessThanMinMessage' => [ |
||
| 85 | 'template' => 'Value must be no less than {min}.', |
||
| 86 | 'parameters' => ['min' => null], |
||
| 87 | ], |
||
| 88 | 'greaterThanMaxMessage' => [ |
||
| 89 | 'template' => 'Value must be no greater than {max}.', |
||
| 90 | 'parameters' => ['max' => 14], |
||
| 91 | ], |
||
| 92 | 'skipOnEmpty' => false, |
||
| 93 | 'skipOnError' => false, |
||
| 94 | 'pattern' => '/2/', |
||
| 95 | ], |
||
| 96 | ], |
||
| 97 | ], |
||
| 98 | ], |
||
| 99 | 'rule without options' => [ |
||
| 100 | new Composite([ |
||
| 101 | new Number(max: 13, pattern: '/1/'), |
||
| 102 | new RuleWithoutOptions(), |
||
| 103 | ]), |
||
| 104 | [ |
||
| 105 | 'skipOnEmpty' => false, |
||
| 106 | 'skipOnError' => false, |
||
| 107 | 'rules' => [ |
||
| 108 | [ |
||
| 109 | 'number', |
||
| 110 | 'min' => null, |
||
| 111 | 'max' => 13, |
||
| 112 | 'incorrectInputMessage' => [ |
||
| 113 | 'template' => 'The allowed types are integer, float and string.', |
||
| 114 | 'parameters' => [], |
||
| 115 | ], |
||
| 116 | 'notNumberMessage' => [ |
||
| 117 | 'template' => 'Value must be a number.', |
||
| 118 | 'parameters' => [], |
||
| 119 | ], |
||
| 120 | 'lessThanMinMessage' => [ |
||
| 121 | 'template' => 'Value must be no less than {min}.', |
||
| 122 | 'parameters' => [ |
||
| 123 | 'min' => null, |
||
| 124 | ], |
||
| 125 | ], |
||
| 126 | 'greaterThanMaxMessage' => [ |
||
| 127 | 'template' => 'Value must be no greater than {max}.', |
||
| 128 | 'parameters' => [ |
||
| 129 | 'max' => 13, |
||
| 130 | ], |
||
| 131 | ], |
||
| 132 | 'skipOnEmpty' => false, |
||
| 133 | 'skipOnError' => false, |
||
| 134 | 'pattern' => '/1/', |
||
| 135 | ], |
||
| 136 | [ |
||
| 137 | 'test', |
||
| 138 | ], |
||
| 139 | ], |
||
| 140 | ], |
||
| 141 | ], |
||
| 142 | 'callable' => [ |
||
| 143 | new Composite([ |
||
| 144 | static fn () => (new Result())->addError('Bad value.'), |
||
| 145 | ]), |
||
| 146 | [ |
||
| 147 | 'skipOnEmpty' => false, |
||
| 148 | 'skipOnError' => false, |
||
| 149 | 'rules' => [ |
||
| 150 | [ |
||
| 151 | 'callback', |
||
| 152 | 'method' => null, |
||
| 153 | 'skipOnEmpty' => false, |
||
| 154 | 'skipOnError' => false, |
||
| 155 | ], |
||
| 156 | ], |
||
| 157 | ], |
||
| 158 | ], |
||
| 159 | 'inheritance' => [ |
||
| 160 | new class () extends Composite { |
||
| 161 | public function getRules(): iterable |
||
| 162 | { |
||
| 163 | return [ |
||
| 164 | new Required(), |
||
| 165 | ]; |
||
| 166 | } |
||
| 167 | |||
| 168 | public function getOptions(): array |
||
| 169 | { |
||
| 170 | return [ |
||
| 171 | 'specific-key' => 42, |
||
| 172 | 'rules' => $this->dumpRulesAsArray(), |
||
| 173 | ]; |
||
| 174 | } |
||
| 175 | }, |
||
| 176 | [ |
||
| 177 | 'specific-key' => 42, |
||
| 178 | 'rules' => [ |
||
| 179 | [ |
||
| 180 | 'required', |
||
| 181 | 'message' => [ |
||
| 182 | 'template' => 'Value cannot be blank.', |
||
| 183 | 'parameters' => [], |
||
| 184 | ], |
||
| 185 | 'notPassedMessage' => [ |
||
| 186 | 'template' => 'Value not passed.', |
||
| 187 | 'parameters' => [], |
||
| 188 | ], |
||
| 189 | 'skipOnError' => false, |
||
| 190 | ], |
||
| 380 |