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 |
||
| 12 | class Tree implements Buildable |
||
| 13 | { |
||
| 14 | const MACRO_METHOD = 'tree'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * List of tree strategies available |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | private $strategies = [ |
||
| 22 | 'nested', |
||
| 23 | 'closure', |
||
| 24 | 'materializedPath', |
||
| 25 | ]; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var ExtensibleClassMetadata |
||
| 29 | */ |
||
| 30 | protected $classMetadata; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $strategy; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var bool |
||
| 39 | */ |
||
| 40 | protected $activateLocking = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | protected $lockingTimeout = 3; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $closure; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param ExtensibleClassMetadata $classMetadata |
||
| 54 | * @param string $strategy |
||
| 55 | */ |
||
| 56 | 5 | public function __construct(ExtensibleClassMetadata $classMetadata, $strategy = 'nested') |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Enable extension |
||
| 64 | */ |
||
| 65 | public static function enable() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Execute the build process |
||
| 84 | */ |
||
| 85 | 3 | View Code Duplication | public function build() |
| 98 | |||
| 99 | /** |
||
| 100 | * Return the name of the actual extension. |
||
| 101 | * |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | 2 | public function getExtensionName() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @param bool $activateLocking |
||
| 111 | * @return Tree |
||
| 112 | */ |
||
| 113 | 1 | public function activateLocking($activateLocking = true) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $strategy |
||
| 122 | * @return Tree |
||
| 123 | */ |
||
| 124 | 2 | public function strategy($strategy) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @param int $lockingTimeout |
||
| 133 | * @return Tree |
||
| 134 | */ |
||
| 135 | 2 | public function lockingTimeout($lockingTimeout) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @param string $closure |
||
| 148 | * @return Tree |
||
| 149 | */ |
||
| 150 | 1 | public function closure($closure) |
|
| 156 | } |
||
| 157 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.