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 |
||
31 | class Text extends TextBase |
||
32 | { |
||
33 | |||
34 | /** |
||
35 | * Replaces format elements in a string with the string representation of an |
||
36 | * object matching the list of arguments specified. You can give as many |
||
37 | * params as you need, or an array with values. |
||
38 | * |
||
39 | * ##Usage |
||
40 | * Using numbers as placeholders (encloses between `{` and `}`), you can get |
||
41 | * the matching string representation of each object given. Use `{0}` for |
||
42 | * the fist object, `{1}` for the second, and so on. |
||
43 | * Example: |
||
44 | * `Text::format('{0} is {1} years old, and have {2} cats.', 'Bob', 65, 101);` |
||
45 | * Returns: 'Bob is 65 years old, and have 101 cats.' |
||
46 | * |
||
47 | * You can also use an array to give objects values. |
||
48 | * Example: `Text::Format('{0} is {1} years old.', ['Bob', 65, 101]);` |
||
49 | * Returns: 'Bob is 65 years old, and have 101 cats.' |
||
50 | * |
||
51 | * If give an key => value array, each key stands for a placeholder variable |
||
52 | * name to be replaced with value key. In this case, order of keys do not |
||
53 | * matter. |
||
54 | * Example: |
||
55 | * `$arg0 = ['name' => 'Bob', 'n' => 101, 'age' => 65];` |
||
56 | * `$format = '{name} is {age} years old, and have {n} cats.';` |
||
57 | * `Text::Format($format, $arg0);` |
||
58 | * Returns: 'Bob is 65 years old, and have 101 cats.' |
||
59 | * |
||
60 | * @param string $format An string containing variable placeholders to be replaced. |
||
61 | * @param string[]|array|mixed $args Object(s) to be replaced into $format. |
||
62 | * placeholders. |
||
63 | * |
||
64 | * @return string |
||
65 | * @throws InvalidArgumentException if $format is not an string or placeholder values are not string-convertibles. |
||
66 | * @todo Implement, for php 5.6+: |
||
67 | * php.net/functions.arguments.html#functions.variable-arg-list.new |
||
68 | * @todo Implement formatting, like IFormatProvider or something like that. |
||
69 | * @author Nelson Martell <[email protected]> |
||
70 | */ |
||
71 | 197 | public static function format($format, $args) |
|
103 | |||
104 | /** |
||
105 | * Ensures that object given is not null. If is `null`, throws and exception. |
||
106 | * |
||
107 | * @param mixed $obj Object to validate |
||
108 | * |
||
109 | * @return mixed Same object |
||
110 | * @throws InvalidArgumentException if object is `null`. |
||
111 | */ |
||
112 | 272 | public static function ensureIsNotNull($obj) |
|
121 | |||
122 | /** |
||
123 | * Ensures that object given is an string. Else, thows an exception |
||
124 | * |
||
125 | * @param mixed $obj Object to validate. |
||
126 | * |
||
127 | * @return string Same object given, but ensured that is an string. |
||
128 | * @throws InvalidArgumentException if object is not an `string`. |
||
129 | */ |
||
130 | 272 | public static function ensureIsString($obj) |
|
139 | |||
140 | /** |
||
141 | * Ensures that given string is not empty. |
||
142 | * |
||
143 | * @param string $string String to validate. |
||
144 | * |
||
145 | * @return string Same string given, but ensured that is not empty. |
||
146 | * @throws InvalidArgumentException if string is null or empty. |
||
147 | */ |
||
148 | View Code Duplication | public static function ensureIsNotEmpty($string) |
|
157 | |||
158 | /** |
||
159 | * Ensures that given string is not empty or whitespaces. |
||
160 | * |
||
161 | * @param string $string String to validate. |
||
162 | * |
||
163 | * @return string Same string given, but ensured that is not whitespaces. |
||
164 | * @throws InvalidArgumentException if object is not an `string`. |
||
165 | * @see trim |
||
166 | */ |
||
167 | View Code Duplication | public static function ensureIsNotWhiteSpaces($string) |
|
176 | |||
177 | /** |
||
178 | * Ensures that an string follows the PHP variables naming convention. |
||
179 | * |
||
180 | * @param string $string String to be ensured. |
||
181 | * |
||
182 | * @return string |
||
183 | * @throws InvalidArgumentException if object is not an `string` or do not |
||
184 | * follows the PHP variables naming convention. |
||
185 | */ |
||
186 | 272 | public static function ensureIsValidVarName($string) |
|
197 | } |
||
198 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.