Skip to content

Commit 8e9a34c

Browse files
VincentLangletondrejmirtes
authored andcommitted
Introduce isSuperTypeOfWithReason
1 parent 5639793 commit 8e9a34c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+880
-350
lines changed

src/Type/Accessory/AccessoryArrayListType.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPStan\Type\ErrorType;
1515
use PHPStan\Type\IntegerRangeType;
1616
use PHPStan\Type\IntersectionType;
17+
use PHPStan\Type\IsSuperTypeOfResult;
1718
use PHPStan\Type\MixedType;
1819
use PHPStan\Type\Traits\MaybeCallableTypeTrait;
1920
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
@@ -93,28 +94,36 @@ public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
9394
}
9495

9596
public function isSuperTypeOf(Type $type): TrinaryLogic
97+
{
98+
return $this->isSuperTypeOfWithReason($type)->result;
99+
}
100+
101+
public function isSuperTypeOfWithReason(Type $type): IsSuperTypeOfResult
96102
{
97103
if ($this->equals($type)) {
98-
return TrinaryLogic::createYes();
104+
return IsSuperTypeOfResult::createYes();
99105
}
100106

101107
if ($type instanceof CompoundType) {
102-
return $type->isSubTypeOf($this);
108+
return $type->isSubTypeOfWithReason($this);
103109
}
104110

105-
return $type->isArray()
106-
->and($type->isList());
111+
return new IsSuperTypeOfResult($type->isArray()->and($type->isList()), []);
107112
}
108113

109114
public function isSubTypeOf(Type $otherType): TrinaryLogic
115+
{
116+
return $this->isSubTypeOfWithReason($otherType)->result;
117+
}
118+
119+
public function isSubTypeOfWithReason(Type $otherType): IsSuperTypeOfResult
110120
{
111121
if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
112-
return $otherType->isSuperTypeOf($this);
122+
return $otherType->isSuperTypeOfWithReason($this);
113123
}
114124

115-
return $otherType->isArray()
116-
->and($otherType->isList())
117-
->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe());
125+
return (new IsSuperTypeOfResult($otherType->isArray()->and($otherType->isList()), []))
126+
->and($otherType instanceof self ? IsSuperTypeOfResult::createYes() : IsSuperTypeOfResult::createMaybe());
118127
}
119128

120129
public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic
@@ -124,7 +133,7 @@ public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLog
124133

125134
public function isAcceptedWithReasonBy(Type $acceptingType, bool $strictTypes): AcceptsResult
126135
{
127-
return new AcceptsResult($this->isSubTypeOf($acceptingType), []);
136+
return $this->isSubTypeOfWithReason($acceptingType)->toAcceptsResult();
128137
}
129138

130139
public function equals(Type $type): bool

src/Type/Accessory/AccessoryLiteralStringType.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPStan\Type\GeneralizePrecision;
1818
use PHPStan\Type\IntegerType;
1919
use PHPStan\Type\IntersectionType;
20+
use PHPStan\Type\IsSuperTypeOfResult;
2021
use PHPStan\Type\MixedType;
2122
use PHPStan\Type\ObjectWithoutClassType;
2223
use PHPStan\Type\StringType;
@@ -85,26 +86,36 @@ public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
8586
}
8687

8788
public function isSuperTypeOf(Type $type): TrinaryLogic
89+
{
90+
return $this->isSuperTypeOfWithReason($type)->result;
91+
}
92+
93+
public function isSuperTypeOfWithReason(Type $type): IsSuperTypeOfResult
8894
{
8995
if ($type instanceof CompoundType) {
90-
return $type->isSubTypeOf($this);
96+
return $type->isSubTypeOfWithReason($this);
9197
}
9298

9399
if ($this->equals($type)) {
94-
return TrinaryLogic::createYes();
100+
return IsSuperTypeOfResult::createYes();
95101
}
96102

97-
return $type->isLiteralString();
103+
return new IsSuperTypeOfResult($type->isLiteralString(), []);
98104
}
99105

100106
public function isSubTypeOf(Type $otherType): TrinaryLogic
107+
{
108+
return $this->isSubTypeOfWithReason($otherType)->result;
109+
}
110+
111+
public function isSubTypeOfWithReason(Type $otherType): IsSuperTypeOfResult
101112
{
102113
if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
103-
return $otherType->isSuperTypeOf($this);
114+
return $otherType->isSuperTypeOfWithReason($this);
104115
}
105116

106-
return $otherType->isLiteralString()
107-
->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe());
117+
return (new IsSuperTypeOfResult($otherType->isLiteralString(), []))
118+
->and($otherType instanceof self ? IsSuperTypeOfResult::createYes() : IsSuperTypeOfResult::createMaybe());
108119
}
109120

110121
public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic
@@ -114,7 +125,7 @@ public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLog
114125

115126
public function isAcceptedWithReasonBy(Type $acceptingType, bool $strictTypes): AcceptsResult
116127
{
117-
return new AcceptsResult($this->isSubTypeOf($acceptingType), []);
128+
return $this->isSubTypeOfWithReason($acceptingType)->toAcceptsResult();
118129
}
119130

120131
public function equals(Type $type): bool

src/Type/Accessory/AccessoryLowercaseStringType.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPStan\Type\GeneralizePrecision;
1818
use PHPStan\Type\IntegerType;
1919
use PHPStan\Type\IntersectionType;
20+
use PHPStan\Type\IsSuperTypeOfResult;
2021
use PHPStan\Type\ObjectWithoutClassType;
2122
use PHPStan\Type\StringType;
2223
use PHPStan\Type\Traits\MaybeCallableTypeTrait;
@@ -81,26 +82,36 @@ public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
8182
}
8283

8384
public function isSuperTypeOf(Type $type): TrinaryLogic
85+
{
86+
return $this->isSuperTypeOfWithReason($type)->result;
87+
}
88+
89+
public function isSuperTypeOfWithReason(Type $type): IsSuperTypeOfResult
8490
{
8591
if ($type instanceof CompoundType) {
86-
return $type->isSubTypeOf($this);
92+
return $type->isSubTypeOfWithReason($this);
8793
}
8894

8995
if ($this->equals($type)) {
90-
return TrinaryLogic::createYes();
96+
return IsSuperTypeOfResult::createYes();
9197
}
9298

93-
return $type->isLowercaseString();
99+
return new IsSuperTypeOfResult($type->isLowercaseString(), []);
94100
}
95101

96102
public function isSubTypeOf(Type $otherType): TrinaryLogic
103+
{
104+
return $this->isSubTypeOfWithReason($otherType)->result;
105+
}
106+
107+
public function isSubTypeOfWithReason(Type $otherType): IsSuperTypeOfResult
97108
{
98109
if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
99-
return $otherType->isSuperTypeOf($this);
110+
return $otherType->isSuperTypeOfWithReason($this);
100111
}
101112

102-
return $otherType->isLowercaseString()
103-
->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe());
113+
return (new IsSuperTypeOfResult($otherType->isLowercaseString(), []))
114+
->and($otherType instanceof self ? IsSuperTypeOfResult::createYes() : IsSuperTypeOfResult::createMaybe());
104115
}
105116

106117
public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic
@@ -110,7 +121,7 @@ public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLog
110121

111122
public function isAcceptedWithReasonBy(Type $acceptingType, bool $strictTypes): AcceptsResult
112123
{
113-
return new AcceptsResult($this->isSubTypeOf($acceptingType), []);
124+
return $this->isSubTypeOfWithReason($acceptingType)->toAcceptsResult();
114125
}
115126

116127
public function equals(Type $type): bool

src/Type/Accessory/AccessoryNonEmptyStringType.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPStan\Type\GeneralizePrecision;
1919
use PHPStan\Type\IntegerType;
2020
use PHPStan\Type\IntersectionType;
21+
use PHPStan\Type\IsSuperTypeOfResult;
2122
use PHPStan\Type\ObjectWithoutClassType;
2223
use PHPStan\Type\StringType;
2324
use PHPStan\Type\Traits\MaybeCallableTypeTrait;
@@ -83,30 +84,40 @@ public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
8384
}
8485

8586
public function isSuperTypeOf(Type $type): TrinaryLogic
87+
{
88+
return $this->isSuperTypeOfWithReason($type)->result;
89+
}
90+
91+
public function isSuperTypeOfWithReason(Type $type): IsSuperTypeOfResult
8692
{
8793
if ($type instanceof CompoundType) {
88-
return $type->isSubTypeOf($this);
94+
return $type->isSubTypeOfWithReason($this);
8995
}
9096

9197
if ($this->equals($type)) {
92-
return TrinaryLogic::createYes();
98+
return IsSuperTypeOfResult::createYes();
9399
}
94100

95101
if ($type->isNonFalsyString()->yes()) {
96-
return TrinaryLogic::createYes();
102+
return IsSuperTypeOfResult::createYes();
97103
}
98104

99-
return $type->isNonEmptyString();
105+
return new IsSuperTypeOfResult($type->isNonEmptyString(), []);
100106
}
101107

102108
public function isSubTypeOf(Type $otherType): TrinaryLogic
109+
{
110+
return $this->isSubTypeOfWithReason($otherType)->result;
111+
}
112+
113+
public function isSubTypeOfWithReason(Type $otherType): IsSuperTypeOfResult
103114
{
104115
if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
105-
return $otherType->isSuperTypeOf($this);
116+
return $otherType->isSuperTypeOfWithReason($this);
106117
}
107118

108-
return $otherType->isNonEmptyString()
109-
->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe());
119+
return (new IsSuperTypeOfResult($otherType->isNonEmptyString(), []))
120+
->and($otherType instanceof self ? IsSuperTypeOfResult::createYes() : IsSuperTypeOfResult::createMaybe());
110121
}
111122

112123
public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic
@@ -116,7 +127,7 @@ public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLog
116127

117128
public function isAcceptedWithReasonBy(Type $acceptingType, bool $strictTypes): AcceptsResult
118129
{
119-
return new AcceptsResult($this->isSubTypeOf($acceptingType), []);
130+
return $this->isSubTypeOfWithReason($acceptingType)->toAcceptsResult();
120131
}
121132

122133
public function equals(Type $type): bool

src/Type/Accessory/AccessoryNonFalsyStringType.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPStan\Type\GeneralizePrecision;
1818
use PHPStan\Type\IntegerType;
1919
use PHPStan\Type\IntersectionType;
20+
use PHPStan\Type\IsSuperTypeOfResult;
2021
use PHPStan\Type\ObjectWithoutClassType;
2122
use PHPStan\Type\StringType;
2223
use PHPStan\Type\Traits\MaybeCallableTypeTrait;
@@ -83,30 +84,40 @@ public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
8384
}
8485

8586
public function isSuperTypeOf(Type $type): TrinaryLogic
87+
{
88+
return $this->isSuperTypeOfWithReason($type)->result;
89+
}
90+
91+
public function isSuperTypeOfWithReason(Type $type): IsSuperTypeOfResult
8692
{
8793
if ($type instanceof CompoundType) {
88-
return $type->isSubTypeOf($this);
94+
return $type->isSubTypeOfWithReason($this);
8995
}
9096

9197
if ($this->equals($type)) {
92-
return TrinaryLogic::createYes();
98+
return IsSuperTypeOfResult::createYes();
9399
}
94100

95-
return $type->isNonFalsyString();
101+
return new IsSuperTypeOfResult($type->isNonFalsyString(), []);
96102
}
97103

98104
public function isSubTypeOf(Type $otherType): TrinaryLogic
105+
{
106+
return $this->isSubTypeOfWithReason($otherType)->result;
107+
}
108+
109+
public function isSubTypeOfWithReason(Type $otherType): IsSuperTypeOfResult
99110
{
100111
if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
101-
return $otherType->isSuperTypeOf($this);
112+
return $otherType->isSuperTypeOfWithReason($this);
102113
}
103114

104115
if ($otherType instanceof AccessoryNonEmptyStringType) {
105-
return TrinaryLogic::createYes();
116+
return IsSuperTypeOfResult::createYes();
106117
}
107118

108-
return $otherType->isNonFalsyString()
109-
->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe());
119+
return (new IsSuperTypeOfResult($otherType->isNonFalsyString(), []))
120+
->and($otherType instanceof self ? IsSuperTypeOfResult::createYes() : IsSuperTypeOfResult::createMaybe());
110121
}
111122

112123
public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic
@@ -116,7 +127,7 @@ public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLog
116127

117128
public function isAcceptedWithReasonBy(Type $acceptingType, bool $strictTypes): AcceptsResult
118129
{
119-
return new AcceptsResult($this->isSubTypeOf($acceptingType), []);
130+
return $this->isSubTypeOfWithReason($acceptingType)->toAcceptsResult();
120131
}
121132

122133
public function equals(Type $type): bool

src/Type/Accessory/AccessoryNumericStringType.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPStan\Type\GeneralizePrecision;
1919
use PHPStan\Type\IntegerType;
2020
use PHPStan\Type\IntersectionType;
21+
use PHPStan\Type\IsSuperTypeOfResult;
2122
use PHPStan\Type\StringType;
2223
use PHPStan\Type\Traits\NonArrayTypeTrait;
2324
use PHPStan\Type\Traits\NonCallableTypeTrait;
@@ -82,26 +83,36 @@ public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
8283
}
8384

8485
public function isSuperTypeOf(Type $type): TrinaryLogic
86+
{
87+
return $this->isSuperTypeOfWithReason($type)->result;
88+
}
89+
90+
public function isSuperTypeOfWithReason(Type $type): IsSuperTypeOfResult
8591
{
8692
if ($type instanceof CompoundType) {
87-
return $type->isSubTypeOf($this);
93+
return $type->isSubTypeOfWithReason($this);
8894
}
8995

9096
if ($this->equals($type)) {
91-
return TrinaryLogic::createYes();
97+
return IsSuperTypeOfResult::createYes();
9298
}
9399

94-
return $type->isNumericString();
100+
return new IsSuperTypeOfResult($type->isNumericString(), []);
95101
}
96102

97103
public function isSubTypeOf(Type $otherType): TrinaryLogic
104+
{
105+
return $this->isSubTypeOfWithReason($otherType)->result;
106+
}
107+
108+
public function isSubTypeOfWithReason(Type $otherType): IsSuperTypeOfResult
98109
{
99110
if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
100-
return $otherType->isSuperTypeOf($this);
111+
return $otherType->isSuperTypeOfWithReason($this);
101112
}
102113

103-
return $otherType->isNumericString()
104-
->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe());
114+
return (new IsSuperTypeOfResult($otherType->isNumericString(), []))
115+
->and($otherType instanceof self ? IsSuperTypeOfResult::createYes() : IsSuperTypeOfResult::createMaybe());
105116
}
106117

107118
public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic
@@ -119,7 +130,7 @@ public function isAcceptedWithReasonBy(Type $acceptingType, bool $strictTypes):
119130
return AcceptsResult::createYes();
120131
}
121132

122-
return new AcceptsResult($this->isSubTypeOf($acceptingType), []);
133+
return $this->isSubTypeOfWithReason($acceptingType)->toAcceptsResult();
123134
}
124135

125136
public function equals(Type $type): bool

0 commit comments

Comments
 (0)