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 |
||
| 16 | abstract class AbstractManyInstancesOperator implements NodeInterface |
||
| 17 | { |
||
| 18 | private $operands; |
||
| 19 | |||
| 20 | public function getOperands() |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Sets the operands. |
||
| 27 | * |
||
| 28 | * @Important |
||
| 29 | * //@param array<array<NodeInterface>> $operands |
||
| 30 | * |
||
| 31 | * @param array<NodeInterface> $operands |
||
| 32 | */ |
||
| 33 | public function setOperands($operands) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Returns a Mouf instance descriptor describing this object. |
||
| 43 | * |
||
| 44 | * @param MoufManager $moufManager |
||
| 45 | * |
||
| 46 | * @return MoufInstanceDescriptor |
||
| 47 | */ |
||
| 48 | View Code Duplication | public function toInstanceDescriptor(MoufManager $moufManager) |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Renders the object as a SQL string. |
||
| 58 | * |
||
| 59 | * @param Connection $dbConnection |
||
| 60 | * @param array $parameters |
||
| 61 | * @param number $indent |
||
| 62 | * @param int $conditionsMode |
||
| 63 | * |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Walks the tree of nodes, calling the visitor passed in parameter. |
||
| 81 | * |
||
| 82 | * @param VisitorInterface $visitor |
||
| 83 | */ |
||
| 84 | View Code Duplication | public function walk(VisitorInterface $visitor) |
|
| 85 | { |
||
| 86 | $node = $this; |
||
| 87 | $result = $visitor->enterNode($node); |
||
| 88 | if ($result instanceof NodeInterface) { |
||
| 89 | $node = $result; |
||
| 90 | } |
||
| 91 | if ($result !== NodeTraverser::DONT_TRAVERSE_CHILDREN) { |
||
| 92 | foreach ($this->operands as $key => $operand) { |
||
| 93 | $result2 = $operand->walk($visitor); |
||
| 94 | if ($result2 === NodeTraverser::REMOVE_NODE) { |
||
| 95 | unset($this->operands[$key]); |
||
| 96 | } elseif ($result2 instanceof NodeInterface) { |
||
| 97 | $this->operands[$key] = $result2; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | return $visitor->leaveNode($node); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Returns the symbol for this operator. |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | abstract protected function getOperatorSymbol(); |
||
| 111 | } |
||
| 112 |
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.