| Total Complexity | 40 |
| Total Lines | 281 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Application often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Application, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Application implements ApplicationInterface, ReflectionInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var ApplicationInterface |
||
| 26 | */ |
||
| 27 | private static $app; |
||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | private $objects = []; |
||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $bind = []; |
||
| 36 | /** |
||
| 37 | * @var ContainerInterface |
||
| 38 | */ |
||
| 39 | private $request; |
||
| 40 | /** |
||
| 41 | * @var CookieInterface |
||
| 42 | */ |
||
| 43 | private $cookie; |
||
| 44 | /** |
||
| 45 | * @var SessionInterface |
||
| 46 | */ |
||
| 47 | private $session; |
||
| 48 | /** |
||
| 49 | * @var ResponseInterface |
||
| 50 | */ |
||
| 51 | private $response; |
||
| 52 | /** |
||
| 53 | * @var ConfigInterface |
||
| 54 | */ |
||
| 55 | private $config; |
||
| 56 | |||
| 57 | public function __construct() |
||
| 58 | { |
||
| 59 | $this->request = new Request(); |
||
| 60 | $this->cookie = new Cookie(); |
||
| 61 | $this->session = new Session(); |
||
| 62 | $this->response = new Response(); |
||
| 63 | $this->config = new Config(); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @return ApplicationInterface |
||
| 68 | */ |
||
| 69 | public static function app(): ApplicationInterface |
||
| 70 | { |
||
| 71 | if (!static::$app instanceof static) { |
||
| 72 | static::$app = new static(); |
||
| 73 | } |
||
| 74 | |||
| 75 | return static::$app; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $key |
||
| 80 | * @param $object |
||
| 81 | */ |
||
| 82 | private function rawSet(string $key, $object) |
||
| 83 | { |
||
| 84 | $this->objects[$key] = $object; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string $key |
||
| 89 | * @param $object |
||
| 90 | * @param null $params |
||
| 91 | * @throws \ReflectionException |
||
| 92 | */ |
||
| 93 | private function iOc(string $key, $object, $params = null): void |
||
| 94 | { |
||
| 95 | $reflection = new \ReflectionClass($object); |
||
| 96 | $constructor = $reflection->getConstructor(); |
||
| 97 | |||
| 98 | if ($constructor && $constructor->getNumberOfParameters()) { |
||
| 99 | $paramsIoC = $this->getParamsIoC($constructor, $params); |
||
| 100 | $this->objects[$key] = $reflection->newInstanceArgs($paramsIoC); |
||
| 101 | |||
| 102 | return; |
||
| 103 | } |
||
| 104 | |||
| 105 | $this->objects[$key] = new $object(); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param $object |
||
| 110 | * @param null $params |
||
| 111 | * @return object |
||
| 112 | * @throws \ReflectionException |
||
| 113 | */ |
||
| 114 | public function new($object, $params = null) |
||
| 115 | { |
||
| 116 | $reflection = new \ReflectionClass($object); |
||
| 117 | $constructor = $reflection->getConstructor(); |
||
| 118 | |||
| 119 | if ($constructor && $constructor->getNumberOfParameters()) { |
||
| 120 | $paramsIoC = $this->getParamsIoC($constructor, $params); |
||
| 121 | |||
| 122 | return $reflection->newInstanceArgs($paramsIoC); |
||
| 123 | } |
||
| 124 | |||
| 125 | return new $object(); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param \ReflectionMethod $constructor |
||
| 130 | * @param $params |
||
| 131 | * @return array |
||
| 132 | * @throws \ReflectionException |
||
| 133 | */ |
||
| 134 | private function getParamsIoC(\ReflectionMethod $constructor, $params): array |
||
| 155 | } |
||
| 156 | |||
| 157 | |||
| 158 | /** |
||
| 159 | * @param string|null $key |
||
| 160 | * @return array|mixed |
||
| 161 | */ |
||
| 162 | public function get(string $key = null) |
||
| 163 | { |
||
| 164 | return (empty($key)) ? $this->objects : $this->objects[$key]; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $key |
||
| 169 | * @param $object |
||
| 170 | * @param null $params |
||
| 171 | * @throws \ReflectionException |
||
| 172 | */ |
||
| 173 | public function set(string $key, $object, $params = null) |
||
| 174 | { |
||
| 175 | ('raw' === $params) ? $this->rawSet($key, $object) : $this->iOc($key, $object, $params); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param string $key |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | public function has(string $key): bool |
||
| 183 | { |
||
| 184 | return isset($this->objects[$key]); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param string $key |
||
| 189 | * @param string $param |
||
| 190 | * @return mixed |
||
| 191 | */ |
||
| 192 | public function getParam(string $key, string $param) |
||
| 193 | { |
||
| 194 | if ($this->has($key) && isset($this->get($key)->$param)) { |
||
| 195 | return $this->get($key)->$param; |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param string $key |
||
| 201 | * @param string $param |
||
| 202 | * @param $value |
||
| 203 | */ |
||
| 204 | public function setParam(string $key, string $param, $value): void |
||
| 205 | { |
||
| 206 | if (isset($this->objects[$key])) { |
||
| 207 | $this->get($key)->$param = $value; |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $key |
||
| 213 | * @param string $param |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | public function hasParam(string $key, string $param) |
||
| 217 | { |
||
| 218 | if ($this->has($key)) { |
||
| 219 | return isset($this->get($key)->$param); |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param array $services |
||
| 225 | * @throws \ReflectionException |
||
| 226 | */ |
||
| 227 | public function setServices(array $services): void |
||
| 228 | { |
||
| 229 | foreach ($services['contracts'] as $interface => $contract) { |
||
| 230 | $this->setBinding($interface, $contract); |
||
| 231 | } |
||
| 232 | |||
| 233 | foreach ($services['services'] as $name => $service) { |
||
| 234 | $this->set($name, ...$service); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param string $key |
||
| 240 | * @return mixed|string |
||
| 241 | */ |
||
| 242 | public function getBinding(string $key) |
||
| 243 | { |
||
| 244 | return $this->bind[$key] ?? $key; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param string $key |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | public function hasBinding(string $key): bool |
||
| 252 | { |
||
| 253 | return array_key_exists($key, $this->bind); |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param string $key |
||
| 258 | * @param $value |
||
| 259 | */ |
||
| 260 | public function setBinding(string $key, $value): void |
||
| 261 | { |
||
| 262 | $this->bind[$key] = $value; |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @return Request |
||
| 267 | */ |
||
| 268 | public function request(): Request |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @return CookieInterface |
||
| 275 | */ |
||
| 276 | public function cookie(): CookieInterface |
||
| 277 | { |
||
| 278 | return $this->cookie; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return SessionInterface |
||
| 283 | */ |
||
| 284 | public function session(): SessionInterface |
||
| 285 | { |
||
| 286 | return $this->session; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @return ResponseInterface |
||
| 291 | */ |
||
| 292 | public function response(): ResponseInterface |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @return ContainerInterface |
||
| 299 | */ |
||
| 300 | public function config(): ContainerInterface |
||
| 301 | { |
||
| 302 | return $this->config; |
||
| 303 | } |
||
| 304 | } |
||
| 305 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths