1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Node;
4:
5: use Override;
6: use PhpParser\Node\Expr\Yield_;
7: use PhpParser\Node\Expr\YieldFrom;
8: use PhpParser\Node\Stmt;
9: use PhpParser\Node\Stmt\ClassMethod;
10: use PhpParser\NodeAbstract;
11: use PHPStan\Analyser\ImpurePoint;
12: use PHPStan\Analyser\StatementResult;
13: use PHPStan\Reflection\ClassReflection;
14: use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
15: use PHPStan\Turbo\ReferencedByTurboExtension;
16: use function array_map;
17: use function count;
18:
19: /**
20: * @api
21: */
22: #[ReferencedByTurboExtension(key: 'methodReturnStatementsNode')]
23: final class MethodReturnStatementsNode extends NodeAbstract implements ReturnStatementsNode
24: {
25:
26: private ClassMethod $classMethod;
27:
28: /**
29: * @param list<ReturnStatement> $returnStatements
30: * @param list<ReturnStatement> $returnStatementsAfterFinally
31: * @param list<YieldStatement> $yieldStatements
32: * @param list<ExecutionEndNode> $executionEnds
33: * @param ImpurePoint[] $impurePoints
34: */
35: public function __construct(
36: ClassMethod $method,
37: private array $returnStatements,
38: private array $returnStatementsAfterFinally,
39: private array $yieldStatements,
40: private StatementResult $statementResult,
41: private array $executionEnds,
42: private array $impurePoints,
43: private ClassReflection $classReflection,
44: private PhpMethodFromParserNodeReflection $methodReflection,
45: )
46: {
47: parent::__construct($method->getAttributes());
48: $this->classMethod = $method;
49: }
50:
51: public function getClassMethod(): ClassMethod
52: {
53: return $this->classMethod;
54: }
55:
56: public function getReturnStatements(): array
57: {
58: return $this->returnStatements;
59: }
60:
61: public function getReturnStatementsAfterFinally(): array
62: {
63: return $this->returnStatementsAfterFinally;
64: }
65:
66: public function getStatementResult(): StatementResult
67: {
68: return $this->statementResult;
69: }
70:
71: public function getExecutionEnds(): array
72: {
73: return $this->executionEnds;
74: }
75:
76: public function getImpurePoints(): array
77: {
78: return $this->impurePoints;
79: }
80:
81: public function returnsByRef(): bool
82: {
83: return $this->classMethod->byRef;
84: }
85:
86: public function hasNativeReturnTypehint(): bool
87: {
88: return $this->classMethod->returnType !== null;
89: }
90:
91: public function getMethodName(): string
92: {
93: return $this->classMethod->name->toString();
94: }
95:
96: public function getYieldStatements(): array
97: {
98: return array_map(static fn (YieldStatement $statement): Yield_|YieldFrom => $statement->getYieldNode(), $this->yieldStatements);
99: }
100:
101: /**
102: * The yields with the scopes they are in.
103: *
104: * @return list<YieldStatement>
105: */
106: public function getYieldStatementsWithScope(): array
107: {
108: return $this->yieldStatements;
109: }
110:
111: public function getClassReflection(): ClassReflection
112: {
113: return $this->classReflection;
114: }
115:
116: public function getMethodReflection(): PhpMethodFromParserNodeReflection
117: {
118: return $this->methodReflection;
119: }
120:
121: /**
122: * @return Stmt[]
123: */
124: public function getStatements(): array
125: {
126: $stmts = $this->classMethod->getStmts();
127: if ($stmts === null) {
128: return [];
129: }
130:
131: return $stmts;
132: }
133:
134: public function isGenerator(): bool
135: {
136: return count($this->yieldStatements) > 0;
137: }
138:
139: #[Override]
140: public function getType(): string
141: {
142: return 'PHPStan_Node_MethodReturnStatementsNode';
143: }
144:
145: /**
146: * @return string[]
147: */
148: #[Override]
149: public function getSubNodeNames(): array
150: {
151: return [];
152: }
153:
154: }
155: