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 |
||
| 21 | abstract class AbstractGenericVisitor extends AbstractVisitor |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var \SplStack |
||
| 25 | */ |
||
| 26 | private $stack; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var mixed |
||
| 30 | */ |
||
| 31 | protected $result; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * {@inheritdoc} |
||
| 35 | */ |
||
| 36 | 444 | View Code Duplication | public function prepare($data, ContextInterface $context) |
|
|
|||
| 37 | { |
||
| 38 | 444 | $this->stack = new \SplStack(); |
|
| 39 | 444 | $this->result = null; |
|
| 40 | |||
| 41 | 444 | return parent::prepare($data, $context); |
|
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | */ |
||
| 47 | 102 | public function visitArray($data, TypeMetadataInterface $type, ContextInterface $context) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | */ |
||
| 63 | 399 | public function visitData($data, TypeMetadataInterface $type, ContextInterface $context) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritdoc} |
||
| 74 | */ |
||
| 75 | 309 | public function startVisitingObject($data, ClassMetadataInterface $class, ContextInterface $context) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | 309 | public function finishVisitingObject($data, ClassMetadataInterface $class, ContextInterface $context) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritdoc} |
||
| 99 | */ |
||
| 100 | 234 | public function getResult() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @param string $class |
||
| 107 | * |
||
| 108 | * @return mixed |
||
| 109 | */ |
||
| 110 | abstract protected function createResult($class); |
||
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | */ |
||
| 115 | 36 | protected function doVisitArray($data, TypeMetadataInterface $type, ContextInterface $context) |
|
| 127 | |||
| 128 | 324 | private function enterScope() |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @return mixed |
||
| 135 | */ |
||
| 136 | 324 | private function leaveScope() |
|
| 147 | } |
||
| 148 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.