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 |
||
21 | abstract class Handler |
||
22 | { |
||
23 | /** |
||
24 | * DB handler to fetch additional field information. |
||
25 | * |
||
26 | * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler |
||
27 | * @deprecated Start to use DBAL $connection instead. |
||
28 | */ |
||
29 | protected $dbHandler; |
||
30 | |||
31 | /** |
||
32 | * Map of criterion operators to the respective function names |
||
33 | * in the DoctrineDatabase DBAL. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $comparatorMap = array( |
||
38 | CriterionOperator::EQ => 'eq', |
||
39 | CriterionOperator::GT => 'gt', |
||
40 | CriterionOperator::GTE => 'gte', |
||
41 | CriterionOperator::LT => 'lt', |
||
42 | CriterionOperator::LTE => 'lte', |
||
43 | ); |
||
44 | |||
45 | /** |
||
46 | * Transformation processor. |
||
47 | * |
||
48 | * @var \eZ\Publish\Core\Persistence\TransformationProcessor |
||
49 | */ |
||
50 | protected $transformationProcessor; |
||
51 | |||
52 | /** |
||
53 | * Creates a new criterion handler. |
||
54 | * |
||
55 | * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $dbHandler |
||
56 | * @param \eZ\Publish\Core\Persistence\TransformationProcessor $transformationProcessor |
||
57 | */ |
||
58 | public function __construct(DatabaseHandler $dbHandler, TransformationProcessor $transformationProcessor) |
||
63 | |||
64 | /** |
||
65 | * Generates query expression for operator and value of a Field Criterion. |
||
66 | * |
||
67 | * @throws \RuntimeException If operator is not handled. |
||
68 | * |
||
69 | * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $query |
||
70 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion |
||
71 | * @param string $column |
||
72 | * |
||
73 | * @return \eZ\Publish\Core\Persistence\Database\Expression |
||
74 | */ |
||
75 | public function handle(SelectQuery $query, Criterion $criterion, $column) |
||
141 | |||
142 | /** |
||
143 | * Returns the given $string prepared for use in SQL LIKE clause. |
||
144 | * |
||
145 | * LIKE clause wildcards '%' and '_' contained in the given $string will be escaped. |
||
146 | * |
||
147 | * @param $string |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | protected function prepareLikeString($string) |
||
155 | |||
156 | /** |
||
157 | * Downcases a given string using string transformation processor. |
||
158 | * |
||
159 | * @param string $string |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | protected function lowerCase($string) |
||
167 | } |
||
168 |
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.