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 |
||
| 62 | class Argument |
||
| 63 | { |
||
| 64 | /** |
||
| 65 | * Flag: The argument is required. |
||
| 66 | */ |
||
| 67 | const REQUIRED = 1; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Flag: The argument is optional. |
||
| 71 | */ |
||
| 72 | const OPTIONAL = 2; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Flag: The argument can be repeated multiple times. |
||
| 76 | */ |
||
| 77 | const MULTI_VALUED = 4; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Flag: The value is parsed as string. |
||
| 81 | */ |
||
| 82 | const STRING = 16; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Flag: The value is parsed as boolean. |
||
| 86 | */ |
||
| 87 | const BOOLEAN = 32; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Flag: The value is parsed as integer. |
||
| 91 | */ |
||
| 92 | const INTEGER = 64; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Flag: The value is parsed as float. |
||
| 96 | */ |
||
| 97 | const FLOAT = 128; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Flag: The value "null" should be parsed as `null`. |
||
| 101 | */ |
||
| 102 | const NULLABLE = 256; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | private $name; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var int |
||
| 111 | */ |
||
| 112 | private $flags; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var mixed |
||
| 116 | */ |
||
| 117 | private $defaultValue; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var string |
||
| 121 | */ |
||
| 122 | private $description; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Creates a new argument. |
||
| 126 | * |
||
| 127 | * @param string $name The argument name |
||
| 128 | * @param int $flags A bitwise combination of the flag constants. |
||
| 129 | * @param string $description A human-readable description of the argument. |
||
|
|
|||
| 130 | * @param mixed $defaultValue The default value of the argument (must be |
||
| 131 | * null for the flag {@link self::REQUIRED}). |
||
| 132 | */ |
||
| 133 | 285 | public function __construct($name, $flags = 0, $description = null, $defaultValue = null) |
|
| 134 | { |
||
| 135 | 285 | Assert::string($name, 'The argument name must be a string. Got: %s'); |
|
| 136 | 283 | Assert::notEmpty($name, 'The argument name must not be empty.'); |
|
| 137 | 282 | Assert::startsWithLetter($name, 'The argument name must start with a letter.'); |
|
| 138 | 280 | Assert::regex($name, '~^[a-zA-Z0-9\-]+$~', 'The argument name must contain letters, digits and hyphens only.'); |
|
| 139 | 279 | Assert::nullOrString($description, 'The argument description must be a string or null. Got: %s'); |
|
| 140 | 278 | Assert::nullOrNotEmpty($description, 'The argument description must not be empty.'); |
|
| 141 | |||
| 142 | 277 | $this->assertFlagsValid($flags); |
|
| 143 | |||
| 144 | 269 | $this->addDefaultFlags($flags); |
|
| 145 | |||
| 146 | 269 | $this->name = $name; |
|
| 147 | 269 | $this->flags = $flags; |
|
| 148 | 269 | $this->description = $description; |
|
| 149 | 269 | $this->defaultValue = $this->isMultiValued() ? array() : null; |
|
| 150 | |||
| 151 | 269 | if ($this->isOptional() || null !== $defaultValue) { |
|
| 152 | 254 | $this->setDefaultValue($defaultValue); |
|
| 153 | } |
||
| 154 | 267 | } |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Returns the name of the argument. |
||
| 158 | * |
||
| 159 | * @return string The argument name. |
||
| 160 | */ |
||
| 161 | 418 | public function getName() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Returns whether the argument is required. |
||
| 168 | * |
||
| 169 | * @return bool Returns `true` if the flag {@link REQUIRED} was passed to |
||
| 170 | * the constructor. |
||
| 171 | */ |
||
| 172 | 451 | public function isRequired() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Returns whether the argument is optional. |
||
| 179 | * |
||
| 180 | * @return bool Returns `true` if the flag {@link OPTIONAL} was passed to |
||
| 181 | * the constructor. |
||
| 182 | */ |
||
| 183 | 451 | public function isOptional() |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Returns whether the argument accepts multiple values. |
||
| 190 | * |
||
| 191 | * @return bool Returns `true` if the flag {@link MULTI_VALUED} was |
||
| 192 | * passed to the constructor. |
||
| 193 | */ |
||
| 194 | 451 | public function isMultiValued() |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Sets the default value. |
||
| 201 | * |
||
| 202 | * If the argument is required, this method throws an exception. |
||
| 203 | * |
||
| 204 | * If the option is multi-valued, the passed value must be an array or |
||
| 205 | * `null`. |
||
| 206 | * |
||
| 207 | * @param mixed $defaultValue The default value. |
||
| 208 | * |
||
| 209 | * @throws InvalidValueException If the default value is invalid. |
||
| 210 | */ |
||
| 211 | 254 | View Code Duplication | public function setDefaultValue($defaultValue = null) |
| 212 | { |
||
| 213 | 254 | if ($this->isRequired()) { |
|
| 214 | 1 | throw new InvalidValueException('Required arguments do not accept default values.'); |
|
| 215 | } |
||
| 216 | |||
| 217 | 253 | if ($this->isMultiValued()) { |
|
| 218 | 37 | if (null === $defaultValue) { |
|
| 219 | 35 | $defaultValue = array(); |
|
| 220 | 2 | } elseif (!is_array($defaultValue)) { |
|
| 221 | 1 | throw new InvalidValueException(sprintf( |
|
| 222 | 'The default value of a multi-valued argument must be an '. |
||
| 223 | 1 | 'array. Got: %s', |
|
| 224 | 1 | is_object($defaultValue) ? get_class($defaultValue) : gettype($defaultValue) |
|
| 225 | )); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | 252 | $this->defaultValue = $defaultValue; |
|
| 230 | 252 | } |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Returns the default value of the argument. |
||
| 234 | * |
||
| 235 | * @return mixed The default value. |
||
| 236 | */ |
||
| 237 | 297 | public function getDefaultValue() |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Parses an argument value. |
||
| 244 | * |
||
| 245 | * Pass one of the flags {@link STRING}, {@link BOOLEAN}, {@link INTEGER} |
||
| 246 | * and {@link FLOAT} to the constructor to configure the result of this |
||
| 247 | * method. You can optionally combine the flags with {@link NULLABLE} to |
||
| 248 | * support the conversion of "null" to `null`. |
||
| 249 | * |
||
| 250 | * @param mixed $value The value to parse. |
||
| 251 | * |
||
| 252 | * @return mixed The parsed value. |
||
| 253 | * |
||
| 254 | * @throws InvalidValueException |
||
| 255 | */ |
||
| 256 | 182 | View Code Duplication | public function parseValue($value) |
| 274 | |||
| 275 | /** |
||
| 276 | * Returns the description text. |
||
| 277 | * |
||
| 278 | * @return string The description text. |
||
| 279 | */ |
||
| 280 | 293 | public function getDescription() |
|
| 284 | |||
| 285 | 277 | private function assertFlagsValid($flags) |
|
| 286 | { |
||
| 319 | |||
| 320 | 269 | private function addDefaultFlags(&$flags) |
|
| 330 | } |
||
| 331 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.