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 |
||
20 | trait GeneratorProxyTrait |
||
21 | { |
||
22 | /** |
||
23 | * Overload this for correct type hint on object type. |
||
24 | * |
||
25 | * @var object|null |
||
26 | */ |
||
27 | protected $object; |
||
28 | |||
29 | /** |
||
30 | * @var mixed |
||
31 | */ |
||
32 | protected $id; |
||
33 | |||
34 | /** |
||
35 | * @var Generator|null |
||
36 | */ |
||
37 | private $generator; |
||
38 | |||
39 | /** |
||
40 | * GeneratorProxyTrait constructor. |
||
41 | * |
||
42 | * @param Generator $generator |
||
43 | * @param mixed $id Object id to use for loading the object on demand. |
||
44 | */ |
||
45 | public function __construct(Generator $generator, $id) |
||
50 | |||
51 | public function __call($name, $arguments) |
||
59 | |||
60 | public function __invoke(...$args) |
||
68 | |||
69 | View Code Duplication | public function __get($name) |
|
81 | |||
82 | View Code Duplication | public function __isset($name) |
|
94 | |||
95 | public function __unset($name) |
||
103 | |||
104 | public function __set($name, $value) |
||
112 | |||
113 | public function __toString() |
||
121 | |||
122 | public function __sleep() |
||
130 | |||
131 | public function __debugInfo() |
||
141 | |||
142 | /** |
||
143 | * Loads the generator to object value and unset's generator. |
||
144 | * |
||
145 | * Can only be called once, so check presence of $this->object before using it. |
||
146 | * |
||
147 | * This assumes generator is made to support bulk loading of several objects, and thus takes object id as input |
||
148 | * in order to return right object at a time, and thus performs two yields per item. |
||
149 | * -> See PR that came with this trait for example generators. |
||
150 | */ |
||
151 | protected function loadObject() |
||
157 | } |
||
158 |