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 Use_ extends BuilderAbstract { |
||
| 13 | protected $name; |
||
| 14 | protected $type; |
||
| 15 | protected $alias = null; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Creates a name use (alias) builder. |
||
| 19 | * |
||
| 20 | * @param Node\Name|string $name Name of the entity (namespace, class, function, constant) to alias |
||
| 21 | * @param int $type One of the Stmt\Use_::TYPE_* constants |
||
| 22 | */ |
||
| 23 | public function __construct($name, $type) { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Sets alias for used name. |
||
| 30 | * |
||
| 31 | * @param string $alias Alias to use (last component of full name by default) |
||
| 32 | * |
||
| 33 | * @return $this The builder instance (for fluid interface) |
||
| 34 | */ |
||
| 35 | protected function as_($alias) { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Returns the built node. |
||
| 49 | * |
||
| 50 | * @return Node The built node |
||
| 51 | */ |
||
| 52 | public function getNode() { |
||
| 58 | } |
||
| 59 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.