| Conditions | 17 |
| Paths | 217 |
| Total Lines | 101 |
| Code Lines | 59 |
| Lines | 28 |
| Ratio | 27.72 % |
| Changes | 7 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 102 | public function actionCreate(AdminEvent $event) |
||
| 103 | { |
||
| 104 | // add configuration only if extra configuration is enabled |
||
| 105 | if (!$this->enableExtraConfiguration) { |
||
| 106 | return; |
||
| 107 | } |
||
| 108 | // action configuration array |
||
| 109 | $configuration = $event->getConfiguration(); |
||
| 110 | // current action admin |
||
| 111 | $admin = $event->getAdmin(); |
||
| 112 | // allowed actions according to the admin |
||
| 113 | $keys = $admin |
||
| 114 | ->getConfiguration() |
||
| 115 | ->getActions(); |
||
| 116 | $allowedActions = array_keys($keys); |
||
| 117 | |||
| 118 | // if no field was provided in configuration, we try to take fields from doctrine metadata |
||
| 119 | if (empty($configuration['fields']) || !count($configuration['fields'])) { |
||
| 120 | $fields = []; |
||
| 121 | $guesser = new FieldTypeGuesser(); |
||
| 122 | $metadata = $this |
||
| 123 | ->entityManager |
||
| 124 | ->getMetadataFactory() |
||
| 125 | ->getMetadataFor($admin->getConfiguration()->getEntityName()); |
||
| 126 | $fieldsName = $metadata->getFieldNames(); |
||
| 127 | |||
| 128 | foreach ($fieldsName as $name) { |
||
| 129 | $type = $metadata->getTypeOfField($name); |
||
| 130 | // get field type from doctrine type |
||
| 131 | $fieldConfiguration = $guesser->getTypeAndOptions($type); |
||
| 132 | |||
| 133 | // if a field configuration was found, we take it |
||
| 134 | if (count($fieldConfiguration)) { |
||
| 135 | $fields[$name] = $fieldConfiguration; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | if (count($fields)) { |
||
| 139 | // adding new fields to action configuration |
||
| 140 | $configuration['fields'] = $fields; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | // configured linked actions |
||
| 144 | if (array_key_exists('_actions', $configuration['fields']) |
||
| 145 | && !array_key_exists('type', $configuration['fields']['_actions']) |
||
| 146 | ) { |
||
| 147 | // in list view, we add by default and an edit and a delete button |
||
| 148 | if ($event->getActionName() == 'list') { |
||
| 149 | View Code Duplication | if (in_array('edit', $allowedActions)) { |
|
| 150 | $configuration['fields']['_actions']['type'] = 'collection'; |
||
| 151 | $configuration['fields']['_actions']['options']['_edit'] = [ |
||
| 152 | 'type' => 'action', |
||
| 153 | 'options' => [ |
||
| 154 | 'title' => $this->applicationConfiguration->getTranslationKey('edit', $event->getAdmin()->getName()), |
||
| 155 | 'route' => $admin->generateRouteName('edit'), |
||
| 156 | 'parameters' => [ |
||
| 157 | 'id' => false |
||
| 158 | ], |
||
| 159 | 'icon' => 'pencil' |
||
| 160 | ] |
||
| 161 | ]; |
||
| 162 | } |
||
| 163 | View Code Duplication | if (in_array('delete', $allowedActions)) { |
|
| 164 | $configuration['fields']['_actions']['type'] = 'collection'; |
||
| 165 | $configuration['fields']['_actions']['options']['_delete'] = [ |
||
| 166 | 'type' => 'action', |
||
| 167 | 'options' => [ |
||
| 168 | 'title' => $this->applicationConfiguration->getTranslationKey('delete', $event->getAdmin()->getName()), |
||
| 169 | 'route' => $admin->generateRouteName('delete'), |
||
| 170 | 'parameters' => [ |
||
| 171 | 'id' => false |
||
| 172 | ], |
||
| 173 | 'icon' => 'remove' |
||
| 174 | ] |
||
| 175 | ]; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | // add default menu actions if none was provided |
||
| 180 | if (empty($configuration['actions'])) { |
||
| 181 | // by default, in list action we add a create linked action |
||
| 182 | if ($event->getActionName() == 'list') { |
||
| 183 | if (in_array('create', $allowedActions)) { |
||
| 184 | $configuration['actions']['create'] = [ |
||
| 185 | 'title' => $this->applicationConfiguration->getTranslationKey('create', $event->getAdmin()->getName()), |
||
| 186 | 'route' => $admin->generateRouteName('create'), |
||
| 187 | 'icon' => 'pencil' |
||
| 188 | ]; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 192 | // for list action, add the delete batch action by defaut |
||
| 193 | if (empty($configuration['batch'])) { |
||
| 194 | if ($event->getActionName() == 'list') { |
||
| 195 | $configuration['batch'] = [ |
||
| 196 | 'delete' => null |
||
| 197 | ]; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | // reset action configuration |
||
| 201 | $event->setConfiguration($configuration); |
||
| 202 | } |
||
| 203 | } |
||
| 204 |
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.