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 |
||
24 | class Stack implements Countable { |
||
25 | /** |
||
26 | * @var $head represents the stack head. |
||
27 | */ |
||
28 | private $head; |
||
29 | /** |
||
30 | * @var $size contains the stack size. Increments each time |
||
31 | * that is inserted a new item. |
||
32 | */ |
||
33 | private $size; |
||
34 | /** |
||
35 | * @var $maxSize if 0 it means that stack is unlimited, else it indicates |
||
36 | * the maximum size of the stack. |
||
37 | */ |
||
38 | private $maxSize; |
||
39 | |||
40 | /** |
||
41 | * Initializes the stack. |
||
42 | * |
||
43 | * @param int $maxSize 0 by default. If greater than 0 is limited. |
||
44 | * @throws \InvalidArgumentException if size is lower than 0. |
||
45 | */ |
||
46 | View Code Duplication | public function __construct($maxSize = 0) { |
|
|
|||
47 | if($maxSize < 0) { |
||
48 | throw new InvalidArgumentException(); |
||
49 | } |
||
50 | $this->head = null; |
||
51 | $this->maxSize = $maxSize; |
||
52 | $this->size = 0; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Returns the stack size. |
||
57 | * |
||
58 | * @return int the length |
||
59 | */ |
||
60 | public function size() : int { |
||
61 | return $this->size; |
||
62 | } |
||
63 | |||
64 | |||
65 | /** |
||
66 | * Checks if the stack is empty. |
||
67 | * |
||
68 | * @return boolean true if is empty, else false. |
||
69 | */ |
||
70 | public function empty() : bool { |
||
71 | return $this->size === 0; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Adds at the end of the stack new node containing |
||
76 | * the data to be stored. |
||
77 | * |
||
78 | * @param mixed $data The data |
||
79 | * @throws DataStructures\Exceptions\FullException if the queue is full. |
||
80 | */ |
||
81 | public function push($data) { |
||
82 | if($this->isFull()) { |
||
83 | throw new FullException(); |
||
84 | } |
||
85 | |||
86 | $newNode = new Node($data); |
||
87 | View Code Duplication | if($this->head === null) { |
|
88 | $this->head = &$newNode; |
||
89 | $newNode->next = null; |
||
90 | } else { |
||
91 | $temp = $this->head; |
||
92 | $this->head = &$newNode; |
||
93 | $newNode->next = &$temp; |
||
94 | } |
||
95 | |||
96 | $this->size++; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Removes and returns the last node in the stack. |
||
101 | * |
||
102 | * @return mixed data in node. |
||
103 | */ |
||
104 | public function pop() { |
||
105 | if($this->head === null) { |
||
106 | return null; |
||
107 | } |
||
108 | |||
109 | $node = $this->head; |
||
110 | $this->head = $this->head->next; |
||
111 | $this->size--; |
||
112 | |||
113 | return $node->data; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Gets the element at the front of the stack without removing it. |
||
118 | * |
||
119 | * @return mixed |
||
120 | */ |
||
121 | public function peek() { |
||
124 | |||
125 | /** |
||
126 | * Returns true if is full the stack and false if there is |
||
127 | * space available. |
||
128 | * |
||
129 | * @return bool |
||
130 | */ |
||
131 | View Code Duplication | public function isFull() { |
|
138 | |||
139 | /** |
||
140 | * Binds to count() method. This is equal to make $this->stack->size(). |
||
141 | * |
||
142 | * @return integer the stack size. 0 if it is empty. |
||
143 | */ |
||
144 | public function count() { |
||
147 | } |
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.