Conditions | 12 |
Paths | 11 |
Total Lines | 40 |
Code Lines | 22 |
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 |
||
26 | public function valid($value) |
||
27 | { |
||
28 | if ($value === 'max') { |
||
29 | return true; |
||
30 | } |
||
31 | |||
32 | if ($value === '^max') { |
||
33 | throw new NotImplementedException("Maximum size is not implemented."); |
||
34 | } |
||
35 | |||
36 | if (! $this->allow_upscaling && $this->upscale) { |
||
37 | throw new NotImplementedException("Upscaling is not allowed."); |
||
38 | } |
||
39 | |||
40 | $this->upscale = str_starts_with($value, '^'); |
||
41 | $isPercent = str_contains($value, 'pct:'); |
||
42 | $percent_value = (int)$this->getPercentValue($value); |
||
43 | |||
44 | if ($isPercent) { |
||
45 | if ($percent_value == 0 || $percent_value < 1) { |
||
46 | throw new BadRequestException("Size $value is invalid."); |
||
47 | } |
||
48 | if ($this->upscale) { |
||
49 | return true; |
||
50 | } |
||
51 | if ($percent_value > 100) { |
||
52 | throw new BadRequestException("Size $value is invalid."); |
||
53 | } |
||
54 | |||
55 | if ($percent_value <= 100) { |
||
56 | return true; |
||
57 | } |
||
58 | } |
||
59 | |||
60 | $all_regex = implode('|', $this->regex); |
||
61 | if (preg_match('/' . $all_regex . '/', $value)) { |
||
62 | return true; |
||
63 | } |
||
64 | |||
65 | throw new BadRequestException("Size $value is invalid."); |
||
66 | } |
||
77 |