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 | final class QueryBuilder implements QueryBuilderInterface |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var ObjectNode |
||
| 20 | */ |
||
| 21 | private $rootNode; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var AbstractNode |
||
| 25 | */ |
||
| 26 | private $node; |
||
| 27 | |||
| 28 | 19 | public function __construct() |
|
| 33 | |||
| 34 | /** |
||
| 35 | * @param array ...$arguments |
||
| 36 | * @return QueryBuilderInterface |
||
| 37 | * @throws \Exception |
||
| 38 | */ |
||
| 39 | 16 | public function add(...$arguments): QueryBuilderInterface |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @param AbstractNode $node |
||
| 50 | * @param bool $allowDefault |
||
| 51 | * |
||
| 52 | * @return QueryBuilderInterface |
||
| 53 | * |
||
| 54 | * @throws \Exception |
||
| 55 | */ |
||
| 56 | 5 | View Code Duplication | public function addToArrayNode(AbstractNode $node, bool $allowDefault = false): QueryBuilderInterface |
| 67 | |||
| 68 | /** |
||
| 69 | * @param string $key |
||
| 70 | * @param AbstractNode $node |
||
| 71 | * @param bool $allowDefault |
||
| 72 | * |
||
| 73 | * @return QueryBuilderInterface |
||
| 74 | * |
||
| 75 | * @throws \Exception |
||
| 76 | */ |
||
| 77 | 17 | View Code Duplication | public function addToObjectNode(string $key, AbstractNode $node, bool $allowDefault = false): QueryBuilderInterface |
| 88 | |||
| 89 | /** |
||
| 90 | * @param AbstractNode $node |
||
| 91 | */ |
||
| 92 | 17 | private function reassignParent(AbstractNode $node) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @return QueryBuilderInterface |
||
| 101 | * |
||
| 102 | * @throws \Exception |
||
| 103 | */ |
||
| 104 | 2 | public function end(): QueryBuilderInterface |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @return ArrayNode |
||
| 115 | */ |
||
| 116 | 5 | public function arrayNode(): ArrayNode |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @param bool|null |
||
| 123 | * @return BoolNode |
||
| 124 | */ |
||
| 125 | 1 | public function boolNode($value = null): BoolNode |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @param float|null |
||
| 132 | * @return FloatNode |
||
| 133 | */ |
||
| 134 | 1 | public function floatNode($value = null): FloatNode |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @param int|null |
||
| 141 | * @return IntNode |
||
| 142 | */ |
||
| 143 | 4 | public function intNode($value = null): IntNode |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @return ObjectNode |
||
| 150 | */ |
||
| 151 | 16 | public function objectNode(): ObjectNode |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @deprecated use boolNode|floatNode|intNode|stringNode |
||
| 158 | * @param string|float|int|bool|null $value |
||
| 159 | * @return ScalarNode |
||
| 160 | */ |
||
| 161 | 1 | public function scalarNode($value = null): ScalarNode |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @param string|null |
||
| 168 | * @return StringNode |
||
| 169 | */ |
||
| 170 | 13 | public function stringNode($value = null): StringNode |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @return \stdClass|null |
||
| 177 | */ |
||
| 178 | 16 | public function serialize() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @param boolean $beautify |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | 16 | public function json(bool $beautify = false): string |
|
| 199 | } |
||
| 200 |
This check looks for function calls that miss required arguments.