Skip to content

Introduce ArrayPadDynamicReturnTypeExtension #4017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 2.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,11 @@ services:
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\ArrayPadDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\ArrayPopFunctionReturnTypeExtension
tags:
Expand Down
54 changes: 54 additions & 0 deletions src/Type/Php/ArrayPadDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;

final class ArrayPadDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'array_pad';
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
if (!isset($functionCall->getArgs()[2])) {
return null;
}

$arrayType = $scope->getType($functionCall->getArgs()[0]->value);
$itemType = $scope->getType($functionCall->getArgs()[2]->value);

$returnType = new ArrayType(
TypeCombinator::union($arrayType->getIterableKeyType(), new IntegerType()),
TypeCombinator::union($arrayType->getIterableValueType(), $itemType),
);

$lengthType = $scope->getType($functionCall->getArgs()[1]->value);
if (
$arrayType->isIterableAtLeastOnce()->yes()
|| $lengthType->isSuperTypeOf(new ConstantIntegerType(0))->no()
) {
$returnType = TypeCombinator::intersect($returnType, new NonEmptyArrayType());
}

if ($arrayType->isList()->yes()) {
$returnType = TypeCombinator::intersect($returnType, new AccessoryArrayListType());
}

return $returnType;
}

}
78 changes: 78 additions & 0 deletions tests/PHPStan/Analyser/nsrt/array_pad.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace ArrayPad;

use function PHPStan\Testing\assertType;

class HelloWorld
{
/**
* @param array<string, string> $arrayString
* @param array<int, int> $arrayInt
* @param non-empty-array<string, string> $nonEmptyArrayString
* @param non-empty-array<int, int> $nonEmptyArrayInt
* @param list<string> $listString
* @param list<int> $listInt
* @param non-empty-list<string> $nonEmptyListString
* @param non-empty-list<int> $nonEmptyListInt
* @param int $int
* @param positive-int $positiveInt
* @param negative-int $negativeInt
* @param positive-int|negative-int $nonZero
*/
public function test(
$arrayString,
$arrayInt,
$nonEmptyArrayString,
$nonEmptyArrayInt,
$listString,
$listInt,
$nonEmptyListString,
$nonEmptyListInt,
$int,
$positiveInt,
$negativeInt,
$nonZero,
): void
{
assertType('array<int|string, string>', array_pad($arrayString, $int, 'foo'));
assertType('non-empty-array<int|string, string>', array_pad($arrayString, $positiveInt, 'foo'));
assertType('non-empty-array<int|string, string>', array_pad($arrayString, $negativeInt, 'foo'));
assertType('non-empty-array<int|string, string>', array_pad($arrayString, $nonZero, 'foo'));

assertType('array<int, \'foo\'|int>', array_pad($arrayInt, $int, 'foo'));
assertType('non-empty-array<int, \'foo\'|int>', array_pad($arrayInt, $positiveInt, 'foo'));
assertType('non-empty-array<int, \'foo\'|int>', array_pad($arrayInt, $negativeInt, 'foo'));
assertType('non-empty-array<int, \'foo\'|int>', array_pad($arrayInt, $nonZero, 'foo'));

assertType('non-empty-array<int|string, string>', array_pad($nonEmptyArrayString, $int, 'foo'));
assertType('non-empty-array<int|string, string>', array_pad($nonEmptyArrayString, $positiveInt, 'foo'));
assertType('non-empty-array<int|string, string>', array_pad($nonEmptyArrayString, $negativeInt, 'foo'));
assertType('non-empty-array<int|string, string>', array_pad($nonEmptyArrayString, $nonZero, 'foo'));

assertType('non-empty-array<int, \'foo\'|int>', array_pad($nonEmptyArrayInt, $int, 'foo'));
assertType('non-empty-array<int, \'foo\'|int>', array_pad($nonEmptyArrayInt, $positiveInt, 'foo'));
assertType('non-empty-array<int, \'foo\'|int>', array_pad($nonEmptyArrayInt, $negativeInt, 'foo'));
assertType('non-empty-array<int, \'foo\'|int>', array_pad($nonEmptyArrayInt, $nonZero, 'foo'));

assertType('list<string>', array_pad($listString, $int, 'foo'));
assertType('non-empty-list<string>', array_pad($listString, $positiveInt, 'foo'));
assertType('non-empty-list<string>', array_pad($listString, $negativeInt, 'foo'));
assertType('non-empty-list<string>', array_pad($listString, $nonZero, 'foo'));

assertType('list<\'foo\'|int>', array_pad($listInt, $int, 'foo'));
assertType('non-empty-list<\'foo\'|int>', array_pad($listInt, $positiveInt, 'foo'));
assertType('non-empty-list<\'foo\'|int>', array_pad($listInt, $negativeInt, 'foo'));
assertType('non-empty-list<\'foo\'|int>', array_pad($listInt, $nonZero, 'foo'));

assertType('non-empty-list<string>', array_pad($nonEmptyListString, $int, 'foo'));
assertType('non-empty-list<string>', array_pad($nonEmptyListString, $positiveInt, 'foo'));
assertType('non-empty-list<string>', array_pad($nonEmptyListString, $negativeInt, 'foo'));
assertType('non-empty-list<string>', array_pad($nonEmptyListString, $nonZero, 'foo'));

assertType('non-empty-list<\'foo\'|int>', array_pad($nonEmptyListInt, $int, 'foo'));
assertType('non-empty-list<\'foo\'|int>', array_pad($nonEmptyListInt, $positiveInt, 'foo'));
assertType('non-empty-list<\'foo\'|int>', array_pad($nonEmptyListInt, $negativeInt, 'foo'));
assertType('non-empty-list<\'foo\'|int>', array_pad($nonEmptyListInt, $nonZero, 'foo'));
}
}
Loading