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 |
||
| 13 | View Code Duplication | class CriteriaConverter |
|
|
|
|||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Criterion handlers. |
||
| 17 | * |
||
| 18 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Query\CriterionHandler[] |
||
| 19 | */ |
||
| 20 | protected $handlers; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Construct from an optional array of Criterion handlers. |
||
| 24 | * |
||
| 25 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Query\CriterionHandler[] $handlers |
||
| 26 | */ |
||
| 27 | public function __construct(iterable $handlers = []) |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Adds handler. |
||
| 34 | * |
||
| 35 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Query\CriterionHandler $handler |
||
| 36 | */ |
||
| 37 | public function addHandler(CriterionHandler $handler) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Generic converter of criteria into query fragments. |
||
| 44 | * |
||
| 45 | * @throws \eZ\Publish\API\Repository\Exceptions\NotImplementedException if Criterion is not applicable to its target |
||
| 46 | * |
||
| 47 | * @param \Doctrine\DBAL\Query\QueryBuilder $query |
||
| 48 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion |
||
| 49 | * |
||
| 50 | * @return \Doctrine\DBAL\Query\Expression\CompositeExpression|string Expression to be used in where clause of query |
||
| 51 | */ |
||
| 52 | public function convertCriteria(QueryBuilder $query, Criterion $criterion) |
||
| 64 | } |
||
| 65 |
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.