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 |
||
10 | class PEMBundle implements \Countable, \IteratorAggregate |
||
11 | { |
||
12 | /** |
||
13 | * Array of PEM objects. |
||
14 | * |
||
15 | * @var PEM[] $_pems |
||
16 | */ |
||
17 | protected $_pems; |
||
18 | |||
19 | /** |
||
20 | * Constructor. |
||
21 | * |
||
22 | * @param PEM ...$pems |
||
23 | */ |
||
24 | 2 | public function __construct(PEM ...$pems) |
|
28 | |||
29 | /** |
||
30 | * Initialize from a string. |
||
31 | * |
||
32 | * @param string $str |
||
33 | * @throws \UnexpectedValueException |
||
34 | * @return self |
||
35 | */ |
||
36 | 3 | public static function fromString($str) |
|
53 | |||
54 | /** |
||
55 | * Initialize from a file. |
||
56 | * |
||
57 | * @param string $filename |
||
58 | * @throws \RuntimeException If file reading fails |
||
59 | * @return self |
||
60 | */ |
||
61 | 2 | View Code Duplication | public static function fromFile($filename) |
69 | |||
70 | /** |
||
71 | * Get self with PEM objects appended. |
||
72 | * |
||
73 | * @param PEM ...$pems |
||
74 | * @return self |
||
75 | */ |
||
76 | 1 | public function withPEMs(PEM ...$pems) |
|
82 | |||
83 | /** |
||
84 | * Get all PEMs in a bundle. |
||
85 | * |
||
86 | * @return PEM[] |
||
87 | */ |
||
88 | 1 | public function all() |
|
92 | |||
93 | /** |
||
94 | * Get the first PEM in a bundle. |
||
95 | * |
||
96 | * @throws \LogicException If bundle contains no PEM objects |
||
97 | * @return PEM |
||
98 | */ |
||
99 | 2 | public function first() |
|
106 | |||
107 | /** |
||
108 | * |
||
109 | * @see \Countable::count() |
||
110 | * @return int |
||
111 | */ |
||
112 | 2 | public function count() |
|
116 | |||
117 | /** |
||
118 | * Get iterator for PEMs. |
||
119 | * |
||
120 | * @see \IteratorAggregate::getIterator() |
||
121 | * @return \ArrayIterator |
||
122 | */ |
||
123 | 1 | public function getIterator() |
|
127 | |||
128 | /** |
||
129 | * Encode bundle to a string of contiguous PEM blocks. |
||
130 | * |
||
131 | * @return string |
||
132 | */ |
||
133 | 2 | public function string() |
|
141 | |||
142 | /** |
||
143 | * |
||
144 | * @return string |
||
145 | */ |
||
146 | 1 | public function __toString() |
|
150 | } |
||
151 |
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.