Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9aed5d1

Browse files
committed
more tests
1 parent a6dbce9 commit 9aed5d1

File tree

12 files changed

+541
-29
lines changed

12 files changed

+541
-29
lines changed

‎src/Reflection/InitializerExprTypeResolver.php‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,15 +1336,6 @@ public function resolveEqualType(Type $leftType, Type $rightType): BooleanType
13361336
return $this->resolveIdenticalType($leftType, $rightType);
13371337
}
13381338

1339-
if ($leftType->isConstantArray()->yes() && $leftType->isIterableAtLeastOnce()->no() && $rightType instanceof ConstantScalarType) {
1340-
// @phpstan-ignore-next-line
1341-
return new ConstantBooleanType($rightType->getValue() == []); // phpcs:ignore
1342-
}
1343-
if ($rightType->isConstantArray()->yes() && $rightType->isIterableAtLeastOnce()->no() && $leftType instanceof ConstantScalarType) {
1344-
// @phpstan-ignore-next-line
1345-
return new ConstantBooleanType($leftType->getValue() == []); // phpcs:ignore
1346-
}
1347-
13481339
if ($leftType instanceof ConstantArrayType && $rightType instanceof ConstantArrayType) {
13491340
return $this->resolveConstantArrayTypeComparison($leftType, $rightType, fn ($leftValueType, $rightValueType): BooleanType => $this->resolveEqualType($leftValueType, $rightValueType));
13501341
}

‎src/Type/ArrayType.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,8 @@ public function looseCompare(Type $type): BooleanType
358358
new ConstantBooleanType(false),
359359
new StringType(),
360360
new NullType(),
361-
new IntegerType()
361+
new IntegerType(),
362+
new FloatType(),
362363
]);
363364

364365
if ($looseFalse->isSuperTypeOf($type)->yes()) {

‎src/Type/BooleanType.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function isScalar(): TrinaryLogic
121121
public function looseCompare(Type $type): BooleanType
122122
{
123123
if ($type->isObject()->yes()) {
124-
return new ConstantBooleanType(false);
124+
return new ConstantBooleanType(true);
125125
}
126126

127127
return new BooleanType();

‎src/Type/Constant/ConstantArrayType.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,11 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
397397

398398
public function looseCompare(Type $type): BooleanType
399399
{
400+
if ($this->isIterableAtLeastOnce()->no() && $type instanceof ConstantScalarType) {
401+
// @phpstan-ignore-next-line
402+
return new ConstantBooleanType($type->getValue() == []); // phpcs:ignore
403+
}
404+
400405
if ($type->isObject()->yes()) {
401406
return new ConstantBooleanType(false);
402407
}

‎src/Type/FloatType.php‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Type\Constant\ConstantArrayType;
88
use PHPStan\Type\Constant\ConstantBooleanType;
99
use PHPStan\Type\Constant\ConstantIntegerType;
10+
use PHPStan\Type\Constant\ConstantStringType;
1011
use PHPStan\Type\Traits\NonArrayTypeTrait;
1112
use PHPStan\Type\Traits\NonCallableTypeTrait;
1213
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
@@ -209,7 +210,20 @@ public function isScalar(): TrinaryLogic
209210

210211
public function looseCompare(Type $type): BooleanType
211212
{
212-
if ($type->isObject()->yes()) {
213+
foreach ($type->getConstantStrings() as $stringType) {
214+
if ($stringType->isNumericString()->yes()) {
215+
continue;
216+
}
217+
return new ConstantBooleanType(false);
218+
}
219+
220+
$looseFalse = new UnionType([
221+
new ObjectWithoutClassType(),
222+
new ArrayType(new MixedType(), new MixedType()),
223+
new ConstantStringType(''),
224+
]);
225+
226+
if ($looseFalse->isSuperTypeOf($type)->yes()) {
213227
return new ConstantBooleanType(false);
214228
}
215229

‎src/Type/IntegerType.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,14 @@ public function isScalar(): TrinaryLogic
131131

132132
public function looseCompare(Type $type): BooleanType
133133
{
134-
if ($type->isObject()->yes()) {
134+
foreach ($type->getConstantStrings() as $stringType) {
135+
if ($stringType->isNumericString()->yes()) {
136+
continue;
137+
}
138+
return new ConstantBooleanType(false);
139+
}
140+
141+
if ($type->isArray()->yes()) {
135142
return new ConstantBooleanType(false);
136143
}
137144

‎src/Type/ObjectType.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use PHPStan\TrinaryLogic;
3434
use PHPStan\Type\Constant\ConstantArrayType;
3535
use PHPStan\Type\Constant\ConstantBooleanType;
36+
use PHPStan\Type\Constant\ConstantIntegerType;
3637
use PHPStan\Type\Constant\ConstantStringType;
3738
use PHPStan\Type\Enum\EnumCaseObjectType;
3839
use PHPStan\Type\Generic\GenericObjectType;
@@ -1004,6 +1005,33 @@ public function isScalar(): TrinaryLogic
10041005

10051006
public function looseCompare(Type $type): BooleanType
10061007
{
1008+
$looseTrue = new UnionType([
1009+
new ConstantBooleanType(true),
1010+
new ConstantIntegerType(1),
1011+
]);
1012+
1013+
if ($looseTrue->isSuperTypeOf($type)->yes()) {
1014+
return new ConstantBooleanType(true);
1015+
}
1016+
1017+
if ($type instanceof ConstantScalarType) {
1018+
return new ConstantBooleanType(false);
1019+
}
1020+
1021+
if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no()) {
1022+
return new ConstantBooleanType(false);
1023+
}
1024+
1025+
$looseFalse = new UnionType([
1026+
new StringType(),
1027+
new NullType(),
1028+
new FloatType(),
1029+
]);
1030+
1031+
if ($looseFalse->isSuperTypeOf($type)->yes()) {
1032+
return new ConstantBooleanType(false);
1033+
}
1034+
10071035
return new BooleanType();
10081036
}
10091037

‎src/Type/StringType.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,12 @@ public function isScalar(): TrinaryLogic
229229

230230
public function looseCompare(Type $type): BooleanType
231231
{
232-
if ($type->isObject()->yes()) {
232+
$looseFalse = new UnionType([
233+
new ObjectWithoutClassType(),
234+
new ArrayType(new MixedType(), new MixedType()),
235+
]);
236+
237+
if ($looseFalse->isSuperTypeOf($type)->yes()) {
233238
return new ConstantBooleanType(false);
234239
}
235240

‎src/Type/Traits/ConstantScalarTypeTrait.php‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Type\Traits;
44

5+
use PHPStan\ShouldNotHappenException;
56
use PHPStan\TrinaryLogic;
67
use PHPStan\Type\AcceptsResult;
78
use PHPStan\Type\BooleanType;
@@ -50,12 +51,25 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
5051

5152
public function looseCompare(Type $type): BooleanType
5253
{
53-
if ($this instanceof ConstantScalarType && $type instanceof ConstantScalarType) {
54+
if (!$this instanceof ConstantScalarType) {
55+
throw new ShouldNotHappenException();
56+
}
57+
58+
if ($type instanceof ConstantScalarType) {
5459
// @phpstan-ignore-next-line
5560
return new ConstantBooleanType($this->getValue() == $type->getValue()); // phpcs:ignore
5661
}
5762

58-
return new BooleanType();
63+
if ($type->isObject()->yes()) {
64+
return $type->looseCompare($this);
65+
}
66+
67+
if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no()) {
68+
// @phpstan-ignore-next-line
69+
return new ConstantBooleanType($this->getValue() == []); // phpcs:ignore
70+
}
71+
72+
return parent::looseCompare($type);
5973
}
6074

6175
public function equals(Type $type): bool

‎src/Type/Traits/ObjectTypeTrait.php‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@
1616
use PHPStan\TrinaryLogic;
1717
use PHPStan\Type\ArrayType;
1818
use PHPStan\Type\BooleanType;
19-
use PHPStan\Type\Constant\ConstantArrayType;
2019
use PHPStan\Type\Constant\ConstantBooleanType;
21-
use PHPStan\Type\Constant\ConstantFloatType;
2220
use PHPStan\Type\Constant\ConstantIntegerType;
23-
use PHPStan\Type\Constant\ConstantStringType;
21+
use PHPStan\Type\ConstantScalarType;
2422
use PHPStan\Type\ErrorType;
25-
use PHPStan\Type\IntegerType;
23+
use PHPStan\Type\FloatType;
2624
use PHPStan\Type\MixedType;
2725
use PHPStan\Type\NullType;
28-
use PHPStan\Type\ObjectWithoutClassType;
2926
use PHPStan\Type\StringType;
3027
use PHPStan\Type\Type;
3128
use PHPStan\Type\UnionType;
@@ -207,16 +204,18 @@ public function looseCompare(Type $type): BooleanType
207204
return new ConstantBooleanType(true);
208205
}
209206

210-
if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no())
211-
{
207+
if ($type instanceof ConstantScalarType) {
208+
return new ConstantBooleanType(false);
209+
}
210+
211+
if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no()) {
212212
return new ConstantBooleanType(false);
213213
}
214214

215215
$looseFalse = new UnionType([
216-
new ConstantBooleanType(false),
217216
new StringType(),
218217
new NullType(),
219-
new IntegerType()
218+
new FloatType(),
220219
]);
221220

222221
if ($looseFalse->isSuperTypeOf($type)->yes()) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /