|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\PHPStan\Rules; |
| 10 | + |
| 11 | +use PhpParser\Node; |
| 12 | +use PhpParser\Node\Expr\MethodCall; |
| 13 | +use PhpParser\Node\Expr\StaticCall; |
| 14 | +use PhpParser\Node\Expr\Variable; |
| 15 | +use PhpParser\Node\Identifier; |
| 16 | +use PhpParser\Node\IntersectionType; |
| 17 | +use PhpParser\Node\Name; |
| 18 | +use PhpParser\Node\NullableType; |
| 19 | +use PhpParser\Node\Stmt\ClassMethod; |
| 20 | +use PhpParser\Node\UnionType; |
| 21 | +use PHPStan\Analyser\Scope; |
| 22 | +use PHPStan\Rules\Rule; |
| 23 | +use PHPStan\Rules\RuleErrorBuilder; |
| 24 | + |
| 25 | +/** |
| 26 | + * @implements Rule<ClassMethod> |
| 27 | + */ |
| 28 | +final readonly class RequireConcreteTypeForMockReturnRule implements Rule |
| 29 | +{ |
| 30 | + public function getNodeType(): string |
| 31 | + { |
| 32 | + return ClassMethod::class; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @return list<\PHPStan\Rules\IdentifierRuleError> |
| 37 | + */ |
| 38 | + public function processNode(Node $node, Scope $scope): array |
| 39 | + { |
| 40 | + if ($node->returnType === null || $node->stmts === null) { |
| 41 | + return []; |
| 42 | + } |
| 43 | + |
| 44 | + if (!$this->returnsMock($node)) { |
| 45 | + return []; |
| 46 | + } |
| 47 | + |
| 48 | + if (!$this->typeNodeIsMockObjectOnly($node->returnType)) { |
| 49 | + return []; |
| 50 | + } |
| 51 | + |
| 52 | + return [ |
| 53 | + RuleErrorBuilder::message('Method returns a mock and declares only MockObject as return type. Use an intersection with a concrete type.') |
| 54 | + ->identifier('Ibexa.requireConcreteTypeForMockReturn') |
| 55 | + ->build(), |
| 56 | + ]; |
| 57 | + } |
| 58 | + |
| 59 | + private function returnsMock(ClassMethod $node): bool |
| 60 | + { |
| 61 | + $mockVariables = []; |
| 62 | + foreach ($node->getStmts() ?? [] as $stmt) { |
| 63 | + if ($stmt instanceof Node\Stmt\Expression && $stmt->expr instanceof Node\Expr\Assign) { |
| 64 | + $assign = $stmt->expr; |
| 65 | + if ($assign->var instanceof Variable && is_string($assign->var->name)) { |
| 66 | + if ($assign->expr instanceof MethodCall && $this->isCreateMockCall($assign->expr)) { |
| 67 | + $mockVariables[$assign->var->name] = true; |
| 68 | + } |
| 69 | + |
| 70 | + if ($assign->expr instanceof StaticCall && $this->isCreateMockCall($assign->expr)) { |
| 71 | + $mockVariables[$assign->var->name] = true; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (!$stmt instanceof Node\Stmt\Return_ || $stmt->expr === null) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + $expr = $stmt->expr; |
| 81 | + if ($expr instanceof MethodCall && $this->isCreateMockCall($expr)) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + |
| 85 | + if ($expr instanceof StaticCall && $this->isCreateMockCall($expr)) { |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + if ($expr instanceof Variable && is_string($expr->name) && isset($mockVariables[$expr->name])) { |
| 90 | + return true; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall $call |
| 99 | + */ |
| 100 | + private function isCreateMockCall(Node $call): bool |
| 101 | + { |
| 102 | + if (!$call->name instanceof Node\Identifier) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + if ($call->name->toString() !== 'createMock') { |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + if ($call instanceof MethodCall) { |
| 111 | + return $call->var instanceof Variable && $call->var->name === 'this'; |
| 112 | + } |
| 113 | + |
| 114 | + return true; |
| 115 | + } |
| 116 | + |
| 117 | + private function typeNodeIsMockObjectOnly(Node $type): bool |
| 118 | + { |
| 119 | + if ($type instanceof NullableType) { |
| 120 | + return $this->typeNodeIsMockObjectOnly($type->type); |
| 121 | + } |
| 122 | + |
| 123 | + if ($type instanceof IntersectionType) { |
| 124 | + $hasMockObject = false; |
| 125 | + foreach ($type->types as $innerType) { |
| 126 | + if ($this->isMockObjectType($innerType)) { |
| 127 | + $hasMockObject = true; |
| 128 | + continue; |
| 129 | + } |
| 130 | + |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + return $hasMockObject; |
| 135 | + } |
| 136 | + |
| 137 | + if ($type instanceof UnionType) { |
| 138 | + $hasMockObject = false; |
| 139 | + foreach ($type->types as $innerType) { |
| 140 | + if ($innerType instanceof Name && $innerType->getLast() === 'null') { |
| 141 | + continue; |
| 142 | + } |
| 143 | + |
| 144 | + if ($this->isMockObjectType($innerType)) { |
| 145 | + $hasMockObject = true; |
| 146 | + continue; |
| 147 | + } |
| 148 | + |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + return $hasMockObject; |
| 153 | + } |
| 154 | + |
| 155 | + return $this->isMockObjectType($type); |
| 156 | + } |
| 157 | + |
| 158 | + private function isMockObjectType(Node $type): bool |
| 159 | + { |
| 160 | + if ($type instanceof Identifier) { |
| 161 | + return $type->toString() === 'MockObject'; |
| 162 | + } |
| 163 | + |
| 164 | + if ($type instanceof Name) { |
| 165 | + return $type->getLast() === 'MockObject'; |
| 166 | + } |
| 167 | + |
| 168 | + return false; |
| 169 | + } |
| 170 | +} |
0 commit comments