Skip to content

Commit 8ee5a52

Browse files
gen_stub: polyfill and use array_any()
1 parent a1d8e52 commit 8ee5a52

1 file changed

Lines changed: 61 additions & 85 deletions

File tree

build/gen_stub.php

Lines changed: 61 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ function reportFilePutContents(string $filename, string $content): void {
4343
}
4444
}
4545

46+
if (!function_exists('array_any')) {
47+
function array_any(array $array, callable $callback): bool {
48+
foreach ($array as $key => $value) {
49+
if ($callback($value, $key)) {
50+
return true;
51+
}
52+
}
53+
return false;
54+
}
55+
}
56+
4657
/**
4758
* @return FileInfo[]
4859
*/
@@ -599,23 +610,11 @@ private function __construct(array $types, bool $isIntersection) {
599610
}
600611

601612
public function isScalar(): bool {
602-
foreach ($this->types as $type) {
603-
if (!$type->isScalar()) {
604-
return false;
605-
}
606-
}
607-
608-
return true;
613+
return !array_any($this->types, static fn (SimpleType $type): bool => !$type->isScalar());
609614
}
610615

611616
public function isNullable(): bool {
612-
foreach ($this->types as $type) {
613-
if ($type->isNull()) {
614-
return true;
615-
}
616-
}
617-
618-
return false;
617+
return array_any($this->types, static fn (SimpleType $type): bool => $type->isNull());
619618
}
620619

621620
public function tryToSimpleType(): ?SimpleType {
@@ -1237,12 +1236,10 @@ public function addForVersionsAbove(string $flag, int $minimumVersionId): void {
12371236
}
12381237

12391238
public function isEmpty(): bool {
1240-
foreach (ALL_PHP_VERSION_IDS as $version) {
1241-
if ($this->flagsByVersion[$version] !== []) {
1242-
return false;
1243-
}
1244-
}
1245-
return true;
1239+
return !array_any(
1240+
ALL_PHP_VERSION_IDS,
1241+
fn (int $version): bool => $this->flagsByVersion[$version] !== []
1242+
);
12461243
}
12471244

12481245
public function generateVersionDependentFlagCode(
@@ -1439,13 +1436,10 @@ private function getModifierNames(): array
14391436

14401437
private function hasParamWithUnknownDefaultValue(): bool
14411438
{
1442-
foreach ($this->args as $arg) {
1443-
if ($arg->defaultValue && !$arg->hasProperDefaultValue()) {
1444-
return true;
1445-
}
1446-
}
1447-
1448-
return false;
1439+
return array_any(
1440+
$this->args,
1441+
static fn (ArgInfo $arg): bool => $arg->defaultValue && !$arg->hasProperDefaultValue()
1442+
);
14491443
}
14501444

14511445
private function equalsApartFromNameAndRefcount(FuncInfo $other): bool {
@@ -1666,12 +1660,11 @@ private function getArginfoFlagsByPhpVersions(): VersionFlags
16661660
$flags[] = "ZEND_ACC_DEPRECATED";
16671661
}
16681662

1669-
foreach ($this->attributes as $attr) {
1670-
switch ($attr->class) {
1671-
case "Deprecated":
1672-
$flags[] = "ZEND_ACC_DEPRECATED";
1673-
break;
1674-
}
1663+
if (array_any(
1664+
$this->attributes,
1665+
static fn (AttributeInfo $attr): bool => $attr->class === "Deprecated"
1666+
)) {
1667+
$flags[] = "ZEND_ACC_DEPRECATED";
16751668
}
16761669

16771670
$flags = new VersionFlags($flags);
@@ -1680,12 +1673,11 @@ private function getArginfoFlagsByPhpVersions(): VersionFlags
16801673
$flags->addForVersionsAbove("ZEND_ACC_COMPILE_TIME_EVAL", PHP_82_VERSION_ID);
16811674
}
16821675

1683-
foreach ($this->attributes as $attr) {
1684-
switch ($attr->class) {
1685-
case "NoDiscard":
1686-
$flags->addForVersionsAbove("ZEND_ACC_NODISCARD", PHP_85_VERSION_ID);
1687-
break;
1688-
}
1676+
if (array_any(
1677+
$this->attributes,
1678+
static fn (AttributeInfo $attr): bool => $attr->class === "NoDiscard"
1679+
)) {
1680+
$flags->addForVersionsAbove("ZEND_ACC_NODISCARD", PHP_85_VERSION_ID);
16891681
}
16901682

16911683
return $flags;
@@ -2635,11 +2627,11 @@ public function __construct(
26352627
?ExposedDocComment $exposedDocComment,
26362628
bool $isFileCacheAllowed
26372629
) {
2638-
foreach ($attributes as $attr) {
2639-
if ($attr->class === "Deprecated") {
2640-
$isDeprecated = true;
2641-
break;
2642-
}
2630+
if (array_any(
2631+
$attributes,
2632+
static fn (AttributeInfo $attr): bool => $attr->class === "Deprecated"
2633+
)) {
2634+
$isDeprecated = true;
26432635
}
26442636

26452637
$this->name = $name;
@@ -3758,11 +3750,11 @@ private function getFlagsByPhpVersion(): VersionFlags
37583750
$flags->addForVersionsAbove("ZEND_ACC_READONLY_CLASS", PHP_82_VERSION_ID);
37593751
}
37603752

3761-
foreach ($this->attributes as $attr) {
3762-
if ($attr->class === "AllowDynamicProperties") {
3763-
$flags->addForVersionsAbove("ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES", PHP_82_VERSION_ID);
3764-
break;
3765-
}
3753+
if (array_any(
3754+
$this->attributes,
3755+
static fn (AttributeInfo $attr): bool => $attr->class === "AllowDynamicProperties"
3756+
)) {
3757+
$flags->addForVersionsAbove("ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES", PHP_82_VERSION_ID);
37663758
}
37673759

37683760
return $flags;
@@ -4169,46 +4161,34 @@ private function isException(array $classMap): bool
41694161

41704162
private function hasConstructor(): bool
41714163
{
4172-
foreach ($this->funcInfos as $funcInfo) {
4173-
if ($funcInfo->name->isConstructor()) {
4174-
return true;
4175-
}
4176-
}
4177-
4178-
return false;
4164+
return array_any(
4165+
$this->funcInfos,
4166+
static fn (FuncInfo $funcInfo): bool => $funcInfo->name->isConstructor()
4167+
);
41794168
}
41804169

41814170
private function hasNonPrivateConstructor(): bool
41824171
{
4183-
foreach ($this->funcInfos as $funcInfo) {
4184-
if ($funcInfo->name->isConstructor() && !($funcInfo->flags & Modifiers::PRIVATE)) {
4185-
return true;
4186-
}
4187-
}
4188-
4189-
return false;
4172+
return array_any(
4173+
$this->funcInfos,
4174+
static fn (FuncInfo $funcInfo): bool => $funcInfo->name->isConstructor() && !($funcInfo->flags & Modifiers::PRIVATE)
4175+
);
41904176
}
41914177

41924178
private function hasDestructor(): bool
41934179
{
4194-
foreach ($this->funcInfos as $funcInfo) {
4195-
if ($funcInfo->name->isDestructor()) {
4196-
return true;
4197-
}
4198-
}
4199-
4200-
return false;
4180+
return array_any(
4181+
$this->funcInfos,
4182+
static fn (FuncInfo $funcInfo): bool => $funcInfo->name->isDestructor()
4183+
);
42014184
}
42024185

42034186
private function hasMethods(): bool
42044187
{
4205-
foreach ($this->funcInfos as $funcInfo) {
4206-
if (!$funcInfo->name->isConstructor() && !$funcInfo->name->isDestructor()) {
4207-
return true;
4208-
}
4209-
}
4210-
4211-
return false;
4188+
return array_any(
4189+
$this->funcInfos,
4190+
static fn (FuncInfo $funcInfo): bool => !$funcInfo->name->isConstructor() && !$funcInfo->name->isDestructor()
4191+
);
42124192
}
42134193

42144194
public function getNamespace(): ?string {
@@ -5138,7 +5118,6 @@ function parseClass(
51385118
): ClassInfo {
51395119
$comments = $class->getComments();
51405120
$alias = null;
5141-
$allowsDynamicProperties = false;
51425121

51435122
$tags = DocCommentTag::parseDocComments($comments);
51445123
$tagMap = DocCommentTag::makeTagMap($tags);
@@ -5154,13 +5133,10 @@ function parseClass(
51545133
}
51555134

51565135
$attributes = AttributeInfo::createFromGroups($class->attrGroups);
5157-
foreach ($attributes as $attribute) {
5158-
switch ($attribute->class) {
5159-
case 'AllowDynamicProperties':
5160-
$allowsDynamicProperties = true;
5161-
break 2;
5162-
}
5163-
}
5136+
$allowsDynamicProperties = array_any(
5137+
$attributes,
5138+
static fn (AttributeInfo $attribute): bool => $attribute->class === 'AllowDynamicProperties'
5139+
);
51645140

51655141
if ($isStrictProperties && $allowsDynamicProperties) {
51665142
throw new Exception("A class may not have '@strict-properties' and '#[\\AllowDynamicProperties]' at the same time.");

0 commit comments

Comments
 (0)