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 |
||
| 24 | trait TranslatableRepositoryTrait |
||
| 25 | { |
||
| 26 | use RepositoryTrait { |
||
| 27 | __construct as private constructFromRepositoryTrait; |
||
| 28 | createQueryBuilderForCollection as private createQueryBuilderForCollectionFromRepositoryTrait; |
||
| 29 | getProperty as private getPropertyFromRepositoryTrait; |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var LocaleContextInterface |
||
| 34 | */ |
||
| 35 | private $localeContext; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string[] |
||
| 39 | */ |
||
| 40 | private $cache; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param DocumentManager $dm |
||
| 44 | * @param UnitOfWork $uow |
||
| 45 | * @param ClassMetadata $class |
||
| 46 | * @param ResourceInterface $resource |
||
| 47 | * @param LocaleContextInterface $localeContext |
||
| 48 | */ |
||
| 49 | 3 | public function __construct( |
|
| 50 | DocumentManager $dm, |
||
| 51 | UnitOfWork $uow, |
||
| 52 | ClassMetadata $class, |
||
| 53 | ResourceInterface $resource, |
||
| 54 | LocaleContextInterface $localeContext |
||
| 55 | ) { |
||
| 56 | 3 | $this->constructFromRepositoryTrait($dm, $uow, $class, $resource); |
|
|
|
|||
| 57 | |||
| 58 | 3 | $this->localeContext = $localeContext; |
|
| 59 | 3 | } |
|
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | */ |
||
| 64 | 1 | public function createQueryBuilderForCollection() |
|
| 65 | { |
||
| 66 | 1 | return $this->createQueryBuilderForCollectionFromRepositoryTrait() |
|
| 67 | 1 | ->field($this->getProperty('locale')) |
|
| 68 | 1 | ->in($this->getLocales()); |
|
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritdoc} |
||
| 73 | */ |
||
| 74 | 1 | View Code Duplication | public function getProperty($property, $root = null) |
| 75 | { |
||
| 76 | 1 | if ($this->cache === null) { |
|
| 77 | 1 | $translatableFields = $this->getClassMetadata()->getFieldNames(); |
|
| 78 | 1 | $translationFields = $this->getDocumentManager() |
|
| 79 | 1 | ->getClassMetadata($this->getClassMetadata()->getAssociationTargetClass($this->getTranslationAlias())) |
|
| 80 | 1 | ->getFieldNames(); |
|
| 81 | |||
| 82 | 1 | $this->cache = array_diff($translationFields, $translatableFields); |
|
| 83 | 1 | } |
|
| 84 | |||
| 85 | 1 | if (in_array($property, $this->cache, true)) { |
|
| 86 | 1 | $root = $this->getTranslationAlias(); |
|
| 87 | 1 | } |
|
| 88 | |||
| 89 | 1 | return $this->getPropertyFromRepositoryTrait($property, $root); |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | 1 | private function getTranslationAlias() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @return string[] |
||
| 102 | */ |
||
| 103 | 1 | View Code Duplication | private function getLocales() |
| 113 | } |
||
| 114 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.