Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Configuration 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Configuration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Configuration implements ConfigurationInterface |
||
23 | { |
||
24 | /** @var bool */ |
||
25 | private $debug; |
||
26 | |||
27 | /** |
||
28 | * @param bool $debug Whether to use the debug mode |
||
29 | */ |
||
30 | public function __construct($debug) |
||
31 | { |
||
32 | $this->debug = (bool) $debug; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * {@inheritDoc} |
||
37 | */ |
||
38 | public function getConfigTreeBuilder() : TreeBuilder |
||
39 | { |
||
40 | $treeBuilder = new TreeBuilder('doctrine'); |
||
41 | $rootNode = $treeBuilder->getRootNode(); |
||
42 | |||
43 | $this->addDbalSection($rootNode); |
||
|
|||
44 | $this->addOrmSection($rootNode); |
||
45 | |||
46 | return $treeBuilder; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Add DBAL section to configuration tree |
||
51 | */ |
||
52 | private function addDbalSection(ArrayNodeDefinition $node) |
||
53 | { |
||
54 | $node |
||
55 | ->children() |
||
56 | ->arrayNode('dbal') |
||
57 | ->beforeNormalization() |
||
58 | ->ifTrue(static function ($v) { |
||
59 | return is_array($v) && ! array_key_exists('connections', $v) && ! array_key_exists('connection', $v); |
||
60 | }) |
||
61 | ->then(static function ($v) { |
||
62 | // Key that should not be rewritten to the connection config |
||
63 | $excludedKeys = ['default_connection' => true, 'types' => true, 'type' => true]; |
||
64 | $connection = []; |
||
65 | View Code Duplication | foreach ($v as $key => $value) { |
|
66 | if (isset($excludedKeys[$key])) { |
||
67 | continue; |
||
68 | } |
||
69 | $connection[$key] = $v[$key]; |
||
70 | unset($v[$key]); |
||
71 | } |
||
72 | $v['default_connection'] = isset($v['default_connection']) ? (string) $v['default_connection'] : 'default'; |
||
73 | $v['connections'] = [$v['default_connection'] => $connection]; |
||
74 | |||
75 | return $v; |
||
76 | }) |
||
77 | ->end() |
||
78 | ->children() |
||
79 | ->scalarNode('default_connection')->end() |
||
80 | ->end() |
||
81 | ->fixXmlConfig('type') |
||
82 | ->children() |
||
83 | ->arrayNode('types') |
||
84 | ->useAttributeAsKey('name') |
||
85 | ->prototype('array') |
||
86 | ->beforeNormalization() |
||
87 | ->ifString() |
||
88 | ->then(static function ($v) { |
||
89 | return ['class' => $v]; |
||
90 | }) |
||
91 | ->end() |
||
92 | ->children() |
||
93 | ->scalarNode('class')->isRequired()->end() |
||
94 | ->booleanNode('commented')->setDeprecated()->end() |
||
95 | ->end() |
||
96 | ->end() |
||
97 | ->end() |
||
98 | ->end() |
||
99 | ->fixXmlConfig('connection') |
||
100 | ->append($this->getDbalConnectionsNode()) |
||
101 | ->end(); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Return the dbal connections node |
||
106 | * |
||
107 | * @return ArrayNodeDefinition |
||
108 | */ |
||
109 | private function getDbalConnectionsNode() |
||
110 | { |
||
111 | $treeBuilder = new TreeBuilder('connections'); |
||
112 | $node = $treeBuilder->getRootNode(); |
||
113 | |||
114 | /** @var ArrayNodeDefinition $connectionNode */ |
||
115 | $connectionNode = $node |
||
116 | ->requiresAtLeastOneElement() |
||
117 | ->useAttributeAsKey('name') |
||
118 | ->prototype('array'); |
||
119 | |||
120 | $this->configureDbalDriverNode($connectionNode); |
||
121 | |||
122 | $connectionNode |
||
123 | ->fixXmlConfig('option') |
||
124 | ->fixXmlConfig('mapping_type') |
||
125 | ->fixXmlConfig('slave') |
||
126 | ->fixXmlConfig('shard') |
||
127 | ->fixXmlConfig('default_table_option') |
||
128 | ->children() |
||
129 | ->scalarNode('driver')->defaultValue('pdo_mysql')->end() |
||
130 | ->scalarNode('platform_service')->end() |
||
131 | ->booleanNode('auto_commit')->end() |
||
132 | ->scalarNode('schema_filter')->end() |
||
133 | ->booleanNode('logging')->defaultValue($this->debug)->end() |
||
134 | ->booleanNode('profiling')->defaultValue($this->debug)->end() |
||
135 | ->booleanNode('profiling_collect_backtrace') |
||
136 | ->defaultValue(false) |
||
137 | ->info('Enables collecting backtraces when profiling is enabled') |
||
138 | ->end() |
||
139 | ->scalarNode('server_version')->end() |
||
140 | ->scalarNode('driver_class')->end() |
||
141 | ->scalarNode('wrapper_class')->end() |
||
142 | ->scalarNode('shard_manager_class')->end() |
||
143 | ->scalarNode('shard_choser')->end() |
||
144 | ->scalarNode('shard_choser_service')->end() |
||
145 | ->booleanNode('keep_slave')->end() |
||
146 | ->arrayNode('options') |
||
147 | ->useAttributeAsKey('key') |
||
148 | ->prototype('scalar')->end() |
||
149 | ->end() |
||
150 | ->arrayNode('mapping_types') |
||
151 | ->useAttributeAsKey('name') |
||
152 | ->prototype('scalar')->end() |
||
153 | ->end() |
||
154 | ->arrayNode('default_table_options') |
||
155 | ->info("This option is used by the schema-tool and affects generated SQL. Possible keys include 'charset','collate', and 'engine'.") |
||
156 | ->useAttributeAsKey('name') |
||
157 | ->prototype('scalar')->end() |
||
158 | ->end() |
||
159 | ->end(); |
||
160 | |||
161 | $slaveNode = $connectionNode |
||
162 | ->children() |
||
163 | ->arrayNode('slaves') |
||
164 | ->useAttributeAsKey('name') |
||
165 | ->prototype('array'); |
||
166 | $this->configureDbalDriverNode($slaveNode); |
||
167 | |||
168 | $shardNode = $connectionNode |
||
169 | ->children() |
||
170 | ->arrayNode('shards') |
||
171 | ->prototype('array') |
||
172 | ->children() |
||
173 | ->integerNode('id') |
||
174 | ->min(1) |
||
175 | ->isRequired() |
||
176 | ->end() |
||
177 | ->end(); |
||
178 | $this->configureDbalDriverNode($shardNode); |
||
179 | |||
180 | return $node; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Adds config keys related to params processed by the DBAL drivers |
||
185 | * |
||
186 | * These keys are available for slave configurations too. |
||
187 | */ |
||
188 | private function configureDbalDriverNode(ArrayNodeDefinition $node) |
||
189 | { |
||
190 | $node |
||
191 | ->children() |
||
192 | ->scalarNode('url')->info('A URL with connection information; any parameter value parsed from this string will override explicitly set parameters')->end() |
||
193 | ->scalarNode('dbname')->end() |
||
194 | ->scalarNode('host')->defaultValue('localhost')->end() |
||
195 | ->scalarNode('port')->defaultNull()->end() |
||
196 | ->scalarNode('user')->defaultValue('root')->end() |
||
197 | ->scalarNode('password')->defaultNull()->end() |
||
198 | ->scalarNode('application_name')->end() |
||
199 | ->scalarNode('charset')->end() |
||
200 | ->scalarNode('path')->end() |
||
201 | ->booleanNode('memory')->end() |
||
202 | ->scalarNode('unix_socket')->info('The unix socket to use for MySQL')->end() |
||
203 | ->booleanNode('persistent')->info('True to use as persistent connection for the ibm_db2 driver')->end() |
||
204 | ->scalarNode('protocol')->info('The protocol to use for the ibm_db2 driver (default to TCPIP if ommited)')->end() |
||
205 | ->booleanNode('service') |
||
206 | ->info('True to use SERVICE_NAME as connection parameter instead of SID for Oracle') |
||
207 | ->end() |
||
208 | ->scalarNode('servicename') |
||
209 | ->info( |
||
210 | 'Overrules dbname parameter if given and used as SERVICE_NAME or SID connection parameter ' . |
||
211 | 'for Oracle depending on the service parameter.' |
||
212 | ) |
||
213 | ->end() |
||
214 | ->scalarNode('sessionMode') |
||
215 | ->info('The session mode to use for the oci8 driver') |
||
216 | ->end() |
||
217 | ->scalarNode('server') |
||
218 | ->info('The name of a running database server to connect to for SQL Anywhere.') |
||
219 | ->end() |
||
220 | ->scalarNode('default_dbname') |
||
221 | ->info( |
||
222 | 'Override the default database (postgres) to connect to for PostgreSQL connexion.' |
||
223 | ) |
||
224 | ->end() |
||
225 | ->scalarNode('sslmode') |
||
226 | ->info( |
||
227 | 'Determines whether or with what priority a SSL TCP/IP connection will be negotiated with ' . |
||
228 | 'the server for PostgreSQL.' |
||
229 | ) |
||
230 | ->end() |
||
231 | ->scalarNode('sslrootcert') |
||
232 | ->info( |
||
233 | 'The name of a file containing SSL certificate authority (CA) certificate(s). ' . |
||
234 | 'If the file exists, the server\'s certificate will be verified to be signed by one of these authorities.' |
||
235 | ) |
||
236 | ->end() |
||
237 | ->scalarNode('sslcert') |
||
238 | ->info( |
||
239 | 'The path to the SSL client certificate file for PostgreSQL.' |
||
240 | ) |
||
241 | ->end() |
||
242 | ->scalarNode('sslkey') |
||
243 | ->info( |
||
244 | 'The path to the SSL client key file for PostgreSQL.' |
||
245 | ) |
||
246 | ->end() |
||
247 | ->scalarNode('sslcrl') |
||
248 | ->info( |
||
249 | 'The file name of the SSL certificate revocation list for PostgreSQL.' |
||
250 | ) |
||
251 | ->end() |
||
252 | ->booleanNode('pooled')->info('True to use a pooled server with the oci8/pdo_oracle driver')->end() |
||
253 | ->booleanNode('MultipleActiveResultSets')->info('Configuring MultipleActiveResultSets for the pdo_sqlsrv driver')->end() |
||
254 | ->booleanNode('use_savepoints')->info('Use savepoints for nested transactions')->end() |
||
255 | ->scalarNode('instancename') |
||
256 | ->info( |
||
257 | 'Optional parameter, complete whether to add the INSTANCE_NAME parameter in the connection.' . |
||
258 | ' It is generally used to connect to an Oracle RAC server to select the name' . |
||
259 | ' of a particular instance.' |
||
260 | ) |
||
261 | ->end() |
||
262 | ->scalarNode('connectstring') |
||
263 | ->info( |
||
264 | 'Complete Easy Connect connection descriptor, see https://docs.oracle.com/database/121/NETAG/naming.htm.' . |
||
265 | 'When using this option, you will still need to provide the user and password parameters, but the other ' . |
||
266 | 'parameters will no longer be used. Note that when using this parameter, the getHost and getPort methods' . |
||
267 | ' from Doctrine\DBAL\Connection will no longer function as expected.' |
||
268 | ) |
||
269 | ->end() |
||
270 | ->end() |
||
271 | ->beforeNormalization() |
||
272 | ->ifTrue(static function ($v) { |
||
273 | return ! isset($v['sessionMode']) && isset($v['session_mode']); |
||
274 | }) |
||
275 | ->then(static function ($v) { |
||
276 | $v['sessionMode'] = $v['session_mode']; |
||
277 | unset($v['session_mode']); |
||
278 | |||
279 | return $v; |
||
280 | }) |
||
281 | ->end() |
||
282 | ->beforeNormalization() |
||
283 | ->ifTrue(static function ($v) { |
||
284 | return ! isset($v['MultipleActiveResultSets']) && isset($v['multiple_active_result_sets']); |
||
285 | }) |
||
286 | ->then(static function ($v) { |
||
287 | $v['MultipleActiveResultSets'] = $v['multiple_active_result_sets']; |
||
288 | unset($v['multiple_active_result_sets']); |
||
289 | |||
290 | return $v; |
||
291 | }) |
||
292 | ->end(); |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Add the ORM section to configuration tree |
||
297 | */ |
||
298 | private function addOrmSection(ArrayNodeDefinition $node) |
||
299 | { |
||
300 | $node |
||
301 | ->children() |
||
302 | ->arrayNode('orm') |
||
303 | ->beforeNormalization() |
||
304 | ->ifTrue(static function ($v) { |
||
305 | if (! empty($v) && ! class_exists(EntityManager::class)) { |
||
306 | throw new LogicException('The doctrine/orm package is required when the doctrine.orm config is set.'); |
||
307 | } |
||
308 | |||
309 | return $v === null || (is_array($v) && ! array_key_exists('entity_managers', $v) && ! array_key_exists('entity_manager', $v)); |
||
310 | }) |
||
311 | ->then(static function ($v) { |
||
312 | $v = (array) $v; |
||
313 | // Key that should not be rewritten to the connection config |
||
314 | $excludedKeys = [ |
||
315 | 'default_entity_manager' => true, |
||
316 | 'auto_generate_proxy_classes' => true, |
||
317 | 'proxy_dir' => true, |
||
318 | 'proxy_namespace' => true, |
||
319 | 'resolve_target_entities' => true, |
||
320 | 'resolve_target_entity' => true, |
||
321 | ]; |
||
322 | $entityManager = []; |
||
323 | View Code Duplication | foreach ($v as $key => $value) { |
|
324 | if (isset($excludedKeys[$key])) { |
||
325 | continue; |
||
326 | } |
||
327 | $entityManager[$key] = $v[$key]; |
||
328 | unset($v[$key]); |
||
329 | } |
||
330 | $v['default_entity_manager'] = isset($v['default_entity_manager']) ? (string) $v['default_entity_manager'] : 'default'; |
||
331 | $v['entity_managers'] = [$v['default_entity_manager'] => $entityManager]; |
||
332 | |||
333 | return $v; |
||
334 | }) |
||
335 | ->end() |
||
336 | ->children() |
||
337 | ->scalarNode('default_entity_manager')->end() |
||
338 | ->scalarNode('auto_generate_proxy_classes')->defaultValue(false) |
||
339 | ->info('Auto generate mode possible values are: "NEVER", "ALWAYS", "FILE_NOT_EXISTS", "EVAL"') |
||
340 | ->validate() |
||
341 | ->ifTrue(function ($v) { |
||
342 | $generationModes = $this->getAutoGenerateModes(); |
||
343 | |||
344 | if (is_int($v) && in_array($v, $generationModes['values']/*array(0, 1, 2, 3)*/)) { |
||
345 | return false; |
||
346 | } |
||
347 | if (is_bool($v)) { |
||
348 | return false; |
||
349 | } |
||
350 | if (is_string($v)) { |
||
351 | if (in_array(strtoupper($v), $generationModes['names']/*array('NEVER', 'ALWAYS', 'FILE_NOT_EXISTS', 'EVAL')*/)) { |
||
352 | return false; |
||
353 | } |
||
354 | } |
||
355 | |||
356 | return true; |
||
357 | }) |
||
358 | ->thenInvalid('Invalid auto generate mode value %s') |
||
359 | ->end() |
||
360 | ->validate() |
||
361 | ->ifString() |
||
362 | ->then(static function ($v) { |
||
363 | return constant('Doctrine\Common\Proxy\AbstractProxyFactory::AUTOGENERATE_' . strtoupper($v)); |
||
364 | }) |
||
365 | ->end() |
||
366 | ->end() |
||
367 | ->scalarNode('proxy_dir')->defaultValue('%kernel.cache_dir%/doctrine/orm/Proxies')->end() |
||
368 | ->scalarNode('proxy_namespace')->defaultValue('Proxies')->end() |
||
369 | ->end() |
||
370 | ->fixXmlConfig('entity_manager') |
||
371 | ->append($this->getOrmEntityManagersNode()) |
||
372 | ->fixXmlConfig('resolve_target_entity', 'resolve_target_entities') |
||
373 | ->append($this->getOrmTargetEntityResolverNode()) |
||
374 | ->end() |
||
375 | ->end(); |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Return ORM target entity resolver node |
||
380 | * |
||
381 | * @return NodeDefinition |
||
382 | */ |
||
383 | private function getOrmTargetEntityResolverNode() |
||
384 | { |
||
385 | $treeBuilder = new TreeBuilder('resolve_target_entities'); |
||
386 | $node = $treeBuilder->getRootNode(); |
||
387 | |||
388 | $node |
||
389 | ->useAttributeAsKey('interface') |
||
390 | ->prototype('scalar') |
||
391 | ->cannotBeEmpty() |
||
392 | ->end(); |
||
393 | |||
394 | return $node; |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Return ORM entity listener node |
||
399 | * |
||
400 | * @return NodeDefinition |
||
401 | */ |
||
402 | private function getOrmEntityListenersNode() |
||
403 | { |
||
404 | $treeBuilder = new TreeBuilder('entity_listeners'); |
||
405 | $node = $treeBuilder->getRootNode(); |
||
406 | |||
407 | $normalizer = static function ($mappings) { |
||
408 | $entities = []; |
||
409 | |||
410 | foreach ($mappings as $entityClass => $mapping) { |
||
411 | $listeners = []; |
||
412 | |||
413 | foreach ($mapping as $listenerClass => $listenerEvent) { |
||
414 | $events = []; |
||
415 | |||
416 | foreach ($listenerEvent as $eventType => $eventMapping) { |
||
417 | if ($eventMapping === null) { |
||
418 | $eventMapping = [null]; |
||
419 | } |
||
420 | |||
421 | foreach ($eventMapping as $method) { |
||
422 | $events[] = [ |
||
423 | 'type' => $eventType, |
||
424 | 'method' => $method, |
||
425 | ]; |
||
426 | } |
||
427 | } |
||
428 | |||
429 | $listeners[] = [ |
||
430 | 'class' => $listenerClass, |
||
431 | 'event' => $events, |
||
432 | ]; |
||
433 | } |
||
434 | |||
435 | $entities[] = [ |
||
436 | 'class' => $entityClass, |
||
437 | 'listener' => $listeners, |
||
438 | ]; |
||
439 | } |
||
440 | |||
441 | return ['entities' => $entities]; |
||
442 | }; |
||
443 | |||
444 | $node |
||
445 | ->beforeNormalization() |
||
446 | // Yaml normalization |
||
447 | ->ifTrue(static function ($v) { |
||
448 | return is_array(reset($v)) && is_string(key(reset($v))); |
||
449 | }) |
||
450 | ->then($normalizer) |
||
451 | ->end() |
||
452 | ->fixXmlConfig('entity', 'entities') |
||
453 | ->children() |
||
454 | ->arrayNode('entities') |
||
455 | ->useAttributeAsKey('class') |
||
456 | ->prototype('array') |
||
457 | ->fixXmlConfig('listener') |
||
458 | ->children() |
||
459 | ->arrayNode('listeners') |
||
460 | ->useAttributeAsKey('class') |
||
461 | ->prototype('array') |
||
462 | ->fixXmlConfig('event') |
||
463 | ->children() |
||
464 | ->arrayNode('events') |
||
465 | ->prototype('array') |
||
466 | ->children() |
||
467 | ->scalarNode('type')->end() |
||
468 | ->scalarNode('method')->defaultNull()->end() |
||
469 | ->end() |
||
470 | ->end() |
||
471 | ->end() |
||
472 | ->end() |
||
473 | ->end() |
||
474 | ->end() |
||
475 | ->end() |
||
476 | ->end() |
||
477 | ->end() |
||
478 | ->end(); |
||
479 | |||
480 | return $node; |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * Return ORM entity manager node |
||
485 | * |
||
486 | * @return ArrayNodeDefinition |
||
487 | */ |
||
488 | private function getOrmEntityManagersNode() |
||
489 | { |
||
490 | $treeBuilder = new TreeBuilder('entity_managers'); |
||
491 | $node = $treeBuilder->getRootNode(); |
||
492 | |||
493 | $node |
||
494 | ->requiresAtLeastOneElement() |
||
495 | ->useAttributeAsKey('name') |
||
496 | ->prototype('array') |
||
497 | ->addDefaultsIfNotSet() |
||
498 | ->append($this->getOrmCacheDriverNode('query_cache_driver')) |
||
499 | ->append($this->getOrmCacheDriverNode('metadata_cache_driver')) |
||
500 | ->append($this->getOrmCacheDriverNode('result_cache_driver')) |
||
501 | ->append($this->getOrmEntityListenersNode()) |
||
502 | ->children() |
||
503 | ->scalarNode('connection')->end() |
||
504 | ->scalarNode('class_metadata_factory_name')->defaultValue('Doctrine\ORM\Mapping\ClassMetadataFactory')->end() |
||
505 | ->scalarNode('default_repository_class')->defaultValue('Doctrine\ORM\EntityRepository')->end() |
||
506 | ->scalarNode('auto_mapping')->defaultFalse()->end() |
||
507 | ->scalarNode('naming_strategy')->defaultValue('doctrine.orm.naming_strategy.default')->end() |
||
508 | ->scalarNode('quote_strategy')->defaultValue('doctrine.orm.quote_strategy.default')->end() |
||
509 | ->scalarNode('entity_listener_resolver')->defaultNull()->end() |
||
510 | ->scalarNode('repository_factory')->defaultValue('doctrine.orm.container_repository_factory')->end() |
||
511 | ->end() |
||
512 | ->children() |
||
513 | ->arrayNode('second_level_cache') |
||
514 | ->children() |
||
515 | ->append($this->getOrmCacheDriverNode('region_cache_driver')) |
||
516 | ->scalarNode('region_lock_lifetime')->defaultValue(60)->end() |
||
517 | ->booleanNode('log_enabled')->defaultValue($this->debug)->end() |
||
518 | ->scalarNode('region_lifetime')->defaultValue(0)->end() |
||
519 | ->booleanNode('enabled')->defaultValue(true)->end() |
||
520 | ->scalarNode('factory')->end() |
||
521 | ->end() |
||
522 | ->fixXmlConfig('region') |
||
523 | ->children() |
||
524 | ->arrayNode('regions') |
||
525 | ->useAttributeAsKey('name') |
||
526 | ->prototype('array') |
||
527 | ->children() |
||
528 | ->append($this->getOrmCacheDriverNode('cache_driver')) |
||
529 | ->scalarNode('lock_path')->defaultValue('%kernel.cache_dir%/doctrine/orm/slc/filelock')->end() |
||
530 | ->scalarNode('lock_lifetime')->defaultValue(60)->end() |
||
531 | ->scalarNode('type')->defaultValue('default')->end() |
||
532 | ->scalarNode('lifetime')->defaultValue(0)->end() |
||
533 | ->scalarNode('service')->end() |
||
534 | ->scalarNode('name')->end() |
||
535 | ->end() |
||
536 | ->end() |
||
537 | ->end() |
||
538 | ->end() |
||
539 | ->fixXmlConfig('logger') |
||
540 | ->children() |
||
541 | ->arrayNode('loggers') |
||
542 | ->useAttributeAsKey('name') |
||
543 | ->prototype('array') |
||
544 | ->children() |
||
545 | ->scalarNode('name')->end() |
||
546 | ->scalarNode('service')->end() |
||
547 | ->end() |
||
548 | ->end() |
||
549 | ->end() |
||
550 | ->end() |
||
551 | ->end() |
||
552 | ->end() |
||
553 | ->fixXmlConfig('hydrator') |
||
554 | ->children() |
||
555 | ->arrayNode('hydrators') |
||
556 | ->useAttributeAsKey('name') |
||
557 | ->prototype('scalar')->end() |
||
558 | ->end() |
||
559 | ->end() |
||
560 | ->fixXmlConfig('mapping') |
||
561 | ->children() |
||
562 | ->arrayNode('mappings') |
||
563 | ->useAttributeAsKey('name') |
||
564 | ->prototype('array') |
||
565 | ->beforeNormalization() |
||
566 | ->ifString() |
||
567 | ->then(static function ($v) { |
||
568 | return ['type' => $v]; |
||
569 | }) |
||
570 | ->end() |
||
571 | ->treatNullLike([]) |
||
572 | ->treatFalseLike(['mapping' => false]) |
||
573 | ->performNoDeepMerging() |
||
574 | ->children() |
||
575 | ->scalarNode('mapping')->defaultValue(true)->end() |
||
576 | ->scalarNode('type')->end() |
||
577 | ->scalarNode('dir')->end() |
||
578 | ->scalarNode('alias')->end() |
||
579 | ->scalarNode('prefix')->end() |
||
580 | ->booleanNode('is_bundle')->end() |
||
581 | ->end() |
||
582 | ->end() |
||
583 | ->end() |
||
584 | ->arrayNode('dql') |
||
585 | ->fixXmlConfig('string_function') |
||
586 | ->fixXmlConfig('numeric_function') |
||
587 | ->fixXmlConfig('datetime_function') |
||
588 | ->children() |
||
589 | ->arrayNode('string_functions') |
||
590 | ->useAttributeAsKey('name') |
||
591 | ->prototype('scalar')->end() |
||
592 | ->end() |
||
593 | ->arrayNode('numeric_functions') |
||
594 | ->useAttributeAsKey('name') |
||
595 | ->prototype('scalar')->end() |
||
596 | ->end() |
||
597 | ->arrayNode('datetime_functions') |
||
598 | ->useAttributeAsKey('name') |
||
599 | ->prototype('scalar')->end() |
||
600 | ->end() |
||
601 | ->end() |
||
602 | ->end() |
||
603 | ->end() |
||
604 | ->fixXmlConfig('filter') |
||
605 | ->children() |
||
606 | ->arrayNode('filters') |
||
607 | ->info('Register SQL Filters in the entity manager') |
||
608 | ->useAttributeAsKey('name') |
||
609 | ->prototype('array') |
||
610 | ->beforeNormalization() |
||
611 | ->ifString() |
||
612 | ->then(static function ($v) { |
||
613 | return ['class' => $v]; |
||
614 | }) |
||
615 | ->end() |
||
616 | ->beforeNormalization() |
||
617 | // The content of the XML node is returned as the "value" key so we need to rename it |
||
618 | ->ifTrue(static function ($v) { |
||
619 | return is_array($v) && isset($v['value']); |
||
620 | }) |
||
621 | ->then(static function ($v) { |
||
622 | $v['class'] = $v['value']; |
||
623 | unset($v['value']); |
||
624 | |||
625 | return $v; |
||
626 | }) |
||
627 | ->end() |
||
628 | ->fixXmlConfig('parameter') |
||
629 | ->children() |
||
630 | ->scalarNode('class')->isRequired()->end() |
||
631 | ->booleanNode('enabled')->defaultFalse()->end() |
||
632 | ->arrayNode('parameters') |
||
633 | ->useAttributeAsKey('name') |
||
634 | ->prototype('variable')->end() |
||
635 | ->end() |
||
636 | ->end() |
||
637 | ->end() |
||
638 | ->end() |
||
639 | ->end() |
||
640 | ->end(); |
||
641 | |||
642 | return $node; |
||
643 | } |
||
644 | |||
645 | /** |
||
646 | * Return a ORM cache driver node for an given entity manager |
||
647 | * |
||
648 | * @param string $name |
||
649 | * |
||
650 | * @return ArrayNodeDefinition |
||
651 | */ |
||
652 | private function getOrmCacheDriverNode($name) |
||
653 | { |
||
654 | $treeBuilder = new TreeBuilder($name); |
||
655 | $node = $treeBuilder->getRootNode(); |
||
656 | |||
657 | $node |
||
658 | ->addDefaultsIfNotSet() |
||
659 | ->beforeNormalization() |
||
660 | ->ifString() |
||
661 | ->then(static function ($v) : array { |
||
662 | return ['type' => $v]; |
||
663 | }) |
||
664 | ->end() |
||
665 | ->children() |
||
666 | ->scalarNode('type')->defaultNull()->end() |
||
667 | ->scalarNode('id')->end() |
||
668 | ->scalarNode('pool')->end() |
||
669 | ->end(); |
||
670 | |||
671 | return $node; |
||
672 | } |
||
673 | |||
674 | /** |
||
675 | * Find proxy auto generate modes for their names and int values |
||
676 | * |
||
677 | * @return array |
||
678 | */ |
||
679 | private function getAutoGenerateModes() |
||
702 | } |
||
703 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.