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 |
||
22 | class Reducer { |
||
23 | |||
24 | /** |
||
25 | * @var \Youshido\GraphQL\Execution\Request |
||
26 | */ |
||
27 | protected $request; |
||
28 | |||
29 | /** |
||
30 | * @var \Youshido\GraphQL\Schema\AbstractSchema |
||
31 | */ |
||
32 | protected $schema; |
||
33 | |||
34 | /** |
||
35 | * @var \Youshido\GraphQL\Type\TypeInterface[] |
||
36 | */ |
||
37 | protected $types; |
||
38 | |||
39 | /** |
||
40 | * @var \Youshido\GraphQL\Execution\Context\ExecutionContext |
||
41 | */ |
||
42 | protected $context; |
||
43 | |||
44 | /** |
||
45 | * QueryReducer constructor. |
||
46 | * |
||
47 | */ |
||
48 | public function __construct(ExecutionContext $executionContext) { |
||
54 | |||
55 | /** |
||
56 | * @param \Drupal\graphql\GraphQL\Execution\Visitor\VisitorInterface $visitor |
||
57 | * |
||
58 | * @return mixed|\Youshido\GraphQL\Parser\Ast\Query |
||
59 | */ |
||
60 | public function reduceRequest(VisitorInterface $visitor) { |
||
68 | |||
69 | /** |
||
70 | * @param \Youshido\GraphQL\Parser\Ast\AbstractAst $query |
||
71 | * @param \Youshido\GraphQL\Type\AbstractType $type |
||
72 | * @param \Drupal\graphql\GraphQL\Execution\Visitor\VisitorInterface $visitor |
||
73 | * |
||
74 | * @return mixed|void |
||
75 | */ |
||
76 | protected function reduceOperation(AbstractAst $query, AbstractType $type, VisitorInterface $visitor) { |
||
101 | |||
102 | /** |
||
103 | * @param $node |
||
104 | * @param \Youshido\GraphQL\Field\FieldInterface $current |
||
105 | * @param \Drupal\graphql\GraphQL\Execution\Visitor\VisitorInterface $visitor |
||
106 | * |
||
107 | * @return \Generator |
||
108 | */ |
||
109 | protected function walkQuery(AbstractAst $node, FieldInterface $current, VisitorInterface $visitor) { |
||
110 | $carry = $visitor->initial(); |
||
111 | |||
112 | if (!($node instanceof Field)) { |
||
113 | /** @var \Youshido\GraphQL\Parser\Ast\Field $field */ |
||
114 | foreach ($node->getFields() as $field) { |
||
115 | if ($field instanceof FragmentInterface) { |
||
116 | if ($field instanceof FragmentReference) { |
||
117 | $field = $this->request->getFragment($field->getName()); |
||
118 | } |
||
119 | |||
120 | $walker = $this->walkQuery($field, $current, $visitor); |
||
121 | $next = $walker->current(); |
||
122 | |||
123 | View Code Duplication | while ($walker->valid()) { |
|
124 | $received = (yield $next); |
||
125 | $carry = $visitor->reduce($carry, $received, $this->context); |
||
126 | $next = $walker->send($received); |
||
127 | } |
||
128 | } |
||
129 | else { |
||
130 | $type = $this->getType($node, $current); |
||
131 | $name = $field->getName(); |
||
132 | |||
133 | if ($name !== Processor::TYPE_NAME_QUERY && ($type instanceof AbstractObjectType || $type instanceof AbstractInterfaceType)) { |
||
134 | if (!$type->hasField($name)) { |
||
135 | $type = $type->getNamedType()->getName(); |
||
136 | $location = $field->getLocation(); |
||
137 | |||
138 | throw new ResolveException(sprintf('Unknown field %s for type %s.', $name, $type), $location); |
||
139 | } |
||
140 | |||
141 | $ast = $type->getField($name); |
||
142 | $walker = $this->walkQuery($field, $ast, $visitor); |
||
143 | $next = $walker->current(); |
||
144 | |||
145 | View Code Duplication | while ($walker->valid()) { |
|
146 | $received = (yield $next); |
||
147 | $carry = $visitor->reduce($carry, $received, $this->context); |
||
148 | $next = $walker->send($received); |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | |||
155 | if ($node instanceof Query || $node instanceof Field) { |
||
156 | yield [$node, $current, $carry]; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param \Youshido\GraphQL\Parser\Ast\AbstractAst $node |
||
162 | * @param \Youshido\GraphQL\Field\FieldInterface $current |
||
163 | * |
||
164 | * @return null|\Youshido\GraphQL\Type\AbstractType|\Youshido\GraphQL\Type\TypeInterface |
||
165 | */ |
||
166 | protected function getType(AbstractAst $node, FieldInterface $current) { |
||
177 | |||
178 | } |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.