| Conditions | 18 |
| Paths | 133 |
| Total Lines | 74 |
| Code Lines | 47 |
| 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 declare(strict_types = 1); |
||
| 45 | public function process(ServerRequestInterface $request, DelegateInterface $delegate) |
||
| 46 | { |
||
| 47 | $uriPath = $request->getUri()->getPath(); |
||
| 48 | |||
| 49 | /** @var Description $description */ |
||
| 50 | foreach ($this->repository as $description) { |
||
| 51 | |||
| 52 | $basePath = $description->getBasePath() ? "{$description->getBasePath()}/" : ""; |
||
| 53 | |||
| 54 | foreach ($description->getPaths() as $path) { |
||
| 55 | $pathPattern = "$basePath{$path->getPath()}"; |
||
| 56 | |||
| 57 | $parameterNames = []; |
||
| 58 | foreach ($path->getOperations() as $operation) { |
||
| 59 | foreach ($operation->getParameters() as $parameter) { |
||
| 60 | if ($parameter->getIn() === Parameter::IN_PATH |
||
| 61 | && ($schema = $parameter->getSchema()) instanceof ScalarSchema |
||
| 62 | ) { |
||
| 63 | $parameterName = $parameter->getName(); |
||
| 64 | $parameterNames[] = $parameterName; |
||
| 65 | $parameterPattern = "(?P<$parameterName>.*)(?=(/|$))"; |
||
| 66 | $typePattern = null; |
||
| 67 | switch ($type = $schema->getType()) { |
||
|
|
|||
| 68 | case Schema::TYPE_INT: |
||
| 69 | $typePattern = '\d+'; |
||
| 70 | break; |
||
| 71 | case Schema::TYPE_NUMBER: |
||
| 72 | $typePattern = '\d+(\.\d+)?'; |
||
| 73 | break; |
||
| 74 | case Schema::TYPE_NULL: |
||
| 75 | $typePattern = 'null'; |
||
| 76 | break; |
||
| 77 | case Schema::TYPE_STRING: |
||
| 78 | /** @var $schema ScalarSchema $routeString */ |
||
| 79 | if ($pattern = $schema->getPattern()) { |
||
| 80 | $typePattern = $pattern; |
||
| 81 | } elseif ($enum = $schema->getEnum()) { |
||
| 82 | $typePattern = '('.implode('|', $enum).')'; |
||
| 83 | } |
||
| 84 | break; |
||
| 85 | default: |
||
| 86 | $typePattern = null; |
||
| 87 | } |
||
| 88 | if ($typePattern) { |
||
| 89 | $parameterPattern = str_replace( |
||
| 90 | "<$parameterName>.*", |
||
| 91 | "<$parameterName>$typePattern", |
||
| 92 | $parameterPattern |
||
| 93 | ); |
||
| 94 | } |
||
| 95 | $pathPattern = str_replace('{'.$parameterName.'}', $parameterPattern, $pathPattern); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | if (preg_match("#^$pathPattern$#", $uriPath, $matches) > 0) { |
||
| 100 | if (strtolower($request->getMethod()) !== $operation->getMethod()) { |
||
| 101 | return Factory::createResponse(405); |
||
| 102 | } |
||
| 103 | |||
| 104 | $request = Meta::requestWith($request, $description, $operation, $path); |
||
| 105 | |||
| 106 | foreach ($parameterNames as $parameterName) { |
||
| 107 | $request = $request->withAttribute($parameterName, $matches[$parameterName]); |
||
| 108 | } |
||
| 109 | |||
| 110 | return $delegate->process($request); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | return Factory::createResponse(404); |
||
| 118 | } |
||
| 119 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.