Complex classes like GcmComponent 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 GcmComponent, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class GcmComponent extends Component { |
||
17 | |||
18 | /** |
||
19 | * Default options |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $_defaults = array( |
||
24 | 'api' => array( |
||
25 | 'key' => '', |
||
26 | 'url' => 'https://gcm-http.googleapis.com/gcm/send' |
||
27 | ), |
||
28 | 'parameters' => array( |
||
29 | 'collapse_key' => null, |
||
30 | 'priority' => 'normal', |
||
31 | 'delay_while_idle' => false, |
||
32 | 'dry_run' => false, |
||
33 | 'time_to_live' => 2419200, |
||
34 | 'restricted_package_name' => null |
||
35 | ) |
||
36 | ); |
||
37 | |||
38 | /** |
||
39 | * Error code and message. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $_errorMessages = array(); |
||
44 | |||
45 | /** |
||
46 | * Response of the request |
||
47 | * |
||
48 | * @var object |
||
49 | */ |
||
50 | protected $_response = null; |
||
51 | |||
52 | /** |
||
53 | * Controller reference |
||
54 | */ |
||
55 | protected $Controller = null; |
||
56 | |||
57 | /** |
||
58 | * A Component collection, used to get more components. |
||
59 | * |
||
60 | * @var ComponentCollection |
||
61 | */ |
||
62 | protected $Collection; |
||
63 | |||
64 | /** |
||
65 | * Constructor |
||
66 | * |
||
67 | * @param ComponentCollection $collection |
||
68 | * @param array $settings |
||
69 | */ |
||
70 | public function __construct(ComponentCollection $collection, $settings = array()) { |
||
81 | |||
82 | /** |
||
83 | * Called before the Controller::beforeFilter(). |
||
84 | * |
||
85 | * @param Controller $controller Controller with components to initialize |
||
86 | * @return void |
||
87 | */ |
||
88 | public function initialize(Controller $controller) { |
||
91 | |||
92 | /** |
||
93 | * send method |
||
94 | * |
||
95 | * @param string|array $ids |
||
96 | * @param array $payload |
||
97 | * @param array $parameters |
||
98 | * @return boolean |
||
99 | */ |
||
100 | public function send($ids = false, $payload = array(), $parameters = array()) { |
||
144 | |||
145 | /** |
||
146 | * sendNotification method |
||
147 | * |
||
148 | * @param string|array $ids |
||
149 | * @param array $notification |
||
150 | * @param array $parameters |
||
151 | * @return boolean |
||
152 | */ |
||
153 | public function sendNotification($ids = false, $notification = array(), $parameters = array()) { |
||
156 | |||
157 | /** |
||
158 | * sendData method |
||
159 | * |
||
160 | * @param string|array $ids |
||
161 | * @param array $data |
||
162 | * @param array $parameters |
||
163 | * @return boolean |
||
164 | */ |
||
165 | public function sendData($ids = false, $data = array(), $parameters = array()) { |
||
168 | |||
169 | /** |
||
170 | * response method |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | public function response() { |
||
181 | |||
182 | /** |
||
183 | * _executePush method |
||
184 | * |
||
185 | * @param json $notification |
||
186 | * @return bool |
||
187 | */ |
||
188 | protected function _executePush($notification = false) { |
||
211 | |||
212 | /** |
||
213 | * _buildMessage method |
||
214 | * |
||
215 | * @param array $ids |
||
216 | * @param array $payload |
||
217 | * @param array $parameters |
||
218 | * @return false|string |
||
219 | */ |
||
220 | protected function _buildMessage($ids = false, $payload = false, $parameters = false) { |
||
237 | |||
238 | /** |
||
239 | * _checkNotification method |
||
240 | * |
||
241 | * @param array $notification |
||
242 | * @return array $notification |
||
243 | */ |
||
244 | protected function _checkNotification($notification = false) { |
||
263 | |||
264 | /** |
||
265 | * _checkData method |
||
266 | * |
||
267 | * @param array $data |
||
268 | * @return array $data |
||
269 | */ |
||
270 | public function _checkData($data = false) { |
||
290 | |||
291 | /** |
||
292 | * _checkParameters method |
||
293 | * |
||
294 | * @param array $parameters |
||
295 | * @return array $parameters |
||
296 | */ |
||
297 | protected function _checkParameters($parameters = false) { |
||
319 | } |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.