| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Zewa; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Zewa\Exception\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Zewa\Exception\LookupException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class Dependency | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @var Container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     private $dependencies; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 | 15 |  |     public function __construct(Config $config, Container $container) | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         $local = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 18 | 15 |  |             '\Zewa\Config' => $config, | 
            
                                                                        
                            
            
                                    
            
            
                | 19 | 15 |  |             '\Zewa\Container' => $container, | 
            
                                                                        
                            
            
                                    
            
            
                | 20 | 15 |  |             '\Zewa\Dependency' => $this | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 15 |  |         $this->dependencies = $container; | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 15 |  |         $this->dependencies->set('__dependencies', $local); | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 15 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 27 | 1 | View Code Duplication |     public function flushDependency(string $class) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 1 |  |         $dependencies = $this->dependencies->get('__dependencies'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 1 |  |         if (isset($dependencies[$class]) === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 1 |  |             unset($dependencies[$class]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  |             $this->dependencies->set('__dependencies', $dependencies); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 14 |  |     public function isDependencyLoaded(string $class) : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 14 |  |         $dependencies = $this->dependencies->get('__dependencies'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 14 |  |         if (isset($dependencies[$class]) === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 13 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 11 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 10 |  |     public function getDependency(string $class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 10 |  |         $dependencies = $this->dependencies->get('__dependencies'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 10 |  |         return $dependencies[$class] ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 54 | 11 | View Code Duplication |     private function load(string $class, $dependency, bool $persist) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 11 |  |         if ($persist === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 10 |  |             $dependencies = $this->dependencies->get('__dependencies'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 10 |  |             $dependencies[$class] = $dependency; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 10 |  |             $this->dependencies->set('__dependencies', $dependencies); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | //        $this->injectAppInstance($dependency); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 11 |  |         return $dependency; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 12 |  |     public function resolve($class, $persist = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 12 |  |         if ($this->isDependencyLoaded($class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 9 |  |             return $this->getDependency($class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 12 |  |             $reflectionClass = new \ReflectionClass($class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 11 |  |             $constructor = $reflectionClass->getConstructor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 11 |  |             if (!$constructor || $constructor->getParameters() === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 10 |  |                 $dependency = new $class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 10 |  |                 return $this->load($class, $dependency, $persist); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 10 |  |             $params = $this->constructConstructorParameters($reflectionClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 10 |  |             $dependency = $reflectionClass->newInstanceArgs($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 10 |  |             return $this->load($class, $dependency, $persist); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |         } catch (\ReflectionException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param \ReflectionClass $reflection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 10 |  |     private function constructConstructorParameters(\ReflectionClass $reflection) : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 10 |  |         $constructor = $reflection->getConstructor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 10 |  |         $params = $constructor->getParameters(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 10 |  |         $injection = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 102 | 10 | View Code Duplication |         foreach ($params as $param) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             // Here we should perform a bunch of checks, such as: isArray(), isCallable(), isDefaultValueAvailable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             // isOptional() etc. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 10 |  |             if (is_null($param->getClass())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |                 $injection[] = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 9 |  |             $injection[] = $this->resolve("\\" . $param->getClass()->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 10 |  |         return $injection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.