| Conditions | 14 |
| Paths | 121 |
| Total Lines | 78 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | 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 |
||
| 47 | public function build() |
||
| 48 | { |
||
| 49 | |||
| 50 | if ($this->isBuild) { |
||
| 51 | return; |
||
| 52 | } |
||
| 53 | |||
| 54 | $settings = $this->getObject(); |
||
| 55 | $reflection = new \ReflectionClass($settings); |
||
| 56 | $properties = $reflection->getProperties(); |
||
| 57 | |||
| 58 | $skipProperties = array('_settings', 'isWritable'); |
||
| 59 | if ($settings instanceof ModuleSettingsContainerInterface) { |
||
| 60 | $skipProperties[] = '_module'; |
||
| 61 | } |
||
| 62 | $children = array(); |
||
| 63 | foreach ($properties as $property) { |
||
| 64 | if (in_array($property->getName(), $skipProperties) || $this->has($property->getName())) { |
||
| 65 | continue; |
||
| 66 | } |
||
| 67 | $property->setAccessible(true); |
||
| 68 | $value = $property->getValue($settings); |
||
| 69 | if ($value instanceof SettingsContainerInterface) { |
||
| 70 | $children[$property->getName()] = $value; |
||
| 71 | continue; |
||
| 72 | } |
||
| 73 | |||
| 74 | $inputName = $property->getName(); |
||
| 75 | |||
| 76 | $inputLabel = isset($this->labelMap[$inputName]) ? $this->labelMap[$inputName] : $inputName; |
||
| 77 | |||
| 78 | if (is_array($inputLabel)){ |
||
| 79 | $priority = isset($inputLabel[1])?$inputLabel[1]:0; |
||
| 80 | $inputLabel = $inputLabel[0]; |
||
| 81 | }else{ |
||
| 82 | $priority = 0; |
||
| 83 | } |
||
| 84 | |||
| 85 | $input = array( |
||
| 86 | 'name' => $inputName, |
||
| 87 | 'options' => array( |
||
| 88 | |||
| 89 | 'label' => $inputLabel |
||
| 90 | ), |
||
| 91 | ); |
||
| 92 | if (is_bool($value)) { |
||
| 93 | $input['type']= 'Checkbox'; |
||
| 94 | $input['attributes']['checked'] = $value; |
||
| 95 | } else { |
||
| 96 | $input['attributes']['value'] = $value; |
||
| 97 | } |
||
| 98 | $this->add($input,['priority'=>$priority]); |
||
| 99 | |||
| 100 | } |
||
| 101 | |||
| 102 | foreach ($children as $name => $child) { |
||
| 103 | $objectClass = ltrim(get_class($settings), '\\'); |
||
| 104 | $moduleName = substr($objectClass, 0, strpos($objectClass, '\\')); |
||
| 105 | $fieldsetName = $moduleName . '/' . ucfirst($name) . 'SettingsFieldset'; |
||
| 106 | |||
| 107 | if ($this->forms->has($fieldsetName)) { |
||
| 108 | $fieldset = $this->forms->get($fieldsetName); |
||
| 109 | if (!$fieldset->getHydrator() instanceof SettingsEntityHydrator) { |
||
| 110 | $fieldset->setHydrator($this->getHydrator()); |
||
| 111 | } |
||
| 112 | } else { |
||
| 113 | $fieldset = new self(); |
||
| 114 | $label = preg_replace('~([A-Z])~', ' $1', $name); |
||
| 115 | $fieldset->setLabel(ucfirst($label)); |
||
| 116 | } |
||
| 117 | $fieldset->setName($name) |
||
| 118 | ->setObject($child); |
||
| 119 | |||
| 120 | |||
| 121 | $this->add($fieldset); |
||
|
|
|||
| 122 | } |
||
| 123 | $this->isBuild = true; |
||
| 124 | } |
||
| 125 | } |
||
| 126 |
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.