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 19 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
70 changes: 70 additions & 0 deletions src/Type/Php/ArrayFlipFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
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();

// make sure items, which get turned into keys contain only valid types
$itemType = $this->sanitizeKeyTypes($itemType);
staabm marked this conversation as resolved.
Show resolved Hide resolved

if ($itemType !== null) {
$flippedArrayType = new ArrayType(
$itemType,
$keyType
);

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the actual feature I primarily want to implement with this PR.

regarding the key/type flipping I did a lot of guesswork.. I need some guidance on how this should look like :-/


return $flippedArrayType;
}
}

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

private function sanitizeKeyTypes(Type $type): ?Type
{
if (
!$type instanceof IntegerType
&& !$type instanceof StringType
) {
return null;
}

return $type;
}
}
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
47 changes: 47 additions & 0 deletions tests/PHPStan/Analyser/data/array-flip.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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);

if (PHP_VERSION_ID < 80000) {
assertType('array|null', $flip);
} else {
assertType('array', $flip);
}
staabm marked this conversation as resolved.
Show resolved Hide resolved
}

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


/**
* @param array<1|2|3, string> $list
*/
function foo5($array)
{
$flip = array_flip($array);
assertType('array<string, 1|2|3>', $flip);
}
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/data/non-empty-array.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ 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)>&nonEmpty', array_flip($array));
}
}