Conditions | 19 |
Paths | 3 |
Total Lines | 177 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
47 | public function register(ContainerInterface $container) |
||
48 | { |
||
49 | if (!isset($container['dbs'])) { |
||
50 | throw new \LogicException( |
||
51 | 'You must register the DoctrineServiceProvider to use the DoctrineOrmServiceProvider.' |
||
52 | ); |
||
53 | } |
||
54 | |||
55 | if (!isset($container['caches'])) { |
||
56 | throw new \LogicException( |
||
57 | 'You must register the DoctrineCacheServiceProvider to use the DoctrineOrmServiceProvider.' |
||
58 | ); |
||
59 | } |
||
60 | |||
61 | // 初始值 |
||
62 | $container->add($this->getOrmDefaults()); |
||
63 | |||
64 | $container['ems.options.initializer'] = $container->protect(function () use ($container) { |
||
65 | static $initialized = false; |
||
66 | |||
67 | if ($initialized) { |
||
68 | return; |
||
69 | } |
||
70 | |||
71 | $initialized = true; |
||
72 | |||
73 | if (!isset($container['ems.options'])) { |
||
74 | $container['ems.options'] = [ |
||
75 | 'default' => $container['orm.options'] ?? [], |
||
76 | ]; |
||
77 | } |
||
78 | |||
79 | $container['ems.options'] = array_map(function ($options) use ($container) { |
||
80 | return array_replace($container['orm.default_options'], $options); |
||
81 | }, $container['ems.options']); |
||
82 | |||
83 | if (!isset($container['ems.default'])) { |
||
84 | $container['ems.default'] = array_keys( |
||
85 | array_slice($container['ems.options'], 0, 1) |
||
86 | )[0]; |
||
87 | } |
||
88 | }); |
||
89 | |||
90 | $container['ems'] = function (Container $container) { |
||
91 | $container['ems.options.initializer'](); |
||
92 | |||
93 | $ems = new Container(); |
||
94 | foreach ($container['ems.options'] as $name => $options) { |
||
95 | $config = $container['ems.default'] === $name |
||
96 | ? $container['orm.config'] |
||
97 | : $container['ems.config'][$name]; |
||
98 | |||
99 | $connection = $container['dbs'][$options['connection']]; |
||
100 | $manager = $container['dbs.event_manager'][$options['connection']]; |
||
101 | |||
102 | if ($targetEntities = $options['resolve_target_entities'] ?? []) { |
||
103 | $manager->addEventSubscriber( |
||
104 | $container['orm.resolve_target_entity']($targetEntities) |
||
105 | ); |
||
106 | } |
||
107 | |||
108 | $ems[$name] = function () use ($connection, $config, $manager) { |
||
109 | return EntityManager::create($connection, $config, $manager); |
||
110 | }; |
||
111 | } |
||
112 | |||
113 | return $ems; |
||
114 | }; |
||
115 | |||
116 | $container['ems.config'] = function (Container $container) { |
||
117 | $container['ems.options.initializer'](); |
||
118 | |||
119 | $configs = new Container(); |
||
120 | foreach ($container['ems.options'] as $name => $options) { |
||
121 | $config = new Configuration(); |
||
122 | $config->setProxyDir($container['orm.proxy_dir']); |
||
123 | $config->setProxyNamespace($container['orm.proxy_namespace']); |
||
124 | $config->setAutoGenerateProxyClasses($container['orm.auto_generate_proxy_classes']); |
||
125 | $config->setCustomStringFunctions($container['orm.custom_functions_string']); |
||
126 | $config->setCustomNumericFunctions($container['orm.custom_functions_numeric']); |
||
127 | $config->setCustomDatetimeFunctions($container['orm.custom_functions_datetime']); |
||
128 | $config->setMetadataCacheImpl($container['orm.cache.factory']('metadata', $options)); |
||
129 | $config->setQueryCacheImpl($container['orm.cache.factory']('query', $options)); |
||
130 | $config->setResultCacheImpl($container['orm.cache.factory']('result', $options)); |
||
131 | $config->setMetadataDriverImpl($container['orm.mapping.chain']($config, $options['mappings'])); |
||
132 | |||
133 | $configs[$name] = $config; |
||
134 | } |
||
135 | |||
136 | return $configs; |
||
137 | }; |
||
138 | |||
139 | $container['orm.cache.factory'] = $container->protect(function ($type, $options) use ($container) { |
||
140 | $type = $type . '_cache_driver'; |
||
141 | |||
142 | $options[$type] = $options[$type] ?? 'array'; |
||
143 | |||
144 | if (!is_array($options[$type])) { |
||
145 | $options[$type] = [ |
||
146 | 'driver' => $options[$type], |
||
147 | ]; |
||
148 | } |
||
149 | |||
150 | $driver = $options[$type]['driver']; |
||
151 | $namespace = $options[$type]['namespace'] ?? null; |
||
152 | |||
153 | $cache = $container['cache_factory']($driver, $options); |
||
154 | $cache->setNamespace($namespace); |
||
155 | |||
156 | return $cache; |
||
157 | }); |
||
158 | |||
159 | $container['orm.mapping.chain'] = $container->protect(function (Configuration $config, array $mappings) { |
||
160 | $chain = new MappingDriverChain(); |
||
161 | |||
162 | foreach ($mappings as $mapping) { |
||
163 | if (!is_array($mapping)) { |
||
164 | throw new \InvalidArgumentException(); |
||
165 | } |
||
166 | |||
167 | $path = $mapping['path']; |
||
168 | $namespace = $mapping['namespace']; |
||
169 | |||
170 | switch ($mapping['type']) { |
||
171 | case 'annotation': |
||
172 | $annotationDriver = $config->newDefaultAnnotationDriver( |
||
173 | $path, |
||
174 | $mapping['use_simple_annotation_reader'] ?? true |
||
175 | ); |
||
176 | $chain->addDriver($annotationDriver, $namespace); |
||
177 | break; |
||
178 | case 'yml': |
||
179 | $chain->addDriver(new YamlDriver($path), $namespace); |
||
180 | break; |
||
181 | case 'simple_yml': |
||
182 | $driver = new SimplifiedYamlDriver([$path => $namespace]); |
||
183 | $chain->addDriver($driver, $namespace); |
||
184 | break; |
||
185 | case 'xml': |
||
186 | $chain->addDriver(new XmlDriver($path), $namespace); |
||
187 | break; |
||
188 | case 'simple_xml': |
||
189 | $driver = new SimplifiedXmlDriver([$path => $namespace]); |
||
190 | $chain->addDriver($driver, $namespace); |
||
191 | break; |
||
192 | default: |
||
193 | throw new \InvalidArgumentException(); |
||
194 | break; |
||
|
|||
195 | } |
||
196 | } |
||
197 | |||
198 | return $chain; |
||
199 | }); |
||
200 | |||
201 | $container['orm.resolve_target_entity'] = $container->protect(function (array $targetEntities) { |
||
202 | $rtel = new ResolveTargetEntityListener(); |
||
203 | |||
204 | foreach ($targetEntities as $originalEntity => $newEntity) { |
||
205 | $rtel->addResolveTargetEntity($originalEntity, $newEntity, []); |
||
206 | } |
||
207 | |||
208 | return $rtel; |
||
209 | }); |
||
210 | |||
211 | // shortcuts for the "first" ORM |
||
212 | $container['orm'] = function (Container $container) { |
||
213 | $ems = $container['ems']; |
||
214 | |||
215 | return $ems[$container['ems.default']]; |
||
216 | }; |
||
217 | |||
218 | $container['orm.config'] = function (Container $container) { |
||
219 | $ems = $container['ems.config']; |
||
220 | |||
221 | return $ems[$container['ems.default']]; |
||
222 | }; |
||
223 | } |
||
224 | |||
240 | } |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.