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 |
||
| 14 | class CliParser |
||
| 15 | { |
||
| 16 | /** @var string */ |
||
| 17 | private $programDescription; |
||
| 18 | |||
| 19 | /** @var array */ |
||
| 20 | private $optionList; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Create CliParser object. |
||
| 24 | * |
||
| 25 | * @param string $programDescription a short sentence indicating what the |
||
| 26 | * program is about |
||
| 27 | * @param array $optionList an array with all options, whether |
||
| 28 | * they are required and whether they |
||
| 29 | * take values |
||
| 30 | * |
||
| 31 | * Examples: |
||
| 32 | * $optionList = [ |
||
| 33 | * ['foo', 'Foo Description', false, false], // no value, not required |
||
| 34 | * ['bar', 'Bar Description', false, true ], // no value, required |
||
| 35 | * ['baz', 'Baz Description', true, false], // value, not required |
||
| 36 | * ['xyz', 'Xyz Description', true, true ], // value, required |
||
| 37 | * ]; |
||
| 38 | * |
||
| 39 | * The first field indicates the name of the parameter, the user will have |
||
| 40 | * to provide --<OPT>, e.g. --foo, --bar, ... |
||
| 41 | * |
||
| 42 | * The second describes the parameter, that is used when the --help |
||
| 43 | * parameter is used. |
||
| 44 | * |
||
| 45 | * The third indicates whether or not the parameter requires a value, e.g. |
||
| 46 | * --baz abc where 'abc' is the value. |
||
| 47 | * |
||
| 48 | * The fourth indicates whether or not the parameter is required |
||
| 49 | */ |
||
| 50 | public function __construct($programDescription, array $optionList) |
||
| 55 | |||
| 56 | public function help() |
||
| 74 | |||
| 75 | public function parse(array $argv) |
||
| 126 | } |
||
| 127 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.