Complex classes like Presenty 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 Presenty, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Presenty |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * An instance's string. |
||
| 14 | * |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | protected $str; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The string's encoding, which should be one of the mbstring module's |
||
| 21 | * supported encodings. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $encoding; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Initializes a Stringy object and assigns both str and encoding properties |
||
| 29 | * the supplied values. $str is cast to a string prior to assignment, and if |
||
| 30 | * $encoding is not specified, it defaults to mb_internal_encoding(). Throws |
||
| 31 | * an InvalidArgumentException if the first argument is an array or object |
||
| 32 | * without a __toString method. |
||
| 33 | * |
||
| 34 | * @param mixed $str Value to modify, after being cast to string |
||
| 35 | * @param string $encoding The character encoding |
||
| 36 | * @throws \InvalidArgumentException if an array or object without a |
||
| 37 | * __toString method is passed as the first argument |
||
| 38 | */ |
||
| 39 | public function __construct($str = '', $encoding = null) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param $str |
||
| 50 | * |
||
| 51 | * @throws \InvalidArgumentException if an array or object without a |
||
| 52 | * __toString method is passed as the first argument |
||
| 53 | */ |
||
| 54 | public function validateArgument($str): void |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Creates a Stringy object and assigns both str and encoding properties |
||
| 71 | * the supplied values. $str is cast to a string prior to assignment, and if |
||
| 72 | * $encoding is not specified, it defaults to mb_internal_encoding(). It |
||
| 73 | * then returns the initialized object. Throws an InvalidArgumentException |
||
| 74 | * if the first argument is an array or object without a __toString method. |
||
| 75 | * |
||
| 76 | * @param mixed $str Value to modify, after being cast to string |
||
| 77 | * @param string $encoding The character encoding |
||
| 78 | * @return static A Stringy object |
||
| 79 | * @throws \InvalidArgumentException if an array or object without a __toString method is passed as the first argument |
||
| 80 | */ |
||
| 81 | public static function create($str = '', $encoding = null) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Returns the value in $str. |
||
| 88 | * |
||
| 89 | * @return string The current value of the $str property |
||
| 90 | */ |
||
| 91 | public function __toString() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Format money |
||
| 98 | * |
||
| 99 | * @param int $decimal |
||
| 100 | * @param $currency |
||
| 101 | * @return Presenty |
||
| 102 | */ |
||
| 103 | public function money(int $decimal = 2, $currency = '€'): self |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Format number |
||
| 122 | * |
||
| 123 | * @param int $decimal |
||
| 124 | * @param $dec_point |
||
| 125 | * @param $thousands_sep |
||
| 126 | * @return Presenty |
||
| 127 | */ |
||
| 128 | public function number(int $decimal = 0, $dec_point = ',', $thousands_sep = '.'): self |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Format boolean representation of string to translated yes/no string |
||
| 145 | * |
||
| 146 | * @param string $keyYes trans for yes |
||
| 147 | * @param string $keyNo trans for no |
||
| 148 | * @return Presenty |
||
| 149 | */ |
||
| 150 | public function boolean($keyYes = 'si', $keyNo = 'no'): self |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | public function isBooleanYes(): bool |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Format url |
||
| 181 | * |
||
| 182 | * @param integer $len |
||
| 183 | * @return Presenty |
||
| 184 | */ |
||
| 185 | public function url(int $len = 50): self |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Format text |
||
| 196 | * |
||
| 197 | * @param integer $len |
||
| 198 | * @return Presenty |
||
| 199 | */ |
||
| 200 | public function description(int $len = 50): self |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Format html anchor |
||
| 211 | * |
||
| 212 | * @param array $arrAnchorAttributes |
||
| 213 | * @return Presenty |
||
| 214 | */ |
||
| 215 | public function anchor(array $arrAnchorAttributes = []): self |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Format mailto |
||
| 234 | * @param array $arrAnchorAttributes |
||
| 235 | * @param string $label |
||
| 236 | * @return Presenty |
||
| 237 | */ |
||
| 238 | public function mailto(?array $arrAnchorAttributes = [], string $label = ''): self |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param string $classPositiveNumber |
||
| 257 | * @param string $classNegativeNumber |
||
| 258 | * @return Presenty |
||
| 259 | */ |
||
| 260 | public function bkgPositiveOrNegative(string $classPositiveNumber = 'label label-success', string $classNegativeNumber = 'label label-danger'): self |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Format date ita |
||
| 278 | * |
||
| 279 | * @return Presenty |
||
| 280 | */ |
||
| 281 | public function dateIta(): self |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Formats an array of strings or numbers to a single chained string. |
||
| 290 | * Automatically trims spaces from letters. |
||
| 291 | * If $excludeZeroNumber is set to TRUE it recognizes 0 as a falsy value and it will be ignored. |
||
| 292 | * @param array $array |
||
| 293 | * @param string $implodeString |
||
| 294 | * @param boolean $excludeZeroNumber |
||
| 295 | * @return Presenty |
||
| 296 | */ |
||
| 297 | public function implode( |
||
| 321 | } |
||
| 322 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.