Conditions | 12 |
Paths | 4 |
Total Lines | 43 |
Code Lines | 26 |
Lines | 10 |
Ratio | 23.26 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
104 | protected function walkQuery(AbstractAst $node, FieldInterface $current, QueryVisitorInterface $visitor) { |
||
105 | $carry = $visitor->initial(); |
||
106 | |||
107 | if (!($node instanceof Field)) { |
||
108 | /** @var \Youshido\GraphQL\Parser\Ast\Field $field */ |
||
109 | foreach ($node->getFields() as $field) { |
||
|
|||
110 | if ($field instanceof FragmentInterface) { |
||
111 | if ($field instanceof FragmentReference) { |
||
112 | $field = $this->request->getFragment($field->getName()); |
||
113 | } |
||
114 | |||
115 | $walker = $this->walkQuery($field, $current, $visitor); |
||
116 | $next = $walker->current(); |
||
117 | |||
118 | View Code Duplication | while ($walker->valid()) { |
|
119 | $received = (yield $next); |
||
120 | $carry = $visitor->reduce($carry, $received); |
||
121 | $next = $walker->send($received); |
||
122 | } |
||
123 | } |
||
124 | else { |
||
125 | $type = $this->getType($node, $current); |
||
126 | $name = $field->getName(); |
||
127 | |||
128 | if (($type instanceof AbstractObjectType || $type instanceof AbstractInterfaceType) && $type->hasField($name)) { |
||
129 | $ast = $type->getField($name); |
||
130 | $walker = $this->walkQuery($field, $ast, $visitor); |
||
131 | $next = $walker->current(); |
||
132 | |||
133 | View Code Duplication | while ($walker->valid()) { |
|
134 | $received = (yield $next); |
||
135 | $carry = $visitor->reduce($carry, $received); |
||
136 | $next = $walker->send($received); |
||
137 | } |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | |||
143 | if ($node instanceof Query || $node instanceof Field) { |
||
144 | yield [$node, $current, $carry]; |
||
145 | } |
||
146 | } |
||
147 | |||
166 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: