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 |
||
| 38 | class Factory |
||
| 39 | { |
||
| 40 | private $_config = null; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Yes, even this Class has dependecies, but once you have a |
||
| 44 | * Configuration, you can create this Factory by its own. |
||
| 45 | * Just take a look at the Specs |
||
| 46 | * |
||
| 47 | * @param Puice\Config $config Configuration Object holding all |
||
| 48 | * Application Dependencies, which will be |
||
| 49 | * passed to all objects created by this |
||
| 50 | * class. If you only want to use this |
||
| 51 | * Class, you have to implement your own |
||
| 52 | * Config Class and pass it here. |
||
| 53 | */ |
||
| 54 | public function __construct(Config $config) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * This is the method doing the magic. You can create every Instance |
||
| 61 | * with it, as long as the Dependencies were allready defined. |
||
| 62 | * |
||
| 63 | * @param string $classType Type of the Dependency as $Namespace.$Classname |
||
| 64 | * @param string $name Name of the Dpenedency |
||
|
|
|||
| 65 | * |
||
| 66 | * @throws \Exception if some required dependencies could not be found. |
||
| 67 | */ |
||
| 68 | public function create($classType, $className = 'default') |
||
| 97 | |||
| 98 | /** |
||
| 99 | * use multiple fetching Strategies for Dependencies |
||
| 100 | */ |
||
| 101 | private function getDependency($type, $name, $isOptional = false) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Just try to get an Instance from the known type |
||
| 138 | */ |
||
| 139 | private function getDependencyFromType($type) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Prepend Default to the class and try to find the class |
||
| 150 | */ |
||
| 151 | View Code Duplication | private function getDefaultDependencyFromInterface($type) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * remove Interface suffix or prefix (or middlefix) and try to find the |
||
| 166 | * class |
||
| 167 | */ |
||
| 168 | View Code Duplication | private function getDependencyFromInterface($type) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * scan the config object for dependencies |
||
| 183 | */ |
||
| 184 | private function getDependencyFromConfig($type, $name) |
||
| 200 | } |
||
| 201 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.