Complex classes like Gravatar 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 Gravatar, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | 1 | final class Gravatar extends \Nette\Object |
|
| 37 | { |
||
| 38 | /** |
||
| 39 | * Define class name |
||
| 40 | */ |
||
| 41 | const CLASS_NAME = __CLASS__; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string - URL constants for the avatar images |
||
| 45 | */ |
||
| 46 | const HTTP_URL = 'http://www.gravatar.com/avatar/'; |
||
| 47 | const HTTPS_URL = 'https://secure.gravatar.com/avatar/'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var int |
||
| 51 | */ |
||
| 52 | private $expiration = 172800; // two days |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The size to use for avatars. |
||
| 56 | * |
||
| 57 | * @var int |
||
| 58 | */ |
||
| 59 | private $size = 80; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The default image to use |
||
| 63 | * Either a string of the gravatar-recognized default image "type" to use, a URL, or FALSE if using the...default gravatar default image (hah) |
||
| 64 | * |
||
| 65 | * @var mixed |
||
| 66 | */ |
||
| 67 | private $defaultImage = FALSE; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The maximum rating to allow for the avatar. |
||
| 71 | * |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | private $maxRating = 'g'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Should we use the secure (HTTPS) URL base? |
||
| 78 | * |
||
| 79 | * @var bool |
||
| 80 | */ |
||
| 81 | private $useSecureUrl = FALSE; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var Utils\Image |
||
| 85 | */ |
||
| 86 | private $image; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | private $type; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var bool |
||
| 95 | */ |
||
| 96 | private $hashEmail = TRUE; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var Caching\Cache |
||
| 100 | */ |
||
| 101 | private $cache; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param Http\Request $httpRequest |
||
| 105 | * @param Caching\Cache $cache |
||
| 106 | */ |
||
| 107 | public function __construct( |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get the email hash to use (after cleaning the string) |
||
| 119 | * |
||
| 120 | * @param string|NULL $email |
||
| 121 | * |
||
| 122 | * @return string - The hashed form of the email, post cleaning |
||
| 123 | */ |
||
| 124 | public function getEmailHash(string $email = NULL) : string |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Set the avatar size to use |
||
| 141 | * |
||
| 142 | * @param int $size - The avatar size to use, must be less than 512 and greater than 0. |
||
| 143 | */ |
||
| 144 | public function setSize(int $size) |
||
| 150 | |||
| 151 | /** |
||
| 152 | 1 | * Get the currently set avatar size |
|
| 153 | 1 | * |
|
| 154 | * @return int |
||
| 155 | */ |
||
| 156 | public function getSize() : int |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param int $size |
||
| 163 | * |
||
| 164 | 1 | * @return bool |
|
| 165 | * |
||
| 166 | * @throws Exceptions\InvalidArgumentException |
||
| 167 | */ |
||
| 168 | 1 | public function isSizeValid(int $size) : bool |
|
| 176 | |||
| 177 | /** |
||
| 178 | 1 | * Set image cache expiration |
|
| 179 | 1 | * |
|
| 180 | * @param int $expiration |
||
| 181 | */ |
||
| 182 | public function setExpiration(int $expiration) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Set the default image to use for avatars |
||
| 189 | * |
||
| 190 | * @param mixed $image - The default image to use. Use boolean FALSE for the gravatar default, a string containing a valid image URL, or a string specifying a recognized gravatar "default". |
||
| 191 | 1 | * |
|
| 192 | * @throws Exceptions\InvalidArgumentException |
||
| 193 | */ |
||
| 194 | public function setDefaultImage($image) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get the current default image setting |
||
| 221 | * |
||
| 222 | 1 | * @param string|NULL $defaultImage |
|
| 223 | 1 | * |
|
| 224 | * @return mixed - False if no default image set, string if one is set |
||
| 225 | */ |
||
| 226 | 1 | public function getDefaultImage(string $defaultImage = NULL) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Set the maximum allowed rating for avatars. |
||
| 241 | * |
||
| 242 | 1 | * @param string $rating - The maximum rating to use for avatars ('g', 'pg', 'r', 'x'). |
|
| 243 | * |
||
| 244 | 1 | * @throws Exceptions\InvalidArgumentException |
|
| 245 | */ |
||
| 246 | public function setMaxRating(string $rating) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Get the current maximum allowed rating for avatars |
||
| 259 | * |
||
| 260 | 1 | * @param string|NULL $maxRating |
|
| 261 | * |
||
| 262 | * @return string - The string representing the current maximum allowed rating ('g', 'pg', 'r', 'x'). |
||
| 263 | */ |
||
| 264 | 1 | public function getMaxRating(string $maxRating = NULL) : string |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Returns the Nette\Image instance |
||
| 275 | * |
||
| 276 | * @return Utils\Image |
||
| 277 | */ |
||
| 278 | public function getImage() : Utils\Image |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns the type of a image |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function getImageType() : string |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Check if we are using the secure protocol for the image URLs |
||
| 295 | * |
||
| 296 | * @return bool - Are we supposed to use the secure protocol? |
||
| 297 | */ |
||
| 298 | public function usingSecureImages() : bool |
||
| 302 | 1 | ||
| 303 | 1 | /** |
|
| 304 | * Enable the use of the secure protocol for image URLs |
||
| 305 | */ |
||
| 306 | public function enableSecureImages() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Disable the use of the secure protocol for image URLs |
||
| 313 | */ |
||
| 314 | public function disableSecureImages() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Create gravatar image |
||
| 321 | * |
||
| 322 | * @param string|NULL $email |
||
| 323 | * @param int|NULL $size |
||
| 324 | * |
||
| 325 | * @return Utils\Image |
||
| 326 | * |
||
| 327 | * @throws Exceptions\InvalidArgumentException |
||
| 328 | */ |
||
| 329 | public function get(string $email = NULL, int $size = NULL) : Utils\Image |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Build the avatar URL based on the provided email address |
||
| 359 | * |
||
| 360 | * @param string|NULL $email |
||
| 361 | * @param int|NULL $size |
||
| 362 | 1 | * @param string|NULL $maxRating |
|
| 363 | * @param string|NULL $defaultImage |
||
| 364 | * |
||
| 365 | * @return string |
||
| 366 | * |
||
| 367 | 1 | * @throws Exceptions\InvalidArgumentException |
|
| 368 | */ |
||
| 369 | public function buildUrl(string $email = NULL, int $size = NULL, string $maxRating = NULL, string $defaultImage = NULL) : string |
||
| 382 | |||
| 383 | 1 | /** |
|
| 384 | * Checks if a gravatar exists for the email. It does this by checking for the presence of 404 in the header |
||
| 385 | 1 | * returned. Will return null if fsockopen fails, for example when the hostname cannot be resolved. |
|
| 386 | * |
||
| 387 | * @param string $email |
||
| 388 | * |
||
| 389 | 1 | * @return bool|NULL Boolean if we could connect, null if no connection to gravatar.com |
|
| 390 | 1 | */ |
|
| 391 | 1 | public function exists(string $email) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * @return Templating\Helpers |
||
| 408 | */ |
||
| 409 | public function createTemplateHelpers() : Templating\Helpers |
||
| 413 | |||
| 414 | /** |
||
| 415 | 1 | * @param string $email |
|
| 416 | * @param int|NULL $size |
||
| 417 | * @param string|NULL $maxRating |
||
| 418 | 1 | * @param string|NULL $defaultImage |
|
| 419 | * |
||
| 420 | * @return Http\Url |
||
| 421 | */ |
||
| 422 | 1 | private function createUrl(string $email, int $size = NULL, string $maxRating = NULL, string $defaultImage = NULL) : Http\Url |
|
| 447 | } |
||
| 448 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: