| Conditions | 8 |
| Paths | 1 |
| Total Lines | 90 |
| Code Lines | 53 |
| 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 |
||
| 34 | public function configureOptions(OptionsResolver $resolver) |
||
| 35 | { |
||
| 36 | parent::configureOptions($resolver); |
||
| 37 | |||
| 38 | $choiceLoader = function (Options $options) { |
||
| 39 | // Unless the choices are given explicitly, load them on demand |
||
| 40 | if (null === $options['data']) { |
||
| 41 | |||
| 42 | if (null !== $options['query_builder']) { |
||
| 43 | $entityLoader = $this->getLoader($options['em'], $options['query_builder'], $options['class']); |
||
| 44 | } else { |
||
| 45 | $queryBuilder = $options['em']->getRepository($options['class'])->createQueryBuilder('e'); |
||
| 46 | $entityLoader = $this->getLoader($options['em'], $queryBuilder, $options['class']); |
||
| 47 | } |
||
| 48 | |||
| 49 | $doctrineChoiceLoader = new DoctrineChoiceLoader( |
||
| 50 | $options['em'], |
||
| 51 | $options['class'], |
||
| 52 | $options['id_reader'], |
||
| 53 | $entityLoader |
||
| 54 | ); |
||
| 55 | |||
| 56 | return $doctrineChoiceLoader; |
||
| 57 | } |
||
| 58 | }; |
||
| 59 | |||
| 60 | $emNormalizer = function (Options $options, $em) { |
||
| 61 | /* @var ManagerRegistry $registry */ |
||
| 62 | if (null !== $em && $em instanceof ObjectManager) { |
||
| 63 | return $em; |
||
| 64 | } |
||
| 65 | |||
| 66 | if ($this->getOption('em') === null) { |
||
| 67 | throw new \RuntimeException(sprintf( |
||
| 68 | 'Object Manager not set'. |
||
| 69 | 'Did you forget to set it? "em"' |
||
| 70 | )); |
||
| 71 | } |
||
| 72 | |||
| 73 | $em = $this->getOption('em')->getManagerForClass($options['class']); |
||
| 74 | |||
| 75 | if (null === $em) { |
||
| 76 | throw new \RuntimeException(sprintf( |
||
| 77 | 'Class "%s" seems not to be a managed Doctrine entity. '. |
||
| 78 | 'Did you forget to map it?', |
||
| 79 | $options['class'] |
||
| 80 | )); |
||
| 81 | } |
||
| 82 | |||
| 83 | return $em; |
||
| 84 | }; |
||
| 85 | |||
| 86 | // Invoke the query builder closure so that we can cache choice lists |
||
| 87 | // for equal query builders |
||
| 88 | $queryBuilderNormalizer = function (Options $options, $queryBuilder) { |
||
| 89 | if (is_callable($queryBuilder)) { |
||
| 90 | $queryBuilder = call_user_func($queryBuilder, $options['em']->getRepository($options['class'])); |
||
| 91 | } |
||
| 92 | |||
| 93 | return $queryBuilder; |
||
| 94 | }; |
||
| 95 | |||
| 96 | // Set the "id_reader" option via the normalizer. This option is not |
||
| 97 | // supposed to be set by the user. |
||
| 98 | $idReaderNormalizer = function (Options $options) { |
||
| 99 | $classMetadata = $options['em']->getClassMetadata($options['class']); |
||
| 100 | return new IdReader($options['em'], $classMetadata); |
||
| 101 | }; |
||
| 102 | |||
| 103 | $resolver->setDefaults(array( |
||
| 104 | 'align' => 'left', |
||
| 105 | 'cellAlign' => 'left', |
||
| 106 | 'type' => 'combo', |
||
| 107 | 'data' => null, |
||
| 108 | 'class' => null, |
||
| 109 | 'query_builder' => null, |
||
| 110 | 'choice_loader' => $choiceLoader, |
||
| 111 | 'choice_label' => null, |
||
| 112 | 'id_reader' => null, |
||
| 113 | 'em' => null, |
||
| 114 | )); |
||
| 115 | $resolver->setRequired(['class']); |
||
| 116 | |||
| 117 | $resolver->setNormalizer('em', $emNormalizer); |
||
| 118 | $resolver->setNormalizer('query_builder', $queryBuilderNormalizer); |
||
| 119 | $resolver->setNormalizer('id_reader', $idReaderNormalizer); |
||
| 120 | |||
| 121 | $resolver->setAllowedTypes('attr', 'array'); |
||
| 122 | $resolver->setAllowedTypes('data', ['array', 'null']); |
||
| 123 | } |
||
| 124 | |||
| 197 |