Complex classes like ReflectionFunctionLikeTrait 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 ReflectionFunctionLikeTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | trait ReflectionFunctionLikeTrait |
||
| 29 | { |
||
| 30 | use InitializationTrait; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var FunctionLike |
||
| 34 | */ |
||
| 35 | protected $functionLikeNode; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Namespace name |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $namespaceName = ''; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array|ReflectionParameter[] |
||
| 46 | */ |
||
| 47 | protected $parameters; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * {@inheritDoc} |
||
| 51 | */ |
||
| 52 | 2 | public function getClosureScopeClass() |
|
| 53 | { |
||
| 54 | 2 | $this->initializeInternalReflection(); |
|
| 55 | |||
| 56 | 2 | return forward_static_call('parent::getClosureScopeClass'); |
|
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritDoc} |
||
| 61 | */ |
||
| 62 | 2 | public function getClosureThis() |
|
| 63 | { |
||
| 64 | 2 | $this->initializeInternalReflection(); |
|
| 65 | |||
| 66 | 2 | return forward_static_call('parent::getClosureThis'); |
|
| 67 | } |
||
| 68 | |||
| 69 | 2 | public function getDocComment() |
|
| 70 | { |
||
| 71 | 2 | $docComment = $this->functionLikeNode->getDocComment(); |
|
| 72 | |||
| 73 | 2 | return $docComment ? $docComment->getText() : false; |
|
| 74 | } |
||
| 75 | |||
| 76 | 2 | public function getEndLine() |
|
| 77 | { |
||
| 78 | 2 | return $this->functionLikeNode->getAttribute('endLine'); |
|
| 79 | } |
||
| 80 | |||
| 81 | 2 | public function getExtension() |
|
| 82 | { |
||
| 83 | 2 | return null; |
|
| 84 | } |
||
| 85 | |||
| 86 | 2 | public function getExtensionName() |
|
| 87 | { |
||
| 88 | 2 | return false; |
|
| 89 | } |
||
| 90 | |||
| 91 | 9 | public function getFileName() |
|
| 92 | { |
||
| 93 | 9 | return $this->functionLikeNode->getAttribute('fileName'); |
|
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritDoc} |
||
| 98 | */ |
||
| 99 | 23 | public function getName() |
|
| 100 | { |
||
| 101 | 23 | if ($this->functionLikeNode instanceof Function_ || $this->functionLikeNode instanceof ClassMethod) { |
|
| 102 | 23 | $functionName = $this->functionLikeNode->name; |
|
| 103 | |||
| 104 | 23 | return $this->namespaceName ? $this->namespaceName . '\\' . $functionName : $functionName; |
|
| 105 | } |
||
| 106 | |||
| 107 | return false; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * {@inheritDoc} |
||
| 112 | */ |
||
| 113 | 9 | public function getNamespaceName() |
|
| 114 | { |
||
| 115 | 9 | return $this->namespaceName; |
|
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get the number of parameters that a function defines, both optional and required. |
||
| 120 | * |
||
| 121 | * @link http://php.net/manual/en/reflectionfunctionabstract.getnumberofparameters.php |
||
| 122 | * |
||
| 123 | * @return int |
||
| 124 | */ |
||
| 125 | 2 | public function getNumberOfParameters() |
|
| 126 | { |
||
| 127 | 2 | return count($this->functionLikeNode->getParams()); |
|
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get the number of required parameters that a function defines. |
||
| 132 | * |
||
| 133 | * @link http://php.net/manual/en/reflectionfunctionabstract.getnumberofrequiredparameters.php |
||
| 134 | * |
||
| 135 | * @return int |
||
| 136 | */ |
||
| 137 | 2 | public function getNumberOfRequiredParameters() |
|
| 138 | { |
||
| 139 | 2 | $requiredParameters = 0; |
|
| 140 | 2 | foreach ($this->getParameters() as $parameter) { |
|
| 141 | 2 | if (!$parameter->isOptional()) { |
|
| 142 | 2 | $requiredParameters++; |
|
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | 2 | return $requiredParameters; |
|
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritDoc} |
||
| 151 | */ |
||
| 152 | 10 | public function getParameters() |
|
| 153 | { |
||
| 154 | 10 | if (!isset($this->parameters)) { |
|
| 155 | 10 | $parameters = []; |
|
| 156 | |||
| 157 | 10 | foreach ($this->functionLikeNode->getParams() as $parameterIndex => $parameterNode) { |
|
| 158 | 10 | $reflectionParameter = new ReflectionParameter( |
|
| 159 | 10 | $this->getName(), |
|
| 160 | 10 | $parameterNode->name, |
|
| 161 | $parameterNode, |
||
| 162 | $parameterIndex, |
||
| 163 | $this |
||
| 164 | ); |
||
| 165 | 10 | $parameters[] = $reflectionParameter; |
|
| 166 | } |
||
| 167 | |||
| 168 | 10 | $this->parameters = $parameters; |
|
| 169 | } |
||
| 170 | |||
| 171 | 10 | return $this->parameters; |
|
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Gets the specified return type of a function |
||
| 176 | * |
||
| 177 | * @return \ReflectionType |
||
| 178 | * |
||
| 179 | * @link http://php.net/manual/en/reflectionfunctionabstract.getreturntype.php |
||
| 180 | */ |
||
| 181 | 1 | public function getReturnType() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritDoc} |
||
| 198 | */ |
||
| 199 | 2 | public function getShortName() |
|
| 200 | { |
||
| 201 | 2 | if ($this->functionLikeNode instanceof Function_ || $this->functionLikeNode instanceof ClassMethod) { |
|
| 202 | 2 | return $this->functionLikeNode->name; |
|
| 203 | } |
||
| 204 | |||
| 205 | return false; |
||
| 206 | } |
||
| 207 | |||
| 208 | 2 | public function getStartLine() |
|
| 209 | { |
||
| 210 | 2 | return $this->functionLikeNode->getAttribute('startLine'); |
|
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * {@inheritDoc} |
||
| 215 | */ |
||
| 216 | 2 | public function getStaticVariables() |
|
| 217 | { |
||
| 218 | 2 | $nodeTraverser = new NodeTraverser(false); |
|
| 219 | 2 | $variablesCollector = new StaticVariablesCollector($this); |
|
| 220 | 2 | $nodeTraverser->addVisitor($variablesCollector); |
|
| 221 | |||
| 222 | /* @see https://github.com/nikic/PHP-Parser/issues/235 */ |
||
| 223 | 2 | $nodeTraverser->traverse($this->functionLikeNode->getStmts() ?: array()); |
|
| 224 | |||
| 225 | 2 | return $variablesCollector->getStaticVariables(); |
|
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Checks if the function has a specified return type |
||
| 230 | * |
||
| 231 | * @return bool |
||
| 232 | * |
||
| 233 | * @link http://php.net/manual/en/reflectionfunctionabstract.hasreturntype.php |
||
| 234 | */ |
||
| 235 | 3 | public function hasReturnType() |
|
| 241 | |||
| 242 | /** |
||
| 243 | * {@inheritDoc} |
||
| 244 | */ |
||
| 245 | 2 | public function inNamespace() |
|
| 246 | { |
||
| 247 | 2 | return !empty($this->namespaceName); |
|
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritDoc} |
||
| 252 | */ |
||
| 253 | 1 | public function isClosure() |
|
| 254 | { |
||
| 255 | 1 | return $this->functionLikeNode instanceof Closure; |
|
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritDoc} |
||
| 260 | */ |
||
| 261 | 1 | public function isDeprecated() |
|
| 266 | |||
| 267 | /** |
||
| 268 | * {@inheritDoc} |
||
| 269 | */ |
||
| 270 | 1 | public function isGenerator() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * {@inheritDoc} |
||
| 284 | */ |
||
| 285 | 1 | public function isInternal() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * {@inheritDoc} |
||
| 293 | */ |
||
| 294 | 1 | public function isUserDefined() |
|
| 299 | |||
| 300 | /** |
||
| 301 | * {@inheritDoc} |
||
| 302 | */ |
||
| 303 | 1 | public function isVariadic() |
|
| 313 | |||
| 314 | /** |
||
| 315 | * {@inheritDoc} |
||
| 316 | */ |
||
| 317 | 2 | public function returnsReference() |
|
| 321 | } |
||
| 322 |