| Conditions | 19 |
| Paths | 910 |
| Total Lines | 81 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 97 | public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription) |
||
| 98 | { |
||
| 99 | if ($fieldDescription->getName() == '_action') { |
||
| 100 | $this->buildActionFieldDescription($fieldDescription); |
||
| 101 | } |
||
| 102 | |||
| 103 | $fieldDescription->setAdmin($admin); |
||
| 104 | |||
| 105 | if ($admin->getModelManager()->hasMetadata($admin->getClass())) { |
||
| 106 | $metadata = $admin->getModelManager()->getMetadata($admin->getClass()); |
||
| 107 | |||
| 108 | // TODO sort on parent associations or node name |
||
| 109 | $defaultSortable = true; |
||
| 110 | if ($metadata->hasAssociation($fieldDescription->getName()) |
||
| 111 | || $metadata->nodename === $fieldDescription->getName() |
||
| 112 | ) { |
||
| 113 | $defaultSortable = false; |
||
| 114 | } |
||
| 115 | |||
| 116 | // TODO get and set parent association mappings, see |
||
| 117 | // https://github.com/sonata-project/SonataDoctrinePhpcrAdminBundle/issues/106 |
||
| 118 | //$fieldDescription->setParentAssociationMappings($parentAssociationMappings); |
||
| 119 | |||
| 120 | // set the default field mapping |
||
| 121 | if (isset($metadata->mappings[$fieldDescription->getName()])) { |
||
| 122 | $fieldDescription->setFieldMapping($metadata->mappings[$fieldDescription->getName()]); |
||
| 123 | if ($fieldDescription->getOption('sortable') !== false) { |
||
| 124 | $fieldDescription->setOption('sortable', $fieldDescription->getOption('sortable', $defaultSortable)); |
||
| 125 | $fieldDescription->setOption('sort_parent_association_mappings', $fieldDescription->getOption('sort_parent_association_mappings', $fieldDescription->getParentAssociationMappings())); |
||
| 126 | $fieldDescription->setOption('sort_field_mapping', $fieldDescription->getOption('sort_field_mapping', $fieldDescription->getFieldMapping())); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | // set the default association mapping |
||
| 131 | if (isset($metadata->associationMappings[$fieldDescription->getName()])) { |
||
| 132 | $fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]); |
||
| 133 | } |
||
| 134 | |||
| 135 | $fieldDescription->setOption('_sort_order', $fieldDescription->getOption('_sort_order', 'ASC')); |
||
| 136 | } |
||
| 137 | |||
| 138 | if (!$fieldDescription->getType()) { |
||
| 139 | throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin))); |
||
| 140 | } |
||
| 141 | |||
| 142 | $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName())); |
||
| 143 | $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName())); |
||
| 144 | |||
| 145 | if (!$fieldDescription->getTemplate()) { |
||
| 146 | |||
| 147 | $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType())); |
||
| 148 | |||
| 149 | if ($fieldDescription->getMappingType() == ClassMetadata::MANY_TO_ONE) { |
||
| 150 | $fieldDescription->setTemplate('SonataDoctrinePHPCRAdminBundle:CRUD:list_phpcr_many_to_one.html.twig'); |
||
| 151 | } |
||
| 152 | |||
| 153 | if ($fieldDescription->getMappingType() == ClassMetadata::MANY_TO_MANY) { |
||
| 154 | $fieldDescription->setTemplate('SonataDoctrinePHPCRAdminBundle:CRUD:list_phpcr_many_to_many.html.twig'); |
||
| 155 | } |
||
| 156 | |||
| 157 | if ($fieldDescription->getMappingType() == 'child' || $fieldDescription->getMappingType() == 'parent') { |
||
| 158 | $fieldDescription->setTemplate('SonataDoctrinePHPCRAdminBundle:CRUD:list_phpcr_one_to_one.html.twig'); |
||
| 159 | } |
||
| 160 | |||
| 161 | if ($fieldDescription->getMappingType() == 'children' || $fieldDescription->getMappingType() == 'referrers') { |
||
| 162 | $fieldDescription->setTemplate('SonataDoctrinePHPCRAdminBundle:CRUD:list_phpcr_one_to_many.html.twig'); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | $mappingTypes = array( |
||
| 167 | ClassMetadata::MANY_TO_ONE, |
||
| 168 | ClassMetadata::MANY_TO_MANY, |
||
| 169 | 'children', |
||
| 170 | 'child', 'parent', |
||
| 171 | 'referrers', |
||
| 172 | ); |
||
| 173 | |||
| 174 | if ($metadata && $metadata->hasAssociation($fieldDescription->getName()) && in_array($fieldDescription->getMappingType(), $mappingTypes)) { |
||
|
|
|||
| 175 | $admin->attachAdminClass($fieldDescription); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 216 |
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: