| Conditions | 3 |
| Paths | 4 |
| Total Lines | 62 |
| Code Lines | 42 |
| 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 |
||
| 145 | public function testPatternNotRequired() |
||
| 146 | { |
||
| 147 | $rules = [ |
||
| 148 | 'a' => ['type' => 'integer', 'pattern' => '\d{5}'], |
||
| 149 | 'b' => ['type' => 'integer', 'pattern' => '\d{5}'], |
||
| 150 | 'c' => ['type' => 'integer', 'pattern' => '\d{5}'] |
||
| 151 | ]; |
||
| 152 | $array = [ |
||
| 153 | 'a' => null, |
||
| 154 | 'b' => '', |
||
| 155 | 'c' => 55555 |
||
| 156 | ]; |
||
| 157 | $this->assertTrue(Validation::validate($array, $rules)); |
||
| 158 | |||
| 159 | $rules = [ |
||
| 160 | 'a' => ['type' => 'int', 'pattern' => '\d{5}', 'required' => true] |
||
| 161 | ]; |
||
| 162 | $array = [ |
||
| 163 | 'a' => null |
||
| 164 | ]; |
||
| 165 | try { |
||
| 166 | $this->assertFalse(Validation::validate($array, $rules)); |
||
| 167 | } catch (Exception $e) { |
||
| 168 | $this->assertEquals('Required value not found for key: a.', $e->getMessage()); |
||
| 169 | } |
||
| 170 | |||
| 171 | $array = [ |
||
| 172 | 'a' => '' |
||
| 173 | ]; |
||
| 174 | try { |
||
| 175 | $this->assertFalse(Validation::validate($array, $rules)); |
||
| 176 | } catch (Exception $e) { |
||
| 177 | $this->assertEquals('Required value not found for key: a.', $e->getMessage()); |
||
| 178 | } |
||
| 179 | |||
| 180 | $rules = [ |
||
| 181 | 'a' => ['type' => 'integer', 'pattern' => '\d{1}', 'required' => true] |
||
| 182 | ]; |
||
| 183 | $array = [ |
||
| 184 | 'a' => 0 |
||
| 185 | ]; |
||
| 186 | $this->assertTrue(Validation::validate($array, $rules)); |
||
| 187 | |||
| 188 | $rules = [ |
||
| 189 | 'a' => ['type' => 'integer', 'required' => true] |
||
| 190 | ]; |
||
| 191 | $array = [ |
||
| 192 | 'a' => 0 |
||
| 193 | ]; |
||
| 194 | $this->assertTrue(Validation::validate($array, $rules)); |
||
| 195 | |||
| 196 | $rules = [ |
||
| 197 | 'a' => ['type' => 'string', 'pattern' => 'IP'], |
||
| 198 | 'b' => ['type' => 'string', 'pattern' => 'IP'], |
||
| 199 | 'c' => ['type' => 'string', 'pattern' => 'IP'] |
||
| 200 | ]; |
||
| 201 | $array = [ |
||
| 202 | 'a' => null, |
||
| 203 | 'b' => '', |
||
| 204 | 'c' => '127.0.0.1' |
||
| 205 | ]; |
||
| 206 | $this->assertTrue(Validation::validate($array, $rules)); |
||
| 207 | |||
| 211 | } |