|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * Copyright 2024, Cake Development Corporation (https://www.cakedc.com) |
| 6 | + * |
| 7 | + * Licensed under The MIT License |
| 8 | + * Redistributions of files must retain the above copyright notice. |
| 9 | + * |
| 10 | + * @copyright Copyright 2024, Cake Development Corporation (https://www.cakedc.com) |
| 11 | + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CakeDC\PHPStan\Rule; |
| 15 | + |
| 16 | +use CakeDC\PHPStan\Rule\Traits\ParseClassNameFromArgTrait; |
| 17 | +use PhpParser\Node; |
| 18 | +use PhpParser\Node\Arg; |
| 19 | +use PhpParser\Node\Expr\MethodCall; |
| 20 | +use PhpParser\Node\Scalar\String_; |
| 21 | +use PHPStan\Analyser\Scope; |
| 22 | +use PHPStan\Rules\Rule; |
| 23 | +use PHPStan\Rules\RuleErrorBuilder; |
| 24 | + |
| 25 | +abstract class LoadObjectExistsCakeClassRule implements Rule |
| 26 | +{ |
| 27 | + use ParseClassNameFromArgTrait; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + protected string $identifier; |
| 33 | + |
| 34 | + /** |
| 35 | + * @return string |
| 36 | + */ |
| 37 | + public function getNodeType(): string |
| 38 | + { |
| 39 | + return MethodCall::class; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param \PhpParser\Node $node |
| 44 | + * @param \PHPStan\Analyser\Scope $scope |
| 45 | + * @return array<\PHPStan\Rules\RuleError> |
| 46 | + */ |
| 47 | + public function processNode(Node $node, Scope $scope): array |
| 48 | + { |
| 49 | + assert($node instanceof MethodCall); |
| 50 | + $args = $node->getArgs(); |
| 51 | + if (!$node->name instanceof Node\Identifier) { |
| 52 | + return []; |
| 53 | + } |
| 54 | + $reference = $scope->getType($node->var)->getReferencedClasses()[0] ?? null; |
| 55 | + if ($reference === null) { |
| 56 | + return []; |
| 57 | + } |
| 58 | + $details = $this->getDetails($reference, $args); |
| 59 | + |
| 60 | + if ( |
| 61 | + $details === null |
| 62 | + || !in_array($node->name->name, $details['sourceMethods']) |
| 63 | + || !$details['alias'] instanceof Arg |
| 64 | + || !$details['alias']->value instanceof String_ |
| 65 | + ) { |
| 66 | + return []; |
| 67 | + } |
| 68 | + |
| 69 | + $inputClassName = $this->getInputClassName( |
| 70 | + $details['alias']->value, |
| 71 | + $details['options'] |
| 72 | + ); |
| 73 | + if ($this->getTargetClassName($inputClassName)) { |
| 74 | + return []; |
| 75 | + } |
| 76 | + |
| 77 | + return [ |
| 78 | + RuleErrorBuilder::message(sprintf( |
| 79 | + 'Call to %s::%s could not find the class for "%s"', |
| 80 | + $reference, |
| 81 | + $node->name->name, |
| 82 | + $inputClassName, |
| 83 | + )) |
| 84 | + ->identifier($this->identifier) |
| 85 | + ->build(), |
| 86 | + ]; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @param \PhpParser\Node\Scalar\String_ $nameArg |
| 91 | + * @param \PhpParser\Node\Arg|null $options |
| 92 | + * @return string |
| 93 | + */ |
| 94 | + protected function getInputClassName(String_ $nameArg, ?Arg $options): string |
| 95 | + { |
| 96 | + $className = $nameArg->value; |
| 97 | + |
| 98 | + if ( |
| 99 | + $options === null |
| 100 | + || !$options->value instanceof Node\Expr\Array_ |
| 101 | + ) { |
| 102 | + return $className; |
| 103 | + } |
| 104 | + foreach ($options->value->items as $item) { |
| 105 | + if ( |
| 106 | + !$item instanceof Node\Expr\ArrayItem |
| 107 | + || !$item->key instanceof String_ |
| 108 | + || $item->key->value !== 'className' |
| 109 | + ) { |
| 110 | + continue; |
| 111 | + } |
| 112 | + $name = $this->parseClassNameFromExprTrait($item->value); |
| 113 | + if ($name !== null) { |
| 114 | + return $name; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return $className; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param string $name |
| 123 | + * @return string|null |
| 124 | + */ |
| 125 | + abstract protected function getTargetClassName(string $name): ?string; |
| 126 | + |
| 127 | + /** |
| 128 | + * @param string $reference |
| 129 | + * @param array<\PhpParser\Node\Arg> $args |
| 130 | + * @return array{'alias': ?\PhpParser\Node\Arg, 'options': ?\PhpParser\Node\Arg, 'sourceMethods':array<string>}|null |
| 131 | + */ |
| 132 | + abstract protected function getDetails(string $reference, array $args): ?array; |
| 133 | +} |
0 commit comments