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 SmoothPhp\EventSourcing; |
||
11 | abstract class Entity implements \SmoothPhp\Contracts\EventSourcing\Entity |
||
12 | { |
||
13 | /** @var AggregateRoot */ |
||
14 | private $aggregateRoot; |
||
15 | |||
16 | /** @var \SmoothPhp\Contracts\EventSourcing\Entity[] */ |
||
17 | private $children = []; |
||
18 | |||
19 | /** |
||
20 | * @param Event $event |
||
21 | * @throws Exception\AggregateRootAlreadyRegistered |
||
22 | */ |
||
23 | 6 | public function handleRecursively(Event $event) |
|
32 | |||
33 | /** |
||
34 | * @param AggregateRootContract $aggregateRoot |
||
35 | * @throws Exception\AggregateRootAlreadyRegistered |
||
36 | */ |
||
37 | 9 | public function registerAggregateRoot(AggregateRootContract $aggregateRoot) |
|
45 | |||
46 | /** |
||
47 | * @param \SmoothPhp\Contracts\EventSourcing\Entity $entity |
||
48 | */ |
||
49 | 3 | public function addChildEntity(\SmoothPhp\Contracts\EventSourcing\Entity $entity) |
|
53 | |||
54 | /** |
||
55 | * @param Event $event |
||
56 | * @throws Exception\NoAggregateRootRegistered |
||
57 | */ |
||
58 | 3 | protected function apply(Event $event) |
|
65 | |||
66 | /** |
||
67 | * Handles event if capable. |
||
68 | * |
||
69 | * @param $event |
||
70 | */ |
||
71 | 6 | View Code Duplication | protected function handle(Event $event) |
79 | |||
80 | /** |
||
81 | * @param Event $event |
||
82 | * @return string |
||
83 | */ |
||
84 | 6 | private function getApplyMethod(Event $event) |
|
90 | } |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.