| Conditions | 11 |
| Paths | 38 |
| Total Lines | 66 |
| Code Lines | 39 |
| Lines | 3 |
| Ratio | 4.55 % |
| Changes | 11 | ||
| 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 |
||
| 100 | public function field($object, $type, $property = null, $helper = null, $layerFile = null) |
||
| 101 | { |
||
| 102 | if ($this->model === null) { |
||
| 103 | if (App::$Debug !== null) { |
||
| 104 | App::$Debug->addMessage('Form model is not defined for field name: [' . strip_tags($object) . ']'); |
||
| 105 | } |
||
| 106 | return null; |
||
| 107 | } |
||
| 108 | |||
| 109 | // can be dots separated object |
||
| 110 | $propertyName = $object; |
||
| 111 | if (Str::contains('.', $propertyName)) { |
||
| 112 | $propertyName = strstr($propertyName, '.', true); |
||
| 113 | } |
||
| 114 | |||
| 115 | // check if model contains current tag name as property |
||
| 116 | if (!property_exists($this->model, $propertyName)) { |
||
| 117 | View Code Duplication | if (App::$Debug !== null) { |
|
| 118 | App::$Debug->addMessage('Form field ["' . $object . '"] is not defined in model: [' . get_class($this->model) . ']', 'error'); |
||
| 119 | } |
||
| 120 | return null; |
||
| 121 | } |
||
| 122 | |||
| 123 | // prepare layer template file path |
||
| 124 | if ($layerFile === null) { |
||
| 125 | switch ($type) { |
||
| 126 | case 'checkbox': |
||
| 127 | $layerFile = static::$structLayer['checkbox']; |
||
| 128 | break; |
||
| 129 | case 'radio': |
||
| 130 | $layerFile = static::$structLayer['radio']; |
||
| 131 | break; |
||
| 132 | default: |
||
| 133 | $layerFile = static::$structLayer['base']; |
||
| 134 | break; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | // prepare labels text and label "for" attr |
||
| 139 | $labelFor = $this->name . '-' . $propertyName; |
||
| 140 | $labelText = $this->model->getLabel($object); |
||
| 141 | $itemValue = $this->model->{$propertyName}; |
||
| 142 | // sounds like a dot-separated $object |
||
| 143 | if ($propertyName !== $object) { |
||
| 144 | $nesting = trim(strstr($object, '.'), '.'); |
||
| 145 | $labelFor .= '-' . Str::replace('.', '-', $nesting); |
||
| 146 | $itemValue = Arr::getByPath($nesting, $itemValue); |
||
| 147 | } |
||
| 148 | |||
| 149 | // initialize form fields constructor and build output dom html value |
||
| 150 | $constructor = new Constructor($this->model, $this->name, $type); |
||
| 151 | $elementDOM = $constructor->makeTag($object, $itemValue, $property); |
||
| 152 | |||
| 153 | // if item is hidden - return tag without assign of global template |
||
| 154 | if ($type === 'hidden') { |
||
| 155 | return $elementDOM; |
||
| 156 | } |
||
| 157 | |||
| 158 | // render output viewer |
||
| 159 | return App::$View->render($layerFile, [ |
||
| 160 | 'name' => $labelFor, |
||
| 161 | 'label' => $labelText, |
||
| 162 | 'item' => $elementDOM, |
||
| 163 | 'help' => self::nohtml($helper) |
||
| 164 | ]); |
||
| 165 | } |
||
| 166 | |||
| 213 | } |
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: