| Conditions | 9 |
| Paths | 12 |
| Total Lines | 67 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 2 | Features | 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 |
||
| 65 | public function exe( |
||
| 66 | RunModel $model, |
||
| 67 | RpcModel $rpc |
||
| 68 | ) { |
||
| 69 | $factory = $model->getApiFactory(); |
||
| 70 | $method = $rpc->getMethod(); |
||
| 71 | /** Проверим существование метода */ |
||
| 72 | if ($factory->has($method) === false) { |
||
| 73 | throw new MethodNotFoundException( |
||
| 74 | 'Method "' . $method . '" not found' |
||
| 75 | ); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** Создаем экземпляр класса |
||
| 79 | * |
||
| 80 | * @var ApiMethodInterface $class |
||
| 81 | */ |
||
| 82 | $class = $factory->get($method); |
||
| 83 | |||
| 84 | /** Проверим соответствие интерфейсу */ |
||
| 85 | $interfaces = (array)class_implements($class); |
||
| 86 | if ( |
||
| 87 | (bool)$interfaces === false |
||
| 88 | || in_array(ApiMethodInterface::class, $interfaces, true) === false |
||
| 89 | ) { |
||
| 90 | throw new InternalErrorException( |
||
| 91 | 'Method "' . $method . '" does not match Interface' |
||
| 92 | ); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** Валидируем парамертры ЗАПРОСА */ |
||
| 96 | if ($class->requestSchema() !== null) { |
||
| 97 | $this->getValidator()->validate( |
||
| 98 | $class->requestSchema(), |
||
| 99 | $rpc->getParams(), |
||
| 100 | 'requestParams' |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | |||
| 104 | $paramsObject = null; |
||
| 105 | if (method_exists($class, 'customParamsObject')) { |
||
| 106 | try { |
||
| 107 | $paramsObject = $this->getMapper()->map($rpc->getParams(), $class->customParamsObject()); |
||
| 108 | } catch (JsonMapper_Exception $e) { |
||
| 109 | throw new ParseErrorException('', 0, $e->getPrevious()); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * засетим в метод RpcRequest |
||
| 115 | * @var ApiMethodAbstract $class |
||
| 116 | */ |
||
| 117 | $class->setRpcRequest(new RpcRequest($rpc, $paramsObject)); |
||
| 118 | |||
| 119 | /** Выполним метод */ |
||
| 120 | $res = $class->execute()->getResult(); |
||
| 121 | |||
| 122 | if ($model->isResponseCheck() && $class->responseSchema() !== null) { |
||
| 123 | /** Валидируем парамертры ОТВЕТА */ |
||
| 124 | $this->getValidator()->validate( |
||
| 125 | $class->responseSchema(), |
||
| 126 | $res, |
||
| 127 | 'responseParams' |
||
| 128 | ); |
||
| 129 | } |
||
| 130 | |||
| 131 | return $res; |
||
| 132 | } |
||
| 158 |