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 |
||
| 11 | class TreePath implements Buildable |
||
| 12 | { |
||
| 13 | const MACRO_METHOD = 'treePath'; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var ExtensibleClassMetadata |
||
| 17 | */ |
||
| 18 | protected $classMetadata; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $fieldName; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $separator; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var mixed |
||
| 32 | */ |
||
| 33 | protected $appendId = null; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var bool |
||
| 37 | */ |
||
| 38 | protected $startsWithSeparator = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var bool |
||
| 42 | */ |
||
| 43 | protected $endsWithSeparator = true; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param ExtensibleClassMetadata $classMetadata |
||
| 47 | * @param string $fieldName |
||
| 48 | * @param string $separator |
||
| 49 | */ |
||
| 50 | 4 | public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName, $separator = '|') |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Enable TreePath |
||
| 59 | */ |
||
| 60 | public static function enable() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Execute the build process |
||
| 69 | */ |
||
| 70 | 3 | View Code Duplication | public function build() |
| 84 | |||
| 85 | /** |
||
| 86 | * Return the name of the actual extension. |
||
| 87 | * |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | 2 | public function getExtensionName() |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $separator |
||
| 97 | * @return TreePath |
||
| 98 | */ |
||
| 99 | 2 | public function separator($separator) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * @param mixed $appendId |
||
| 108 | * @return TreePath |
||
| 109 | */ |
||
| 110 | 1 | public function appendId($appendId) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @param bool $startsWithSeparator |
||
| 119 | * @return TreePath |
||
| 120 | */ |
||
| 121 | 1 | public function startsWithSeparator($startsWithSeparator = true) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @param bool $endsWithSeparator |
||
| 130 | * @return TreePath |
||
| 131 | */ |
||
| 132 | 1 | public function endsWithSeparator($endsWithSeparator = true) |
|
| 138 | } |
||
| 139 |
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.