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 Tokens implements \Iterator, \Countable { |
||
9 | private $tokens; |
||
10 | private $iterator = 0; |
||
11 | |||
12 | public function __construct(array $tokens) { |
||
15 | |||
16 | public function count() { |
||
19 | |||
20 | // Iterator Functions |
||
21 | public function current() { |
||
24 | |||
25 | public function key() { |
||
28 | |||
29 | public function next() { |
||
32 | |||
33 | public function valid() { |
||
36 | |||
37 | public function rewind() { |
||
40 | |||
41 | private function getKeysOfTokenType($tokenType) { |
||
44 | |||
45 | private function getKeyToSlice($tokenType) { |
||
46 | $keys = $this->getKeysOfTokenType($tokenType); |
||
47 | if (empty($keys)) return false; |
||
48 | $key = $keys[0]; |
||
49 | for ($i = 0; $key < $this->iterator && isset($keys[$i]); $i++) $key = $keys[$i]; |
||
50 | return $key; |
||
51 | } |
||
52 | |||
53 | View Code Duplication | public function from($tokenType, $inclusive = false) { |
|
59 | |||
60 | View Code Duplication | public function to($tokenType, $inclusive = false) { |
|
66 | |||
67 | public function skip($count) { |
||
70 | |||
71 | public function splitOnToken($tokenType) { |
||
83 | |||
84 | public function trim() { |
||
96 | |||
97 | public function removeLine() { |
||
102 | |||
103 | public function read($offset = 0) { |
||
106 | |||
107 | public function type($offset = 0) { |
||
110 | } |
||
111 |
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.