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 |
||
| 11 | class Node extends \stdClass |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * The processed data |
||
| 15 | * @var mixed |
||
| 16 | */ |
||
| 17 | protected $i_result; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Class constructor |
||
| 21 | * |
||
| 22 | * @param \stdClass $data |
||
| 23 | */ |
||
| 24 | 7 | public function __construct(\stdClass $data) |
|
| 25 | { |
||
| 26 | 7 | $this->i_result = $data; |
|
| 27 | |||
| 28 | 7 | foreach ($data as $key => $value) { |
|
|
|
|||
| 29 | 7 | if ($key === 'i_result') { |
|
| 30 | continue; |
||
| 31 | } |
||
| 32 | |||
| 33 | 7 | $this->$key = $value; |
|
| 34 | } |
||
| 35 | 7 | } |
|
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * Replace nodes with their results |
||
| 40 | * |
||
| 41 | * @param array|object $target |
||
| 42 | */ |
||
| 43 | 2 | View Code Duplication | protected function applyNodeResults(&$target) |
| 44 | { |
||
| 45 | 2 | if ($target instanceof self) { |
|
| 46 | $target = $target->getResult(); |
||
| 47 | $this->applyNodeResults($target); |
||
| 48 | 2 | } elseif (is_array($target) || is_object($target)) { |
|
| 49 | 2 | foreach ($target as &$value) { |
|
| 50 | 2 | $this->applyNodeResults($value); |
|
| 51 | } |
||
| 52 | } |
||
| 53 | 2 | } |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Get the processed result |
||
| 57 | * |
||
| 58 | * @return mixed |
||
| 59 | */ |
||
| 60 | 2 | public function getResult() |
|
| 61 | { |
||
| 62 | 2 | if ($this->i_result instanceof PromiseInterface) { |
|
| 63 | $this->i_result->wait(); |
||
| 64 | |||
| 65 | if ($this->i_result instanceof PromiseInterface) { |
||
| 66 | throw new \LogicException("Promise result not replaced with data"); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | 2 | $result = $this->i_result; |
|
| 71 | |||
| 72 | 2 | $this->applyNodeResults($result); |
|
| 73 | 2 | return $result; |
|
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Set the result after processing |
||
| 78 | * |
||
| 79 | * @param mixed $result |
||
| 80 | */ |
||
| 81 | 2 | public function setResult($result) |
|
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * Test if the node has an instruction for a processor |
||
| 89 | * |
||
| 90 | * @param Processor $processor |
||
| 91 | * @return boolean |
||
| 92 | */ |
||
| 93 | 2 | public function hasInstruction(Processor $processor) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Get an instruction for a processor |
||
| 101 | * |
||
| 102 | * @param Processor $processor |
||
| 103 | * @return mixed |
||
| 104 | */ |
||
| 105 | 7 | public function getInstruction(Processor $processor) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Resolve nodes within the value |
||
| 118 | * |
||
| 119 | * @param mixed $value |
||
| 120 | * @return mixed |
||
| 121 | */ |
||
| 122 | 7 | View Code Duplication | protected function resolve($value) |
| 136 | |||
| 137 | /** |
||
| 138 | * Apply processing to this node |
||
| 139 | * |
||
| 140 | * @param Processor $processor |
||
| 141 | */ |
||
| 142 | 2 | public function apply(Processor $processor) |
|
| 156 | } |
||
| 157 |