Skip to content
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

Added ArrayFlipFunctionReturnTypeExtension, Cover non-empty-array in array_flip #583

Merged
merged 26 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d9a9856
Cover non-empty-array in `array_flip`
staabm Jul 19, 2021
01e44ff
Implement ArrayFlipFunctionReturnTypeExtension
staabm Jul 19, 2021
bcd34a7
Update config.neon
staabm Jul 19, 2021
770e204
Update config.neon
staabm Jul 19, 2021
5b0170a
Update ArrayFlipFunctionReturnTypeExtension.php
staabm Jul 19, 2021
497306c
Update ArrayFlipFunctionReturnTypeExtension.php
staabm Jul 19, 2021
76e7b5f
Update ArrayFlipFunctionReturnTypeExtension.php
staabm Jul 19, 2021
ec6be31
Update ArrayFlipFunctionReturnTypeExtension.php
staabm Jul 19, 2021
1e5d3c3
Update ArrayFlipFunctionReturnTypeExtension.php
staabm Jul 19, 2021
a7a1379
Update ArrayFlipFunctionReturnTypeExtension.php
staabm Jul 19, 2021
821a17b
Update ArrayFlipFunctionReturnTypeExtension.php
staabm Jul 19, 2021
f779f2d
Apply suggestions from code review
staabm Jul 19, 2021
8f9a677
fix CS
clxmstaab Jul 20, 2021
09a2b57
more test coverage
clxmstaab Jul 20, 2021
cd7200a
fix test
clxmstaab Jul 20, 2021
41aa24c
simplify
clxmstaab Jul 20, 2021
d188e9a
fix version dependent expectations
clxmstaab Jul 20, 2021
5819f7b
fix types
clxmstaab Jul 20, 2021
bffacfb
fix types
clxmstaab Jul 20, 2021
7cb6819
Update ArrayFlipFunctionReturnTypeExtension.php
clxmstaab Jul 20, 2021
51b7211
yield different tests based on phpversion
clxmstaab Jul 20, 2021
d8bf630
utlize ArrayType::castToArrayKeyType()
clxmstaab Jul 20, 2021
01b6303
more testcoverage
clxmstaab Jul 20, 2021
c8c1a61
rm use
clxmstaab Jul 20, 2021
593941a
fix typo
clxmstaab Jul 20, 2021
5128569
remove php8 only test
clxmstaab Jul 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 @@ -928,6 +928,11 @@ services:
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

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

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

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;

class ArrayFlipFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
{

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

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
if (count($functionCall->args) !== 1) {
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
}

$array = $functionCall->args[0]->value;
$argType = $scope->getType($array);

if ($argType->isArray()->yes()) {
$keyType = $argType->getIterableKeyType();
$itemType = $argType->getIterableValueType();

$itemType = ArrayType::castToArrayKeyType($itemType);

$flippedArrayType = new ArrayType(
$itemType,
$keyType
);

if ($argType->isIterableAtLeastOnce()->yes()) {
$flippedArrayType = TypeCombinator::intersect($flippedArrayType, new NonEmptyArrayType());
}

return $flippedArrayType;
}

return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
}

}
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/infer-array-key.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/offset-value-after-assign.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2112.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-flip.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-map-closure.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-sum.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4573.php');
Expand Down
43 changes: 43 additions & 0 deletions tests/PHPStan/Analyser/data/array-flip.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace ArrayFlip;

use function PHPStan\Testing\assertType;

/**
* @param int[] $integerList
*/
function foo($integerList)
{
$flip = array_flip($integerList);
assertType('array<int, (int|string)>', $flip);
}

/**
* @param mixed[] $list
*/
function foo3($list)
{
$flip = array_flip($list);

assertType('array<int|string, (int|string)>', $flip);
}

/**
* @param array<int, 1|2|3> $array
*/
function foo4($array)
{
$flip = array_flip($array);
assertType('array<1|2|3, int>', $flip);
}


/**
* @param array<1|2|3, string> $array
*/
function foo5($array)
{
$flip = array_flip($array);
assertType('array<string, 1|2|3>', $flip);
}
6 changes: 5 additions & 1 deletion tests/PHPStan/Analyser/data/non-empty-array.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ public function doFoo(
/**
* @param non-empty-array $array
* @param non-empty-list $list
* @param non-empty-array<string> $stringArray
*/
public function arrayFunctions($array, $list): void
public function arrayFunctions($array, $list, $stringArray): void
{
assertType('array&nonEmpty', array_combine($array, $array));
assertType('array&nonEmpty', array_combine($list, $list));
Expand All @@ -47,5 +48,8 @@ public function arrayFunctions($array, $list): void
assertType('array&nonEmpty', array_merge([], $array));
assertType('array&nonEmpty', array_merge($array, []));
assertType('array&nonEmpty', array_merge($array, $array));

assertType('array<int|string, (int|string)>&nonEmpty', array_flip($array));
assertType('array<string, (int|string)>&nonEmpty', array_flip($stringArray));
}
}