Total Complexity | 43 |
Total Lines | 415 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like Metagen 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 Metagen, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class Metagen |
||
33 | { |
||
34 | /** |
||
35 | * @var Helper |
||
36 | */ |
||
37 | public $helper; |
||
38 | /** |
||
39 | * @var \MyTextSanitizer |
||
40 | */ |
||
41 | public $myts; |
||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | public $title; |
||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | public $originalTitle; |
||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | public $keywords; |
||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | public $categoryPath; |
||
58 | /** |
||
59 | * @var string |
||
60 | */ |
||
61 | public $description; |
||
62 | /** |
||
63 | * @var int |
||
64 | */ |
||
65 | public $minChar = 4; |
||
66 | |||
67 | /** |
||
68 | * @param string $title |
||
69 | * @param string $keywords |
||
70 | * @param string $description |
||
71 | * @param string $categoryPath |
||
72 | */ |
||
73 | public function __construct($title, $keywords = '', $description = '', $categoryPath = '') |
||
74 | { |
||
75 | /** @var Helper $this- >helper */ |
||
76 | $this->helper = Helper::getInstance(); |
||
77 | $this->myts = \MyTextSanitizer::getInstance(); |
||
78 | $this->setCategoryPath($categoryPath); |
||
79 | $this->setTitle($title); |
||
80 | $this->setDescription($description); |
||
81 | if ('' == $keywords) { |
||
82 | $keywords = $this->createMetaKeywords(); |
||
83 | } |
||
84 | $this->setKeywords($keywords); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param string $title |
||
89 | */ |
||
90 | public function setTitle($title) |
||
91 | { |
||
92 | $this->title = $this->html2text($title); |
||
93 | $this->originalTitle = $this->title; |
||
94 | $titleTag = []; |
||
95 | $titleTag['module'] = $this->helper->getModule()->getVar('name'); |
||
96 | if (isset($this->title) && ('' != $this->title) && (\mb_strtoupper($this->title) != \mb_strtoupper($titleTag['module']))) { |
||
|
|||
97 | $titleTag['title'] = $this->title; |
||
98 | } |
||
99 | if (isset($this->categoryPath) && ('' != $this->categoryPath)) { |
||
100 | $titleTag['category'] = $this->categoryPath; |
||
101 | } |
||
102 | $ret = $titleTag['title'] ?? ''; |
||
103 | if (isset($titleTag['category']) && '' != $titleTag['category']) { |
||
104 | if ('' != $ret) { |
||
105 | $ret .= ' - '; |
||
106 | } |
||
107 | $ret .= $titleTag['category']; |
||
108 | } |
||
109 | if (isset($titleTag['module']) && '' != $titleTag['module']) { |
||
110 | if ('' != $ret) { |
||
111 | $ret .= ' - '; |
||
112 | } |
||
113 | $ret .= $titleTag['module']; |
||
114 | } |
||
115 | $this->title = $ret; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param string $keywords |
||
120 | */ |
||
121 | public function setKeywords($keywords) |
||
122 | { |
||
123 | $this->keywords = $keywords; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param string $categoryPath |
||
128 | */ |
||
129 | public function setCategoryPath($categoryPath) |
||
130 | { |
||
131 | $categoryPath = $this->html2text($categoryPath); |
||
132 | $this->categoryPath = $categoryPath; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param string $description |
||
137 | */ |
||
138 | public function setDescription($description) |
||
139 | { |
||
140 | $description = $this->html2text($description); |
||
141 | $description = $this->purifyText($description); |
||
142 | $this->description = $description; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Does nothing |
||
147 | */ |
||
148 | public function createTitleTag() |
||
149 | { |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param int $maxWords |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | public function createMetaDescription($maxWords = 30) |
||
158 | { |
||
159 | $description = $this->purifyText($this->description); |
||
160 | $description = $this->html2text($description); |
||
161 | $words = \explode(' ', $description); |
||
162 | $ret = ''; |
||
163 | $i = 1; |
||
164 | $wordCount = \count($words); |
||
165 | foreach ($words as $word) { |
||
166 | $ret .= $word; |
||
167 | if ($i < $wordCount) { |
||
168 | $ret .= ' '; |
||
169 | } |
||
170 | ++$i; |
||
171 | } |
||
172 | |||
173 | return $ret; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param string $text |
||
178 | * @param int $minChar |
||
179 | * |
||
180 | * @return array |
||
181 | */ |
||
182 | public function findMetaKeywords($text, $minChar) |
||
183 | { |
||
184 | $keywords = []; |
||
185 | $text = $this->purifyText($text); |
||
186 | $text = $this->html2text($text); |
||
187 | $originalKeywords = \explode(' ', $text); |
||
188 | foreach ($originalKeywords as $originalKeyword) { |
||
189 | $secondRoundKeywords = \explode("'", $originalKeyword); |
||
190 | foreach ($secondRoundKeywords as $secondRoundKeyword) { |
||
191 | if (\mb_strlen($secondRoundKeyword) >= $minChar) { |
||
192 | if (!\in_array($secondRoundKeyword, $keywords, true)) { |
||
193 | $keywords[] = \trim($secondRoundKeyword); |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 | |||
199 | return $keywords; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @return string |
||
204 | */ |
||
205 | public function createMetaKeywords() |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Does nothing |
||
220 | */ |
||
221 | public function autoBuildMetaKeywords() |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Build Metatags |
||
227 | */ |
||
228 | public function buildAutoMetaTags() |
||
229 | { |
||
230 | $this->keywords = $this->createMetaKeywords(); |
||
231 | $this->description = $this->createMetaDescription(); |
||
232 | //$this->title = $this->createTitleTag(); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Creates meta tags |
||
237 | */ |
||
238 | public function createMetaTags() |
||
239 | { |
||
240 | global $xoopsTpl, $xoTheme; |
||
241 | if ('' != $this->keywords) { |
||
242 | $xoTheme->addMeta('meta', 'keywords', $this->keywords); |
||
243 | } |
||
244 | if ('' != $this->description) { |
||
245 | $xoTheme->addMeta('meta', 'description', $this->description); |
||
246 | } |
||
247 | if ('' != $this->title) { |
||
248 | $xoopsTpl->assign('xoops_pagetitle', $this->title); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Return true if the string is length > 0 |
||
254 | * |
||
255 | * @credit psylove |
||
256 | * @param mixed $var |
||
257 | * @return bool |
||
258 | */ |
||
259 | public static function emptyString($var) |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Create a title for the short_url field of an article |
||
266 | * |
||
267 | * @credit psylove |
||
268 | * |
||
269 | * @param string|array $title title of the article |
||
270 | * @param bool $withExt do we add an html extension or not |
||
271 | * |
||
272 | * @return string short url for article |
||
273 | */ |
||
274 | public static function generateSeoTitle($title = '', $withExt = true) |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * @param $text |
||
350 | * @param bool $keyword |
||
351 | * |
||
352 | * @return mixed |
||
353 | */ |
||
354 | public function purifyText($text, $keyword = false) |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * @param string $document |
||
390 | * |
||
391 | * @return mixed |
||
392 | */ |
||
393 | public function html2text($document) |
||
449 |