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 | class ContentLanguageHandler extends AbstractHandler implements ContentLanguageHandlerInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * {@inheritdoc} |
||
| 22 | */ |
||
| 23 | public function create(CreateStruct $struct) |
||
| 29 | |||
| 30 | /** |
||
| 31 | * {@inheritdoc} |
||
| 32 | */ |
||
| 33 | public function update(Language $struct) |
||
| 34 | { |
||
| 35 | $this->logger->logCall(__METHOD__, array('struct' => $struct)); |
||
| 36 | $return = $this->persistenceHandler->contentLanguageHandler()->update($struct); |
||
| 37 | |||
| 38 | $this->cache->invalidateTags(['language-' . $struct->id]); |
||
| 39 | |||
| 40 | return $return; |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * {@inheritdoc} |
||
| 45 | */ |
||
| 46 | View Code Duplication | public function load($id) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc} |
||
| 65 | */ |
||
| 66 | public function loadList(array $ids): iterable |
||
| 67 | { |
||
| 68 | return $this->getMultipleCacheItems( |
||
|
|
|||
| 69 | $ids, |
||
| 70 | 'ez-language-', |
||
| 71 | View Code Duplication | function (array $cacheMissIds) { |
|
| 72 | $this->logger->logCall(__CLASS__ . '::loadList', ['languages' => $cacheMissIds]); |
||
| 73 | |||
| 74 | return $this->persistenceHandler->contentLanguageHandler()->loadList($cacheMissIds); |
||
| 75 | }, |
||
| 76 | function (Language $language) { |
||
| 77 | return ['language-' . $language->id]; |
||
| 78 | } |
||
| 79 | ); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | View Code Duplication | public function loadByLanguageCode($languageCode) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * {@inheritdoc} |
||
| 104 | */ |
||
| 105 | public function loadListByLanguageCodes(array $languageCodes): iterable |
||
| 106 | { |
||
| 107 | return $this->getMultipleCacheItems( |
||
| 108 | $languageCodes, |
||
| 109 | 'ez-language-code-', |
||
| 110 | View Code Duplication | function (array $cacheMissIds) { |
|
| 111 | $this->logger->logCall(__CLASS__ . '::loadListByLanguageCodes', ['languages' => $cacheMissIds]); |
||
| 112 | |||
| 113 | return $this->persistenceHandler->contentLanguageHandler()->loadListByLanguageCodes($cacheMissIds); |
||
| 114 | }, |
||
| 115 | function (Language $language) { |
||
| 116 | return ['language-' . $language->id]; |
||
| 117 | } |
||
| 118 | ); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | public function loadAll() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | public function delete($id) |
||
| 143 | } |
||
| 144 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.