Complex classes like XrefTokenResolver 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 XrefTokenResolver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class XrefTokenResolver |
||
| 11 | { |
||
| 12 | /** @var TokenResolverInterface */ |
||
| 13 | protected $tokenResolver; |
||
| 14 | /** @var Xref */ |
||
| 15 | protected $xref; |
||
| 16 | /** @var array */ |
||
| 17 | protected $registeredTokenValues; |
||
| 18 | /** @var boolean */ |
||
| 19 | protected $ignoreUnknownFilters; |
||
| 20 | /** @var string */ |
||
| 21 | protected $tokenRegex; |
||
| 22 | /** @var string */ |
||
| 23 | protected $tokenPrefix; |
||
| 24 | /** @var string */ |
||
| 25 | protected $tokenSuffix; |
||
| 26 | /** @var string */ |
||
| 27 | protected $tokenFilterDelimiter; |
||
| 28 | /** @var TokenParser */ |
||
| 29 | protected $tokenParser; |
||
| 30 | |||
| 31 | 5 | public function __construct(TokenResolverInterface $tokenResolver = null) |
|
| 35 | |||
| 36 | /** |
||
| 37 | * Check if token resolver was set. |
||
| 38 | * |
||
| 39 | * @return boolean |
||
| 40 | */ |
||
| 41 | 5 | public function hasTokenResolver() |
|
| 45 | |||
| 46 | /** |
||
| 47 | * Get token resolver. |
||
| 48 | * |
||
| 49 | * @return TokenResolverInterface|null |
||
| 50 | */ |
||
| 51 | 5 | public function getTokenResolver() |
|
| 52 | { |
||
| 53 | 5 | if (!$this->hasTokenResolver()) { |
|
| 54 | return null; |
||
| 55 | } |
||
| 56 | 5 | return $this->tokenResolver; |
|
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Set token resolver. |
||
| 61 | * |
||
| 62 | * @param TokenResolverInterface|null $tokenResolver The new value for token resolver or null for none. |
||
| 63 | * @return $this |
||
| 64 | */ |
||
| 65 | 5 | public function setTokenResolver($tokenResolver = null) |
|
| 66 | { |
||
| 67 | 5 | $this->tokenResolver = $tokenResolver; |
|
| 68 | 5 | return $this; |
|
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Check if xref was set. |
||
| 73 | * |
||
| 74 | * @return boolean |
||
| 75 | */ |
||
| 76 | 4 | public function hasXref() |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Get xref. |
||
| 83 | * |
||
| 84 | * @return Xref|null |
||
| 85 | */ |
||
| 86 | public function getXref() |
||
| 87 | { |
||
| 88 | if (!$this->hasXref()) { |
||
| 89 | return null; |
||
| 90 | } |
||
| 91 | return $this->xref; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Set xref. |
||
| 96 | * |
||
| 97 | * @param Xref|null $xref The new value for xref or null for none. |
||
| 98 | * @return $this |
||
| 99 | */ |
||
| 100 | public function setXref($xref = null) |
||
| 101 | { |
||
| 102 | $this->xref = $xref; |
||
| 103 | return $this; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Check if registered token values was set. |
||
| 108 | * |
||
| 109 | * @return boolean |
||
| 110 | */ |
||
| 111 | 4 | public function hasRegisteredTokenValues() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Get registered token values. |
||
| 118 | * |
||
| 119 | * @return array|null |
||
| 120 | */ |
||
| 121 | 4 | public function getRegisteredTokenValues() |
|
| 122 | { |
||
| 123 | 4 | if (!$this->hasRegisteredTokenValues()) { |
|
| 124 | return null; |
||
| 125 | } |
||
| 126 | 4 | return $this->registeredTokenValues; |
|
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Set registered token values. |
||
| 131 | * |
||
| 132 | * @param array|null $registeredTokenValues The new value for registered token values or null for none. |
||
| 133 | * @return $this |
||
| 134 | */ |
||
| 135 | 5 | public function setRegisteredTokenValues($registeredTokenValues = null) |
|
| 136 | { |
||
| 137 | 5 | $this->registeredTokenValues = $registeredTokenValues; |
|
|
|
|||
| 138 | 5 | return $this; |
|
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Check if ignore unknown filters was set. |
||
| 143 | * |
||
| 144 | * @return boolean |
||
| 145 | */ |
||
| 146 | public function hasIgnoreUnknownFilters() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get ignore unknown filters. |
||
| 153 | * |
||
| 154 | * @return boolean|null |
||
| 155 | */ |
||
| 156 | public function getIgnoreUnknownFilters() |
||
| 157 | { |
||
| 158 | if (!$this->hasIgnoreUnknownFilters()) { |
||
| 159 | return null; |
||
| 160 | } |
||
| 161 | return $this->ignoreUnknownFilters; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Set ignore unknown filters. |
||
| 166 | * |
||
| 167 | * @param boolean|null $ignoreUnknownFilters The new value for ignore unknown filters or null for default. |
||
| 168 | * @return $this |
||
| 169 | */ |
||
| 170 | public function setIgnoreUnknownFilters($ignoreUnknownFilters = null) |
||
| 171 | { |
||
| 172 | $this->ignoreUnknownFilters = $ignoreUnknownFilters; |
||
| 173 | return $this; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Check if token regex was set. |
||
| 178 | * |
||
| 179 | * @return boolean |
||
| 180 | */ |
||
| 181 | 5 | public function hasTokenRegex() |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Get token regex. |
||
| 188 | * |
||
| 189 | * @return string|null |
||
| 190 | */ |
||
| 191 | public function getTokenRegex() |
||
| 192 | { |
||
| 193 | if (!$this->hasTokenRegex()) { |
||
| 194 | return null; |
||
| 195 | } |
||
| 196 | return $this->tokenRegex; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Set token regex. |
||
| 201 | * |
||
| 202 | * @param string|null $tokenRegex The new value for token regex or null for default. |
||
| 203 | * @return $this |
||
| 204 | */ |
||
| 205 | public function setTokenRegex($tokenRegex = null) |
||
| 206 | { |
||
| 207 | $this->tokenRegex = $tokenRegex; |
||
| 208 | if ($this->tokenRegex !== $tokenRegex) { |
||
| 209 | $this->tokenParser = null; |
||
| 210 | } |
||
| 211 | return $this; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Check if token prefix was set. |
||
| 216 | * |
||
| 217 | * @return boolean |
||
| 218 | */ |
||
| 219 | 5 | public function hasTokenPrefix() |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Get token prefix. |
||
| 226 | * |
||
| 227 | * @return string|null |
||
| 228 | */ |
||
| 229 | 5 | public function getTokenPrefix() |
|
| 230 | { |
||
| 231 | 5 | if (!$this->hasTokenPrefix()) { |
|
| 232 | return null; |
||
| 233 | } |
||
| 234 | 5 | return $this->tokenPrefix; |
|
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Set token prefix. |
||
| 239 | * |
||
| 240 | * @param string|null $tokenPrefix The new value for token prefix or null for default. |
||
| 241 | * @return $this |
||
| 242 | */ |
||
| 243 | 5 | public function setTokenPrefix($tokenPrefix = null) |
|
| 244 | { |
||
| 245 | 5 | $this->tokenPrefix = $tokenPrefix; |
|
| 246 | 5 | if ($this->tokenPrefix !== $tokenPrefix) { |
|
| 247 | $this->tokenParser = null; |
||
| 248 | } |
||
| 249 | 5 | return $this; |
|
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Check if token suffix was set. |
||
| 254 | * |
||
| 255 | * @return boolean |
||
| 256 | */ |
||
| 257 | 5 | public function hasTokenSuffix() |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Get token suffix. |
||
| 264 | * |
||
| 265 | * @return string|null |
||
| 266 | */ |
||
| 267 | 5 | public function getTokenSuffix() |
|
| 268 | { |
||
| 269 | 5 | if (!$this->hasTokenSuffix()) { |
|
| 270 | return null; |
||
| 271 | } |
||
| 272 | 5 | return $this->tokenSuffix; |
|
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Set token suffix. |
||
| 277 | * |
||
| 278 | * @param string|null $tokenSuffix The new value for token suffix or null for default. |
||
| 279 | * @return $this |
||
| 280 | */ |
||
| 281 | 5 | public function setTokenSuffix($tokenSuffix = null) |
|
| 282 | { |
||
| 283 | 5 | $this->tokenSuffix = $tokenSuffix; |
|
| 284 | 5 | if ($this->tokenSuffix !== $tokenSuffix) { |
|
| 285 | $this->tokenParser = null; |
||
| 286 | } |
||
| 287 | 5 | return $this; |
|
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Check if token filter delimiter was set. |
||
| 292 | * |
||
| 293 | * @return boolean |
||
| 294 | */ |
||
| 295 | 5 | public function hasTokenFilterDelimiter() |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Get token filter delimiter. |
||
| 302 | * |
||
| 303 | * @return string|null |
||
| 304 | */ |
||
| 305 | public function getTokenFilterDelimiter() |
||
| 306 | { |
||
| 307 | if (!$this->hasTokenFilterDelimiter()) { |
||
| 308 | return null; |
||
| 309 | } |
||
| 310 | return $this->tokenFilterDelimiter; |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Set token filter delimiter. |
||
| 315 | * |
||
| 316 | * @param string|null $tokenFilterDelimiter The new value for token filter delimiter or null for default. |
||
| 317 | * @return $this |
||
| 318 | */ |
||
| 319 | public function setTokenFilterDelimiter($tokenFilterDelimiter = null) |
||
| 327 | |||
| 328 | 5 | public function getTokenParser() |
|
| 345 | |||
| 346 | 5 | public function resolve(TokenCollection $tokens) |
|
| 351 | } |
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needlebut will only accept an array as$haystack.