1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Reflection;
4:
5: use function array_key_exists;
6:
7: /**
8: * Describes how a function/method parameter is passed: by value or by reference.
9: *
10: * Three modes:
11: * - **No**: Passed by value — the argument expression is evaluated and its value is copied.
12: * - **ReadsArgument**: Passed by reference, but the function reads the existing variable.
13: * The variable must already exist. Example: `sort(&$array)`.
14: * - **CreatesNewVariable**: Passed by reference, and the function may create the variable
15: * if it doesn't exist. Example: `preg_match($pattern, $subject, &$matches)` where
16: * `$matches` doesn't need to be defined beforehand.
17: *
18: * This distinction matters for PHPStan's scope analysis — when a function takes a
19: * parameter by reference with "creates new variable" semantics, PHPStan knows the
20: * variable will exist after the call even if it wasn't defined before.
21: *
22: * Used as the return type of ParameterReflection::passedByReference().
23: *
24: * @api
25: */
26: final class PassedByReference
27: {
28:
29: private const NO = 1;
30: private const READS_ARGUMENT = 2;
31: private const CREATES_NEW_VARIABLE = 3;
32:
33: /** @var self[] */
34: private static array $registry = [];
35:
36: private function __construct(private int $value)
37: {
38: }
39:
40: private static function create(int $value): self
41: {
42: if (!array_key_exists($value, self::$registry)) {
43: self::$registry[$value] = new self($value);
44: }
45:
46: return self::$registry[$value];
47: }
48:
49: public static function createNo(): self
50: {
51: return self::create(self::NO);
52: }
53:
54: public static function createCreatesNewVariable(): self
55: {
56: return self::create(self::CREATES_NEW_VARIABLE);
57: }
58:
59: public static function createReadsArgument(): self
60: {
61: return self::create(self::READS_ARGUMENT);
62: }
63:
64: public function no(): bool
65: {
66: return $this->value === self::NO;
67: }
68:
69: public function yes(): bool
70: {
71: return !$this->no();
72: }
73:
74: public function equals(self $other): bool
75: {
76: return $this->value === $other->value;
77: }
78:
79: public function createsNewVariable(): bool
80: {
81: return $this->value === self::CREATES_NEW_VARIABLE;
82: }
83:
84: /** CreatesNewVariable > ReadsArgument > No. */
85: public function combine(self $other): self
86: {
87: if ($this->value > $other->value) {
88: return $this;
89: } elseif ($this->value < $other->value) {
90: return $other;
91: }
92:
93: return $this;
94: }
95:
96: }
97: