Complex classes like Project 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Project, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Project |
||
16 | { |
||
17 | /** |
||
18 | * The name of the project |
||
19 | * @var string |
||
20 | */ |
||
21 | private $name; |
||
22 | |||
23 | /** |
||
24 | * The account the project is associated with |
||
25 | * @var integer |
||
26 | */ |
||
27 | private $accountId; |
||
28 | |||
29 | /** |
||
30 | * The significance level at which you would like to declare winning and |
||
31 | * losing variations. A lower number minimizes the time needed to declare a |
||
32 | * winning or losing variation, but increases the risk that your results |
||
33 | * aren't true winners and losers. The precision for this number is up to 4 |
||
34 | * decimal places. |
||
35 | * |
||
36 | * @var number |
||
37 | */ |
||
38 | private $confidenceThreshold; |
||
39 | |||
40 | /** |
||
41 | * The ID of a Dynamic Customer Profile Service associated with this project. |
||
42 | * @var integer |
||
43 | */ |
||
44 | private $dcpServiceId; |
||
45 | |||
46 | /** |
||
47 | * The platform of the project. Can be 'web', 'ios', 'android' or 'custom'. |
||
48 | * @var string |
||
49 | */ |
||
50 | private $platform; |
||
51 | |||
52 | /** |
||
53 | * The language to generate example code in. |
||
54 | * @var array[string] |
||
55 | */ |
||
56 | private $sdks; |
||
57 | |||
58 | /** |
||
59 | * The current status of the project. Can be 'active' or 'archived'. |
||
60 | * @var string |
||
61 | */ |
||
62 | private $status; |
||
63 | |||
64 | /** |
||
65 | * Web snippet-specific configuration for this project. |
||
66 | * @var WebSnippet |
||
67 | */ |
||
68 | private $webSnippet; |
||
69 | |||
70 | /** |
||
71 | * The time that the project was originally created |
||
72 | * @var string |
||
73 | */ |
||
74 | private $created; |
||
75 | |||
76 | /** |
||
77 | * The unique identifier for the project. |
||
78 | * @var integer |
||
79 | */ |
||
80 | private $id; |
||
81 | |||
82 | /** |
||
83 | * Whether or not this project was created under Optimizely Classic. |
||
84 | * @var boolean |
||
85 | */ |
||
86 | private $isClassic; |
||
87 | |||
88 | /** |
||
89 | * The time the project was last modified |
||
90 | * @var string |
||
91 | */ |
||
92 | private $lastModified; |
||
93 | |||
94 | /** |
||
95 | * The token used to identify your mobile app to Optimizely. (mobile only) |
||
96 | * @var string |
||
97 | 8 | */ |
|
98 | private $socketToken; |
||
99 | 8 | ||
100 | /** |
||
101 | 6 | * Constructor. |
|
102 | 6 | */ |
|
103 | 6 | public function __construct($options = array()) |
|
129 | 3 | ||
130 | 3 | /** |
|
131 | 3 | * Returns this object as array. |
|
132 | 3 | */ |
|
133 | 3 | public function toArray() |
|
160 | 7 | ||
161 | 7 | public function getName() |
|
165 | 5 | ||
166 | public function setName($name) |
||
170 | 7 | ||
171 | 7 | public function getAccountId() |
|
175 | 4 | ||
176 | public function setAccountId($accountId) |
||
180 | 3 | ||
181 | 3 | public function getConfidenceThreshold() |
|
185 | 4 | ||
186 | public function setConfidenceThreshold($confidenceThreshold) |
||
190 | 3 | ||
191 | 3 | public function getDcpServiceId() |
|
195 | 4 | ||
196 | public function setDcpServiceId($dcpServiceId) |
||
200 | 3 | ||
201 | 3 | public function getPlatform() |
|
205 | 4 | ||
206 | public function setPlatform($platform) |
||
210 | 3 | ||
211 | 3 | public function getSdks() |
|
215 | 5 | ||
216 | public function setSdks($sdks) |
||
220 | 7 | ||
221 | 7 | public function getStatus() |
|
225 | 5 | ||
226 | public function setStatus($status) |
||
230 | 7 | ||
231 | 7 | public function getWebSnippet() |
|
235 | 5 | ||
236 | public function setWebSnippet($webSnippet) |
||
240 | 7 | ||
241 | 7 | public function getCreated() |
|
245 | 5 | ||
246 | public function setCreated($created) |
||
250 | 6 | ||
251 | 6 | public function getId() |
|
255 | 5 | ||
256 | public function setId($id) |
||
260 | 6 | ||
261 | 6 | public function getIsClassic() |
|
265 | 5 | ||
266 | public function setIsClassic($isClassic) |
||
270 | 6 | ||
271 | 6 | public function getLastModified() |
|
275 | |||
276 | public function setLastModified($lastModified) |
||
280 | |||
281 | public function getSocketToken() |
||
285 | |||
286 | public function setSocketToken($socketToken) |
||
290 | } |
||
291 | |||
294 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.