Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 25 | class GcmComponent extends Component |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Default config |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $_defaultConfig = [ |
||
| 34 | 'api' => [ |
||
| 35 | 'key' => null, |
||
| 36 | 'url' => 'https://gcm-http.googleapis.com/gcm/send' |
||
| 37 | ], |
||
| 38 | 'parameters' => [ |
||
| 39 | 'collapse_key' => null, |
||
| 40 | 'priority' => 'normal', |
||
| 41 | 'delay_while_idle' => false, |
||
| 42 | 'dry_run' => false, |
||
| 43 | 'time_to_live' => 0, |
||
| 44 | 'restricted_package_name' => null |
||
| 45 | ] |
||
| 46 | ]; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Error code and message. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $_errorMessages = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Response of the request |
||
| 57 | * |
||
| 58 | * @var object |
||
| 59 | */ |
||
| 60 | protected $_response = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Constructor |
||
| 64 | * |
||
| 65 | * @param ComponentRegistry $collection A ComponentRegistry |
||
|
|
|||
| 66 | * @param array $config Array of configuration settings |
||
| 67 | */ |
||
| 68 | public function __construct(ComponentRegistry $registry, array $config = []) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * send method |
||
| 80 | * |
||
| 81 | * @param string|array $ids |
||
| 82 | * @param array $payload |
||
| 83 | * @param array $parameters |
||
| 84 | * @return boolean |
||
| 85 | */ |
||
| 86 | public function send($ids = false, array $payload = [], array $parameters = []) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * sendNotification method |
||
| 133 | * |
||
| 134 | * @param string|array $ids |
||
| 135 | * @param array $notification |
||
| 136 | * @param array $parameters |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | public function sendNotification($ids = false, array $notification = [], array $parameters = []) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * sendData method |
||
| 146 | * |
||
| 147 | * @param string|array $ids |
||
| 148 | * @param array $data |
||
| 149 | * @param array $parameters |
||
| 150 | * @return void |
||
| 151 | */ |
||
| 152 | public function sendData($ids = false, array $data = [], array $parameters = []) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * response method |
||
| 159 | * |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | public function response() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * _executePush method |
||
| 173 | * |
||
| 174 | * @param json $message |
||
| 175 | * @return boolean |
||
| 176 | */ |
||
| 177 | protected function _executePush($message = false) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * _buildMessage method |
||
| 205 | * |
||
| 206 | * @param array $ids |
||
| 207 | * @param array $payload |
||
| 208 | * @param array $parameters |
||
| 209 | * @return json |
||
| 210 | */ |
||
| 211 | protected function _buildMessage($ids = false, $payload = false, $parameters = false) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * _checkNotification method |
||
| 232 | * |
||
| 233 | * @param array $notification |
||
| 234 | * @return array $notification |
||
| 235 | */ |
||
| 236 | protected function _checkNotification($notification = false) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * _checkData method |
||
| 259 | * |
||
| 260 | * @param array $data |
||
| 261 | * @return array $data |
||
| 262 | */ |
||
| 263 | public function _checkData($data = false) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * _checkParameters method |
||
| 287 | * |
||
| 288 | * @param array $parameters |
||
| 289 | * @return array $parameters |
||
| 290 | */ |
||
| 291 | protected function _checkParameters($parameters = false) |
||
| 314 | } |
||
| 315 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.