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 |
||
| 8 | class Interface_ extends ClassLike |
||
| 9 | { |
||
| 10 | /** @var Node\Name[] Extended interfaces */ |
||
| 11 | public $extends; |
||
| 12 | |||
| 13 | protected static $specialNames = array( |
||
| 14 | 'self' => true, |
||
| 15 | 'parent' => true, |
||
| 16 | 'static' => true, |
||
| 17 | ); |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Constructs a class node. |
||
| 21 | * |
||
| 22 | * @param string $name Name |
||
| 23 | * @param array $subNodes Array of the following optional subnodes: |
||
| 24 | * 'extends' => array(): Name of extended interfaces |
||
| 25 | * 'stmts' => array(): Statements |
||
| 26 | * @param array $attributes Additional attributes |
||
| 27 | */ |
||
| 28 | public function __construct($name, array $subNodes = array(), array $attributes = array()) { |
||
| 29 | parent::__construct($attributes); |
||
| 30 | $this->name = $name; |
||
| 31 | $this->extends = isset($subNodes['extends']) ? $subNodes['extends'] : array(); |
||
| 32 | $this->stmts = isset($subNodes['stmts']) ? $subNodes['stmts'] : array(); |
||
| 33 | |||
| 34 | View Code Duplication | if (isset(self::$specialNames[strtolower($this->name)])) { |
|
|
|
|||
| 35 | throw new Error(sprintf('Cannot use \'%s\' as class name as it is reserved', $this->name)); |
||
| 36 | } |
||
| 37 | |||
| 38 | View Code Duplication | foreach ($this->extends as $interface) { |
|
| 39 | if (isset(self::$specialNames[strtolower($interface)])) { |
||
| 40 | throw new Error( |
||
| 41 | sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface), |
||
| 42 | $interface->getAttributes() |
||
| 43 | ); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | public function getSubNodeNames() { |
||
| 51 | } |
||
| 52 |
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.