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 |
||
18 | trait AbstractTrait |
||
19 | { |
||
20 | /** |
||
21 | * @var \eZ\Publish\Core\Persistence\Cache\PersistenceLogger |
||
22 | */ |
||
23 | protected $logger; |
||
24 | |||
25 | /** |
||
26 | * Helper for getting multiple cache items in one call and do the id extraction for you. |
||
27 | * |
||
28 | * Cache items must be stored with a key in the following format "${keyPrefix}${id}", like "ez-content-info-${id}", |
||
29 | * in order for this method to be able to prefix key on id's and also extract key prefix afterwards. |
||
30 | * |
||
31 | * @param array $ids |
||
32 | * @param string $keyPrefix E.g "ez-content-" |
||
33 | * @param callable $backendLoader Function for loading missing objects, gets array with missing id's as argument, |
||
34 | * expects return value to be array with id as key. Missing items should be missing. |
||
35 | * @param callable $cacheTagger Function for tagging loaded object, gets object as argument, return array of tags. |
||
36 | * @param string $keySuffix Optional, e.g "-by-identifier" |
||
37 | * |
||
38 | * @return array |
||
39 | */ |
||
40 | final protected function getMultipleCacheValues( |
||
100 | } |
||
101 |
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.