Conditions | 17 |
Paths | 63 |
Total Lines | 109 |
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 |
||
101 | public function appendFormFieldElement(AdminInterface $admin, $subject, $elementId) |
||
102 | { |
||
103 | // child rows marked as toDelete |
||
104 | $toDelete = []; |
||
105 | // retrieve the subject |
||
106 | $formBuilder = $admin->getFormBuilder(); |
||
107 | |||
108 | // get the field element |
||
109 | $childFormBuilder = $this->getChildFormBuilder($formBuilder, $elementId); |
||
110 | |||
111 | if ($childFormBuilder) { |
||
112 | $formData = $admin->getRequest()->get($formBuilder->getName(), []); |
||
113 | if (array_key_exists($childFormBuilder->getName(), $formData)) { |
||
114 | $formData = $admin->getRequest()->get($formBuilder->getName(), []); |
||
115 | $i = 0; |
||
116 | foreach ($formData[$childFormBuilder->getName()] as $name => &$field) { |
||
117 | $toDelete[$i] = false; |
||
118 | if (array_key_exists('_delete', $field)) { |
||
119 | $toDelete[$i] = true; |
||
120 | unset($field['_delete']); |
||
121 | } |
||
122 | ++$i; |
||
123 | } |
||
124 | } |
||
125 | $admin->getRequest()->request->set($formBuilder->getName(), $formData); |
||
126 | } |
||
127 | |||
128 | $form = $formBuilder->getForm(); |
||
129 | $form->setData($subject); |
||
130 | $form->handleRequest($admin->getRequest()); |
||
131 | |||
132 | //Child form not found (probably nested one) |
||
133 | //if childFormBuilder was not found resulted in fatal error getName() method call on non object |
||
134 | if (!$childFormBuilder) { |
||
135 | $propertyAccessor = $this->pool->getPropertyAccessor(); |
||
136 | $entity = $admin->getSubject(); |
||
137 | |||
138 | $path = $this->getElementAccessPath($elementId, $entity); |
||
139 | |||
140 | $collection = $propertyAccessor->getValue($entity, $path); |
||
141 | |||
142 | if ($collection instanceof DoctrinePersistentCollection || $collection instanceof PersistentCollection) { |
||
143 | //since doctrine 2.4 |
||
144 | $entityClassName = $collection->getTypeClass()->getName(); |
||
145 | } elseif ($collection instanceof Collection) { |
||
146 | $entityClassName = $this->getEntityClassName($admin, explode('.', preg_replace('#\[\d*?\]#', '', $path))); |
||
147 | } else { |
||
148 | throw new \Exception('unknown collection class'); |
||
149 | } |
||
150 | |||
151 | $collection->add(new $entityClassName()); |
||
152 | $propertyAccessor->setValue($entity, $path, $collection); |
||
153 | |||
154 | $fieldDescription = null; |
||
155 | } else { |
||
156 | // retrieve the FieldDescription |
||
157 | $fieldDescription = $admin->getFormFieldDescription($childFormBuilder->getName()); |
||
158 | |||
159 | try { |
||
160 | $value = $fieldDescription->getValue($form->getData()); |
||
161 | } catch (NoValueException $e) { |
||
162 | $value = null; |
||
163 | } |
||
164 | |||
165 | // retrieve the posted data |
||
166 | $data = $admin->getRequest()->get($formBuilder->getName()); |
||
167 | |||
168 | if (!isset($data[$childFormBuilder->getName()])) { |
||
169 | $data[$childFormBuilder->getName()] = []; |
||
170 | } |
||
171 | |||
172 | $objectCount = null === $value ? 0 : \count($value); |
||
173 | $postCount = \count($data[$childFormBuilder->getName()]); |
||
174 | |||
175 | $fields = array_keys($fieldDescription->getAssociationAdmin()->getFormFieldDescriptions()); |
||
176 | |||
177 | // for now, not sure how to do that |
||
178 | $value = []; |
||
179 | foreach ($fields as $name) { |
||
180 | $value[$name] = ''; |
||
181 | } |
||
182 | |||
183 | // add new elements to the subject |
||
184 | while ($objectCount < $postCount) { |
||
185 | // append a new instance into the object |
||
186 | $this->addNewInstance($form->getData(), $fieldDescription); |
||
187 | ++$objectCount; |
||
188 | } |
||
189 | |||
190 | $this->addNewInstance($form->getData(), $fieldDescription); |
||
191 | } |
||
192 | |||
193 | $finalForm = $admin->getFormBuilder()->getForm(); |
||
194 | $finalForm->setData($subject); |
||
195 | |||
196 | // bind the data |
||
197 | $finalForm->setData($form->getData()); |
||
198 | |||
199 | // back up delete field |
||
200 | if (\count($toDelete) > 0) { |
||
201 | $i = 0; |
||
202 | foreach ($finalForm->get($childFormBuilder->getName()) as $childField) { |
||
203 | $childField->get('_delete')->setData(isset($toDelete[$i]) && $toDelete[$i]); |
||
204 | ++$i; |
||
205 | } |
||
206 | } |
||
207 | |||
208 | return [$fieldDescription, $finalForm]; |
||
209 | } |
||
210 | |||
328 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.