Complex classes like AbstractProxy 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 AbstractProxy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | abstract class AbstractProxy |
||
| 24 | { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Indent for source code |
||
| 28 | * |
||
| 29 | * @var int |
||
| 30 | */ |
||
| 31 | protected $indent = 4; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * List of advices that are used for generation of child |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $advices = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * PHP expression string for accessing LSB information |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected static $staticLsbExpression = 'static::class'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Constructs an abstract proxy class |
||
| 49 | * |
||
| 50 | * @param array $advices List of advices |
||
| 51 | */ |
||
| 52 | 4 | public function __construct(array $advices = []) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Returns text representation of class |
||
| 59 | * |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | abstract public function __toString(); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Indent block of code |
||
| 66 | * |
||
| 67 | * @param string $text Non-indented text |
||
| 68 | * |
||
| 69 | * @return string Indented text |
||
| 70 | */ |
||
| 71 | 4 | protected function indent($text) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Returns list of string representation of parameters |
||
| 83 | * |
||
| 84 | * @param array|ReflectionParameter[] $parameters List of parameters |
||
| 85 | * |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | 4 | protected function getParameters(array $parameters) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Return string representation of parameter |
||
| 100 | * |
||
| 101 | * @param ReflectionParameter $parameter Reflection parameter |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | 2 | protected function getParameterCode(ReflectionParameter $parameter) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Replace concrete advices with list of ids |
||
| 129 | * |
||
| 130 | * @param array $advices |
||
| 131 | * |
||
| 132 | * @return array flatten list of advices |
||
| 133 | */ |
||
| 134 | 4 | private function flattenAdvices(array $advices) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Prepares a line with args from the method definition |
||
| 150 | * |
||
| 151 | * @param ReflectionFunctionAbstract $functionLike |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | 4 | protected function prepareArgsLine(ReflectionFunctionAbstract $functionLike) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Creates a function code from Reflection |
||
| 186 | * |
||
| 187 | * @param ReflectionFunctionAbstract $functionLike Reflection for method |
||
| 188 | * @param string $body Body of method |
||
| 189 | * |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | 4 | protected function getOverriddenFunction(ReflectionFunctionAbstract $functionLike, $body) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Resolves parameter typehint |
||
| 234 | * |
||
| 235 | * @param ReflectionParameter $parameter Instance of reflection parameter |
||
| 236 | * |
||
| 237 | * @return string Resolved type name (FQN for classes) |
||
| 238 | */ |
||
| 239 | 2 | protected function resolveParameterTypeName(ReflectionParameter $parameter) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Resolves type name from |
||
| 266 | * |
||
| 267 | * @param string $typeName Raw name of the type |
||
| 268 | * @param ReflectionType|null $reflectionType Type (if present for PHP7) |
||
| 269 | * |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | 2 | private function resolveTypeName($typeName, $reflectionType = null) |
|
| 294 | } |
||
| 295 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: