Skip to content

Commit b5accb3

Browse files
committed
HasOffsetType - put constructor parameter type natively
1 parent 0296a91 commit b5accb3

7 files changed

+14
-129
lines changed

Diff for: build/phpstan.neon

-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ parameters:
8989
message: "#^Parameter \\#1 (?:\\$argument|\\$objectOrClass) of class ReflectionClass constructor expects class\\-string\\<PHPStan\\\\ExtensionInstaller\\\\GeneratedConfig\\>\\|PHPStan\\\\ExtensionInstaller\\\\GeneratedConfig, string given\\.$#"
9090
count: 1
9191
path: ../src/Diagnose/PHPStanDiagnoseExtension.php
92-
- '#^Parameter \#1 \$offsetType of class PHPStan\\Type\\Accessory\\HasOffsetType constructor expects PHPStan\\Type\\Constant\\ConstantIntegerType\|PHPStan\\Type\\Constant\\ConstantStringType#'
9392
- '#^Short ternary operator is not allowed#'
9493
reportStaticMethodSignatures: true
9594
tmpDir: %rootDir%/tmp

Diff for: src/Type/Accessory/HasOffsetType.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ class HasOffsetType implements CompoundType, AccessoryType
4444

4545
/**
4646
* @api
47-
* @param ConstantStringType|ConstantIntegerType $offsetType
4847
*/
49-
public function __construct(private Type $offsetType)
48+
public function __construct(private ConstantStringType|ConstantIntegerType $offsetType)
5049
{
5150
}
5251

Diff for: src/Type/Php/ArrayKeyExistsFunctionTypeSpecifyingExtension.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPStan\Analyser\TypeSpecifierContext;
1515
use PHPStan\Reflection\FunctionReflection;
1616
use PHPStan\Type\Accessory\HasOffsetType;
17+
use PHPStan\Type\Accessory\NonEmptyArrayType;
1718
use PHPStan\Type\ArrayType;
1819
use PHPStan\Type\Constant\ConstantIntegerType;
1920
use PHPStan\Type\Constant\ConstantStringType;
@@ -58,10 +59,20 @@ public function specifyTypes(
5859
$keyType = $scope->getType($key);
5960
$arrayType = $scope->getType($array);
6061

61-
if (!$keyType instanceof ConstantIntegerType
62+
if (
63+
!$keyType instanceof ConstantIntegerType
6264
&& !$keyType instanceof ConstantStringType
63-
&& !$arrayType->isIterableAtLeastOnce()->no()) {
65+
) {
6466
if ($context->true()) {
67+
if ($arrayType->isIterableAtLeastOnce()->no()) {
68+
return $this->typeSpecifier->create(
69+
$array,
70+
new NonEmptyArrayType(),
71+
$context,
72+
$scope,
73+
);
74+
}
75+
6576
$arrayKeyType = $arrayType->getIterableKeyType();
6677
if ($keyType->isString()->yes()) {
6778
$arrayKeyType = $arrayKeyType->toString();

Diff for: tests/PHPStan/Type/Accessory/HasMethodTypeTest.php

-5
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,6 @@ public function dataIsSuperTypeOf(): array
7070
new HasPropertyType('bar'),
7171
TrinaryLogic::createMaybe(),
7272
],
73-
[
74-
new HasMethodType('foo'),
75-
new HasOffsetType(new MixedType()),
76-
TrinaryLogic::createMaybe(),
77-
],
7873
[
7974
new HasMethodType('foo'),
8075
new IterableType(new MixedType(), new MixedType()),

Diff for: tests/PHPStan/Type/Accessory/HasPropertyTypeTest.php

-5
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ public function dataIsSuperTypeOf(): array
6060
new HasPropertyType('bar'),
6161
TrinaryLogic::createMaybe(),
6262
],
63-
[
64-
new HasPropertyType('foo'),
65-
new HasOffsetType(new MixedType()),
66-
TrinaryLogic::createMaybe(),
67-
],
6863
[
6964
new HasPropertyType('foo'),
7065
new IterableType(new MixedType(), new MixedType()),

Diff for: tests/PHPStan/Type/IntersectionTypeTest.php

-47
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PHPStan\Testing\PHPStanTestCase;
99
use PHPStan\TrinaryLogic;
1010
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
11-
use PHPStan\Type\Accessory\HasOffsetType;
1211
use PHPStan\Type\Accessory\HasPropertyType;
1312
use PHPStan\Type\Accessory\NonEmptyArrayType;
1413
use PHPStan\Type\Accessory\OversizedArrayType;
@@ -154,52 +153,6 @@ public function dataIsSuperTypeOf(): Iterator
154153
TrinaryLogic::createNo(),
155154
];
156155

157-
yield [
158-
new IntersectionType([
159-
new ArrayType(new MixedType(), new MixedType()),
160-
new HasOffsetType(new StringType()),
161-
]),
162-
new ConstantArrayType([
163-
new ConstantStringType('a'),
164-
new ConstantStringType('b'),
165-
new ConstantStringType('c'),
166-
], [
167-
new ConstantIntegerType(1),
168-
new ConstantIntegerType(2),
169-
new ConstantIntegerType(3),
170-
]),
171-
TrinaryLogic::createMaybe(),
172-
];
173-
174-
yield [
175-
new IntersectionType([
176-
new ArrayType(new MixedType(), new MixedType()),
177-
new HasOffsetType(new StringType()),
178-
]),
179-
new ConstantArrayType([
180-
new ConstantStringType('a'),
181-
new ConstantStringType('b'),
182-
new ConstantStringType('c'),
183-
new ConstantStringType('d'),
184-
new ConstantStringType('e'),
185-
new ConstantStringType('f'),
186-
new ConstantStringType('g'),
187-
new ConstantStringType('h'),
188-
new ConstantStringType('i'),
189-
], [
190-
new ConstantIntegerType(1),
191-
new ConstantIntegerType(2),
192-
new ConstantIntegerType(3),
193-
new ConstantIntegerType(1),
194-
new ConstantIntegerType(2),
195-
new ConstantIntegerType(3),
196-
new ConstantIntegerType(1),
197-
new ConstantIntegerType(2),
198-
new ConstantIntegerType(3),
199-
]),
200-
TrinaryLogic::createMaybe(),
201-
];
202-
203156
yield [
204157
new IntersectionType([
205158
new ObjectType(Traversable::class),

Diff for: tests/PHPStan/Type/TypeCombinatorTest.php

-67
Original file line numberDiff line numberDiff line change
@@ -873,20 +873,6 @@ public function dataUnion(): iterable
873873
UnionType::class,
874874
"'bar'|'barr'|'baz'|'bazz'|'foo'|'fooo'|'lorem'|'loremm'|'loremmm'",
875875
],
876-
[
877-
[
878-
new IntersectionType([
879-
new ArrayType(new MixedType(), new StringType()),
880-
new HasOffsetType(new StringType()),
881-
]),
882-
new IntersectionType([
883-
new ArrayType(new MixedType(), new StringType()),
884-
new HasOffsetType(new StringType()),
885-
]),
886-
],
887-
IntersectionType::class,
888-
'array<string>&hasOffset(string)',
889-
],
890876
[
891877
[
892878
new IntersectionType([
@@ -1857,22 +1843,6 @@ public function dataUnion(): iterable
18571843
UnionType::class,
18581844
'array{a: int, b: int}|array{b: int, c: int}',
18591845
],
1860-
[
1861-
[
1862-
TypeCombinator::intersect(new StringType(), new HasOffsetType(new IntegerType())),
1863-
TypeCombinator::intersect(new StringType(), new HasOffsetType(new IntegerType())),
1864-
],
1865-
IntersectionType::class,
1866-
'string&hasOffset(int)',
1867-
],
1868-
[
1869-
[
1870-
TypeCombinator::intersect(new ConstantStringType('abc'), new HasOffsetType(new IntegerType())),
1871-
TypeCombinator::intersect(new ConstantStringType('abc'), new HasOffsetType(new IntegerType())),
1872-
],
1873-
IntersectionType::class,
1874-
'\'abc\'&hasOffset(int)',
1875-
],
18761846
[
18771847
[
18781848
StaticTypeFactory::falsey(),
@@ -3150,24 +3120,6 @@ public function dataIntersect(): iterable
31503120
IntersectionType::class,
31513121
'array<string, string>&hasOffset(\'a\')',
31523122
],
3153-
[
3154-
[
3155-
new ArrayType(new StringType(), new StringType()),
3156-
new HasOffsetType(new StringType()),
3157-
new HasOffsetType(new StringType()),
3158-
],
3159-
IntersectionType::class,
3160-
'array<string, string>&hasOffset(string)',
3161-
],
3162-
[
3163-
[
3164-
new ArrayType(new MixedType(), new MixedType()),
3165-
new HasOffsetType(new StringType()),
3166-
new HasOffsetType(new StringType()),
3167-
],
3168-
IntersectionType::class,
3169-
'array&hasOffset(string)',
3170-
],
31713123
[
31723124
[
31733125
new ConstantArrayType(
@@ -3253,17 +3205,6 @@ public function dataIntersect(): iterable
32533205
ClosureType::class,
32543206
'Closure(): mixed',
32553207
],
3256-
[
3257-
[
3258-
new UnionType([
3259-
new ArrayType(new MixedType(), new StringType()),
3260-
new NullType(),
3261-
]),
3262-
new HasOffsetType(new StringType()),
3263-
],
3264-
IntersectionType::class,
3265-
'array<string>&hasOffset(string)',
3266-
],
32673208
[
32683209
[
32693210
new ArrayType(new MixedType(), new MixedType()),
@@ -3772,14 +3713,6 @@ public function dataIntersect(): iterable
37723713
ConstantArrayType::class,
37733714
'array{a: int, b: int}',
37743715
],
3775-
[
3776-
[
3777-
new StringType(),
3778-
new HasOffsetType(new IntegerType()),
3779-
],
3780-
IntersectionType::class,
3781-
'string&hasOffset(int)',
3782-
],
37833716
[
37843717
[
37853718
new BenevolentUnionType([new IntegerType(), new StringType()]),

0 commit comments

Comments
 (0)