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