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 |
||
| 17 | class PhpcrOdmAgent implements AgentInterface |
||
| 18 | { |
||
| 19 | private $documentManager; |
||
| 20 | |||
| 21 | public function __construct( |
||
| 26 | |||
| 27 | /** |
||
| 28 | * {@inheritdoc} |
||
| 29 | */ |
||
| 30 | public function find($identifier, string $class = null) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritdoc} |
||
| 43 | */ |
||
| 44 | public function findMany(array $identifiers, string $class = null) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * {@inheritdoc} |
||
| 51 | */ |
||
| 52 | public function getCapabilities(): Capabilities |
||
| 53 | { |
||
| 54 | return Capabilities::create([ |
||
| 55 | 'can_set_parent' => true, |
||
| 56 | 'supported_comparators' => [ |
||
| 57 | Comparison::EQUALS, |
||
| 58 | Comparison::NOT_EQUALS, |
||
| 59 | Comparison::LESS_THAN, |
||
| 60 | Comparison::LESS_THAN_EQUAL, |
||
| 61 | Comparison::GREATER_THAN, |
||
| 62 | Comparison::GREATER_THAN_EQUAL, |
||
| 63 | Comparison::IN, |
||
| 64 | Comparison::NOT_IN, |
||
| 65 | Comparison::CONTAINS, |
||
| 66 | Comparison::NOT_CONTAINS, |
||
| 67 | Comparison::NULL, |
||
| 68 | Comparison::NOT_NULL, |
||
| 69 | ], |
||
| 70 | ]); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritdoc} |
||
| 75 | */ |
||
| 76 | public function persist($object) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | */ |
||
| 84 | public function remove($object) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | */ |
||
| 92 | public function flush() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritdoc} |
||
| 99 | */ |
||
| 100 | public function getCanonicalClassFqn(string $classFqn): string |
||
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritdoc} |
||
| 107 | */ |
||
| 108 | View Code Duplication | public function getIdentifier($object) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritdoc} |
||
| 129 | */ |
||
| 130 | View Code Duplication | public function setParent($object, $parent) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | */ |
||
| 150 | View Code Duplication | public function supports(string $class): bool |
|
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | public function query(Query $query): \Traversable |
||
| 198 | |||
| 199 | /** |
||
| 200 | * {@inheritdoc} |
||
| 201 | */ |
||
| 202 | public function queryCount(Query $query): int |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Return the document mangaer instance (for use in events). |
||
| 209 | */ |
||
| 210 | public function getDocumentManager(): DocumentManagerInterface |
||
| 214 | } |
||
| 215 |
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.