Conditions | 20 |
Paths | 910 |
Total Lines | 80 |
Code Lines | 41 |
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 |
||
82 | public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription) |
||
83 | { |
||
84 | if ($fieldDescription->getName() == '_action' || $fieldDescription->getType() === 'actions') { |
||
85 | $this->buildActionFieldDescription($fieldDescription); |
||
86 | } |
||
87 | |||
88 | $fieldDescription->setAdmin($admin); |
||
89 | |||
90 | if ($admin->getModelManager()->hasMetadata($admin->getClass())) { |
||
91 | $metadata = $admin->getModelManager()->getMetadata($admin->getClass()); |
||
92 | |||
93 | // TODO sort on parent associations or node name |
||
94 | $defaultSortable = true; |
||
95 | if ($metadata->hasAssociation($fieldDescription->getName()) |
||
96 | || $metadata->nodename === $fieldDescription->getName() |
||
97 | ) { |
||
98 | $defaultSortable = false; |
||
99 | } |
||
100 | |||
101 | // TODO get and set parent association mappings, see |
||
102 | // https://github.com/sonata-project/SonataDoctrinePhpcrAdminBundle/issues/106 |
||
103 | //$fieldDescription->setParentAssociationMappings($parentAssociationMappings); |
||
104 | |||
105 | // set the default field mapping |
||
106 | if (isset($metadata->mappings[$fieldDescription->getName()])) { |
||
107 | $fieldDescription->setFieldMapping($metadata->mappings[$fieldDescription->getName()]); |
||
108 | if ($fieldDescription->getOption('sortable') !== false) { |
||
109 | $fieldDescription->setOption('sortable', $fieldDescription->getOption('sortable', $defaultSortable)); |
||
110 | $fieldDescription->setOption('sort_parent_association_mappings', $fieldDescription->getOption('sort_parent_association_mappings', $fieldDescription->getParentAssociationMappings())); |
||
111 | $fieldDescription->setOption('sort_field_mapping', $fieldDescription->getOption('sort_field_mapping', $fieldDescription->getFieldMapping())); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | // set the default association mapping |
||
116 | if (isset($metadata->associationMappings[$fieldDescription->getName()])) { |
||
117 | $fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]); |
||
118 | } |
||
119 | |||
120 | $fieldDescription->setOption('_sort_order', $fieldDescription->getOption('_sort_order', 'ASC')); |
||
121 | } |
||
122 | |||
123 | if (!$fieldDescription->getType()) { |
||
124 | throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin))); |
||
125 | } |
||
126 | |||
127 | $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName())); |
||
128 | $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName())); |
||
129 | |||
130 | if (!$fieldDescription->getTemplate()) { |
||
131 | $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType())); |
||
132 | |||
133 | if ($fieldDescription->getMappingType() == ClassMetadata::MANY_TO_ONE) { |
||
134 | $fieldDescription->setTemplate('SonataDoctrinePHPCRAdminBundle:CRUD:list_phpcr_many_to_one.html.twig'); |
||
135 | } |
||
136 | |||
137 | if ($fieldDescription->getMappingType() == ClassMetadata::MANY_TO_MANY) { |
||
138 | $fieldDescription->setTemplate('SonataDoctrinePHPCRAdminBundle:CRUD:list_phpcr_many_to_many.html.twig'); |
||
139 | } |
||
140 | |||
141 | if ($fieldDescription->getMappingType() == 'child' || $fieldDescription->getMappingType() == 'parent') { |
||
142 | $fieldDescription->setTemplate('SonataDoctrinePHPCRAdminBundle:CRUD:list_phpcr_one_to_one.html.twig'); |
||
143 | } |
||
144 | |||
145 | if ($fieldDescription->getMappingType() == 'children' || $fieldDescription->getMappingType() == 'referrers') { |
||
146 | $fieldDescription->setTemplate('SonataDoctrinePHPCRAdminBundle:CRUD:list_phpcr_one_to_many.html.twig'); |
||
147 | } |
||
148 | } |
||
149 | |||
150 | $mappingTypes = array( |
||
151 | ClassMetadata::MANY_TO_ONE, |
||
152 | ClassMetadata::MANY_TO_MANY, |
||
153 | 'children', |
||
154 | 'child', 'parent', |
||
155 | 'referrers', |
||
156 | ); |
||
157 | |||
158 | if ($metadata && $metadata->hasAssociation($fieldDescription->getName()) && in_array($fieldDescription->getMappingType(), $mappingTypes)) { |
||
|
|||
159 | $admin->attachAdminClass($fieldDescription); |
||
160 | } |
||
161 | } |
||
162 | |||
214 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: