Total Complexity | 44 |
Total Lines | 263 |
Duplicated Lines | 0 % |
Changes | 9 | ||
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 $allowedMetaKeys = array( |
||
18 | 'authors' => 'array', |
||
19 | 'canonicalURL' => 'uri', |
||
20 | 'coverArt' => 'array', |
||
21 | 'dateCreated' => 'datetime', |
||
22 | 'dateModified' => 'datetime', |
||
23 | 'datePublished' => 'datetime', |
||
24 | 'excerpt' => 'string', |
||
25 | 'keywords' => 'array', |
||
26 | 'links' => 'array', |
||
27 | 'thumbnailURL' => 'uri', |
||
28 | 'transparentToolbar' => 'boolean', |
||
29 | 'videoURL' => 'uri', |
||
30 | 'accessoryText' => 'string', |
||
31 | 'isCandidateToBeFeatured' => 'boolean', |
||
32 | 'isHidden' => 'boolean', |
||
33 | 'isPreview' => 'boolean', |
||
34 | 'isSponsored' => 'boolean', |
||
35 | 'maturityRating' => 'string' |
||
36 | ); |
||
37 | private $allowedParameters = array( |
||
38 | 'accessoryText' => 'string', |
||
39 | 'isCandidateToBeFeatured' => 'boolean', |
||
40 | 'isHidden' => 'boolean', |
||
41 | 'isPreview' => 'boolean', |
||
42 | 'isSponsored' => 'boolean', |
||
43 | 'maturityRating' => 'string', |
||
44 | 'targetTerritoryCountryCodes' => 'string' |
||
45 | ); |
||
46 | |||
47 | public function __construct($articleId, $status = 'draft') |
||
51 | } |
||
52 | |||
53 | public function setTitle($title) { |
||
54 | $this->title = $title; |
||
55 | } |
||
56 | |||
57 | public function getTitle() |
||
58 | { |
||
59 | return $this->title; |
||
60 | } |
||
61 | |||
62 | public function setSubTitle($subTitle) { |
||
63 | $this->sub_title = $subTitle; |
||
64 | } |
||
65 | |||
66 | public function getSubTitle() |
||
67 | { |
||
68 | return $this->sub_title; |
||
69 | } |
||
70 | |||
71 | public function setLanguage($language) |
||
72 | { |
||
73 | $this->language = $language; |
||
74 | } |
||
75 | |||
76 | public function getLanguage() |
||
77 | { |
||
78 | return $this->language; |
||
79 | } |
||
80 | |||
81 | public function setCategory($category) |
||
82 | { |
||
83 | $this->category = $category; |
||
84 | } |
||
85 | |||
86 | public function getCategory() |
||
87 | { |
||
88 | return $this->category; |
||
89 | } |
||
90 | |||
91 | public function setTemplate($template) |
||
92 | { |
||
93 | $this->template = $template; |
||
94 | } |
||
95 | |||
96 | public function getTemplate() |
||
99 | } |
||
100 | |||
101 | public function setMetaData($metaData) |
||
102 | { |
||
103 | $metaObj = $this->getMetaData(); |
||
104 | if (is_null($metaObj)) { |
||
105 | $metaObj = new \stdClass(); |
||
106 | } |
||
107 | |||
108 | $errors = array(); |
||
109 | if (is_array($metaData)) { |
||
110 | foreach ($metaData as $key => $value) { |
||
111 | if (isset($this->allowedMetaKeys[$key])) { |
||
112 | $type = gettype($value); |
||
113 | switch ($this->allowedMetaKeys[$key]) { |
||
114 | case 'uri': |
||
115 | if (substr($value, 0, 4) === 'http') { |
||
116 | $metaObj->{$key} = $value; |
||
117 | } else { |
||
118 | $errors[] = array( |
||
119 | 'key' => $key, |
||
120 | 'message' => 'Not a valid URI' |
||
121 | ); |
||
122 | } |
||
123 | break; |
||
124 | |||
125 | case 'datetime': |
||
126 | if ($value instanceof \DateTime) { |
||
127 | $metaObj->{$key} = $value->format('c'); |
||
128 | } else { |
||
129 | $errors[] = array( |
||
130 | 'key' => $key, |
||
131 | 'message' => 'Not a valid DateTime object' |
||
132 | ); |
||
133 | } |
||
134 | break; |
||
135 | |||
136 | case 'array': |
||
137 | if (is_array($value)) { |
||
138 | $metaObj->{$key} = $value; |
||
139 | } else { |
||
140 | $errors[] = array( |
||
141 | 'key' => $key, |
||
142 | 'message' => 'Not a valid Array' |
||
143 | ); |
||
144 | } |
||
145 | break; |
||
146 | |||
147 | default: |
||
148 | if ($type === $this->allowedMetaKeys[$key]) { |
||
149 | $metaObj->{$key} = $value; |
||
150 | } else { |
||
151 | $errors[] = array( |
||
152 | 'key' => $key, |
||
153 | 'message' => 'Expected ' . $this->allowedMetaKeys[$key] . '; received ' . $type |
||
154 | ); |
||
155 | } |
||
156 | break; |
||
157 | } |
||
158 | } else { |
||
159 | $errors[] = array( |
||
160 | 'key' => $key, |
||
161 | 'message' => 'Not a valid MetaData key' |
||
162 | ); |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | |||
167 | if (!empty($errors)) { |
||
168 | throw new \ErrorException('Invalid MetaData: ' . print_r($errors, true)); |
||
|
|||
169 | } |
||
170 | |||
171 | $this->metaData = $metaObj; |
||
172 | } |
||
173 | |||
174 | public function getMetaData() |
||
175 | { |
||
176 | return $this->metaData; |
||
177 | } |
||
178 | |||
179 | public function setParameters($parameters) |
||
180 | { |
||
181 | $parameterObj = $this->getParameters(); |
||
182 | if (is_null($parameterObj)) { |
||
183 | $parameterObj = new \stdClass(); |
||
184 | } |
||
185 | |||
186 | $errors = array(); |
||
187 | if (is_array($parameters)) { |
||
188 | foreach ($parameters as $key => $value) { |
||
189 | if (isset($this->allowedParameters[$key])) { |
||
190 | $type = gettype($value); |
||
191 | if ($type === $this->allowedMetaKeys[$key]) { |
||
192 | $parameterObj->{$key} = $value; |
||
193 | } else { |
||
194 | $errors[] = array( |
||
195 | 'key' => $key, |
||
196 | 'message' => 'Expected ' . $this->allowedParameters[$key] . '; received ' . $type |
||
197 | ); |
||
198 | } |
||
199 | } else { |
||
200 | $errors[] = array( |
||
201 | 'key' => $key, |
||
202 | 'message' => 'Not a valid Parameter' |
||
203 | ); |
||
204 | } |
||
205 | } |
||
206 | } |
||
207 | |||
208 | if (!empty($errors)) { |
||
209 | throw new \ErrorException('Invalid Parameters: ' . print_r($errors, true)); |
||
210 | } |
||
211 | |||
212 | $this->parameters = $parameterObj; |
||
213 | } |
||
214 | |||
215 | public function getParameters() |
||
216 | { |
||
217 | return $this->parameters; |
||
218 | } |
||
219 | |||
220 | public function getJson() |
||
221 | { |
||
222 | $article = new \stdClass(); |
||
223 | $article->article_id = $this->article_id; |
||
224 | $article->language = $this->getLanguage(); |
||
225 | $article->status = $this->status; |
||
226 | $article->title = $this->getTitle(); |
||
227 | $article->sub_title = $this->getSubTitle(); |
||
228 | $article->category = $this->getCategory(); |
||
229 | $article->template = $this->getTemplate(); |
||
230 | $article->metadata = $this->getMetaData(); |
||
231 | $article->parameters = $this->getParameters(); |
||
232 | $article->components = $this->getComponents(); |
||
233 | |||
234 | return json_encode($article, JSON_UNESCAPED_UNICODE); |
||
235 | } |
||
236 | |||
237 | public function getComponents($format = null) |
||
238 | { |
||
239 | $output = array(); |
||
240 | foreach ($this->components as $component) { |
||
241 | array_push($output, $component->getComponent()); |
||
242 | } |
||
243 | |||
244 | if ($format === 'json') { |
||
245 | return json_encode($output, JSON_UNESCAPED_UNICODE); |
||
246 | } |
||
247 | |||
248 | return $output; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @param AbstractComponent $component |
||
253 | * @throws \ErrorException |
||
254 | * @return void |
||
255 | */ |
||
256 | public function setComponents($components) |
||
268 | } |
||
269 | } |
||
270 | } |
||
271 |