Conditions | 15 |
Paths | 16 |
Total Lines | 46 |
Code Lines | 25 |
Lines | 3 |
Ratio | 6.52 % |
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 |
||
30 | throw new \InvalidArgumentException('$minLength was not a positive integer value'); |
||
31 | } |
||
32 | |||
33 | if ($maxLength < 0) { |
||
34 | throw new \InvalidArgumentException('$maxLength was not a positive integer value'); |
||
35 | } |
||
36 | |||
37 | if ($allowNull === true && $value === null) { |
||
38 | return null; |
||
39 | } |
||
40 | |||
41 | if (is_scalar($value)) { |
||
42 | $value = "{$value}"; |
||
43 | } |
||
44 | |||
45 | if (is_object($value) && method_exists($value, '__toString')) { |
||
46 | $value = (string)$value; |
||
47 | } |
||
48 | |||
49 | View Code Duplication | if (!is_string($value)) { |
|
|
|||
50 | throw new Exception("Value '" . var_export($value, true) . "' is not a string"); |
||
51 | } |
||
52 | |||
53 | $valueLength = strlen($value); |
||
54 | |||
55 | if ($valueLength < $minLength || $valueLength > $maxLength) { |
||
56 | throw new Exception( |
||
57 | sprintf( |
||
58 | "Value '%s' with length '%d' is less than '%d' or greater than '%d'", |
||
59 | $value, |
||
60 | $valueLength, |
||
61 | $minLength, |
||
62 | $maxLength |
||
63 | ) |
||
64 | ); |
||
65 | } |
||
66 | |||
67 | return $value; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Explodes a string into an array using the given delimiter. |
||
72 | * |
||
73 | * For example, given the string 'foo,bar,baz', this would return the array ['foo', 'bar', 'baz']. |
||
74 | * |
||
75 | * @param string $value The string to explode. |
||
76 | * @param string $delimiter The non-empty delimiter to explode on. |
||
92 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.