| Conditions | 17 |
| Paths | 1299 |
| Total Lines | 88 |
| Code Lines | 55 |
| 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 |
||
| 114 | public static function main(string $controllerName, string $methodName, DIContainer $container, array $urlParams = null) { |
||
| 115 | if (!is_null($urlParams)) { |
||
| 116 | $container->query(IRequest::class)->setUrlParameters($urlParams); |
||
| 117 | } elseif (isset($container['urlParams']) && !is_null($container['urlParams'])) { |
||
| 118 | $container->query(IRequest::class)->setUrlParameters($container['urlParams']); |
||
| 119 | } |
||
| 120 | $appName = $container['AppName']; |
||
| 121 | |||
| 122 | // first try $controllerName then go for \OCA\AppName\Controller\$controllerName |
||
| 123 | try { |
||
| 124 | $controller = $container->query($controllerName); |
||
| 125 | } catch (QueryException $e) { |
||
| 126 | if (strpos($controllerName, '\\Controller\\') !== false) { |
||
| 127 | // This is from a global registered app route that is not enabled. |
||
| 128 | [/*OC(A)*/, $app, /* Controller/Name*/] = explode('\\', $controllerName, 3); |
||
| 129 | throw new HintException('App ' . strtolower($app) . ' is not enabled'); |
||
| 130 | } |
||
| 131 | |||
| 132 | if ($appName === 'core') { |
||
| 133 | $appNameSpace = 'OC\\Core'; |
||
| 134 | } else { |
||
| 135 | $appNameSpace = self::buildAppNamespace($appName); |
||
| 136 | } |
||
| 137 | $controllerName = $appNameSpace . '\\Controller\\' . $controllerName; |
||
| 138 | $controller = $container->query($controllerName); |
||
| 139 | } |
||
| 140 | |||
| 141 | // initialize the dispatcher and run all the middleware before the controller |
||
| 142 | /** @var Dispatcher $dispatcher */ |
||
| 143 | $dispatcher = $container['Dispatcher']; |
||
| 144 | |||
| 145 | list( |
||
| 146 | $httpHeaders, |
||
| 147 | $responseHeaders, |
||
| 148 | $responseCookies, |
||
| 149 | $output, |
||
| 150 | $response |
||
| 151 | ) = $dispatcher->dispatch($controller, $methodName); |
||
| 152 | |||
| 153 | $io = $container[IOutput::class]; |
||
| 154 | |||
| 155 | if (!is_null($httpHeaders)) { |
||
| 156 | $io->setHeader($httpHeaders); |
||
| 157 | } |
||
| 158 | |||
| 159 | foreach ($responseHeaders as $name => $value) { |
||
| 160 | $io->setHeader($name . ': ' . $value); |
||
| 161 | } |
||
| 162 | |||
| 163 | foreach ($responseCookies as $name => $value) { |
||
| 164 | $expireDate = null; |
||
| 165 | if ($value['expireDate'] instanceof \DateTime) { |
||
| 166 | $expireDate = $value['expireDate']->getTimestamp(); |
||
| 167 | } |
||
| 168 | $sameSite = $value['sameSite'] ?? 'Lax'; |
||
| 169 | |||
| 170 | $io->setCookie( |
||
| 171 | $name, |
||
| 172 | $value['value'], |
||
| 173 | $expireDate, |
||
| 174 | $container->getServer()->getWebRoot(), |
||
| 175 | null, |
||
| 176 | $container->getServer()->getRequest()->getServerProtocol() === 'https', |
||
| 177 | true, |
||
| 178 | $sameSite |
||
| 179 | ); |
||
| 180 | } |
||
| 181 | |||
| 182 | /* |
||
| 183 | * Status 204 does not have a body and no Content Length |
||
| 184 | * Status 304 does not have a body and does not need a Content Length |
||
| 185 | * https://tools.ietf.org/html/rfc7230#section-3.3 |
||
| 186 | * https://tools.ietf.org/html/rfc7230#section-3.3.2 |
||
| 187 | */ |
||
| 188 | $emptyResponse = false; |
||
| 189 | if (preg_match('/^HTTP\/\d\.\d (\d{3}) .*$/', $httpHeaders, $matches)) { |
||
| 190 | $status = (int)$matches[1]; |
||
| 191 | if ($status === Http::STATUS_NO_CONTENT || $status === Http::STATUS_NOT_MODIFIED) { |
||
| 192 | $emptyResponse = true; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | if (!$emptyResponse) { |
||
| 197 | if ($response instanceof ICallbackResponse) { |
||
| 198 | $response->callback($io); |
||
| 199 | } elseif (!is_null($output)) { |
||
| 200 | $io->setHeader('Content-Length: ' . strlen($output)); |
||
| 201 | $io->setOutput($output); |
||
| 202 | } |
||
| 229 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: