Conditions | 9 |
Paths | 16 |
Total Lines | 64 |
Code Lines | 35 |
Lines | 14 |
Ratio | 21.88 % |
Changes | 2 | ||
Bugs | 1 | 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 |
||
38 | public function __construct($intValue = null, $stringValue = null) |
||
39 | { |
||
40 | // Validates filters for only null or int/string value types. |
||
41 | parent::__construct($intValue, $stringValue); |
||
42 | |||
43 | if ($intValue === null) { |
||
44 | $this->intValue = $intValue; |
||
45 | |||
46 | // Ignore string value if intValue is null. |
||
47 | $this->stringValue = ''; |
||
48 | } else { |
||
49 | // Validation of values |
||
50 | View Code Duplication | if ($this->IntValue < 0) { |
|
|
|||
51 | $args = [ |
||
52 | 'position' => '1st', |
||
53 | 'actual' => $intValue, |
||
54 | ]; |
||
55 | |||
56 | $msg = nml_msg('Invalid argument value.'); |
||
57 | $msg .= nml_msg( |
||
58 | ' {position} argument must to be a positive number; "{actual}" given.', |
||
59 | $args |
||
60 | ); |
||
61 | |||
62 | throw new InvalidArgumentException($msg); |
||
63 | } // Integer is valid |
||
64 | |||
65 | if ($stringValue !== null) { |
||
66 | if ($this->StringValue != '') { |
||
67 | $pattern = '~^([a-z])$~'; // 1 char |
||
68 | |||
69 | if (strlen($this->StringValue) > 1) { |
||
70 | $start = '~^([a-z]|-)'; |
||
71 | $middle = '([a-z]|[0-9]|-)*'; |
||
72 | $end = '([a-z]|[0-9])$~'; |
||
73 | |||
74 | $pattern = $start.$middle.$end; |
||
75 | } |
||
76 | |||
77 | $correct = (boolean) preg_match($pattern, $this->StringValue); |
||
78 | |||
79 | if ($correct) { |
||
80 | //Último chequeo: que no hayan 2 '-' consecutivos. |
||
81 | $correct = strpos($this->StringValue, '--') == false ? true : false; |
||
82 | } |
||
83 | |||
84 | if (!$correct) { |
||
85 | $args = [ |
||
86 | 'position' => '2nd', |
||
87 | 'actual' => $stringValue, |
||
88 | ]; |
||
89 | |||
90 | $msg = nml_msg('Invalid argument value.'); |
||
91 | $msg .= nml_msg( |
||
92 | ' {position} parameter has invalid chars; "{actual}" given.', |
||
93 | $args |
||
94 | ); |
||
95 | |||
96 | throw new InvalidArgumentException($msg); |
||
97 | } |
||
98 | } |
||
99 | } // String is valid |
||
100 | } |
||
101 | } |
||
102 | |||
231 |
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.