We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 11 |
Paths | 34 |
Total Lines | 52 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Tests | 31 |
CRAP Score | 11 |
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 |
||
140 | 37 | public function execute(array $data, array $context = [], $schemaName = null) |
|
141 | { |
||
142 | 37 | if (null !== $this->dispatcher) { |
|
143 | 32 | $event = new ExecutorContextEvent($context); |
|
144 | 32 | $this->dispatcher->dispatch(Events::EXECUTOR_CONTEXT, $event); |
|
145 | 32 | $context = $event->getExecutorContext(); |
|
146 | } |
||
147 | |||
148 | 37 | if ($this->promiseAdapter) { |
|
149 | 33 | if (!$this->promiseAdapter instanceof PromiseAdapterInterface && !is_callable([$this->promiseAdapter, 'wait'])) { |
|
150 | 1 | throw new \RuntimeException( |
|
151 | 1 | sprintf( |
|
152 | 1 | 'PromiseAdapter should be an object instantiating "%s" or "%s" with a "wait" method.', |
|
153 | 1 | PromiseAdapterInterface::class, |
|
154 | 1 | PromiseAdapter::class |
|
155 | ) |
||
156 | ); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | 36 | $schema = $this->getSchema($schemaName); |
|
161 | |||
162 | 36 | $startTime = microtime(true); |
|
163 | 36 | $startMemoryUsage = memory_get_usage(true); |
|
164 | |||
165 | 36 | $this->executor->setPromiseAdapter($this->promiseAdapter); |
|
166 | // this is needed when not using only generated types |
||
167 | 36 | if ($this->defaultFieldResolver) { |
|
168 | 32 | $this->executor->setDefaultFieldResolver($this->defaultFieldResolver); |
|
169 | } |
||
170 | |||
171 | 36 | $result = $this->executor->execute( |
|
172 | 36 | $schema, |
|
173 | 36 | isset($data[ParserInterface::PARAM_QUERY]) ? $data[ParserInterface::PARAM_QUERY] : null, |
|
174 | 36 | $context, |
|
175 | 36 | $context, |
|
176 | 36 | $data[ParserInterface::PARAM_VARIABLES], |
|
177 | 36 | isset($data[ParserInterface::PARAM_OPERATION_NAME]) ? $data[ParserInterface::PARAM_OPERATION_NAME] : null |
|
178 | ); |
||
179 | |||
180 | 36 | if ($this->promiseAdapter) { |
|
181 | 32 | $result = $this->promiseAdapter->wait($result); |
|
|
|||
182 | } |
||
183 | |||
184 | 36 | if (!is_object($result) || !$result instanceof ExecutionResult) { |
|
185 | 2 | throw new \RuntimeException( |
|
186 | 2 | sprintf('Execution result should be an object instantiating "%s".', ExecutionResult::class) |
|
187 | ); |
||
188 | } |
||
189 | |||
190 | 34 | return $this->prepareResult($result, $startTime, $startMemoryUsage); |
|
191 | } |
||
192 | |||
239 |
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 implementation 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 interface: