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 |
||
20 | class Validator |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * Class properties |
||
25 | */ |
||
26 | |||
27 | /** |
||
28 | * The available validator methods |
||
29 | * |
||
30 | * @type array |
||
31 | */ |
||
32 | public static $methods = array(); |
||
33 | |||
34 | /** |
||
35 | * The string to validate |
||
36 | * |
||
37 | * @type string |
||
38 | */ |
||
39 | protected $str; |
||
40 | |||
41 | /** |
||
42 | * The custom exception message to throw on validation failure |
||
43 | * |
||
44 | * @type string |
||
45 | */ |
||
46 | protected $err; |
||
47 | |||
48 | /** |
||
49 | * Flag for whether the default validation methods have been added or not |
||
50 | * |
||
51 | * @type boolean |
||
52 | */ |
||
53 | protected static $defaultAdded = false; |
||
54 | |||
55 | |||
56 | /** |
||
57 | * Methods |
||
58 | */ |
||
59 | |||
60 | /** |
||
61 | * Sets up the validator chain with the string and optional error message |
||
62 | * |
||
63 | * @param string $str The string to validate |
||
64 | * @param string $err The optional custom exception message to throw on validation failure |
||
65 | */ |
||
66 | public function __construct($str, $err = null) |
||
75 | |||
76 | /** |
||
77 | * Adds default validators on first use |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | public static function addDefault() |
||
126 | |||
127 | /** |
||
128 | * Add a custom validator to our list of validation methods |
||
129 | * |
||
130 | * @param string $method The name of the validator method |
||
131 | * @param callable $callback The callback to perform on validation |
||
132 | * @return void |
||
133 | */ |
||
134 | public static function addValidator($method, $callback) |
||
138 | |||
139 | /** |
||
140 | * Magic "__call" method |
||
141 | * |
||
142 | * Allows the ability to arbitrarily call a validator with an optional prefix |
||
143 | * of "is" or "not" by simply calling an instance property like a callback |
||
144 | * |
||
145 | * @param string $method The callable method to execute |
||
146 | * @param array $args The argument array to pass to our callback |
||
147 | * @throws BadMethodCallException If an attempt was made to call a validator modifier that doesn't exist |
||
148 | * @throws ValidationException If the validation check returns false |
||
149 | * @return Validator|boolean |
||
150 | */ |
||
151 | public function __call($method, $args) |
||
201 | } |
||
202 |
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.