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 |
||
26 | abstract class StatementRepository |
||
27 | { |
||
28 | /** |
||
29 | * Finds a {@link Statement} by id. |
||
30 | * |
||
31 | * @param string $statementId The statement id to filter by |
||
32 | * @param Actor|null $authority (Optional) actor that must be the authority |
||
33 | * of the returned statement |
||
34 | * |
||
35 | * @return Statement The statement |
||
36 | * |
||
37 | * @throws NotFoundException if no Statement with the given UUID does exist |
||
38 | */ |
||
39 | View Code Duplication | final public function findStatementById($statementId, Actor $authority = null) |
|
61 | |||
62 | /** |
||
63 | * Finds a voided {@link Statement} by id. |
||
64 | * |
||
65 | * @param string $voidedStatementId The voided statement id to filter |
||
66 | * by |
||
67 | * @param Actor|null $authority (Optional) actor that must be the |
||
68 | * authority of the returned statement |
||
69 | * |
||
70 | * @return Statement The statement |
||
71 | * |
||
72 | * @throws NotFoundException if no voided Statement with the given UUID |
||
73 | * does exist |
||
74 | */ |
||
75 | View Code Duplication | final public function findVoidedStatementById($voidedStatementId, Actor $authority = null) |
|
97 | |||
98 | /** |
||
99 | * Finds a collection of {@link Statement Statements} filtered by the given |
||
100 | * criteria. |
||
101 | * |
||
102 | * @param StatementsFilter $criteria The criteria to filter by |
||
103 | * @param Actor|null $authority (Optional) actor that must be the |
||
104 | * authority of the returned statements |
||
105 | * |
||
106 | * @return Statement[] The statements |
||
107 | */ |
||
108 | final public function findStatementsBy(StatementsFilter $criteria, Actor $authority = null) |
||
125 | |||
126 | /** |
||
127 | * Writes a {@link Statement} to the underlying data storage. |
||
128 | * |
||
129 | * @param Statement $statement The statement to store |
||
130 | * @param bool $flush Whether or not to flush the managed objects |
||
131 | * immediately (i.e. write them to the data |
||
132 | * storage) |
||
133 | * |
||
134 | * @return string The UUID of the created Statement |
||
135 | */ |
||
136 | final public function storeStatement(Statement $statement, $flush = true) |
||
151 | |||
152 | /** |
||
153 | * Loads a certain {@link MappedStatement} from the data storage. |
||
154 | * |
||
155 | * @param array $criteria Criteria to filter a statement by |
||
156 | * |
||
157 | * @return MappedStatement The mapped statement |
||
158 | */ |
||
159 | abstract protected function findMappedStatement(array $criteria); |
||
160 | |||
161 | /** |
||
162 | * Loads {@link MappedStatement mapped statements} from the data storage. |
||
163 | * |
||
164 | * @param array $criteria Criteria to filter statements by |
||
165 | * |
||
166 | * @return MappedStatement[] The mapped statements |
||
167 | */ |
||
168 | abstract protected function findMappedStatements(array $criteria); |
||
169 | |||
170 | /** |
||
171 | * Writes a {@link MappedStatement mapped statement} to the underlying |
||
172 | * data storage. |
||
173 | * |
||
174 | * @param MappedStatement $mappedStatement The statement to store |
||
175 | * @param bool $flush Whether or not to flush the managed |
||
176 | * objects immediately (i.e. write |
||
177 | * them to the data storage) |
||
178 | */ |
||
179 | abstract protected function storeMappedStatement(MappedStatement $mappedStatement, $flush); |
||
180 | } |
||
181 |
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.