| Conditions | 11 |
| Paths | 38 |
| Total Lines | 66 |
| Code Lines | 39 |
| Lines | 3 |
| Ratio | 4.55 % |
| Changes | 10 | ||
| Bugs | 6 | 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 |
||
| 115 | public function field($object, $type, $property = null, $helper = null, $layerFile = null) |
||
| 116 | { |
||
| 117 | if ($this->model === null) { |
||
| 118 | if (App::$Debug !== null) { |
||
| 119 | App::$Debug->addMessage('Form model is not defined for field name: [' . strip_tags($object) . ']'); |
||
| 120 | } |
||
| 121 | return null; |
||
| 122 | } |
||
| 123 | |||
| 124 | // can be dots separated object |
||
| 125 | $propertyName = $object; |
||
| 126 | if (Str::contains('.', $propertyName)) { |
||
| 127 | $propertyName = strstr($propertyName, '.', true); |
||
| 128 | } |
||
| 129 | |||
| 130 | // check if model contains current tag name as property |
||
| 131 | if (!property_exists($this->model, $propertyName)) { |
||
| 132 | View Code Duplication | if (App::$Debug !== null) { |
|
| 133 | App::$Debug->addMessage('Form field ["' . $object . '"] is not defined in model: [' . get_class($this->model) . ']', 'error'); |
||
| 134 | } |
||
| 135 | return null; |
||
| 136 | } |
||
| 137 | |||
| 138 | // prepare layer template file path |
||
| 139 | if ($layerFile === null) { |
||
| 140 | switch ($type) { |
||
| 141 | case 'checkbox': |
||
| 142 | $layerFile = static::$structLayer['checkbox']; |
||
| 143 | break; |
||
| 144 | case 'radio': |
||
| 145 | $layerFile = static::$structLayer['radio']; |
||
| 146 | break; |
||
| 147 | default: |
||
| 148 | $layerFile = static::$structLayer['base']; |
||
| 149 | break; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | // prepare labels text and label "for" attr |
||
| 154 | $labelFor = $this->name . '-' . $propertyName; |
||
| 155 | $labelText = $this->model->getLabel($object); |
||
| 156 | $itemValue = $this->model->{$propertyName}; |
||
| 157 | // sounds like a dot-separated $object |
||
| 158 | if ($propertyName !== $object) { |
||
| 159 | $nesting = trim(strstr($object, '.'), '.'); |
||
| 160 | $labelFor .= '-' . Str::replace('.', '-', $nesting); |
||
| 161 | $itemValue = Arr::getByPath($nesting, $itemValue); |
||
| 162 | } |
||
| 163 | |||
| 164 | // initialize form fields constructor and build output dom html value |
||
| 165 | $constructor = new Constructor($this->model, $this->name, $type); |
||
| 166 | $elementDOM = $constructor->makeTag($object, $itemValue, $property); |
||
| 167 | |||
| 168 | // if item is hidden - return tag without assign of global template |
||
| 169 | if ($type === 'hidden') { |
||
| 170 | return $elementDOM; |
||
| 171 | } |
||
| 172 | |||
| 173 | // render output viewer |
||
| 174 | return App::$View->render($layerFile, [ |
||
| 175 | 'name' => $labelFor, |
||
| 176 | 'label' => $labelText, |
||
| 177 | 'item' => $elementDOM, |
||
| 178 | 'help' => self::nohtml($helper) |
||
| 179 | ]); |
||
| 180 | } |
||
| 181 | |||
| 228 | } |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: