Skip to content

Commit 5d27dcb

Browse files
committed
Use the ref metatype for declaring MAY_BE_ARRAY_OF_REF
1 parent 0b2a1a6 commit 5d27dcb

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
@@ -114,21 +114,17 @@ class ArrayType extends SimpleType {
114114
/** @var Type */
115115
public $valueType;
116116

117-
/** @var bool */
118-
public $supportsReferences;
119-
120117
public static function createGenericArray(): self
121118
{
122-
return new ArrayType(Type::fromString("int|string"), Type::fromString("mixed"), false);
119+
return new ArrayType(Type::fromString("int|string"), Type::fromString("mixed"));
123120
}
124121

125-
public function __construct(Type $keyType, Type $valueType, bool $supportsReferences)
122+
public function __construct(Type $keyType, Type $valueType)
126123
{
127124
parent::__construct("array", true);
128125

129126
$this->keyType = $keyType;
130127
$this->valueType = $valueType;
131-
$this->supportsReferences = $supportsReferences;
132128
}
133129

134130
public function toOptimizerTypeMask(): string {
@@ -138,10 +134,6 @@ public function toOptimizerTypeMask(): string {
138134
$this->valueType->toOptimizerTypeMaskForArrayValue(),
139135
];
140136

141-
if ($this->supportsReferences) {
142-
$typeMasks[] = "MAY_BE_ARRAY_OF_REF";
143-
}
144-
145137
return implode("|", $typeMasks);
146138
}
147139

@@ -153,8 +145,7 @@ public function equals(SimpleType $other): bool {
153145
assert(get_class($other) === self::class);
154146

155147
return Type::equals($this->keyType, $other->keyType) &&
156-
Type::equals($this->valueType, $other->valueType) &&
157-
$this->supportsReferences === $other->supportsReferences;
148+
Type::equals($this->valueType, $other->valueType);
158149
}
159150
}
160151

@@ -218,13 +209,13 @@ public static function fromString(string $typeString): SimpleType
218209
$matches = [];
219210
$isArray = preg_match("/(.*)\s*\[\s*\]/", $typeString, $matches);
220211
if ($isArray) {
221-
return new ArrayType(Type::fromString("int"), Type::fromString($matches[1]), false);
212+
return new ArrayType(Type::fromString("int"), Type::fromString($matches[1]));
222213
}
223214

224215
$matches = [];
225-
$isArray = preg_match("/array(-ref)?\s*<\s*([A-Za-z0-9_-|]+)\s*,\s*([A-Za-z0-9_-|]+)\s*>/i", $typeString, $matches);
216+
$isArray = preg_match("/array\s*<\s*([A-Za-z0-9_-|]+)\s*,\s*([A-Za-z0-9_-|]+)\s*>/i", $typeString, $matches);
226217
if ($isArray) {
227-
return new ArrayType(Type::fromString($matches[2]), Type::fromString($matches[3]), !empty($matches[1]));
218+
return new ArrayType(Type::fromString($matches[1]), Type::fromString($matches[2]));
228219
}
229220

230221
return new SimpleType($typeString, false);
@@ -400,31 +391,33 @@ class Type {
400391
/** @var SimpleType[] */
401392
public $types;
402393

403-
/**
404-
* @param SimpleType[] $types
405-
*/
406-
public function __construct(array $types) {
407-
$this->types = $types;
408-
}
394+
/** @var bool */
395+
public $isReferenceSupported;
409396

410397
public static function fromNode(Node $node): Type {
411398
if ($node instanceof Node\UnionType) {
412-
return new Type(array_map(['SimpleType', 'fromNode'], $node->types));
399+
return new Type(array_map(['SimpleType', 'fromNode'], $node->types), false);
413400
}
401+
414402
if ($node instanceof Node\NullableType) {
415-
return new Type([
416-
SimpleType::fromNode($node->type),
417-
SimpleType::null(),
418-
]);
403+
return new Type(
404+
[
405+
SimpleType::fromNode($node->type),
406+
SimpleType::null(),
407+
],
408+
false
409+
);
419410
}
420-
return new Type([SimpleType::fromNode($node)]);
411+
412+
return new Type([SimpleType::fromNode($node)], false);
421413
}
422414

423415
public static function fromString(string $typeString): self {
424416
$typeString .= "|";
425417
$simpleTypes = [];
426418
$simpleTypeOffset = 0;
427419
$inArray = false;
420+
$referenceSupported = false;
428421

429422
$typeStringLength = strlen($typeString);
430423
for ($i = 0; $i < $typeStringLength; $i++) {
@@ -446,13 +439,26 @@ public static function fromString(string $typeString): self {
446439

447440
if ($char === "|") {
448441
$simpleTypeName = trim(substr($typeString, $simpleTypeOffset, $i - $simpleTypeOffset));
449-
$simpleTypes[] = SimpleType::fromString($simpleTypeName);
442+
443+
if (strtolower($simpleTypeName) === "ref") {
444+
$referenceSupported = true;
445+
} else {
446+
$simpleTypes[] = SimpleType::fromString($simpleTypeName);
447+
}
450448

451449
$simpleTypeOffset = $i + 1;
452450
}
453451
}
454452

455-
return new Type($simpleTypes);
453+
return new Type($simpleTypes, $referenceSupported);
454+
}
455+
456+
/**
457+
* @param SimpleType[] $types
458+
*/
459+
private function __construct(array $types, bool $isReferenceSupported) {
460+
$this->types = $types;
461+
$this->isReferenceSupported = $isReferenceSupported;
456462
}
457463

458464
public function isScalar(): bool {
@@ -476,9 +482,15 @@ public function isNullable(): bool {
476482
}
477483

478484
public function getWithoutNull(): Type {
479-
return new Type(array_filter($this->types, function(SimpleType $type) {
480-
return !$type->isNull();
481-
}));
485+
return new Type(
486+
array_filter(
487+
$this->types,
488+
function(SimpleType $type) {
489+
return !$type->isNull();
490+
}
491+
),
492+
false
493+
);
482494
}
483495

484496
public function tryToSimpleType(): ?SimpleType {
@@ -529,6 +541,10 @@ public function toOptimizerTypeMaskForArrayValue(): string {
529541
$typeMasks[] = $type->toOptimizerTypeMaskForArrayValue();
530542
}
531543

544+
if ($this->isReferenceSupported) {
545+
$typeMasks[] = "MAY_BE_ARRAY_OF_REF";
546+
}
547+
532548
return implode("|", $typeMasks);
533549
}
534550

@@ -570,6 +586,10 @@ public static function equals(?Type $a, ?Type $b): bool {
570586
}
571587
}
572588

589+
if ($a->isReferenceSupported !== $b->isReferenceSupported) {
590+
return false;
591+
}
592+
573593
return true;
574594
}
575595

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)