Total Complexity | 56 |
Total Lines | 252 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like element often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use element, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class element |
||
11 | { |
||
12 | public $dom; |
||
13 | public $version; |
||
14 | public $encoding; |
||
15 | public static $static; |
||
16 | /** |
||
17 | * DOMElement. |
||
18 | * |
||
19 | * @var DOMElement |
||
20 | */ |
||
21 | public $element; |
||
22 | |||
23 | public function __construct($version = '1.0', $encoding = 'utf-8') |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Array to element |
||
33 | * |
||
34 | * @return \HTML\array2element |
||
35 | */ |
||
36 | public function array2el() |
||
37 | { |
||
38 | return new array2element(); |
||
39 | } |
||
40 | |||
41 | public function pre($content, $options = ['echo' => true]) |
||
42 | { |
||
43 | if ($this->isArrObj($content)) { |
||
44 | $content = json_encode($content, false, true); |
||
45 | } |
||
46 | $element = "<pre>$content</pre>"; |
||
47 | if ($options['echo']) { |
||
48 | echo $content; |
||
49 | } else { |
||
50 | return $content; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * var_dump inside pretext. |
||
56 | */ |
||
57 | public function htmlvar_dump($content) |
||
58 | { |
||
59 | echo '<pre>'; |
||
60 | var_dump($content); |
||
61 | echo '</pre>'; |
||
62 | } |
||
63 | |||
64 | public function script(array $source = [], $html = false, $print = false) |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | |||
87 | public function direct(string $element, string $content, array $attr = []) |
||
88 | { |
||
89 | $create = $this->dom->createElement($element, $content); |
||
90 | if (!empty($attributes)) { |
||
91 | $create = $this->fill_attributes($create, $attr); |
||
92 | } |
||
93 | var_dump($create); |
||
94 | $this->dom->appendChild($create); |
||
95 | |||
96 | return $this->dom->saveHTML(); |
||
97 | } |
||
98 | |||
99 | public function css(array $source) |
||
109 | } |
||
110 | |||
111 | public function js(array $source) |
||
112 | { |
||
113 | $result = ''; |
||
114 | $config = defined('CONFIG') && isset(CONFIG['cache']['key']) ? '?cache=' . CONFIG['cache']['key'] : ''; |
||
115 | foreach ($source as $path) { |
||
116 | if (!empty($path)) { |
||
117 | $path .= $config; |
||
118 | $result .= '<script src="' . $path . '"></script>'; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | return $result; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Proxy CSS. |
||
127 | * |
||
128 | * @param bool $html |
||
129 | * @param bool $print |
||
130 | * @param string $rel |
||
131 | */ |
||
132 | public function link(array $source = [], $html = false, $print = false, $rel = 'stylesheet') |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | |||
155 | public function get_local_asset($path) |
||
156 | { |
||
157 | if (is_string($path) && filter_var($path, FILTER_VALIDATE_URL)) { |
||
158 | $src_ = $path; |
||
159 | } else { |
||
160 | if (is_string($path)) { |
||
161 | $src_ = helper::asset_find([$path]); |
||
162 | } elseif (is_array($path)) { |
||
163 | $src_ = helper::asset_find($path); |
||
164 | } else { |
||
165 | throw new Exception('path required string or array, instead of ' . gettype($path), 1); |
||
166 | } |
||
167 | if ($src_) { |
||
168 | $src_ = helper::webkit_asset($src_); |
||
169 | } |
||
170 | } |
||
171 | |||
172 | return $src_; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * echo print_r in pretext. |
||
177 | * |
||
178 | * @param mixed $str |
||
179 | */ |
||
180 | public function printr(...$str) |
||
187 | } |
||
188 | |||
189 | public function select($attributes = []) |
||
190 | { |
||
191 | $select = $this->dom->createElement('select'); |
||
192 | foreach ($attributes as $key => $value) { |
||
193 | if ('option' == $key) { |
||
194 | continue; |
||
195 | } |
||
196 | $select->setAttribute($key, (string) $value); |
||
197 | } |
||
198 | if (isset($attributes['option'])) { |
||
199 | foreach ($attributes['option'] as $option_attr) { |
||
200 | $option = $this->single_create('option', $option_attr['inner'], $option_attr); |
||
201 | $select->appendChild($option); |
||
202 | } |
||
203 | } |
||
204 | $this->dom->appendChild($select); |
||
205 | |||
206 | return $this; |
||
207 | } |
||
208 | |||
209 | public function single_create(string $element, $content = '', array $attributes = []) |
||
210 | { |
||
211 | if (empty($attributes) && is_array($content)) { |
||
212 | $attributes = $content; |
||
213 | $content = ''; |
||
214 | } |
||
215 | $element = $this->dom->createElement($element, $content); |
||
216 | if (!empty($attributes)) { |
||
217 | $element = $this->fill_attributes($element, $attributes); |
||
218 | } |
||
219 | |||
220 | return $element; |
||
221 | } |
||
222 | |||
223 | public function create(string $element, $content = '', array $attributes = [], array $options = ['formatOutput' => false]) |
||
224 | { |
||
225 | if ('select' == $element) { |
||
226 | return $this->select($attributes); |
||
227 | } |
||
228 | if (empty($attributes) && is_array($content)) { |
||
229 | $attributes = $content; |
||
230 | $content = ''; |
||
231 | } |
||
232 | //exit(var_dump($attributes, $content)); |
||
233 | $element = $this->dom->createElement($element, $content); |
||
234 | if (!empty($attributes)) { |
||
235 | $element = $this->fill_attributes($element, $attributes); |
||
236 | } |
||
237 | $this->dom->appendChild($element); |
||
238 | |||
239 | return $this; |
||
240 | } |
||
241 | |||
242 | public function fill_attributes(DOMElement $element, array $attributes) |
||
249 | } |
||
250 | |||
251 | public function outerText() |
||
257 | } |
||
258 | |||
259 | public function isArrObj($str) |
||
262 | } |
||
263 | } |
||
264 |