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 | 51 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 37 |
| 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 |
||
| 143 | 50 | public function execute(array $data, array $context = [], $schemaName = null) |
|
| 144 | { |
||
| 145 | 50 | if (null !== $this->dispatcher) { |
|
| 146 | 45 | $event = new ExecutorContextEvent($context); |
|
| 147 | 45 | $this->dispatcher->dispatch(Events::EXECUTOR_CONTEXT, $event); |
|
| 148 | 45 | $context = $event->getExecutorContext(); |
|
| 149 | 45 | } |
|
| 150 | |||
| 151 | 50 | if ($this->promiseAdapter) { |
|
| 152 | 46 | 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 | 'Overblog\\GraphQLBundle\\Executor\\Promise\\PromiseAdapterInterface', |
|
| 157 | 'GraphQL\\Executor\\Promise\\PromiseAdapter' |
||
| 158 | 1 | ) |
|
| 159 | 1 | ); |
|
| 160 | } |
||
| 161 | 45 | } |
|
| 162 | |||
| 163 | 49 | $schema = $this->getSchema($schemaName); |
|
| 164 | |||
| 165 | 48 | $startTime = microtime(true); |
|
| 166 | 48 | $startMemoryUsage = memory_get_usage(true); |
|
| 167 | |||
| 168 | 48 | $this->executor->setPromiseAdapter($this->promiseAdapter); |
|
| 169 | |||
| 170 | 48 | $result = $this->executor->execute( |
|
| 171 | 48 | $schema, |
|
| 172 | 48 | isset($data[ParserInterface::PARAM_QUERY]) ? $data[ParserInterface::PARAM_QUERY] : null, |
|
| 173 | 48 | $context, |
|
| 174 | 48 | $context, |
|
| 175 | 48 | $data[ParserInterface::PARAM_VARIABLES], |
|
| 176 | 48 | isset($data[ParserInterface::PARAM_OPERATION_NAME]) ? $data[ParserInterface::PARAM_OPERATION_NAME] : null |
|
| 177 | 48 | ); |
|
| 178 | |||
| 179 | 48 | if ($this->promiseAdapter && $this->promiseAdapter->isThenable($result)) { |
|
| 180 | 10 | $result = $this->promiseAdapter->wait($result); |
|
|
|
|||
| 181 | 10 | } |
|
| 182 | |||
| 183 | 48 | if (!is_object($result) || !$result instanceof ExecutionResult) { |
|
| 184 | 2 | throw new \RuntimeException( |
|
| 185 | 2 | sprintf( |
|
| 186 | 2 | 'Execution result should be an object instantiating "%s".', |
|
| 187 | 'GraphQL\\Executor\\ExecutionResult' |
||
| 188 | 2 | ) |
|
| 189 | 2 | ); |
|
| 190 | } |
||
| 191 | |||
| 192 | 46 | return $this->prepareResult($result, $startTime, $startMemoryUsage); |
|
| 193 | } |
||
| 194 | |||
| 234 |
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: