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 Class_ extends ClassLike |
||
9 | { |
||
10 | const MODIFIER_PUBLIC = 1; |
||
11 | const MODIFIER_PROTECTED = 2; |
||
12 | const MODIFIER_PRIVATE = 4; |
||
13 | const MODIFIER_STATIC = 8; |
||
14 | const MODIFIER_ABSTRACT = 16; |
||
15 | const MODIFIER_FINAL = 32; |
||
16 | |||
17 | const VISIBILITY_MODIFER_MASK = 7; // 1 | 2 | 4 |
||
18 | |||
19 | /** @var int Type */ |
||
20 | public $type; |
||
21 | /** @var null|Node\Name Name of extended class */ |
||
22 | public $extends; |
||
23 | /** @var Node\Name[] Names of implemented interfaces */ |
||
24 | public $implements; |
||
25 | |||
26 | protected static $specialNames = array( |
||
27 | 'self' => true, |
||
28 | 'parent' => true, |
||
29 | 'static' => true, |
||
30 | ); |
||
31 | |||
32 | /** |
||
33 | * Constructs a class node. |
||
34 | * |
||
35 | * @param string|null $name Name |
||
36 | * @param array $subNodes Array of the following optional subnodes: |
||
37 | * 'type' => 0 : Type |
||
38 | * 'extends' => null : Name of extended class |
||
39 | * 'implements' => array(): Names of implemented interfaces |
||
40 | * 'stmts' => array(): Statements |
||
41 | * @param array $attributes Additional attributes |
||
42 | */ |
||
43 | public function __construct($name, array $subNodes = array(), array $attributes = array()) { |
||
44 | parent::__construct($attributes); |
||
45 | $this->type = isset($subNodes['type']) ? $subNodes['type'] : 0; |
||
46 | $this->name = $name; |
||
47 | $this->extends = isset($subNodes['extends']) ? $subNodes['extends'] : null; |
||
48 | $this->implements = isset($subNodes['implements']) ? $subNodes['implements'] : array(); |
||
49 | $this->stmts = isset($subNodes['stmts']) ? $subNodes['stmts'] : array(); |
||
50 | |||
51 | View Code Duplication | if (null !== $this->name && isset(self::$specialNames[strtolower($this->name)])) { |
|
|
|||
52 | throw new Error(sprintf('Cannot use \'%s\' as class name as it is reserved', $this->name)); |
||
53 | } |
||
54 | |||
55 | View Code Duplication | if (isset(self::$specialNames[strtolower($this->extends)])) { |
|
56 | throw new Error( |
||
57 | sprintf('Cannot use \'%s\' as class name as it is reserved', $this->extends), |
||
58 | $this->extends->getAttributes() |
||
59 | ); |
||
60 | } |
||
61 | |||
62 | View Code Duplication | foreach ($this->implements as $interface) { |
|
63 | if (isset(self::$specialNames[strtolower($interface)])) { |
||
64 | throw new Error( |
||
65 | sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface), |
||
66 | $interface->getAttributes() |
||
67 | ); |
||
68 | } |
||
69 | } |
||
70 | } |
||
71 | |||
72 | public function getSubNodeNames() { |
||
75 | |||
76 | public function isAbstract() { |
||
79 | |||
80 | public function isFinal() { |
||
83 | |||
84 | public function isAnonymous() { |
||
87 | |||
88 | /** |
||
89 | * @internal |
||
90 | */ |
||
91 | public static function verifyModifier($a, $b) { |
||
112 | } |
||
113 |
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.