Skip to content

Commit

Permalink
HasOffsetType - put constructor parameter type natively
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 30, 2024
1 parent 0296a91 commit b5accb3
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 129 deletions.
1 change: 0 additions & 1 deletion build/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ parameters:
message: "#^Parameter \\#1 (?:\\$argument|\\$objectOrClass) of class ReflectionClass constructor expects class\\-string\\<PHPStan\\\\ExtensionInstaller\\\\GeneratedConfig\\>\\|PHPStan\\\\ExtensionInstaller\\\\GeneratedConfig, string given\\.$#"
count: 1
path: ../src/Diagnose/PHPStanDiagnoseExtension.php
- '#^Parameter \#1 \$offsetType of class PHPStan\\Type\\Accessory\\HasOffsetType constructor expects PHPStan\\Type\\Constant\\ConstantIntegerType\|PHPStan\\Type\\Constant\\ConstantStringType#'
- '#^Short ternary operator is not allowed#'
reportStaticMethodSignatures: true
tmpDir: %rootDir%/tmp
Expand Down
3 changes: 1 addition & 2 deletions src/Type/Accessory/HasOffsetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ class HasOffsetType implements CompoundType, AccessoryType

/**
* @api
* @param ConstantStringType|ConstantIntegerType $offsetType
*/
public function __construct(private Type $offsetType)
public function __construct(private ConstantStringType|ConstantIntegerType $offsetType)
{
}

Expand Down
15 changes: 13 additions & 2 deletions src/Type/Php/ArrayKeyExistsFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
Expand Down Expand Up @@ -58,10 +59,20 @@ public function specifyTypes(
$keyType = $scope->getType($key);
$arrayType = $scope->getType($array);

if (!$keyType instanceof ConstantIntegerType
if (
!$keyType instanceof ConstantIntegerType
&& !$keyType instanceof ConstantStringType
&& !$arrayType->isIterableAtLeastOnce()->no()) {
) {
if ($context->true()) {
if ($arrayType->isIterableAtLeastOnce()->no()) {
return $this->typeSpecifier->create(
$array,
new NonEmptyArrayType(),
$context,
$scope,
);
}

$arrayKeyType = $arrayType->getIterableKeyType();
if ($keyType->isString()->yes()) {
$arrayKeyType = $arrayKeyType->toString();
Expand Down
5 changes: 0 additions & 5 deletions tests/PHPStan/Type/Accessory/HasMethodTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ public function dataIsSuperTypeOf(): array
new HasPropertyType('bar'),
TrinaryLogic::createMaybe(),
],
[
new HasMethodType('foo'),
new HasOffsetType(new MixedType()),
TrinaryLogic::createMaybe(),
],
[
new HasMethodType('foo'),
new IterableType(new MixedType(), new MixedType()),
Expand Down
5 changes: 0 additions & 5 deletions tests/PHPStan/Type/Accessory/HasPropertyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ public function dataIsSuperTypeOf(): array
new HasPropertyType('bar'),
TrinaryLogic::createMaybe(),
],
[
new HasPropertyType('foo'),
new HasOffsetType(new MixedType()),
TrinaryLogic::createMaybe(),
],
[
new HasPropertyType('foo'),
new IterableType(new MixedType(), new MixedType()),
Expand Down
47 changes: 0 additions & 47 deletions tests/PHPStan/Type/IntersectionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPStan\Testing\PHPStanTestCase;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\HasPropertyType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\Accessory\OversizedArrayType;
Expand Down Expand Up @@ -154,52 +153,6 @@ public function dataIsSuperTypeOf(): Iterator
TrinaryLogic::createNo(),
];

yield [
new IntersectionType([
new ArrayType(new MixedType(), new MixedType()),
new HasOffsetType(new StringType()),
]),
new ConstantArrayType([
new ConstantStringType('a'),
new ConstantStringType('b'),
new ConstantStringType('c'),
], [
new ConstantIntegerType(1),
new ConstantIntegerType(2),
new ConstantIntegerType(3),
]),
TrinaryLogic::createMaybe(),
];

yield [
new IntersectionType([
new ArrayType(new MixedType(), new MixedType()),
new HasOffsetType(new StringType()),
]),
new ConstantArrayType([
new ConstantStringType('a'),
new ConstantStringType('b'),
new ConstantStringType('c'),
new ConstantStringType('d'),
new ConstantStringType('e'),
new ConstantStringType('f'),
new ConstantStringType('g'),
new ConstantStringType('h'),
new ConstantStringType('i'),
], [
new ConstantIntegerType(1),
new ConstantIntegerType(2),
new ConstantIntegerType(3),
new ConstantIntegerType(1),
new ConstantIntegerType(2),
new ConstantIntegerType(3),
new ConstantIntegerType(1),
new ConstantIntegerType(2),
new ConstantIntegerType(3),
]),
TrinaryLogic::createMaybe(),
];

yield [
new IntersectionType([
new ObjectType(Traversable::class),
Expand Down
67 changes: 0 additions & 67 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -873,20 +873,6 @@ public function dataUnion(): iterable
UnionType::class,
"'bar'|'barr'|'baz'|'bazz'|'foo'|'fooo'|'lorem'|'loremm'|'loremmm'",
],
[
[
new IntersectionType([
new ArrayType(new MixedType(), new StringType()),
new HasOffsetType(new StringType()),
]),
new IntersectionType([
new ArrayType(new MixedType(), new StringType()),
new HasOffsetType(new StringType()),
]),
],
IntersectionType::class,
'array<string>&hasOffset(string)',
],
[
[
new IntersectionType([
Expand Down Expand Up @@ -1857,22 +1843,6 @@ public function dataUnion(): iterable
UnionType::class,
'array{a: int, b: int}|array{b: int, c: int}',
],
[
[
TypeCombinator::intersect(new StringType(), new HasOffsetType(new IntegerType())),
TypeCombinator::intersect(new StringType(), new HasOffsetType(new IntegerType())),
],
IntersectionType::class,
'string&hasOffset(int)',
],
[
[
TypeCombinator::intersect(new ConstantStringType('abc'), new HasOffsetType(new IntegerType())),
TypeCombinator::intersect(new ConstantStringType('abc'), new HasOffsetType(new IntegerType())),
],
IntersectionType::class,
'\'abc\'&hasOffset(int)',
],
[
[
StaticTypeFactory::falsey(),
Expand Down Expand Up @@ -3150,24 +3120,6 @@ public function dataIntersect(): iterable
IntersectionType::class,
'array<string, string>&hasOffset(\'a\')',
],
[
[
new ArrayType(new StringType(), new StringType()),
new HasOffsetType(new StringType()),
new HasOffsetType(new StringType()),
],
IntersectionType::class,
'array<string, string>&hasOffset(string)',
],
[
[
new ArrayType(new MixedType(), new MixedType()),
new HasOffsetType(new StringType()),
new HasOffsetType(new StringType()),
],
IntersectionType::class,
'array&hasOffset(string)',
],
[
[
new ConstantArrayType(
Expand Down Expand Up @@ -3253,17 +3205,6 @@ public function dataIntersect(): iterable
ClosureType::class,
'Closure(): mixed',
],
[
[
new UnionType([
new ArrayType(new MixedType(), new StringType()),
new NullType(),
]),
new HasOffsetType(new StringType()),
],
IntersectionType::class,
'array<string>&hasOffset(string)',
],
[
[
new ArrayType(new MixedType(), new MixedType()),
Expand Down Expand Up @@ -3772,14 +3713,6 @@ public function dataIntersect(): iterable
ConstantArrayType::class,
'array{a: int, b: int}',
],
[
[
new StringType(),
new HasOffsetType(new IntegerType()),
],
IntersectionType::class,
'string&hasOffset(int)',
],
[
[
new BenevolentUnionType([new IntegerType(), new StringType()]),
Expand Down

0 comments on commit b5accb3

Please sign in to comment.