| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * This file is part of the Ray.Di package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license http://opensource.org/licenses/MIT MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace Ray\Di; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | final class Argument implements \Serializable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     private $index; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $isDefaultAvailable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $meta; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @var \ReflectionParameter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     private $reflection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 68 |  |     public function __construct(\ReflectionParameter $parameter, string $name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 68 |  |         $type = $this->getType($parameter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 68 |  |         $isOptional = $parameter->isOptional(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 68 |  |         $this->isDefaultAvailable = $parameter->isDefaultValueAvailable() || $isOptional; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 68 |  |         if ($isOptional) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 12 |  |             $this->default = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 68 |  |         $this->setDefaultValue($parameter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 68 |  |         $this->index = $type . '-' . $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 68 |  |         $this->reflection = $parameter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 68 |  |         $this->meta = sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 68 |  |             "dependency '%s' with name '%s' used in %s:%d ($%s)", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 68 |  |             $type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 68 |  |             $name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 68 |  |             $this->reflection->getDeclaringFunction()->getFileName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 68 |  |             $this->reflection->getDeclaringFunction()->getStartLine(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 68 |  |             $parameter->getName() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 68 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 41 |  |     public function __toString() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 41 |  |         return $this->index; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * Return reflection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 39 |  |     public function get() : \ReflectionParameter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 39 |  |         return $this->reflection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 13 |  |     public function isDefaultAvailable() : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 13 |  |         return $this->isDefaultAvailable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 7 |  |     public function getDefaultValue() | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 7 |  |         return $this->default; | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 7 |  |     public function getMeta() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 7 |  |         return $this->meta; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 3 |  |     public function serialize() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $ref = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 3 |  |             $this->reflection->getDeclaringFunction()->class, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 3 |  |             $this->reflection->getDeclaringFunction()->name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 3 |  |             $this->reflection->getName() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 3 |  |         return \serialize([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 3 |  |             $this->index, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 3 |  |             $this->isDefaultAvailable, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 3 |  |             $this->default, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 3 |  |             $this->meta, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 3 |  |             $ref | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 4 |  |     public function unserialize($serialized) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 4 |  |         list($this->index, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 4 |  |             $this->isDefaultAvailable, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 4 |  |             $this->default, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 4 |  |             $this->meta, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             $ref | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 4 |  |         ) = unserialize($serialized); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 4 |  |         $this->reflection = new \ReflectionParameter([$ref[0], $ref[1]], $ref[2]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 68 |  |     private function setDefaultValue(\ReflectionParameter $parameter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 68 |  |         if (! $this->isDefaultAvailable) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 66 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 12 |  |             $this->default = $parameter->getDefaultValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 1 |  |         } /* @noinspection PhpRedundantCatchClauseInspection */ catch (\ReflectionException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             // probably it is internal class like \PDO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  |             $this->default = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 12 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 68 |  |     private function getType(\ReflectionParameter $parameter) : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 68 |  |         $type = $parameter->getType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 68 |  |         if ($type instanceof \ReflectionType && \in_array((string) $type, ['bool', 'int', 'string', 'array', 'resource', 'callable'], true)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 3 |  |             return ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 68 |  |         return (string) $type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 146 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |  |