1 | <?php |
||
17 | trait ServiceContainerTrait |
||
18 | { |
||
19 | use SingletonTrait; |
||
20 | |||
21 | |||
22 | /** |
||
23 | * Service class map |
||
24 | * |
||
25 | * These class can have static getInstance() method for reuse, see |
||
26 | * {@see SingleInstanceTrait}. |
||
27 | * |
||
28 | * For easy inherit and not conflict with user registered class, do not |
||
29 | * set value here, set them in {@see buildClassMap()}. |
||
30 | * |
||
31 | * @var string[] |
||
32 | */ |
||
33 | protected $classMap = []; |
||
34 | |||
35 | /** |
||
36 | * Service instances |
||
37 | * |
||
38 | * @var object[] |
||
39 | */ |
||
40 | protected $instances = []; |
||
41 | |||
42 | |||
43 | /** |
||
44 | * Constructor |
||
45 | */ |
||
46 | protected function __construct() |
||
50 | |||
51 | |||
52 | /** |
||
53 | * Build service class map |
||
54 | * |
||
55 | * @return string[] |
||
56 | */ |
||
57 | protected function buildClassMap() |
||
64 | |||
65 | |||
66 | /** |
||
67 | * Create service instance by name |
||
68 | * |
||
69 | * Will try below sequence to create instance: |
||
70 | * |
||
71 | * - createName() in this container |
||
72 | * - Name::getInstance() |
||
73 | * - new Name |
||
74 | * |
||
75 | * @param string $name |
||
76 | * @return object |
||
77 | * @throws ServiceInstanceCreationFailException |
||
78 | */ |
||
79 | protected function createService($name) |
||
108 | |||
109 | |||
110 | /** |
||
111 | * Get service instance by name |
||
112 | * |
||
113 | * The return type maybe variable, so this method is inner use only. Make |
||
114 | * getFoo() methods in client class to identify return type. |
||
115 | * |
||
116 | * When $forcenew is true, it will ignore exists service instance and |
||
117 | * create a new one-time use instance, which will not be stored in instance |
||
118 | * array. |
||
119 | * |
||
120 | * $forcenew does not affect instances create according to class map, |
||
121 | * because they use singleton or single instance pattern. |
||
122 | * |
||
123 | * @param string $name |
||
124 | * @param boolean $forcenew |
||
125 | * @return object |
||
126 | */ |
||
127 | protected function get($name, $forcenew = false) |
||
142 | |||
143 | |||
144 | /** |
||
145 | * @see ServiceContainerInterface::getRegistered() |
||
146 | * |
||
147 | * @param string $name |
||
148 | * @return object |
||
149 | */ |
||
150 | public function getRegistered($name) |
||
156 | |||
157 | |||
158 | /** |
||
159 | * @see ServiceContainerInterface::register() |
||
160 | * |
||
161 | * @param string $name |
||
162 | * @param string|object $service |
||
163 | * @return static |
||
164 | */ |
||
165 | public function register($name, $service) |
||
175 | |||
176 | |||
177 | /** |
||
178 | * @see ServiceContainerInterface::registerClass() |
||
179 | * |
||
180 | * @param string $name |
||
181 | * @param string $className Full qualified name without leading '\' |
||
182 | * @return static |
||
183 | */ |
||
184 | public function registerClass($name, $className) |
||
192 | |||
193 | |||
194 | /** |
||
195 | * @see ServiceContainerInterface::registerInstance() |
||
196 | * |
||
197 | * @param string $name |
||
198 | * @param object $instance |
||
199 | * @return static |
||
200 | */ |
||
201 | public function registerInstance($name, $instance) |
||
209 | |||
210 | |||
211 | /** |
||
212 | * Unify service name case style |
||
213 | * |
||
214 | * @param string $name |
||
215 | * @return string |
||
216 | */ |
||
217 | protected function unifyNameStyle($name) |
||
221 | } |
||
222 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.