| Total Complexity | 71 |
| Total Lines | 491 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Methods 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.
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 Methods, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Methods |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var ClassLikeStorageProvider |
||
| 20 | */ |
||
| 21 | private $classlike_storage_provider; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var \Psalm\Config |
||
| 25 | */ |
||
| 26 | private $config; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array<string, MethodChecker> |
||
| 30 | */ |
||
| 31 | private $method_checkers = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var bool |
||
| 35 | */ |
||
| 36 | public $collect_references = false; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param ClassLikeStorageProvider $storage_provider |
||
| 40 | */ |
||
| 41 | public function __construct( |
||
| 42 | \Psalm\Config $config, |
||
| 43 | ClassLikeStorageProvider $storage_provider |
||
| 44 | ) { |
||
| 45 | $this->classlike_storage_provider = $storage_provider; |
||
| 46 | $this->config = $config; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Whether or not a given method exists |
||
| 51 | * |
||
| 52 | * @param string $method_id |
||
| 53 | * @param CodeLocation|null $code_location |
||
| 54 | * |
||
| 55 | * @return bool |
||
| 56 | */ |
||
| 57 | public function methodExists( |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $method_id |
||
| 135 | * |
||
| 136 | * @return array<int, \Psalm\Storage\FunctionLikeParameter> |
||
| 137 | */ |
||
| 138 | public function getMethodParams($method_id) |
||
| 139 | { |
||
| 140 | if ($method_id = $this->getDeclaringMethodId($method_id)) { |
||
| 141 | $storage = $this->getStorage($method_id); |
||
| 142 | |||
| 143 | return $storage->params; |
||
| 144 | } |
||
| 145 | |||
| 146 | throw new \UnexpectedValueException('Cannot get method params for ' . $method_id); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $method_id |
||
| 151 | * |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | public function isVariadic($method_id) |
||
| 155 | { |
||
| 156 | $method_id = (string) $this->getDeclaringMethodId($method_id); |
||
| 157 | |||
| 158 | list($fq_class_name, $method_name) = explode('::', $method_id); |
||
| 159 | |||
| 160 | return $this->classlike_storage_provider->get($fq_class_name)->methods[$method_name]->variadic; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param string $method_id |
||
| 165 | * @param string $self_class |
||
| 166 | * @param array<int, PhpParser\Node\Arg>|null $args |
||
| 167 | * |
||
| 168 | * @return Type\Union|null |
||
| 169 | */ |
||
| 170 | public function getMethodReturnType($method_id, &$self_class, array $args = null) |
||
| 171 | { |
||
| 172 | if ($this->config->use_phpdoc_methods_without_call) { |
||
| 173 | list($original_fq_class_name, $original_method_name) = explode('::', $method_id); |
||
| 174 | |||
| 175 | $original_class_storage = $this->classlike_storage_provider->get($original_fq_class_name); |
||
| 176 | |||
| 177 | if (isset($original_class_storage->pseudo_methods[strtolower($original_method_name)])) { |
||
| 178 | return $original_class_storage->pseudo_methods[strtolower($original_method_name)]->return_type; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | $declaring_method_id = $this->getDeclaringMethodId($method_id); |
||
| 183 | |||
| 184 | if (!$declaring_method_id) { |
||
| 185 | return null; |
||
| 186 | } |
||
| 187 | |||
| 188 | $appearing_method_id = $this->getAppearingMethodId($method_id); |
||
| 189 | |||
| 190 | if (!$appearing_method_id) { |
||
| 191 | return null; |
||
| 192 | } |
||
| 193 | |||
| 194 | list($appearing_fq_class_name, $appearing_method_name) = explode('::', $appearing_method_id); |
||
| 195 | |||
| 196 | $appearing_fq_class_storage = $this->classlike_storage_provider->get($appearing_fq_class_name); |
||
| 197 | |||
| 198 | if (!$appearing_fq_class_storage->user_defined && CallMap::inCallMap($appearing_method_id)) { |
||
| 199 | if ($appearing_method_id === 'Closure::fromcallable' |
||
| 200 | && isset($args[0]->value->inferredType) |
||
| 201 | && $args[0]->value->inferredType->isSingle() |
||
| 202 | ) { |
||
| 203 | foreach ($args[0]->value->inferredType->getTypes() as $atomic_type) { |
||
| 204 | if ($atomic_type instanceof Type\Atomic\TCallable || $atomic_type instanceof Type\Atomic\Fn) { |
||
| 205 | $callable_type = clone $atomic_type; |
||
| 206 | |||
| 207 | return new Type\Union([new Type\Atomic\Fn( |
||
| 208 | 'Closure', |
||
| 209 | $callable_type->params, |
||
| 210 | $callable_type->return_type |
||
| 211 | )]); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } |
||
| 215 | return CallMap::getReturnTypeFromCallMap($appearing_method_id); |
||
| 216 | } |
||
| 217 | |||
| 218 | $storage = $this->getStorage($declaring_method_id); |
||
| 219 | |||
| 220 | if ($storage->return_type) { |
||
| 221 | $self_class = $appearing_fq_class_name; |
||
| 222 | |||
| 223 | return clone $storage->return_type; |
||
| 224 | } |
||
| 225 | |||
| 226 | $class_storage = $this->classlike_storage_provider->get($appearing_fq_class_name); |
||
| 227 | |||
| 228 | if (!isset($class_storage->overridden_method_ids[$appearing_method_name])) { |
||
| 229 | return null; |
||
| 230 | } |
||
| 231 | |||
| 232 | foreach ($class_storage->overridden_method_ids[$appearing_method_name] as $overridden_method_id) { |
||
| 233 | $overridden_storage = $this->getStorage($overridden_method_id); |
||
| 234 | |||
| 235 | if ($overridden_storage->return_type) { |
||
| 236 | if ($overridden_storage->return_type->isNull()) { |
||
| 237 | return Type::getVoid(); |
||
| 238 | } |
||
| 239 | |||
| 240 | list($fq_overridden_class) = explode('::', $overridden_method_id); |
||
| 241 | |||
| 242 | $overridden_class_storage = |
||
| 243 | $this->classlike_storage_provider->get($fq_overridden_class); |
||
| 244 | |||
| 245 | $overridden_return_type = clone $overridden_storage->return_type; |
||
| 246 | |||
| 247 | if ($overridden_class_storage->template_types) { |
||
| 248 | $generic_types = []; |
||
| 249 | $overridden_return_type->replaceTemplateTypesWithStandins( |
||
| 250 | $overridden_class_storage->template_types, |
||
| 251 | $generic_types |
||
| 252 | ); |
||
| 253 | } |
||
| 254 | |||
| 255 | $self_class = $overridden_class_storage->name; |
||
| 256 | |||
| 257 | return $overridden_return_type; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | return null; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $method_id |
||
| 266 | * |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | public function getMethodReturnsByRef($method_id) |
||
| 270 | { |
||
| 271 | $method_id = $this->getDeclaringMethodId($method_id); |
||
| 272 | |||
| 273 | if (!$method_id) { |
||
| 274 | return false; |
||
| 275 | } |
||
| 276 | |||
| 277 | list($fq_class_name) = explode('::', $method_id); |
||
| 278 | |||
| 279 | $fq_class_storage = $this->classlike_storage_provider->get($fq_class_name); |
||
| 280 | |||
| 281 | if (!$fq_class_storage->user_defined && CallMap::inCallMap($method_id)) { |
||
| 282 | return false; |
||
| 283 | } |
||
| 284 | |||
| 285 | $storage = $this->getStorage($method_id); |
||
| 286 | |||
| 287 | return $storage->returns_by_ref; |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $method_id |
||
| 292 | * @param CodeLocation|null $defined_location |
||
| 293 | * |
||
| 294 | * @return CodeLocation|null |
||
| 295 | */ |
||
| 296 | public function getMethodReturnTypeLocation( |
||
| 297 | $method_id, |
||
| 298 | CodeLocation &$defined_location = null |
||
| 299 | ) { |
||
| 300 | $method_id = $this->getDeclaringMethodId($method_id); |
||
| 301 | |||
| 302 | if ($method_id === null) { |
||
| 303 | return null; |
||
| 304 | } |
||
| 305 | |||
| 306 | $storage = $this->getStorage($method_id); |
||
| 307 | |||
| 308 | if (!$storage->return_type_location) { |
||
| 309 | $overridden_method_ids = $this->getOverriddenMethodIds($method_id); |
||
| 310 | |||
| 311 | foreach ($overridden_method_ids as $overridden_method_id) { |
||
| 312 | $overridden_storage = $this->getStorage($overridden_method_id); |
||
| 313 | |||
| 314 | if ($overridden_storage->return_type_location) { |
||
| 315 | $defined_location = $overridden_storage->return_type_location; |
||
| 316 | break; |
||
| 317 | } |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | return $storage->return_type_location; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param string $method_id |
||
| 326 | * |
||
| 327 | * @return array<int, \Psalm\Storage\Assertion> |
||
| 328 | */ |
||
| 329 | public function getMethodAssertions($method_id) |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param string $method_id |
||
| 352 | * @param string $declaring_method_id |
||
| 353 | * |
||
| 354 | * @return void |
||
| 355 | */ |
||
| 356 | public function setDeclaringMethodId( |
||
| 357 | $method_id, |
||
| 358 | $declaring_method_id |
||
| 359 | ) { |
||
| 360 | list($fq_class_name, $method_name) = explode('::', $method_id); |
||
| 361 | |||
| 362 | $class_storage = $this->classlike_storage_provider->get($fq_class_name); |
||
| 363 | |||
| 364 | $class_storage->declaring_method_ids[$method_name] = $declaring_method_id; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param string $method_id |
||
| 369 | * @param string $appearing_method_id |
||
| 370 | * |
||
| 371 | * @return void |
||
| 372 | */ |
||
| 373 | public function setAppearingMethodId( |
||
| 374 | $method_id, |
||
| 375 | $appearing_method_id |
||
| 376 | ) { |
||
| 377 | list($fq_class_name, $method_name) = explode('::', $method_id); |
||
| 378 | |||
| 379 | $class_storage = $this->classlike_storage_provider->get($fq_class_name); |
||
| 380 | |||
| 381 | $class_storage->appearing_method_ids[$method_name] = $appearing_method_id; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param string $method_id |
||
| 386 | * |
||
| 387 | * @return string|null |
||
| 388 | */ |
||
| 389 | public function getDeclaringMethodId($method_id) |
||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Get the class this method appears in (vs is declared in, which could give a trait) |
||
| 408 | * |
||
| 409 | * @param string $method_id |
||
| 410 | * |
||
| 411 | * @return string|null |
||
| 412 | */ |
||
| 413 | public function getAppearingMethodId($method_id) |
||
| 414 | { |
||
| 415 | $method_id = strtolower($method_id); |
||
| 416 | |||
| 417 | list($fq_class_name, $method_name) = explode('::', $method_id); |
||
| 418 | |||
| 419 | $class_storage = $this->classlike_storage_provider->get($fq_class_name); |
||
| 420 | |||
| 421 | if (isset($class_storage->appearing_method_ids[$method_name])) { |
||
| 422 | return $class_storage->appearing_method_ids[$method_name]; |
||
| 423 | } |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @param string $method_id |
||
| 428 | * |
||
| 429 | * @return array<string> |
||
| 430 | */ |
||
| 431 | public function getOverriddenMethodIds($method_id) |
||
| 432 | { |
||
| 433 | list($fq_class_name, $method_name) = explode('::', $method_id); |
||
| 434 | |||
| 435 | $class_storage = $this->classlike_storage_provider->get($fq_class_name); |
||
| 436 | |||
| 437 | if (isset($class_storage->overridden_method_ids[$method_name])) { |
||
| 438 | return $class_storage->overridden_method_ids[$method_name]; |
||
| 439 | } |
||
| 440 | |||
| 441 | return []; |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param string $original_method_id |
||
| 446 | * |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | public function getCasedMethodId($original_method_id) |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param string $method_id |
||
| 466 | * |
||
| 467 | * @return MethodStorage |
||
| 468 | */ |
||
| 469 | public function getStorage($method_id) |
||
| 470 | { |
||
| 471 | list($fq_class_name, $method_name) = explode('::', $method_id); |
||
| 472 | |||
| 473 | $class_storage = $this->classlike_storage_provider->get($fq_class_name); |
||
| 474 | |||
| 475 | $method_name_lc = strtolower($method_name); |
||
| 476 | |||
| 477 | if (!isset($class_storage->methods[$method_name_lc])) { |
||
| 478 | throw new \UnexpectedValueException('$storage should not be null for ' . $method_id); |
||
| 479 | } |
||
| 480 | |||
| 481 | return $class_storage->methods[$method_name_lc]; |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param string $method_id |
||
| 486 | * |
||
| 487 | * @return MethodChecker|null |
||
| 488 | */ |
||
| 489 | public function getCachedChecker($method_id) |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @param string $method_id |
||
| 500 | * @param MethodChecker $checker |
||
| 501 | * |
||
| 502 | * @return void |
||
| 503 | */ |
||
| 504 | public function cacheChecker($method_id, MethodChecker $checker) |
||
| 507 | } |
||
| 508 | } |
||
| 509 |