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 namespace Limoncello\Validation\Execution; |
||
| 30 | final class BlockSerializer implements BlockSerializerInterface |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * Index of the first block. |
||
| 34 | */ |
||
| 35 | const FIRST_BLOCK_INDEX = 0; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | private $currentBlockIndex = 0; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private $serializedBlocks = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var int[] |
||
| 49 | */ |
||
| 50 | private $blocksWithStart = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var int[] |
||
| 54 | */ |
||
| 55 | private $blocksWithEnd = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @inheritdoc |
||
| 59 | */ |
||
| 60 | public function serialize(ExecutionBlockInterface $block): BlockSerializerInterface |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @inheritdoc |
||
| 69 | * |
||
| 70 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 71 | */ |
||
| 72 | public function addBlock(ExecutionBlockInterface $block): int |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | public function getSerializedBlocks(): array |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @inheritdoc |
||
| 100 | */ |
||
| 101 | public function getBlocksWithStart(): array |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @inheritdoc |
||
| 108 | */ |
||
| 109 | public function getBlocksWithEnd(): array |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @inheritdoc |
||
| 116 | */ |
||
| 117 | public function clearBlocks(): BlockSerializerInterface |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @inheritdoc |
||
| 128 | */ |
||
| 129 | public function clearBlocksWithStart(): BlockSerializerInterface |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @inheritdoc |
||
| 138 | */ |
||
| 139 | public function clearBlocksWithEnd(): BlockSerializerInterface |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @inheritdoc |
||
| 148 | */ |
||
| 149 | public function get(): array |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @inheritdoc |
||
| 160 | */ |
||
| 161 | public static function unserializeBlocks(array $serialized): array |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @inheritdoc |
||
| 170 | */ |
||
| 171 | public static function unserializeBlocksWithStart(array $serialized): array |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @inheritdoc |
||
| 180 | */ |
||
| 181 | public static function unserializeBlocksWithEnd(array $serialized): array |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param int $key |
||
| 190 | * @param array $properties |
||
| 191 | * |
||
| 192 | * @return mixed |
||
| 193 | */ |
||
| 194 | private static function readProperty(int $key, array $properties) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @return BlockSerializerInterface |
||
| 203 | */ |
||
| 204 | private function reset(): BlockSerializerInterface |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param ProcedureBlockInterface $procedure |
||
| 211 | * |
||
| 212 | * @return int |
||
| 213 | */ |
||
| 214 | private function serializeProcedure(ProcedureBlockInterface $procedure): int |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param IfExpressionInterface $ifExpression |
||
| 243 | * |
||
| 244 | * @return int |
||
| 245 | */ |
||
| 246 | private function serializeIfExpression(IfExpressionInterface $ifExpression): int |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param AndExpressionInterface $andExpression |
||
| 276 | * |
||
| 277 | * @return int |
||
| 278 | */ |
||
| 279 | View Code Duplication | private function serializeAndExpression(AndExpressionInterface $andExpression): int |
|
| 296 | |||
| 297 | /** |
||
| 298 | * @param OrExpressionInterface $orExpression |
||
| 299 | * |
||
| 300 | * @return int |
||
| 301 | */ |
||
| 302 | View Code Duplication | private function serializeOrExpression(OrExpressionInterface $orExpression): int |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @return int |
||
| 322 | */ |
||
| 323 | private function allocateIndex(): int |
||
| 329 | } |
||
| 330 |
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.