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 |
||
14 | class Op extends \OCA\Documents\Db { |
||
15 | |||
16 | const DB_TABLE = '`*PREFIX*documents_op`'; |
||
17 | |||
18 | protected $tableName = '`*PREFIX*documents_op`'; |
||
19 | |||
20 | protected $insertStatement = 'INSERT INTO `*PREFIX*documents_op` (`es_id`, `optype`, `member`, `opspec`) VALUES (?, ?, ?, ?)'; |
||
21 | |||
22 | public static function addOpsArray($esId, $memberId, $ops){ |
||
23 | $opObj = new Op(); |
||
24 | foreach ($ops as $op) { |
||
25 | if (!$opObj->canInsertOp($esId, $memberId, $op)){ |
||
26 | continue; |
||
27 | } |
||
28 | $opObj->setData(array( |
||
29 | $esId, |
||
30 | $op['optype'], |
||
31 | $memberId, |
||
32 | json_encode($op) |
||
33 | )); |
||
34 | $opObj->insert(); |
||
35 | } |
||
36 | |||
37 | return $opObj->getHeadSeq($esId); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @returns "" when there are no Ops, or the seq of the last Op |
||
42 | */ |
||
43 | public function getHeadSeq($esId){ |
||
53 | |||
54 | public function getOpsAfterJson($esId, $seq){ |
||
69 | |||
70 | public function getOpsAfter($esId, $seq){ |
||
84 | |||
85 | public function addMember($esId, $memberId, $fullName, $userId, $color, $imageUrl){ |
||
99 | |||
100 | View Code Duplication | public function removeCursor($esId, $memberId){ |
|
|
|||
101 | $op = array( |
||
102 | 'optype' => 'RemoveCursor', |
||
103 | 'memberid' => (string) $memberId, |
||
104 | 'reason' => 'server-idle', |
||
105 | 'timestamp' => $this->getMillisecondsAsString() |
||
106 | ); |
||
107 | $this->insertOp($esId, $memberId, $op); |
||
108 | } |
||
109 | |||
110 | public function removeMember($esId, $memberId){ |
||
111 | $op = array( |
||
112 | 'optype' => 'RemoveMember', |
||
113 | 'memberid' => (string) $memberId, |
||
114 | 'timestamp' => $this->getMillisecondsAsString() |
||
115 | ); |
||
116 | $this->insertOp($esId, $memberId, $op); |
||
117 | } |
||
118 | |||
119 | //TODO: Implement https://github.com/kogmbh/WebODF/blob/master/webodf/lib/ops/OpUpdateMember.js#L95 |
||
120 | View Code Duplication | public function changeNick($esId, $memberId, $fullName){ |
|
121 | $op = array( |
||
122 | 'optype' => 'UpdateMember', |
||
123 | 'memberid' => (string) $memberId, |
||
124 | 'timestamp' => $this->getMillisecondsAsString(), |
||
125 | 'setProperties' => array( |
||
126 | 'fullName' => $fullName, |
||
127 | ) |
||
128 | ); |
||
129 | $this->insertOp($esId, $memberId, $op); |
||
130 | } |
||
131 | |||
132 | protected function insertOp($esId, $memberId, $op){ |
||
133 | if ($this->canInsertOp($esId, $memberId, $op)){ |
||
134 | $op = new Op(array( |
||
135 | $esId, |
||
136 | $op['optype'], |
||
137 | $memberId, |
||
138 | json_encode($op) |
||
139 | )); |
||
140 | $op->insert(); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | protected function canInsertOp($esId, $memberId, $op){ |
||
145 | $cursorOps = array('AddCursor', 'RemoveCursor'); |
||
146 | $memberOps = array('AddMember', 'RemoveMember'); |
||
147 | $result = true; |
||
148 | |||
149 | switch ($op['optype']){ |
||
150 | View Code Duplication | case 'AddCursor': |
|
151 | $ops = $this->getFilteredMemberOps($esId, $memberId, $cursorOps); |
||
152 | $result = !count($ops) || $ops[0]['optype'] === 'RemoveCursor'; |
||
153 | break; |
||
154 | View Code Duplication | case 'RemoveCursor': |
|
155 | $ops = $this->getFilteredMemberOps($esId, $memberId, $cursorOps); |
||
156 | $result = count($ops) && $ops[0]['optype'] === 'AddCursor'; |
||
157 | break; |
||
158 | View Code Duplication | case 'AddMember': |
|
159 | $ops = $this->getFilteredMemberOps($esId, $memberId, $memberOps); |
||
160 | $result = !count($ops) || $ops[0]['optype'] === 'RemoveMember'; |
||
161 | break; |
||
162 | View Code Duplication | case 'RemoveMember': |
|
163 | $ops = $this->getFilteredMemberOps($esId, $memberId, $memberOps); |
||
164 | $result = count($ops) && $ops[0]['optype'] === 'AddMember'; |
||
165 | break; |
||
166 | } |
||
167 | return $result; |
||
168 | } |
||
169 | |||
170 | protected function getFilteredMemberOps($esId, $memberId, $targetOps){ |
||
171 | $stmt = $this->buildInQuery('optype', $targetOps); |
||
172 | $result = $this->execute(' |
||
173 | SELECT `optype` FROM ' . $this->tableName . ' |
||
174 | WHERE es_id=? AND member=? AND ' . $stmt . 'ORDER BY `seq` DESC', |
||
175 | array_merge(array($esId, $memberId), $targetOps) |
||
176 | ); |
||
177 | $ops = $result->fetchAll(); |
||
178 | if (!is_array($ops)){ |
||
179 | $ops = array(); |
||
180 | } |
||
181 | return $ops; |
||
182 | } |
||
183 | |||
184 | protected function getMillisecondsAsString(){ |
||
190 | } |
||
191 |
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.