Skip to content

Commit 9e9777c

Browse files
authored
Improve and sync Accessory types accepts() methods
1 parent 916e2d7 commit 9e9777c

11 files changed

+106
-15
lines changed

src/Type/Accessory/AccessoryArrayListType.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,19 @@ public function getConstantStrings(): array
7676

7777
public function accepts(Type $type, bool $strictTypes): AcceptsResult
7878
{
79+
$isArray = $type->isArray();
80+
$isList = $type->isList();
81+
$isListArray = $isArray->and($isList);
82+
83+
if ($isListArray->yes()) {
84+
return AcceptsResult::createYes();
85+
}
86+
7987
if ($type instanceof CompoundType) {
8088
return $type->isAcceptedBy($this, $strictTypes);
8189
}
8290

83-
$isArray = $type->isArray();
84-
$isList = $type->isList();
85-
86-
return new AcceptsResult($isArray->and($isList), []);
91+
return new AcceptsResult($isListArray, []);
8792
}
8893

8994
public function isSuperTypeOf(Type $type): IsSuperTypeOfResult

src/Type/Accessory/AccessoryLiteralStringType.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,17 @@ public function accepts(Type $type, bool $strictTypes): AcceptsResult
7474
if ($type instanceof MixedType) {
7575
return AcceptsResult::createNo();
7676
}
77+
78+
$isLiteralString = $type->isLiteralString();
79+
if ($isLiteralString->yes()) {
80+
return AcceptsResult::createYes();
81+
}
82+
7783
if ($type instanceof CompoundType) {
7884
return $type->isAcceptedBy($this, $strictTypes);
7985
}
8086

81-
return new AcceptsResult($type->isLiteralString(), []);
87+
return new AcceptsResult($isLiteralString, []);
8288
}
8389

8490
public function isSuperTypeOf(Type $type): IsSuperTypeOfResult

src/Type/Accessory/AccessoryLowercaseStringType.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,17 @@ public function getConstantStrings(): array
7171

7272
public function accepts(Type $type, bool $strictTypes): AcceptsResult
7373
{
74+
$isLowercaseString = $type->isLowercaseString();
75+
76+
if ($isLowercaseString->yes()) {
77+
return AcceptsResult::createYes();
78+
}
79+
7480
if ($type instanceof CompoundType) {
7581
return $type->isAcceptedBy($this, $strictTypes);
7682
}
7783

78-
return new AcceptsResult($type->isLowercaseString(), []);
84+
return new AcceptsResult($isLowercaseString, []);
7985
}
8086

8187
public function isSuperTypeOf(Type $type): IsSuperTypeOfResult

src/Type/Accessory/AccessoryNonEmptyStringType.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,17 @@ public function getConstantStrings(): array
7272

7373
public function accepts(Type $type, bool $strictTypes): AcceptsResult
7474
{
75-
if ($type->isNonEmptyString()->yes()) {
75+
$isNonEmptyString = $type->isNonEmptyString();
76+
77+
if ($isNonEmptyString->yes()) {
7678
return AcceptsResult::createYes();
7779
}
80+
7881
if ($type instanceof CompoundType) {
7982
return $type->isAcceptedBy($this, $strictTypes);
8083
}
8184

82-
return new AcceptsResult($type->isNonEmptyString(), []);
85+
return new AcceptsResult($isNonEmptyString, []);
8386
}
8487

8588
public function isSuperTypeOf(Type $type): IsSuperTypeOfResult

src/Type/Accessory/AccessoryNonFalsyStringType.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,17 @@ public function getConstantStrings(): array
7474

7575
public function accepts(Type $type, bool $strictTypes): AcceptsResult
7676
{
77+
$isNonFalsyString = $type->isNonFalsyString();
78+
79+
if ($isNonFalsyString->yes()) {
80+
return AcceptsResult::createYes();
81+
}
82+
7783
if ($type instanceof CompoundType) {
7884
return $type->isAcceptedBy($this, $strictTypes);
7985
}
8086

81-
return new AcceptsResult($type->isNonFalsyString(), []);
87+
return new AcceptsResult($isNonFalsyString, []);
8288
}
8389

8490
public function isSuperTypeOf(Type $type): IsSuperTypeOfResult

src/Type/Accessory/AccessoryNumericStringType.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,17 @@ public function getConstantStrings(): array
7171

7272
public function accepts(Type $type, bool $strictTypes): AcceptsResult
7373
{
74+
$isNumericString = $type->isNumericString();
75+
76+
if ($isNumericString->yes()) {
77+
return AcceptsResult::createYes();
78+
}
79+
7480
if ($type instanceof CompoundType) {
7581
return $type->isAcceptedBy($this, $strictTypes);
7682
}
7783

78-
return new AcceptsResult($type->isNumericString(), []);
84+
return new AcceptsResult($isNumericString, []);
7985
}
8086

8187
public function isSuperTypeOf(Type $type): IsSuperTypeOfResult

src/Type/Accessory/AccessoryUppercaseStringType.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,17 @@ public function getConstantStrings(): array
7171

7272
public function accepts(Type $type, bool $strictTypes): AcceptsResult
7373
{
74+
$isUppercaseString = $type->isUppercaseString();
75+
76+
if ($isUppercaseString->yes()) {
77+
return AcceptsResult::createYes();
78+
}
79+
7480
if ($type instanceof CompoundType) {
7581
return $type->isAcceptedBy($this, $strictTypes);
7682
}
7783

78-
return new AcceptsResult($type->isUppercaseString(), []);
84+
return new AcceptsResult($isUppercaseString, []);
7985
}
8086

8187
public function isSuperTypeOf(Type $type): IsSuperTypeOfResult

src/Type/Accessory/NonEmptyArrayType.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,19 @@ public function getConstantStrings(): array
7676

7777
public function accepts(Type $type, bool $strictTypes): AcceptsResult
7878
{
79+
$isArray = $type->isArray();
80+
$isIterableAtLeastOnce = $type->isIterableAtLeastOnce();
81+
$isNonEmptyArray = $isArray->and($isIterableAtLeastOnce);
82+
83+
if ($isNonEmptyArray->yes()) {
84+
return AcceptsResult::createYes();
85+
}
86+
7987
if ($type instanceof CompoundType) {
8088
return $type->isAcceptedBy($this, $strictTypes);
8189
}
8290

83-
$isArray = $type->isArray();
84-
$isIterableAtLeastOnce = $type->isIterableAtLeastOnce();
85-
86-
return new AcceptsResult($isArray->and($isIterableAtLeastOnce), []);
91+
return new AcceptsResult($isNonEmptyArray, []);
8792
}
8893

8994
public function isSuperTypeOf(Type $type): IsSuperTypeOfResult

tests/PHPStan/Rules/Functions/ClosureReturnTypeRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,9 @@ public function testBugFunctionMethodConstants(): void
133133
$this->analyse([__DIR__ . '/data/bug-anonymous-function-method-constant.php'], []);
134134
}
135135

136+
public function testBug13964(): void
137+
{
138+
$this->analyse([__DIR__ . '/data/bug-13964.php'], []);
139+
}
140+
136141
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Bug13964;
4+
5+
/** @var array<string, array<mixed>> $state */
6+
$state = (fn()=>[])();
7+
8+
$state = array_map(function (array $item): array {
9+
if (array_key_exists('type', $item) && array_key_exists('data', $item)) {
10+
return $item;
11+
}
12+
13+
return [
14+
'type' => 'hello',
15+
'data' => [],
16+
];
17+
}, $state);

0 commit comments

Comments
 (0)