| Conditions | 10 |
| Paths | 6 |
| Total Lines | 46 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 85 | public function onMethodInject(IAnnotatable $instance, \ReflectionMethod $method, Container $container) |
||
| 86 | { |
||
| 87 | $allowMethods = $this->injectAnnotation['allowMethod']; |
||
| 88 | $logger = $container->logger; |
||
|
|
|||
| 89 | |||
| 90 | // 指定無しの場合はチェックしない(すべてのメソッドを許可する) |
||
| 91 | if ($allowMethods !== null) { |
||
| 92 | if (!is_array($allowMethods)) { |
||
| 93 | $allowMethods = [$allowMethods]; |
||
| 94 | } |
||
| 95 | |||
| 96 | for ($i = 0; $i < count($allowMethods); $i++) { |
||
| 97 | if (!preg_match("/^(?:(?:P(?:OS|U)|GE)T|(?:p(?:os|u)|ge)t|DELETE|delete)$/", $allowMethods[$i])) { |
||
| 98 | $errorMsg = "Invalid value '" . $allowMethods[$i] . "' in 'allowMethod' attribute of @Header."; |
||
| 99 | throw new AnnotationException($errorMsg); |
||
| 100 | } |
||
| 101 | $allowMethods[$i] = strtoupper($allowMethods[$i]); |
||
| 102 | } |
||
| 103 | |||
| 104 | // 複数指定した場合、一つでも許可されていればOK |
||
| 105 | if (!array_key_exists($container->requestMethod, array_flip($allowMethods))) { |
||
| 106 | $errorMsg = "Not allowed request method '" . $container->requestMethod; |
||
| 107 | throw new InvalidRequestException($errorMsg); |
||
| 108 | } |
||
| 109 | |||
| 110 | $logger->debug("Accepted method '" . implode(',', $allowMethods) . "'"); |
||
| 111 | } |
||
| 112 | |||
| 113 | $ext = $this->injectAnnotation['contentType'] ?: 'html'; |
||
| 114 | |||
| 115 | if (!is_string($ext)) { |
||
| 116 | $errorMsg = "contentType' attribute of @Header must be string."; |
||
| 117 | throw new AnnotationException($errorMsg); |
||
| 118 | } |
||
| 119 | |||
| 120 | $contentType = null; |
||
| 121 | if (array_key_exists($ext, $this->contentTypeList)) { |
||
| 122 | $contentType = $this->contentTypeList[$ext]; |
||
| 123 | } |
||
| 124 | if ($contentType === null) { |
||
| 125 | $errorMsg = "Invalid value '$ext' in 'contentType' attribute of @Header."; |
||
| 126 | throw new AnnotationException($errorMsg); |
||
| 127 | } |
||
| 128 | |||
| 129 | $this->readAnnotation['contentType'] = $ext; |
||
| 130 | $logger->debug("Accepted contentType '$ext'"); |
||
| 131 | } |
||
| 133 |