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 |
||
| 12 | class LanguageHandlerMock implements LanguageHandler |
||
| 13 | { |
||
| 14 | protected $languages = array(); |
||
| 15 | |||
| 16 | public function __construct() |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Create a new language. |
||
| 43 | * |
||
| 44 | * @param \eZ\Publish\SPI\Persistence\Content\Language\CreateStruct $struct |
||
| 45 | * |
||
| 46 | * @return \eZ\Publish\SPI\Persistence\Content\Language |
||
| 47 | */ |
||
| 48 | public function create(CreateStruct $struct) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Update language. |
||
| 55 | * |
||
| 56 | * @param \eZ\Publish\SPI\Persistence\Content\Language $struct |
||
| 57 | */ |
||
| 58 | public function update(Language $struct) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get language by id. |
||
| 65 | * |
||
| 66 | * @param mixed $id |
||
| 67 | * |
||
| 68 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If language could not be found by $id |
||
| 69 | * |
||
| 70 | * @return \eZ\Publish\SPI\Persistence\Content\Language |
||
| 71 | */ |
||
| 72 | public function load($id) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get language by Language Code (eg: eng-GB). |
||
| 84 | * |
||
| 85 | * @param string $languageCode |
||
| 86 | * |
||
| 87 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If language could not be found by $languageCode |
||
| 88 | * |
||
| 89 | * @return \eZ\Publish\SPI\Persistence\Content\Language |
||
| 90 | */ |
||
| 91 | public function loadByLanguageCode($languageCode) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get all languages. |
||
| 103 | * |
||
| 104 | * Return list of languages where key of hash is language code. |
||
| 105 | * |
||
| 106 | * @return \eZ\Publish\SPI\Persistence\Content\Language[] |
||
| 107 | */ |
||
| 108 | public function loadAll() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Delete a language. |
||
| 115 | * |
||
| 116 | * @todo Might throw an exception if the language is still associated with some content / types / (...) ? |
||
| 117 | * |
||
| 118 | * @param mixed $id |
||
| 119 | */ |
||
| 120 | public function delete($id) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritdoc} |
||
| 127 | */ |
||
| 128 | View Code Duplication | public function loadList(array $ids): iterable |
|
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | View Code Duplication | public function loadListByLanguageCodes(array $languageCodes): iterable |
|
| 154 | } |
||
| 155 |
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.