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 GoogleFonts 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 GoogleFonts, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class GoogleFonts |
||
| 7 | { |
||
| 8 | |||
| 9 | const WEBFONTURL = 'https://www.googleapis.com/webfonts/v1/webfonts'; |
||
| 10 | |||
| 11 | const SORT_BY = [ |
||
| 12 | 'alpha', |
||
| 13 | 'date', |
||
| 14 | 'popularity', |
||
| 15 | 'style', |
||
| 16 | 'trending' |
||
| 17 | ]; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Google API key string |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private $apiKey; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The location where the temporary JSON file should be stored |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $file_location; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * how fonts should be sorted when retrieved from Google |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | public $sortOrder = 'popularity'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * This is the original font array retrieved from Google |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $fontList; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * This is the array of fonts ordered into types |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $orderedList; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Error message to be displayed if no fonts exists |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | public $error_message = 'Error: No fonts exist with the given parameters'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Constructor |
||
| 57 | * @param string|false $apiKey This should either be set to your Google API key or left empty |
||
| 58 | */ |
||
| 59 | 1 | public function __construct($apiKey = false) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Sets the Google API Key |
||
| 67 | * @param string|false $apiKey This needs to be your Google API Key |
||
| 68 | * @return $this |
||
| 69 | */ |
||
| 70 | 1 | public function setApiKey($apiKey) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Returns the Google API key if it has been set else will return false |
||
| 80 | * @return string|false This sill be the set Google API key or false |
||
| 81 | */ |
||
| 82 | 1 | public function getApiKey() |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Sets the file locations where the font lists are stored |
||
| 92 | * @param string $location This should be the location that you wish to store the font list files |
||
| 93 | * @return $this |
||
| 94 | */ |
||
| 95 | 1 | public function setFontFileLocation($location) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * This is the location where the fonts file is stored |
||
| 108 | * @return string Returns the file storage location |
||
| 109 | */ |
||
| 110 | 1 | public function getFontFileLocation() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Returns an array of weights available |
||
| 117 | * @return array|string If any weights exist will return an array else will return the error_message |
||
| 118 | */ |
||
| 119 | public function getFontWeights() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns an array of subsets available |
||
| 126 | * @return array|string If any subsets exist will return an array else will return the error_message |
||
| 127 | */ |
||
| 128 | public function getFontSubsets() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Returns an array of types/categories available |
||
| 135 | * @return array|string If any types/categories exist will return an array else will return the error_message |
||
| 136 | */ |
||
| 137 | public function getFontTypes() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Returns all of the fonts available with the selected weight/italic value |
||
| 144 | * @param string $weight This should be the weight value |
||
| 145 | * @return array|string |
||
| 146 | */ |
||
| 147 | public function getFontsByWeight($weight) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Returns an array of fonts available with a given subset |
||
| 160 | * @param string $subset This should be the subset you ant to list the fonts by |
||
| 161 | * @return array|string If any fonts exists an array will be returned else the error message will be returned |
||
| 162 | */ |
||
| 163 | public function getFontsBySubset($subset) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Returns an array of fonts available with a given type/category |
||
| 170 | * @param string $style This should be the font type that you want to list fonts by |
||
| 171 | * @return array|string If any fonts exists an array will be returned else the error message will be returned |
||
| 172 | */ |
||
| 173 | public function getFontsByType($style) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Sorts all of the retrieve fonts into a custom array |
||
| 180 | * @param array|string $types This should be what you want to sort the fonts on |
||
| 181 | * @param array $font this should be the font information array |
||
| 182 | * @param string $style The main array item that you want to sort the font within e.g. weight, subset or category |
||
| 183 | */ |
||
| 184 | protected function sortFontType($types, $font, $style = 'type') |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Retrieve a list of all of the fonts from Google Fonts API |
||
| 197 | */ |
||
| 198 | protected function retrieveFonts() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * The Google fonts URL will be returned with the path information |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public function googleFontsURI() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Builds the formatted URI path to retrieve the list of fonts from Google |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | protected function buildQueryString() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Retrieves the ordered Google Fonts file |
||
| 232 | * @return boolean Returns true on success and false on failure |
||
| 233 | */ |
||
| 234 | protected function getJSONFile() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Sorts all of the fonts into a custom JSON file |
||
| 247 | * @return boolean If the file has successfully been created will return true else retruns false |
||
| 248 | */ |
||
| 249 | protected function sortFonts() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Creates the temporary file containing the list of fonts |
||
| 265 | * @return boolean Returns true on success false on failure |
||
| 266 | */ |
||
| 267 | protected function createJSONFile() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * List all of the types available |
||
| 276 | * @param string $list The type that you are listing |
||
| 277 | * @return array|string If any types exist an array will be returned else will return the error message |
||
| 278 | */ |
||
| 279 | View Code Duplication | protected function listFontTypes($list = 'weight') |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Returns an ordered list of the Google Fonts available |
||
| 292 | * @param string $option This should be the $option that you want all of the fonts for |
||
| 293 | * @param string $list This needs to be the value that you are searching on |
||
| 294 | * @return array|string If any fonts exist for the given parameters an array will be returned else will return the error message |
||
| 295 | */ |
||
| 296 | View Code Duplication | protected function listFonts($option, $list = 'weight') |
|
| 305 | } |
||
| 306 |