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 |
||
| 27 | class ObjectFactory { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Instantiate an object based on a specification array. |
||
| 31 | * |
||
| 32 | * The specification array must contain a 'class' key with string value |
||
| 33 | * that specifies the class name to instantiate or a 'factory' key with |
||
| 34 | * a callable (is_callable() === true). It can optionally contain |
||
| 35 | * an 'args' key that provides arguments to pass to the |
||
| 36 | * constructor/callable. |
||
| 37 | * |
||
| 38 | * Values in the arguments collection which are Closure instances will be |
||
| 39 | * expanded by invoking them with no arguments before passing the |
||
| 40 | * resulting value on to the constructor/callable. This can be used to |
||
| 41 | * pass IDatabase instances or other live objects to the |
||
| 42 | * constructor/callable. This behavior can be suppressed by adding |
||
| 43 | * closure_expansion => false to the specification. |
||
| 44 | * |
||
| 45 | * The specification may also contain a 'calls' key that describes method |
||
| 46 | * calls to make on the newly created object before returning it. This |
||
| 47 | * pattern is often known as "setter injection". The value of this key is |
||
| 48 | * expected to be an associative array with method names as keys and |
||
| 49 | * argument lists as values. The argument list will be expanded (or not) |
||
| 50 | * in the same way as the 'args' key for the main object. |
||
| 51 | * |
||
| 52 | * @param array $spec Object specification |
||
| 53 | * @return object |
||
| 54 | * @throws InvalidArgumentException when object specification does not |
||
| 55 | * contain 'class' or 'factory' keys |
||
| 56 | * @throws ReflectionException when 'args' are supplied and 'class' |
||
| 57 | * constructor is non-public or non-existent |
||
| 58 | */ |
||
| 59 | public static function getObjectFromSpec( $spec ) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Iterate a list and call any closures it contains. |
||
| 98 | * |
||
| 99 | * @param array $list List of things |
||
| 100 | * @return array List with any Closures replaced with their output |
||
| 101 | */ |
||
| 102 | protected static function expandClosures( $list ) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Construct an instance of the given class using the given arguments. |
||
| 115 | * |
||
| 116 | * PHP's `call_user_func_array()` doesn't work with object construction so |
||
| 117 | * we have to use other measures. Starting with PHP 5.6.0 we could use the |
||
| 118 | * "splat" operator (`...`) to unpack the array into an argument list. |
||
| 119 | * Sadly there is no way to conditionally include a syntax construct like |
||
| 120 | * a new operator in a way that allows older versions of PHP to still |
||
| 121 | * parse the file. Instead, we will try a loop unrolling technique that |
||
| 122 | * works for 0-10 arguments. If we are passed 11 or more arguments we will |
||
| 123 | * take the performance penalty of using |
||
| 124 | * `ReflectionClass::newInstanceArgs()` to construct the desired object. |
||
| 125 | * |
||
| 126 | * @param string $clazz Class name |
||
| 127 | * @param array $args Constructor arguments |
||
| 128 | * @return mixed Constructed instance |
||
| 129 | */ |
||
| 130 | public static function constructClassInstance( $clazz, $args ) { |
||
| 199 | } |
||
| 200 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.