| Conditions | 17 |
| Paths | 63 |
| Total Lines | 111 |
| 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 |
||
| 109 | public function appendFormFieldElement(AdminInterface $admin, $subject, $elementId) |
||
| 110 | { |
||
| 111 | // child rows marked as toDelete |
||
| 112 | $toDelete = []; |
||
| 113 | // retrieve the subject |
||
| 114 | $formBuilder = $admin->getFormBuilder(); |
||
| 115 | |||
| 116 | // get the field element |
||
| 117 | $childFormBuilder = $this->getChildFormBuilder($formBuilder, $elementId); |
||
| 118 | |||
| 119 | if ($childFormBuilder) { |
||
| 120 | $formData = $admin->getRequest()->get($formBuilder->getName(), []); |
||
| 121 | if (\array_key_exists($childFormBuilder->getName(), $formData)) { |
||
| 122 | $formData = $admin->getRequest()->get($formBuilder->getName(), []); |
||
| 123 | $i = 0; |
||
| 124 | foreach ($formData[$childFormBuilder->getName()] as $name => &$field) { |
||
| 125 | $toDelete[$i] = false; |
||
| 126 | if (\array_key_exists(self::FORM_FIELD_DELETE, $field)) { |
||
| 127 | $toDelete[$i] = true; |
||
| 128 | unset($field[self::FORM_FIELD_DELETE]); |
||
| 129 | } |
||
| 130 | ++$i; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | $admin->getRequest()->request->set($formBuilder->getName(), $formData); |
||
| 134 | } |
||
| 135 | |||
| 136 | $form = $formBuilder->getForm(); |
||
| 137 | $form->setData($subject); |
||
| 138 | $form->handleRequest($admin->getRequest()); |
||
| 139 | |||
| 140 | //Child form not found (probably nested one) |
||
| 141 | //if childFormBuilder was not found resulted in fatal error getName() method call on non object |
||
| 142 | if (!$childFormBuilder) { |
||
| 143 | $propertyAccessor = $this->pool->getPropertyAccessor(); |
||
| 144 | $entity = $admin->getSubject(); |
||
| 145 | |||
| 146 | $path = $this->getElementAccessPath($elementId, $entity); |
||
| 147 | |||
| 148 | $collection = $propertyAccessor->getValue($entity, $path); |
||
|
|
|||
| 149 | |||
| 150 | if ($collection instanceof DoctrinePersistentCollection || $collection instanceof PersistentCollection) { |
||
| 151 | //since doctrine 2.4 |
||
| 152 | $entityClassName = $collection->getTypeClass()->getName(); |
||
| 153 | } elseif ($collection instanceof Collection) { |
||
| 154 | $entityClassName = $this->getEntityClassName($admin, explode('.', preg_replace('#\[\d*?\]#', '', $path))); |
||
| 155 | } else { |
||
| 156 | throw new \Exception('unknown collection class'); |
||
| 157 | } |
||
| 158 | |||
| 159 | $collection->add(new $entityClassName()); |
||
| 160 | $propertyAccessor->setValue($entity, $path, $collection); |
||
| 161 | |||
| 162 | $fieldDescription = null; |
||
| 163 | } else { |
||
| 164 | // retrieve the FieldDescription |
||
| 165 | $fieldDescription = $admin->getFormFieldDescription($childFormBuilder->getName()); |
||
| 166 | |||
| 167 | try { |
||
| 168 | $value = $fieldDescription->getValue($form->getData()); |
||
| 169 | } catch (NoValueException $e) { |
||
| 170 | $value = null; |
||
| 171 | } |
||
| 172 | |||
| 173 | // retrieve the posted data |
||
| 174 | $data = $admin->getRequest()->get($formBuilder->getName()); |
||
| 175 | |||
| 176 | if (!isset($data[$childFormBuilder->getName()])) { |
||
| 177 | $data[$childFormBuilder->getName()] = []; |
||
| 178 | } |
||
| 179 | |||
| 180 | $objectCount = null === $value ? 0 : \count($value); |
||
| 181 | $postCount = \count($data[$childFormBuilder->getName()]); |
||
| 182 | |||
| 183 | $fields = array_keys($fieldDescription->getAssociationAdmin()->getFormFieldDescriptions()); |
||
| 184 | |||
| 185 | // for now, not sure how to do that |
||
| 186 | $value = []; |
||
| 187 | foreach ($fields as $name) { |
||
| 188 | $value[$name] = ''; |
||
| 189 | } |
||
| 190 | |||
| 191 | // add new elements to the subject |
||
| 192 | while ($objectCount < $postCount) { |
||
| 193 | // append a new instance into the object |
||
| 194 | $this->addNewInstance($form->getData(), $fieldDescription); |
||
| 195 | ++$objectCount; |
||
| 196 | } |
||
| 197 | |||
| 198 | $this->addNewInstance($form->getData(), $fieldDescription); |
||
| 199 | } |
||
| 200 | |||
| 201 | $finalForm = $admin->getFormBuilder()->getForm(); |
||
| 202 | $finalForm->setData($subject); |
||
| 203 | |||
| 204 | // bind the data |
||
| 205 | $finalForm->setData($form->getData()); |
||
| 206 | |||
| 207 | // back up delete field |
||
| 208 | if (\count($toDelete) > 0) { |
||
| 209 | $i = 0; |
||
| 210 | foreach ($finalForm->get($childFormBuilder->getName()) as $childField) { |
||
| 211 | if ($childField->has(self::FORM_FIELD_DELETE)) { |
||
| 212 | $childField->get(self::FORM_FIELD_DELETE)->setData($toDelete[$i] ?? false); |
||
| 213 | } |
||
| 214 | ++$i; |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | return [$fieldDescription, $finalForm]; |
||
| 219 | } |
||
| 220 | |||
| 357 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.