| Conditions | 3 |
| Paths | 1 |
| Total Lines | 160 |
| Code Lines | 94 |
| 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 |
||
| 49 | public function register(UnboxFactory $factory) |
||
|
|
|||
| 50 | { |
||
| 51 | $factory->set( |
||
| 52 | Settings::class, |
||
| 53 | new Settings($this->settings) |
||
| 54 | ); |
||
| 55 | |||
| 56 | $factory->alias( |
||
| 57 | "settings", |
||
| 58 | Settings::class |
||
| 59 | ); |
||
| 60 | |||
| 61 | $factory->register( |
||
| 62 | Environment::class, |
||
| 63 | function () { |
||
| 64 | return new Environment($_SERVER); |
||
| 65 | } |
||
| 66 | ); |
||
| 67 | |||
| 68 | $factory->alias( |
||
| 69 | EnvironmentInterface::class, |
||
| 70 | Environment::class |
||
| 71 | ); |
||
| 72 | |||
| 73 | $factory->alias( |
||
| 74 | "environment", |
||
| 75 | Environment::class |
||
| 76 | ); |
||
| 77 | |||
| 78 | $factory->register( |
||
| 79 | Request::class, |
||
| 80 | function (Environment $environment) { |
||
| 81 | return Request::createFromEnvironment($environment); |
||
| 82 | } |
||
| 83 | ); |
||
| 84 | |||
| 85 | $factory->alias( |
||
| 86 | ServerRequestInterface::class, |
||
| 87 | Request::class |
||
| 88 | ); |
||
| 89 | |||
| 90 | $factory->alias( |
||
| 91 | "request", |
||
| 92 | Request::class |
||
| 93 | ); |
||
| 94 | |||
| 95 | $factory->register( |
||
| 96 | Response::class, |
||
| 97 | function (Settings $settings) { |
||
| 98 | $headers = new Headers(['Content-Type' => 'text/html; charset=UTF-8']); |
||
| 99 | $response = new Response(200, $headers); |
||
| 100 | |||
| 101 | return $response->withProtocolVersion($settings['httpVersion']); |
||
| 102 | } |
||
| 103 | ); |
||
| 104 | |||
| 105 | $factory->alias( |
||
| 106 | ResponseInterface::class, |
||
| 107 | Response::class |
||
| 108 | ); |
||
| 109 | |||
| 110 | $factory->alias( |
||
| 111 | "response", |
||
| 112 | Response::class |
||
| 113 | ); |
||
| 114 | |||
| 115 | $factory->register( |
||
| 116 | Router::class, |
||
| 117 | function (Settings $settings) { |
||
| 118 | $routerCacheFile = false; |
||
| 119 | if ($settings->has('routerCacheFile') && file_exists($settings['routerCacheFile'])) { |
||
| 120 | $routerCacheFile = $settings['routerCacheFile']; |
||
| 121 | } |
||
| 122 | return (new Router)->setCacheFile($routerCacheFile); |
||
| 123 | } |
||
| 124 | ); |
||
| 125 | |||
| 126 | $factory->alias( |
||
| 127 | RouterInterface::class, |
||
| 128 | Router::class |
||
| 129 | ); |
||
| 130 | |||
| 131 | $factory->alias( |
||
| 132 | "router", |
||
| 133 | Router::class |
||
| 134 | ); |
||
| 135 | |||
| 136 | $factory->register( |
||
| 137 | RequestResponse::class |
||
| 138 | ); |
||
| 139 | |||
| 140 | $factory->alias( |
||
| 141 | InvocationStrategyInterface::class, |
||
| 142 | RequestResponse::class |
||
| 143 | ); |
||
| 144 | |||
| 145 | $factory->alias( |
||
| 146 | "foundHandler", |
||
| 147 | RequestResponse::class |
||
| 148 | ); |
||
| 149 | |||
| 150 | $factory->register( |
||
| 151 | PhpError::class, |
||
| 152 | function (Settings $settings) { |
||
| 153 | return new PhpError($settings['displayErrorDetails']); |
||
| 154 | } |
||
| 155 | ); |
||
| 156 | |||
| 157 | $factory->alias( |
||
| 158 | "phpErrorHandler", |
||
| 159 | PhpError::class |
||
| 160 | ); |
||
| 161 | |||
| 162 | $factory->register( |
||
| 163 | Error::class, |
||
| 164 | function (Settings $settings) { |
||
| 165 | return new Error($settings['displayErrorDetails']); |
||
| 166 | } |
||
| 167 | ); |
||
| 168 | |||
| 169 | $factory->alias( |
||
| 170 | "errorHandler", |
||
| 171 | Error::class |
||
| 172 | ); |
||
| 173 | |||
| 174 | $factory->register( |
||
| 175 | NotFound::class |
||
| 176 | ); |
||
| 177 | |||
| 178 | $factory->alias( |
||
| 179 | "notFoundHandler", |
||
| 180 | NotFound::class |
||
| 181 | ); |
||
| 182 | |||
| 183 | $factory->register( |
||
| 184 | NotAllowed::class |
||
| 185 | ); |
||
| 186 | |||
| 187 | $factory->alias( |
||
| 188 | "notAllowedHandler", |
||
| 189 | NotAllowed::class |
||
| 190 | ); |
||
| 191 | |||
| 192 | $factory->register( |
||
| 193 | CallableResolver::class, |
||
| 194 | function (ContainerInterface $container) { |
||
| 195 | return new CallableResolver($container); |
||
| 196 | } |
||
| 197 | ); |
||
| 198 | |||
| 199 | $factory->alias( |
||
| 200 | CallableResolverInterface::class, |
||
| 201 | CallableResolver::class |
||
| 202 | ); |
||
| 203 | |||
| 204 | $factory->alias( |
||
| 205 | "callableResolver", |
||
| 206 | CallableResolver::class |
||
| 207 | ); |
||
| 208 | } |
||
| 209 | } |
||
| 210 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: