Skip to content

Commit 3b2f52e

Browse files
committed
Use the ref metatype for declaring MAY_BE_ARRAY_OF_REF
1 parent d514c3b commit 3b2f52e

File tree

9 files changed

+61
-41
lines changed

9 files changed

+61
-41
lines changed

Zend/zend_builtin_functions.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function is_subclass_of(mixed $object_or_class, string $class, bool $allow_strin
4545
function is_a(mixed $object_or_class, string $class, bool $allow_string = false): bool {}
4646

4747
/**
48-
* @return array-ref<string, mixed>
48+
* @return array<string, mixed|ref>
4949
* @refcount 1
5050
*/
5151
function get_class_vars(string $class): array {}
@@ -132,7 +132,7 @@ function get_declared_interfaces(): array {}
132132
function get_defined_functions(bool $exclude_disabled = true): array {}
133133

134134
/**
135-
* @return array-ref<string, mixed>
135+
* @return array<string, mixed|ref>
136136
* @refcount 1
137137
*/
138138
function get_defined_vars(): array {}

Zend/zend_builtin_functions_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: b2ad660f538e2d5c2c432bb58b037ae5160f8833 */
2+
* Stub hash: f87d92c002674c431827895a8d8b3a5da3b95482 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_version, 0, 0, IS_STRING, 0)
55
ZEND_END_ARG_INFO()

build/gen_stub.php

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,17 @@ class ArrayType extends SimpleType {
120120
/** @var Type */
121121
public $valueType;
122122

123-
/** @var bool */
124-
public $supportsReferences;
125-
126123
public static function createGenericArray(): self
127124
{
128-
return new ArrayType(Type::fromString("int|string"), Type::fromString("mixed"), false);
125+
return new ArrayType(Type::fromString("int|string"), Type::fromString("mixed"));
129126
}
130127

131-
public function __construct(Type $keyType, Type $valueType, bool $supportsReferences)
128+
public function __construct(Type $keyType, Type $valueType)
132129
{
133130
parent::__construct("array", true);
134131

135132
$this->keyType = $keyType;
136133
$this->valueType = $valueType;
137-
$this->supportsReferences = $supportsReferences;
138134
}
139135

140136
public function toOptimizerTypeMask(): string {
@@ -144,10 +140,6 @@ public function toOptimizerTypeMask(): string {
144140
$this->valueType->toOptimizerTypeMaskForArrayValue(),
145141
];
146142

147-
if ($this->supportsReferences) {
148-
$typeMasks[] = "MAY_BE_ARRAY_OF_REF";
149-
}
150-
151143
return implode("|", $typeMasks);
152144
}
153145

@@ -159,8 +151,7 @@ public function equals(SimpleType $other): bool {
159151
assert(get_class($other) === self::class);
160152

161153
return Type::equals($this->keyType, $other->keyType) &&
162-
Type::equals($this->valueType, $other->valueType) &&
163-
$this->supportsReferences === $other->supportsReferences;
154+
Type::equals($this->valueType, $other->valueType);
164155
}
165156
}
166157

@@ -224,13 +215,13 @@ public static function fromString(string $typeString): SimpleType
224215
$matches = [];
225216
$isArray = preg_match("/(.*)\s*\[\s*\]/", $typeString, $matches);
226217
if ($isArray) {
227-
return new ArrayType(Type::fromString("int"), Type::fromString($matches[1]), false);
218+
return new ArrayType(Type::fromString("int"), Type::fromString($matches[1]));
228219
}
229220

230221
$matches = [];
231-
$isArray = preg_match("/array(-ref)?\s*<\s*([A-Za-z0-9_-|]+)\s*,\s*([A-Za-z0-9_-|]+)\s*>/i", $typeString, $matches);
222+
$isArray = preg_match("/array\s*<\s*([A-Za-z0-9_-|]+)\s*,\s*([A-Za-z0-9_-|]+)\s*>/i", $typeString, $matches);
232223
if ($isArray) {
233-
return new ArrayType(Type::fromString($matches[2]), Type::fromString($matches[3]), !empty($matches[1]));
224+
return new ArrayType(Type::fromString($matches[1]), Type::fromString($matches[2]));
234225
}
235226

236227
return new SimpleType($typeString, false);
@@ -406,31 +397,33 @@ class Type {
406397
/** @var SimpleType[] */
407398
public $types;
408399

409-
/**
410-
* @param SimpleType[] $types
411-
*/
412-
public function __construct(array $types) {
413-
$this->types = $types;
414-
}
400+
/** @var bool */
401+
public $isReferenceSupported;
415402

416403
public static function fromNode(Node $node): Type {
417404
if ($node instanceof Node\UnionType) {
418-
return new Type(array_map(['SimpleType', 'fromNode'], $node->types));
405+
return new Type(array_map(['SimpleType', 'fromNode'], $node->types), false);
419406
}
407+
420408
if ($node instanceof Node\NullableType) {
421-
return new Type([
422-
SimpleType::fromNode($node->type),
423-
SimpleType::null(),
424-
]);
409+
return new Type(
410+
[
411+
SimpleType::fromNode($node->type),
412+
SimpleType::null(),
413+
],
414+
false
415+
);
425416
}
426-
return new Type([SimpleType::fromNode($node)]);
417+
418+
return new Type([SimpleType::fromNode($node)], false);
427419
}
428420

429421
public static function fromString(string $typeString): self {
430422
$typeString .= "|";
431423
$simpleTypes = [];
432424
$simpleTypeOffset = 0;
433425
$inArray = false;
426+
$referenceSupported = false;
434427

435428
$typeStringLength = strlen($typeString);
436429
for ($i = 0; $i < $typeStringLength; $i++) {
@@ -452,13 +445,26 @@ public static function fromString(string $typeString): self {
452445

453446
if ($char === "|") {
454447
$simpleTypeName = trim(substr($typeString, $simpleTypeOffset, $i - $simpleTypeOffset));
455-
$simpleTypes[] = SimpleType::fromString($simpleTypeName);
448+
449+
if (strtolower($simpleTypeName) === "ref") {
450+
$referenceSupported = true;
451+
} else {
452+
$simpleTypes[] = SimpleType::fromString($simpleTypeName);
453+
}
456454

457455
$simpleTypeOffset = $i + 1;
458456
}
459457
}
460458

461-
return new Type($simpleTypes);
459+
return new Type($simpleTypes, $referenceSupported);
460+
}
461+
462+
/**
463+
* @param SimpleType[] $types
464+
*/
465+
private function __construct(array $types, bool $isReferenceSupported) {
466+
$this->types = $types;
467+
$this->isReferenceSupported = $isReferenceSupported;
462468
}
463469

464470
public function isScalar(): bool {
@@ -482,9 +488,15 @@ public function isNullable(): bool {
482488
}
483489

484490
public function getWithoutNull(): Type {
485-
return new Type(array_filter($this->types, function(SimpleType $type) {
486-
return !$type->isNull();
487-
}));
491+
return new Type(
492+
array_filter(
493+
$this->types,
494+
function(SimpleType $type) {
495+
return !$type->isNull();
496+
}
497+
),
498+
false
499+
);
488500
}
489501

490502
public function tryToSimpleType(): ?SimpleType {
@@ -535,6 +547,10 @@ public function toOptimizerTypeMaskForArrayValue(): string {
535547
$typeMasks[] = $type->toOptimizerTypeMaskForArrayValue();
536548
}
537549

550+
if ($this->isReferenceSupported) {
551+
$typeMasks[] = "MAY_BE_ARRAY_OF_REF";
552+
}
553+
538554
return implode("|", $typeMasks);
539555
}
540556

@@ -576,6 +592,10 @@ public static function equals(?Type $a, ?Type $b): bool {
576592
}
577593
}
578594

595+
if ($a->isReferenceSupported !== $b->isReferenceSupported) {
596+
return false;
597+
}
598+
579599
return true;
580600
}
581601

ext/filter/filter.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function filter_var(mixed $value, int $filter = FILTER_DEFAULT, array|int $optio
1212
function filter_input_array(int $type, array|int $options = FILTER_DEFAULT, bool $add_empty = true): array|false|null {}
1313

1414
/**
15-
* @return array-ref<int|string, mixed>|false|null
15+
* @return array<int|string, mixed|ref>|false|null
1616
* @refcount 1
1717
*/
1818
function filter_var_array(array $array, array|int $options = FILTER_DEFAULT, bool $add_empty = true): array|false|null {}

ext/filter/filter_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: a5db1b691947245e9b3458e7a5a1ffe62a802d8b */
2+
* Stub hash: 34a64a7c42c7129146fac699c801fe0257fe0d6f */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_filter_has_var, 0, 2, _IS_BOOL, 0)
55
ZEND_ARG_TYPE_INFO(0, input_type, IS_LONG, 0)

ext/pcre/php_pcre.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function preg_split(string $pattern, string $subject, int $limit = -1, int $flag
3838
function preg_quote(string $str, ?string $delimiter = null): string {}
3939

4040
/**
41-
* @return array-ref<int|string, mixed>|false
41+
* @return array<int|string, mixed|ref>|false
4242
* @refcount 1
4343
*/
4444
function preg_grep(string $pattern, array $array, int $flags = 0): array|false {}

ext/pcre/php_pcre_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: d0eeb138b997db89537baf129ee6146303dae63d */
2+
* Stub hash: 18ea8e0fc49c4daab52d346ba68afc77cc3f1552 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_preg_match, 0, 2, MAY_BE_LONG|MAY_BE_FALSE)
55
ZEND_ARG_TYPE_INFO(0, pattern, IS_STRING, 0)

ext/spl/php_spl.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function iterator_apply(Traversable $iterator, callable $callback, ?array $args
5151
function iterator_count(Traversable $iterator): int {}
5252

5353
/**
54-
* @return array-ref<int|string, mixed>
54+
* @return array<int|string, mixed|ref>
5555
* @refcount 1
5656
*/
5757
function iterator_to_array(Traversable $iterator, bool $preserve_keys = true): array {}

ext/spl/php_spl_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: a200c26a76a814b4ac8ab238e884f2d823097b2b */
2+
* Stub hash: a3102a82fa37455c2a5941b6e4f56204baa43950 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_implements, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE)
55
ZEND_ARG_INFO(0, object_or_class)

0 commit comments

Comments
 (0)