Conditions | 20 |
Paths | 910 |
Total Lines | 114 |
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 |
||
81 | public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription): void |
||
82 | { |
||
83 | if ('_action' === $fieldDescription->getName() || 'actions' === $fieldDescription->getType()) { |
||
84 | $this->buildActionFieldDescription($fieldDescription); |
||
85 | } |
||
86 | |||
87 | $fieldDescription->setAdmin($admin); |
||
88 | $metadata = null; |
||
89 | |||
90 | if ($admin->getModelManager()->hasMetadata($admin->getClass())) { |
||
91 | /** @var ClassMetadata $metadata */ |
||
92 | $metadata = $admin->getModelManager()->getMetadata($admin->getClass()); |
||
93 | |||
94 | // TODO sort on parent associations or node name |
||
95 | $defaultSortable = true; |
||
96 | if ($metadata->hasAssociation($fieldDescription->getName()) |
||
97 | || $metadata->nodename === $fieldDescription->getName() |
||
98 | ) { |
||
99 | $defaultSortable = false; |
||
100 | } |
||
101 | |||
102 | // TODO get and set parent association mappings, see |
||
103 | // https://github.com/sonata-project/SonataDoctrinePhpcrAdminBundle/issues/106 |
||
104 | //$fieldDescription->setParentAssociationMappings($parentAssociationMappings); |
||
105 | |||
106 | // set the default field mapping |
||
107 | if (isset($metadata->mappings[$fieldDescription->getName()])) { |
||
108 | $fieldDescription->setFieldMapping($metadata->mappings[$fieldDescription->getName()]); |
||
109 | if (false !== $fieldDescription->getOption('sortable')) { |
||
110 | $fieldDescription->setOption( |
||
111 | 'sortable', |
||
112 | $fieldDescription->getOption('sortable', $defaultSortable) |
||
113 | ); |
||
114 | $fieldDescription->setOption( |
||
115 | 'sort_parent_association_mappings', |
||
116 | $fieldDescription->getOption( |
||
117 | 'sort_parent_association_mappings', |
||
118 | $fieldDescription->getParentAssociationMappings() |
||
119 | ) |
||
120 | ); |
||
121 | $fieldDescription->setOption( |
||
122 | 'sort_field_mapping', |
||
123 | $fieldDescription->getOption( |
||
124 | 'sort_field_mapping', |
||
125 | $fieldDescription->getFieldMapping() |
||
126 | ) |
||
127 | ); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | // set the default association mapping |
||
132 | if (isset($metadata->associationMappings[$fieldDescription->getName()])) { |
||
|
|||
133 | $fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]); |
||
134 | } |
||
135 | |||
136 | $fieldDescription->setOption( |
||
137 | '_sort_order', |
||
138 | $fieldDescription->getOption('_sort_order', 'ASC') |
||
139 | ); |
||
140 | } |
||
141 | |||
142 | if (!$fieldDescription->getType()) { |
||
143 | throw new \RuntimeException(sprintf( |
||
144 | 'Please define a type for field `%s` in `%s`', |
||
145 | $fieldDescription->getName(), |
||
146 | \get_class($admin) |
||
147 | )); |
||
148 | } |
||
149 | |||
150 | $fieldDescription->setOption( |
||
151 | 'code', |
||
152 | $fieldDescription->getOption('code', $fieldDescription->getName()) |
||
153 | ); |
||
154 | $fieldDescription->setOption( |
||
155 | 'label', |
||
156 | $fieldDescription->getOption('label', $fieldDescription->getName()) |
||
157 | ); |
||
158 | |||
159 | if (!$fieldDescription->getTemplate()) { |
||
160 | $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType())); |
||
161 | |||
162 | if (ClassMetadata::MANY_TO_ONE === $fieldDescription->getMappingType()) { |
||
163 | $fieldDescription->setTemplate('@SonataAdmin/CRUD/Association/list_many_to_one.html.twig'); |
||
164 | } |
||
165 | |||
166 | if (ClassMetadata::MANY_TO_MANY === $fieldDescription->getMappingType()) { |
||
167 | $fieldDescription->setTemplate('@SonataAdmin/CRUD/Association/list_many_to_many.html.twig'); |
||
168 | } |
||
169 | |||
170 | if ('child' === $fieldDescription->getMappingType() || 'parent' === $fieldDescription->getMappingType()) { |
||
171 | $fieldDescription->setTemplate('@SonataAdmin/CRUD/Association/list_one_to_one.html.twig'); |
||
172 | } |
||
173 | |||
174 | if ('children' === $fieldDescription->getMappingType() || 'referrers' === $fieldDescription->getMappingType()) { |
||
175 | $fieldDescription->setTemplate('@SonataAdmin/CRUD/Association/list_one_to_many.html.twig'); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | $mappingTypes = [ |
||
180 | ClassMetadata::MANY_TO_ONE, |
||
181 | ClassMetadata::MANY_TO_MANY, |
||
182 | 'children', |
||
183 | 'child', |
||
184 | 'parent', |
||
185 | 'referrers', |
||
186 | ]; |
||
187 | |||
188 | if ($metadata |
||
189 | && $metadata->hasAssociation($fieldDescription->getName()) |
||
190 | && \in_array($fieldDescription->getMappingType(), $mappingTypes, true) |
||
191 | ) { |
||
192 | $admin->attachAdminClass($fieldDescription); |
||
193 | } |
||
194 | } |
||
195 | |||
245 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.