Skip to content

Commit a9c2a2c

Browse files
committed
isDecimalIntegerString test
1 parent 818433d commit a9c2a2c

File tree

4 files changed

+64
-16
lines changed

4 files changed

+64
-16
lines changed

src/Type/Accessory/AccessoryNonFalsyStringType.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,6 @@ public function isScalar(): TrinaryLogic
347347

348348
public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
349349
{
350-
if ($type->isString()->yes() && $type->isNonFalsyString()->no()) {
351-
return new ConstantBooleanType(false);
352-
}
353-
354350
$falseyTypes = StaticTypeFactory::falsey();
355351
if ($falseyTypes->isSuperTypeOf($type)->yes()) {
356352
return new ConstantBooleanType(false);

src/Type/ArrayType.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use PHPStan\ShouldNotHappenException;
1313
use PHPStan\TrinaryLogic;
1414
use PHPStan\Type\Accessory\AccessoryArrayListType;
15-
use PHPStan\Type\Accessory\AccessoryDecimalIntegerStringType;
1615
use PHPStan\Type\Accessory\HasOffsetValueType;
1716
use PHPStan\Type\Accessory\NonEmptyArrayType;
1817
use PHPStan\Type\Constant\ConstantArrayType;
@@ -52,15 +51,11 @@ class ArrayType implements Type
5251
/** @api */
5352
public function __construct(Type $keyType, private Type $itemType)
5453
{
55-
$desc = $keyType->describe(VerbosityLevel::value());
56-
if (in_array($desc, ['(int|string)', '(int|non-decimal-int-string)'], true)) {
54+
if ($keyType->describe(VerbosityLevel::value()) === '(int|string)') {
5755
$keyType = new MixedType();
5856
}
5957
if ($keyType instanceof StrictMixedType && !$keyType instanceof TemplateStrictMixedType) {
60-
$keyType = new UnionType([
61-
new StringType(),
62-
new IntegerType(),
63-
]);
58+
$keyType = new UnionType([new StringType(), new IntegerType()]);
6459
}
6560

6661
$this->keyType = $keyType;
@@ -121,7 +116,7 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
121116
{
122117
if ($type instanceof self || $type instanceof ConstantArrayType) {
123118
return $this->getItemType()->isSuperTypeOf($type->getItemType())
124-
->and($this->getKeyType()->isSuperTypeOf($type->getKeyType()));
119+
->and($this->getIterableKeyType()->isSuperTypeOf($type->getIterableKeyType()));
125120
}
126121

127122
if ($type instanceof CompoundType) {
@@ -206,10 +201,10 @@ public function getIterableKeyType(): Type
206201
{
207202
$keyType = $this->keyType;
208203
if ($keyType instanceof MixedType && !$keyType instanceof TemplateMixedType) {
209-
return new BenevolentUnionType([new IntegerType(), new IntersectionType([new StringType(), new AccessoryDecimalIntegerStringType(inverse: true)])]);
204+
return new BenevolentUnionType([new IntegerType(), new StringType()]);
210205
}
211206
if ($keyType instanceof StrictMixedType) {
212-
return new BenevolentUnionType([new IntegerType(), new IntersectionType([new StringType(), new AccessoryDecimalIntegerStringType(inverse: true)])]);
207+
return new BenevolentUnionType([new IntegerType(), new StringType()]);
213208
}
214209

215210
return $keyType;

src/Type/Constant/ConstantStringType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public function isNumericString(): TrinaryLogic
331331

332332
public function isDecimalIntegerString(): TrinaryLogic
333333
{
334-
return parent::isDecimalIntegerString();
334+
return TrinaryLogic::createFromBoolean((string) (int) $this->value === $this->value);
335335
}
336336

337337
public function isNonEmptyString(): TrinaryLogic

tests/PHPStan/Type/Constant/ConstantStringTypeTest.php

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,63 @@ public function testSetInvalidValue(): void
185185
$this->assertInstanceOf(ErrorType::class, $result);
186186
}
187187

188-
public function test
188+
public static function dataIsDecimalIntegerString(): iterable
189+
{
190+
yield [
191+
'0',
192+
TrinaryLogic::createYes(),
193+
];
194+
yield [
195+
'1',
196+
TrinaryLogic::createYes(),
197+
];
198+
yield [
199+
'1234',
200+
TrinaryLogic::createYes(),
201+
];
202+
yield [
203+
'-1',
204+
TrinaryLogic::createYes(),
205+
];
206+
yield [
207+
'+1',
208+
TrinaryLogic::createNo(),
209+
];
210+
yield [
211+
'00',
212+
TrinaryLogic::createNo(),
213+
];
214+
yield [
215+
'01',
216+
TrinaryLogic::createNo(),
217+
];
218+
yield [
219+
'18E+3',
220+
TrinaryLogic::createNo(),
221+
];
222+
yield [
223+
'1.2',
224+
TrinaryLogic::createNo(),
225+
];
226+
yield [
227+
'1,3',
228+
TrinaryLogic::createNo(),
229+
];
230+
yield [
231+
'foo',
232+
TrinaryLogic::createNo(),
233+
];
234+
yield [
235+
'1foo',
236+
TrinaryLogic::createNo(),
237+
];
238+
}
239+
240+
#[DataProvider('dataIsDecimalIntegerString')]
241+
public function testIsDecimalIntegerString(string $value, TrinaryLogic $expected): void
242+
{
243+
$type = new ConstantStringType($value);
244+
$this->assertSame($expected->describe(), $type->isDecimalIntegerString()->describe());
245+
}
189246

190247
}

0 commit comments

Comments
 (0)