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 | * Example: |
||
| 47 | * `Text::format('{0} is {1} years old, and have {2} cats.', 'Bob', 65, 101);` |
||
| 48 | * Returns: 'Bob is 65 years old, and have 101 cats.' |
||
| 49 | * |
||
| 50 | * You can also use an array to give objects values. |
||
| 51 | * Example: `Text::Format('{0} is {1} years old.', ['Bob', 65, 101]);` |
||
| 52 | * Returns: 'Bob is 65 years old, and have 101 cats.' |
||
| 53 | * |
||
| 54 | * If give an key => value array, each key stands for a placeholder variable |
||
| 55 | * name to be replaced with value key. In this case, order of keys do not |
||
| 56 | * matter. |
||
| 57 | * Example: |
||
| 58 | * `$arg0 = ['name' => 'Bob', 'n' => 101, 'age' => 65];` |
||
| 59 | * `$format = '{name} is {age} years old, and have {n} cats.';` |
||
| 60 | * `Text::Format($format, $arg0);` |
||
| 61 | * Returns: 'Bob is 65 years old, and have 101 cats.' |
||
| 62 | * |
||
| 63 | * @param string $format An string containing variable placeholders to be replaced. |
||
| 64 | * @param string[]|array|mixed $args Object(s) to be replaced into $format. |
||
| 65 | * placeholders. |
||
| 66 | * |
||
| 67 | * @return string |
||
| 68 | * @throws InvalidArgumentException if $format is not an string or placeholder values are not string-convertibles. |
||
| 69 | * @todo Implement, for php 5.6+: |
||
| 70 | * php.net/functions.arguments.html#functions.variable-arg-list.new |
||
| 71 | * @todo Implement formatting, like IFormatProvider or something like that. |
||
| 72 | * @author Nelson Martell <[email protected]> |
||
| 73 | */ |
||
| 74 | 197 | public static function format($format, $args) |
|
| 75 | { |
||
| 76 | 197 | static $options = [ |
|
| 77 | 'before' => '{', |
||
| 78 | 'after' => '}', |
||
| 79 | ]; |
||
| 80 | |||
| 81 | 197 | $originalData = func_num_args() === 2 ? (array) $args : array_slice(func_get_args(), 1); |
|
| 82 | |||
| 83 | 197 | $data = []; |
|
| 84 | // Sanitize values to be convertibles into strings |
||
| 85 | 197 | foreach ($originalData as $placeholder => $value) { |
|
| 86 | 197 | if (!is_string($placeholder) && !is_integer($placeholder)) { |
|
| 87 | $msg = 'Placeholder must to be a of string or integer type; "{1}" type given.'; |
||
| 88 | throw new InvalidArgumentException(msg($msg, typeof($placeholder))); |
||
| 89 | } |
||
| 90 | |||
| 91 | 197 | $valueType = typeof($value); |
|
| 92 | |||
| 93 | 197 | if ($valueType->canBeString() === false) { |
|
| 94 | 3 | $msg = 'Value for "{{0}}" placeholder must to be a string or object convertible to string; "{1}" type given.'; |
|
|
1 ignored issue
–
show
|
|||
| 95 | 3 | throw new InvalidArgumentException(msg($msg, $placeholder, $valueType)); |
|
| 96 | } |
||
| 97 | |||
| 98 | // This is to work-arround a bug in use of ``asort()`` function in ``Text::insert`` (at v3.2.5) |
||
| 99 | // without SORT_STRING flag... by forcing value to be string. |
||
| 100 | 197 | settype($value, 'string'); |
|
| 101 | 197 | $data[$placeholder] = $value; |
|
| 102 | } |
||
| 103 | |||
| 104 | 197 | return static::insert($format, $data, $options); |
|
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Ensures that object given is not null. If is `null`, throws and exception. |
||
| 109 | * |
||
| 110 | * @param mixed $obj Object to validate |
||
| 111 | * |
||
| 112 | * @return mixed Same object |
||
| 113 | * @throws InvalidArgumentException if object is `null`. |
||
| 114 | */ |
||
| 115 | 272 | public static function ensureIsNotNull($obj) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Ensures that object given is an string. Else, thows an exception |
||
| 127 | * |
||
| 128 | * @param mixed $obj Object to validate. |
||
| 129 | * |
||
| 130 | * @return string Same object given, but ensured that is an string. |
||
| 131 | * @throws InvalidArgumentException if object is not an `string`. |
||
| 132 | */ |
||
| 133 | 272 | public static function ensureIsString($obj) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Ensures that given string is not empty. |
||
| 145 | * |
||
| 146 | * @param string $string String to validate. |
||
| 147 | * |
||
| 148 | * @return string Same string given, but ensured that is not empty. |
||
| 149 | * @throws InvalidArgumentException if string is null or empty. |
||
| 150 | */ |
||
| 151 | View Code Duplication | public static function ensureIsNotEmpty($string) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Ensures that given string is not empty or whitespaces. |
||
| 163 | * |
||
| 164 | * @param string $string String to validate. |
||
| 165 | * |
||
| 166 | * @return string Same string given, but ensured that is not whitespaces. |
||
| 167 | * @throws InvalidArgumentException if object is not an `string`. |
||
| 168 | * @see \trim() |
||
| 169 | */ |
||
| 170 | View Code Duplication | public static function ensureIsNotWhiteSpaces($string) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Ensures that an string follows the PHP variables naming convention. |
||
| 182 | * |
||
| 183 | * @param string $string String to be ensured. |
||
| 184 | * |
||
| 185 | * @return string |
||
| 186 | * @throws InvalidArgumentException if object is not an `string` or do not |
||
| 187 | * follows the PHP variables naming convention. |
||
| 188 | */ |
||
| 189 | 272 | public static function ensureIsValidVarName($string) |
|
| 200 | } |
||
| 201 |
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.