Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
34 | class Text extends TextBase |
||
35 | { |
||
36 | |||
37 | /** |
||
38 | * Replaces format elements in a string with the string representation of an |
||
39 | * object matching the list of arguments specified. You can give as many |
||
40 | * params as you need, or an array with values. |
||
41 | * |
||
42 | * ##Usage |
||
43 | * Using numbers as placeholders (encloses between `{` and `}`), you can get |
||
44 | * the matching string representation of each object given. Use `{0}` for |
||
45 | * the fist object, `{1}` for the second, and so on: |
||
46 | * |
||
47 | * ```php |
||
48 | * $format = '{0} is {1} years old, and have {2} cats.'; |
||
49 | * echo Text::format($format, 'Bob', 65, 101); // 'Bob is 65 years old, and have 101 cats.' |
||
50 | * ``` |
||
51 | * |
||
52 | * You can also use an array to give objects values: |
||
53 | * |
||
54 | * ```php |
||
55 | * $format = '{0} is {1} years old, and have {2} cats.'; |
||
56 | * $data = ['Bob', 65, 101]; |
||
57 | * echo Text::format($format, $data); // 'Bob is 65 years old, and have 101 cats.' |
||
58 | * ``` |
||
59 | * |
||
60 | * This is specially useful to be able to use non-numeric placeholders (named placeholders): |
||
61 | * |
||
62 | * ```php |
||
63 | * $format = '{name} is {age} years old, and have {n} cats.'; |
||
64 | * $data = ['name' => 'Bob', 'n' => 101, 'age' => 65]; |
||
65 | * echo Text::format($format, $data); // 'Bob is 65 years old, and have 101 cats.' |
||
66 | * ``` |
||
67 | * |
||
68 | * For numeric placeholders, yo can convert the array into a list of arguments. |
||
69 | * |
||
70 | * ```php |
||
71 | * $format = '{0} is {1} years old, and have {2} cats.'; |
||
72 | * $data = ['Bob', 65, 101]; |
||
73 | * echo Text::format($format, ...$data); // 'Bob is 65 years old, and have 101 cats.' |
||
74 | * ``` |
||
75 | * |
||
76 | * > Note: If objects are not convertible to string, it will throws and catchable exception |
||
77 | * (`InvalidArgumentException`). |
||
78 | * |
||
79 | * @param string $format An string containing variable placeholders to be replaced. If you provide name |
||
80 | * placeholders, you must pass the target array as |
||
81 | * @param array|mixed $args Object(s) to be replaced into $format placeholders. |
||
82 | * You can provide one item only of type array for named placeholders replacement. For numeric placeholders, you |
||
83 | * can still pass the array or convert it into arguments by using the '...' syntax instead. |
||
84 | * |
||
85 | * @return string |
||
86 | * @throws InvalidArgumentException if $format is not an string or placeholder values are not string-convertibles. |
||
87 | * @todo Implement formatting, like IFormatProvider or something like that. |
||
88 | * @author Nelson Martell <[email protected]> |
||
89 | */ |
||
90 | 234 | public static function format($format, ...$args) |
|
122 | |||
123 | /** |
||
124 | * Ensures that object given is not null. If is `null`, throws and exception. |
||
125 | * |
||
126 | * @param mixed $obj Object to validate |
||
127 | * |
||
128 | * @return mixed Same object |
||
129 | * @throws InvalidArgumentException if object is `null`. |
||
130 | */ |
||
131 | 303 | public static function ensureIsNotNull($obj) |
|
140 | |||
141 | /** |
||
142 | * Ensures that object given is an string. Else, thows an exception |
||
143 | * |
||
144 | * @param mixed $obj Object to validate. |
||
145 | * |
||
146 | * @return string Same object given, but ensured that is an string. |
||
147 | * @throws InvalidArgumentException if object is not an `string`. |
||
148 | */ |
||
149 | 303 | public static function ensureIsString($obj) |
|
158 | |||
159 | /** |
||
160 | * Ensures that given string is not empty. |
||
161 | * |
||
162 | * @param string $string String to validate. |
||
163 | * |
||
164 | * @return string Same string given, but ensured that is not empty. |
||
165 | * @throws InvalidArgumentException if string is null or empty. |
||
166 | */ |
||
167 | View Code Duplication | public static function ensureIsNotEmpty($string) |
|
176 | |||
177 | /** |
||
178 | * Ensures that given string is not empty or whitespaces. |
||
179 | * |
||
180 | * @param string $string String to validate. |
||
181 | * |
||
182 | * @return string Same string given, but ensured that is not whitespaces. |
||
183 | * @throws InvalidArgumentException if object is not an `string`. |
||
184 | * @see \trim() |
||
185 | */ |
||
186 | View Code Duplication | public static function ensureIsNotWhiteSpaces($string) |
|
195 | |||
196 | /** |
||
197 | * Ensures that an string follows the PHP variables naming convention. |
||
198 | * |
||
199 | * @param string $string String to be ensured. |
||
200 | * |
||
201 | * @return string |
||
202 | * @throws InvalidArgumentException if object is not an `string` or do not |
||
203 | * follows the PHP variables naming convention. |
||
204 | */ |
||
205 | 303 | public static function ensureIsValidVarName($string) |
|
216 | } |
||
217 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.