Conditions | 5 |
Paths | 2 |
Total Lines | 52 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
123 | public function SearchForm() |
||
124 | { |
||
125 | $form = parent::SearchForm(); |
||
126 | $fields = $form->Fields(); |
||
127 | |||
128 | if ($this->modelClass == Contact::class) { |
||
129 | $contacts = Contact::get(); |
||
130 | $config = Contact::config(); |
||
131 | $has_one = $config->has_one; |
||
132 | $has_many = $config->has_many; |
||
133 | $many_many = $config->many_many; |
||
134 | $belongs_many_many = $config->belongs_many_many; |
||
135 | $associations = array_merge( |
||
136 | $has_one, |
||
137 | $has_many, |
||
138 | $many_many, |
||
139 | $belongs_many_many |
||
140 | ); |
||
141 | |||
142 | foreach ($fields as $field) { |
||
143 | if ($field instanceof TextField) { |
||
144 | $name = $field->getName(); |
||
145 | $title = $field->Title(); |
||
146 | $db_field = str_replace(["q[", "]"], "", $name); |
||
147 | $class = $this->modelClass; |
||
148 | |||
149 | // If this is a relation, switch class name |
||
150 | if (strpos($name, "__")) { |
||
151 | $parts = explode("__", $db_field); |
||
152 | $class = $associations[$parts[0]]; |
||
153 | $db_field = $parts[1]; |
||
154 | } |
||
155 | |||
156 | $fields->replaceField( |
||
157 | $name, |
||
158 | ModelAdminAutoCompleteField::create( |
||
159 | $name, |
||
160 | $title, |
||
161 | $field->Value(), |
||
162 | $class, |
||
163 | $db_field |
||
164 | )->setForm($form) |
||
165 | ->setDisplayField($db_field) |
||
166 | ->setLabelField($db_field) |
||
167 | ->setStoredField($db_field) |
||
168 | ); |
||
169 | } |
||
170 | } |
||
171 | |||
172 | } |
||
173 | |||
174 | return $form; |
||
175 | } |
||
176 | } |