Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplifications #2131

Merged
merged 5 commits into from Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/ignore-by-php-version.neon.php
Expand Up @@ -24,7 +24,7 @@
$includes[] = __DIR__ . '/enum-adapter-errors.neon';
}

if (PHP_VERSION_ID >= 70300 && PHP_VERSION_ID < 80000) {
if (PHP_VERSION_ID < 80000) {
$includes[] = __DIR__ . '/more-enum-adapter-errors.neon';
}

Expand Down
5 changes: 4 additions & 1 deletion src/Rules/RuleLevelHelper.php
Expand Up @@ -114,7 +114,10 @@ public function accepts(Type $acceptingType, Type $acceptedType, bool $strictTyp
}

$accepts = $acceptingType->accepts($acceptedType, $strictTypes);
if (!$accepts->yes() && $acceptingType instanceof UnionType) {
if ($accepts->yes()) {
return true;
}
if ($acceptingType instanceof UnionType) {
foreach ($acceptingType->getTypes() as $innerType) {
if (self::accepts($innerType, $acceptedType, $strictTypes)) {
return true;
Expand Down
4 changes: 3 additions & 1 deletion src/Type/ArrayType.php
Expand Up @@ -93,7 +93,9 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic
$itemType = $this->getItemType();
foreach ($type->getKeyTypes() as $i => $keyType) {
$valueType = $type->getValueTypes()[$i];
$result = $result->and($thisKeyType->accepts($keyType, $strictTypes))->and($itemType->accepts($valueType, $strictTypes));
$acceptsKey = $thisKeyType->accepts($keyType, $strictTypes);
$acceptsValue = $itemType->accepts($valueType, $strictTypes);
$result = $result->and($acceptsKey)->and($acceptsValue);
}

return $result;
Expand Down
7 changes: 3 additions & 4 deletions src/Type/IntersectionType.php
Expand Up @@ -118,13 +118,12 @@ public function getConstantStrings(): array

public function accepts(Type $otherType, bool $strictTypes): TrinaryLogic
{
$results = [];
foreach ($this->types as $type) {
if (!$type->accepts($otherType, $strictTypes)->yes()) {
return TrinaryLogic::createNo();
}
$results[] = $type->accepts($otherType, $strictTypes);
}

return TrinaryLogic::createYes();
return TrinaryLogic::createYes()->and(...$results);
}

public function isSuperTypeOf(Type $otherType): TrinaryLogic
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Traits/ConstantScalarTypeTrait.php
Expand Up @@ -20,7 +20,7 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic
return $type->isAcceptedBy($this, $strictTypes);
}

return TrinaryLogic::createNo();
return parent::accepts($type, $strictTypes)->and(TrinaryLogic::createMaybe());
}

public function isSuperTypeOf(Type $type): TrinaryLogic
Expand Down
7 changes: 1 addition & 6 deletions tests/PHPStan/Levels/data/acceptTypes-5.json
Expand Up @@ -174,11 +174,6 @@
"line": 707,
"ignorable": true
},
{
"message": "Parameter #1 $numericString of method Levels\\AcceptTypes\\NumericStrings::doBar() expects numeric-string, string given.",
"line": 708,
"ignorable": true
},
{
"message": "Parameter #1 $nonEmpty of method Levels\\AcceptTypes\\AcceptNonEmpty::doBar() expects non-empty-array, array{} given.",
"line": 733,
Expand All @@ -194,4 +189,4 @@
"line": 763,
"ignorable": true
}
]
]
7 changes: 6 additions & 1 deletion tests/PHPStan/Levels/data/acceptTypes-7.json
Expand Up @@ -154,9 +154,14 @@
"line": 692,
"ignorable": true
},
{
"message": "Parameter #1 $numericString of method Levels\\AcceptTypes\\NumericStrings::doBar() expects numeric-string, string given.",
"line": 708,
"ignorable": true
},
{
"message": "Parameter #2 $array of function implode expects array|null, array|int|string given.",
"line": 756,
"ignorable": true
}
]
]
4 changes: 2 additions & 2 deletions tests/PHPStan/Type/IntersectionTypeTest.php
Expand Up @@ -42,7 +42,7 @@ public function dataAccepts(): Iterator
yield [
$intersectionType,
new IterableType(new MixedType(), new ObjectType('Item')),
TrinaryLogic::createNo(),
TrinaryLogic::createMaybe(),
];

yield [
Expand All @@ -57,7 +57,7 @@ public function dataAccepts(): Iterator
yield [
TypeCombinator::intersect(new ArrayType(new MixedType(), new MixedType()), new CallableType()),
new CallableType(),
TrinaryLogic::createNo(),
TrinaryLogic::createMaybe(),
];
}

Expand Down