Total Complexity | 52 |
Total Lines | 310 |
Duplicated Lines | 0 % |
Changes | 11 | ||
Bugs | 0 | Features | 0 |
Complex classes like Article 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 Article, and based on these observations, apply Extract Interface, too.
1 | <?php namespace FlatPlan; |
||
5 | class Article { |
||
6 | |||
7 | private $components = array(); |
||
8 | private $status; |
||
9 | private $article_id; |
||
10 | private $title; |
||
11 | private $sub_title; |
||
12 | private $language = 'en'; |
||
13 | private $category; |
||
14 | private $template; |
||
15 | private $metaData = null; |
||
16 | private $parameters = null; |
||
17 | private $localization = null; |
||
18 | private $allowedMetaKeys = array( |
||
19 | 'authors' => 'array', |
||
20 | 'canonicalURL' => 'uri', |
||
21 | 'coverArt' => 'array', |
||
22 | 'dateCreated' => 'datetime', |
||
23 | 'dateModified' => 'datetime', |
||
24 | 'datePublished' => 'datetime', |
||
25 | 'excerpt' => 'string', |
||
26 | 'keywords' => 'array', |
||
27 | 'links' => 'array', |
||
28 | 'thumbnailURL' => 'uri', |
||
29 | 'transparentToolbar' => 'boolean', |
||
30 | 'videoURL' => 'uri', |
||
31 | 'accessoryText' => 'string', |
||
32 | 'isCandidateToBeFeatured' => 'boolean', |
||
33 | 'isHidden' => 'boolean', |
||
34 | 'isPreview' => 'boolean', |
||
35 | 'isSponsored' => 'boolean', |
||
36 | 'maturityRating' => 'string' |
||
37 | ); |
||
38 | private $allowedParameters = array( |
||
39 | 'accessoryText' => 'string', |
||
40 | 'isCandidateToBeFeatured' => 'boolean', |
||
41 | 'isHidden' => 'boolean', |
||
42 | 'isPreview' => 'boolean', |
||
43 | 'isSponsored' => 'boolean', |
||
44 | 'maturityRating' => 'string', |
||
45 | 'targetTerritoryCountryCodes' => 'string', |
||
46 | 'isPaid' => 'boolean' |
||
47 | ); |
||
48 | private $allowedLocalization = array( |
||
49 | 'region' => 'string', |
||
50 | ); |
||
51 | |||
52 | public function __construct($articleId, $status = 'draft') |
||
56 | } |
||
57 | |||
58 | public function setTitle($title) { |
||
59 | $this->title = $title; |
||
60 | } |
||
61 | |||
62 | public function getTitle() |
||
63 | { |
||
64 | return $this->title; |
||
65 | } |
||
66 | |||
67 | public function setSubTitle($subTitle) { |
||
68 | $this->sub_title = $subTitle; |
||
69 | } |
||
70 | |||
71 | public function getSubTitle() |
||
72 | { |
||
73 | return $this->sub_title; |
||
74 | } |
||
75 | |||
76 | public function setLanguage($language) |
||
77 | { |
||
78 | $this->language = $language; |
||
79 | } |
||
80 | |||
81 | public function getLanguage() |
||
82 | { |
||
83 | return $this->language; |
||
84 | } |
||
85 | |||
86 | public function setCategory($category) |
||
87 | { |
||
88 | $this->category = $category; |
||
89 | } |
||
90 | |||
91 | public function getCategory() |
||
92 | { |
||
93 | return $this->category; |
||
94 | } |
||
95 | |||
96 | public function setTemplate($template) |
||
97 | { |
||
98 | $this->template = $template; |
||
99 | } |
||
100 | |||
101 | public function getTemplate() |
||
104 | } |
||
105 | |||
106 | public function setMetaData($metaData) |
||
107 | { |
||
108 | $metaObj = $this->getMetaData(); |
||
109 | if (is_null($metaObj)) { |
||
110 | $metaObj = new \stdClass(); |
||
111 | } |
||
112 | |||
113 | $errors = array(); |
||
114 | if (is_array($metaData)) { |
||
115 | foreach ($metaData as $key => $value) { |
||
116 | if (isset($this->allowedMetaKeys[$key])) { |
||
117 | $type = gettype($value); |
||
118 | switch ($this->allowedMetaKeys[$key]) { |
||
119 | case 'uri': |
||
120 | if (substr($value, 0, 4) === 'http') { |
||
121 | $metaObj->{$key} = $value; |
||
122 | } else { |
||
123 | $errors[] = array( |
||
124 | 'key' => $key, |
||
125 | 'message' => 'Not a valid URI' |
||
126 | ); |
||
127 | } |
||
128 | break; |
||
129 | |||
130 | case 'datetime': |
||
131 | if ($value instanceof \DateTime) { |
||
132 | $metaObj->{$key} = $value->format('c'); |
||
133 | } else { |
||
134 | $errors[] = array( |
||
135 | 'key' => $key, |
||
136 | 'message' => 'Not a valid DateTime object' |
||
137 | ); |
||
138 | } |
||
139 | break; |
||
140 | |||
141 | case 'array': |
||
142 | if (is_array($value)) { |
||
143 | $metaObj->{$key} = $value; |
||
144 | } else { |
||
145 | $errors[] = array( |
||
146 | 'key' => $key, |
||
147 | 'message' => 'Not a valid Array' |
||
148 | ); |
||
149 | } |
||
150 | break; |
||
151 | |||
152 | default: |
||
153 | if ($type === $this->allowedMetaKeys[$key]) { |
||
154 | $metaObj->{$key} = $value; |
||
155 | } else { |
||
156 | $errors[] = array( |
||
157 | 'key' => $key, |
||
158 | 'message' => 'Expected ' . $this->allowedMetaKeys[$key] . '; received ' . $type |
||
159 | ); |
||
160 | } |
||
161 | break; |
||
162 | } |
||
163 | } else { |
||
164 | $errors[] = array( |
||
165 | 'key' => $key, |
||
166 | 'message' => 'Not a valid MetaData key' |
||
167 | ); |
||
168 | } |
||
169 | } |
||
170 | } |
||
171 | |||
172 | if (!empty($errors)) { |
||
173 | throw new \ErrorException('Invalid MetaData: ' . print_r($errors, true)); |
||
|
|||
174 | } |
||
175 | |||
176 | $this->metaData = $metaObj; |
||
177 | } |
||
178 | |||
179 | public function getMetaData() |
||
180 | { |
||
181 | return $this->metaData; |
||
182 | } |
||
183 | |||
184 | public function setParameters($parameters) |
||
185 | { |
||
186 | $parameterObj = $this->getParameters(); |
||
187 | if (is_null($parameterObj)) { |
||
188 | $parameterObj = new \stdClass(); |
||
189 | } |
||
190 | |||
191 | $errors = array(); |
||
192 | if (is_array($parameters)) { |
||
193 | foreach ($parameters as $key => $value) { |
||
194 | if (isset($this->allowedParameters[$key])) { |
||
195 | $type = gettype($value); |
||
196 | if ($type === $this->allowedParameters[$key]) { |
||
197 | $parameterObj->{$key} = $value; |
||
198 | } else { |
||
199 | $errors[] = array( |
||
200 | 'key' => $key, |
||
201 | 'message' => 'Expected ' . $this->allowedParameters[$key] . '; received ' . $type |
||
202 | ); |
||
203 | } |
||
204 | } else { |
||
205 | $errors[] = array( |
||
206 | 'key' => $key, |
||
207 | 'message' => 'Not a valid Parameter' |
||
208 | ); |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | |||
213 | if (!empty($errors)) { |
||
214 | throw new \ErrorException('Invalid Parameters: ' . print_r($errors, true)); |
||
215 | } |
||
216 | |||
217 | $this->parameters = $parameterObj; |
||
218 | } |
||
219 | |||
220 | public function getParameters() |
||
221 | { |
||
222 | return $this->parameters; |
||
223 | } |
||
224 | |||
225 | public function setLocalization($localization) |
||
226 | { |
||
227 | $localizationObj = $this->getLocalization(); |
||
228 | if (is_null($localizationObj)) { |
||
229 | $localizationObj = new \stdClass(); |
||
230 | } |
||
231 | |||
232 | $errors = array(); |
||
233 | if (is_array($localization)) { |
||
234 | foreach ($localization as $key => $value) { |
||
235 | if (isset($this->allowedLocalization[$key])) { |
||
236 | $type = gettype($value); |
||
237 | if ($type === $this->allowedLocalization[$key]) { |
||
238 | $localizationObj->{$key} = $value; |
||
239 | } else { |
||
240 | $errors[] = array( |
||
241 | 'key' => $key, |
||
242 | 'message' => 'Expected ' . $this->allowedLocalization[$key] . '; received ' . $type |
||
243 | ); |
||
244 | } |
||
245 | } else { |
||
246 | $errors[] = array( |
||
247 | 'key' => $key, |
||
248 | 'message' => 'Not a valid Localization Parameter' |
||
249 | ); |
||
250 | } |
||
251 | } |
||
252 | } |
||
253 | |||
254 | if (!empty($errors)) { |
||
255 | throw new \ErrorException('Invalid Localization Parameters: ' . print_r($errors, true)); |
||
256 | } |
||
257 | |||
258 | $this->localization = $localizationObj; |
||
259 | } |
||
260 | |||
261 | public function getLocalization() |
||
262 | { |
||
263 | return $this->localization; |
||
264 | } |
||
265 | |||
266 | public function getJson() |
||
267 | { |
||
268 | $article = new \stdClass(); |
||
269 | $article->article_id = $this->article_id; |
||
270 | $article->language = $this->getLanguage(); |
||
271 | $article->status = $this->status; |
||
272 | $article->title = $this->getTitle(); |
||
273 | $article->sub_title = $this->getSubTitle(); |
||
274 | $article->category = $this->getCategory(); |
||
275 | $article->template = $this->getTemplate(); |
||
276 | $article->metadata = $this->getMetaData(); |
||
277 | $article->parameters = $this->getParameters(); |
||
278 | $article->localization = $this->getLocalization(); |
||
279 | $article->components = $this->getComponents(); |
||
280 | |||
281 | return json_encode($article, JSON_UNESCAPED_UNICODE); |
||
282 | } |
||
283 | |||
284 | public function getComponents($format = null) |
||
285 | { |
||
286 | $output = array(); |
||
287 | foreach ($this->components as $component) { |
||
288 | array_push($output, $component->getComponent()); |
||
289 | } |
||
290 | |||
291 | if ($format === 'json') { |
||
292 | return json_encode($output, JSON_UNESCAPED_UNICODE); |
||
293 | } |
||
294 | |||
295 | return $output; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * @param AbstractComponent $component |
||
300 | * @throws \ErrorException |
||
301 | * @return void |
||
302 | */ |
||
303 | public function setComponents($components) |
||
315 | } |
||
316 | } |
||
317 | } |
||
318 |