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