| Total Complexity | 44 |
| Total Lines | 169 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 1 |
Complex classes like Rudra 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 Rudra, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Rudra implements RudraInterface, ContainerInterface |
||
| 16 | { |
||
| 17 | use InstantiationsTrait; |
||
| 18 | |||
| 19 | public static ?RudraInterface $rudra = null; |
||
| 20 | private array $data = []; |
||
| 21 | |||
| 22 | public function setServices(array $services): void |
||
| 23 | { |
||
| 24 | ($this->has("binding")) ?: $this->set(["binding", new Container($services["contracts"])]); |
||
| 25 | ($this->has("services")) ?: $this->set(["services", new Container($services["services"])]); |
||
| 26 | ($this->has("config")) ?: $this->set(["config", new Container($services["config"])]); |
||
| 27 | } |
||
| 28 | |||
| 29 | public function binding(): ContainerInterface |
||
| 30 | { |
||
| 31 | if ($this->has("binding")) return $this->get("binding"); |
||
| 32 | throw new \InvalidArgumentException("Service not preinstalled"); |
||
| 33 | } |
||
| 34 | |||
| 35 | public function services(): ContainerInterface |
||
| 36 | { |
||
| 37 | if ($this->has("services")) return $this->get("services"); |
||
| 38 | throw new \InvalidArgumentException("Service not preinstalled"); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function config(): ContainerInterface |
||
| 42 | { |
||
| 43 | if ($this->has("config")) return $this->get("config"); |
||
| 44 | throw new \InvalidArgumentException("Service not preinstalled"); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function request(): RequestInterface |
||
| 48 | { |
||
| 49 | return $this->containerize(Request::class); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function response(): ResponseInterface |
||
| 53 | { |
||
| 54 | return $this->containerize(ResponseInterface::class); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function cookie(): Cookie |
||
| 58 | { |
||
| 59 | return $this->containerize(Cookie::class); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function session(): Session |
||
| 63 | { |
||
| 64 | return $this->containerize(Session::class); |
||
| 65 | } |
||
| 66 | |||
| 67 | /* |
||
| 68 | | Creates an object without adding to the container |
||
| 69 | */ |
||
| 70 | public function new($object, $params = null) |
||
| 71 | { |
||
| 72 | $reflection = new \ReflectionClass($object); |
||
| 73 | $constructor = $reflection->getConstructor(); |
||
| 74 | |||
| 75 | if ($constructor && $constructor->getNumberOfParameters()) { |
||
| 76 | $paramsIoC = $this->getParamsIoC($constructor, $params); |
||
| 77 | |||
| 78 | return $reflection->newInstanceArgs($paramsIoC); |
||
| 79 | } |
||
| 80 | |||
| 81 | return new $object(); |
||
| 82 | } |
||
| 83 | |||
| 84 | public static function run(): RudraInterface |
||
| 91 | } |
||
| 92 | |||
| 93 | public function get(string $key = null) |
||
| 94 | { |
||
| 95 | if (isset($key) && !$this->has($key)) { |
||
| 96 | if (!$this->services()->has($key)) { |
||
| 97 | throw new \InvalidArgumentException("Service is not installed"); |
||
| 98 | } |
||
| 99 | |||
| 100 | $this->set([$key, $this->services()->get($key)]); |
||
| 101 | } |
||
| 102 | |||
| 103 | return empty($key) ? $this->data : $this->data[$key]; |
||
| 104 | } |
||
| 105 | |||
| 106 | public function set(array $data): void |
||
| 107 | { |
||
| 108 | list($key, $object) = $data; |
||
| 109 | |||
| 110 | if (is_array($object)) { |
||
| 111 | if (array_key_exists(1, $object) && !is_object($object[0])) { |
||
| 112 | $this->iOc($key, $object[0], $object[1]); |
||
| 113 | return; |
||
| 114 | } |
||
| 115 | |||
| 116 | $this->setObject($object[0], $key); |
||
| 117 | return; |
||
| 118 | } |
||
| 119 | |||
| 120 | $this->setObject($object, $key); |
||
| 121 | } |
||
| 122 | |||
| 123 | public function has(string $key): bool |
||
| 124 | { |
||
| 125 | return array_key_exists($key, $this->data); |
||
| 126 | } |
||
| 127 | |||
| 128 | private function setObject($object, $key): void |
||
| 129 | { |
||
| 130 | (is_object($object)) ? $this->mergeData($key, $object) : $this->iOc($key, $object); |
||
| 131 | } |
||
| 132 | |||
| 133 | private function mergeData(string $key, $object) |
||
| 136 | } |
||
| 137 | |||
| 138 | private function iOc(string $key, $object, $params = null): void |
||
| 139 | { |
||
| 140 | $reflection = new \ReflectionClass($object); |
||
| 141 | $constructor = $reflection->getConstructor(); |
||
| 142 | |||
| 143 | if ($constructor && $constructor->getNumberOfParameters()) { |
||
| 150 | } |
||
| 151 | |||
| 152 | private function getParamsIoC(\ReflectionMethod $constructor, $params): array |
||
| 184 | } |
||
| 185 | } |
||
| 186 |
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