Complex classes like Token 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 Token, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Token |
||
| 15 | { |
||
| 16 | /** @var integer[] */ |
||
| 17 | protected $offsets; |
||
| 18 | /** @var string eg. [[tokenName|filter]] */ |
||
| 19 | protected $tokenString; |
||
| 20 | /** @var string eg. tokenName */ |
||
| 21 | protected $tokenName; |
||
| 22 | /** @var mixed */ |
||
| 23 | protected $tokenValue = null; |
||
| 24 | /** @var mixed */ |
||
| 25 | protected $unfilteredTokenValue = null; |
||
| 26 | /** @var boolean */ |
||
| 27 | protected $isResolved = False; |
||
| 28 | /** @var boolean */ |
||
| 29 | protected $isFiltered = False; |
||
| 30 | /** @var boolean */ |
||
| 31 | protected $isInjected = False; |
||
| 32 | /** @var string[] */ |
||
| 33 | protected $filters; |
||
| 34 | /** @var string[] */ |
||
| 35 | protected $unresolvedFilters; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param integer $offset The offset where the token string is positioned in the string to be injected. |
||
| 39 | * @param string $tokenString The token string. (eg. [[tokenName|filter1]]) |
||
| 40 | * @param string $tokenName The name part of the token string. |
||
| 41 | * @param string|null $tokenValue The resolved value of the token. Null if not resolved. |
||
| 42 | * @param array $filters Array of filter names applied to the value. |
||
| 43 | * @param array $unresolvedFilters Array of filter names that could not be applied to the value. |
||
| 44 | */ |
||
| 45 | 14 | public function __construct($offset, $tokenString, $tokenName, $tokenValue = null, array $filters = array(), |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Get the token string. |
||
| 59 | * |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | 14 | public function getTokenString() |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Get the token name. |
||
| 69 | * |
||
| 70 | * @return string |
||
| 71 | */ |
||
| 72 | 1 | public function getTokenName() |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Set token name. |
||
| 79 | * |
||
| 80 | * @param string $value The new value. |
||
| 81 | * @return $this |
||
| 82 | */ |
||
| 83 | 14 | public function setTokenName($value) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Check if the token value was set. |
||
| 91 | * |
||
| 92 | * @return boolean |
||
| 93 | */ |
||
| 94 | 8 | public function hasTokenValue() |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Get the token value. |
||
| 101 | * |
||
| 102 | * @return string|null |
||
| 103 | */ |
||
| 104 | 8 | public function getTokenValue() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Set the token value. |
||
| 114 | * |
||
| 115 | * @param string $value The new value. |
||
| 116 | * @return $this |
||
| 117 | */ |
||
| 118 | 14 | public function setTokenValue($value) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Check if the unfiltered token value was set. |
||
| 130 | * |
||
| 131 | * @return boolean |
||
| 132 | */ |
||
| 133 | 3 | public function hasUnfilteredTokenValue() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Get the unfiltered token value. |
||
| 140 | * |
||
| 141 | * @return string|null |
||
| 142 | */ |
||
| 143 | public function getUnfilteredTokenValue() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Set the unfiltered token value. |
||
| 153 | * |
||
| 154 | * @param string $value The new value. |
||
| 155 | * @return $this |
||
| 156 | */ |
||
| 157 | 14 | public function setUnfilteredTokenValue($value) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Get if the token value is resolved. |
||
| 169 | * |
||
| 170 | * @return boolean |
||
| 171 | */ |
||
| 172 | 14 | public function getIsResolved() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Set if the token value is resolved. |
||
| 179 | * |
||
| 180 | * @param boolean $value The new value. |
||
| 181 | * @return $this |
||
| 182 | */ |
||
| 183 | 14 | public function setIsResolved($value) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Get if the token value is filtered. |
||
| 191 | * |
||
| 192 | * @return boolean |
||
| 193 | */ |
||
| 194 | 1 | public function getIsFiltered() |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Set if the token value is filtered. |
||
| 201 | * |
||
| 202 | * @param boolean $value The new value. |
||
| 203 | * @return $this |
||
| 204 | */ |
||
| 205 | 14 | public function setIsFiltered($value) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Get if the token value was injected. |
||
| 213 | * |
||
| 214 | * @return boolean |
||
| 215 | */ |
||
| 216 | 7 | public function getIsInjected() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Set if the token value was injected. |
||
| 223 | * |
||
| 224 | * @param boolean $value The new value. |
||
| 225 | * @return $this |
||
| 226 | */ |
||
| 227 | 7 | public function setIsInjected($value) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Get the array of offsets. |
||
| 235 | * |
||
| 236 | * @return integer[] |
||
| 237 | */ |
||
| 238 | 7 | public function getOffsets() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Set the array of offsets. |
||
| 245 | * |
||
| 246 | * @param integer[] $value The new array of offsets. |
||
| 247 | * @return $this |
||
| 248 | */ |
||
| 249 | 14 | public function setOffsets($value) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Add a new offset. |
||
| 257 | * |
||
| 258 | * @param integer $offset |
||
| 259 | * @return $this |
||
| 260 | */ |
||
| 261 | 1 | public function addOffset($offset) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Adjust the given offset by adding the given delta value. |
||
| 269 | * |
||
| 270 | * @param integer $offset |
||
| 271 | * @param integer $delta |
||
| 272 | * @return $this |
||
| 273 | */ |
||
| 274 | 7 | public function adjustOffset($offset, $delta) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Check if the filters array was set. |
||
| 287 | * |
||
| 288 | * @return boolean |
||
| 289 | */ |
||
| 290 | 10 | public function hasFilters() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Get the filters array. |
||
| 297 | * |
||
| 298 | * @return string[] |
||
| 299 | */ |
||
| 300 | 1 | public function getFilters() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Set the filters array. |
||
| 307 | * |
||
| 308 | * @param string[] $value The new value. |
||
| 309 | * @return $this |
||
| 310 | */ |
||
| 311 | 14 | public function setFilters($value) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Check if the unresolved filters array was set. |
||
| 319 | * |
||
| 320 | * @return boolean |
||
| 321 | */ |
||
| 322 | 1 | public function hasUnresolvedFilters() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Get the unresolved filters array. |
||
| 329 | * |
||
| 330 | * @return string[] |
||
| 331 | */ |
||
| 332 | 1 | public function getUnresolvedFilters() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Set the unresolved filters array. |
||
| 339 | * |
||
| 340 | * @param string[] $value The new value. |
||
| 341 | * @return $this |
||
| 342 | */ |
||
| 343 | 14 | public function setUnresolvedFilters($value) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * @param TokenResolverInterface $tokenResolver |
||
| 351 | * @param boolean $ignoreUnknownTokens |
||
| 352 | * @return $this |
||
| 353 | */ |
||
| 354 | 14 | public function resolveUnfilteredValue(TokenResolverInterface $tokenResolver, $ignoreUnknownTokens = True) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @param boolean $ignoreUnknownFilters |
||
| 368 | * @throws UnknownFilterException |
||
| 369 | * @return $this |
||
| 370 | */ |
||
| 371 | 3 | public function applyFilters($ignoreUnknownFilters = True) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Attempt to resolve and filter the token value. |
||
| 392 | * |
||
| 393 | * @param TokenResolverInterface $tokenResolver |
||
| 394 | * @param boolean $ignoreUnknownTokens |
||
| 395 | * @param boolean $ignoreUnknownFilters |
||
| 396 | * @throws UnknownFilterException |
||
| 397 | * |
||
| 398 | * If using RegisteredTokenResolver: |
||
| 399 | * @throws UnknownTokenException If the token name was not registered and set not to ignore unknown tokens. |
||
| 400 | * |
||
| 401 | * If using ScopeTokenResolver: |
||
| 402 | * @throws UnknownTokenException |
||
| 403 | * @throws OutOfScopeException |
||
| 404 | * @throws ScopeTokenValueSerializationException |
||
| 405 | * @throws TokenFormatException |
||
| 406 | * @return $this |
||
| 407 | */ |
||
| 408 | 1 | public function resolve(TokenResolverInterface $tokenResolver, $ignoreUnknownTokens = True, |
|
| 422 | } |