Skip to content
Merged
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
17 changes: 12 additions & 5 deletions src/Type/Php/ArrayColumnFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\ShouldNotHappenException;
Expand All @@ -25,6 +26,10 @@
class ArrayColumnFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

public function __construct(private PhpVersion $phpVersion)
{
}

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'array_column';
Expand Down Expand Up @@ -187,15 +192,17 @@ private function castToArrayKeyType(Type $type): Type
{
$isArray = $type->isArray();
if ($isArray->yes()) {
return new IntegerType();
return $this->phpVersion->throwsTypeErrorForInternalFunctions() ? new NeverType() : new IntegerType();
}
if ($isArray->no()) {
return ArrayType::castToArrayKeyType($type);
}
return TypeCombinator::union(
ArrayType::castToArrayKeyType(TypeCombinator::remove($type, new ArrayType(new MixedType(), new MixedType()))),
new IntegerType(),
);
$withoutArrayType = TypeCombinator::remove($type, new ArrayType(new MixedType(), new MixedType()));
$keyType = ArrayType::castToArrayKeyType($withoutArrayType);
if ($this->phpVersion->throwsTypeErrorForInternalFunctions()) {
return $keyType;
}
return TypeCombinator::union($keyType, new IntegerType());
}

}
7 changes: 7 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,14 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6399.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4357.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5817.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/array-column.php');
if (PHP_VERSION_ID >= 80000) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-column-php8.php');
} else {
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-column-php7.php');
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6497.php');

if (PHP_VERSION_ID >= 70400) {
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Analyser/data/array-column-php7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace ArrayColumn\Php7;

use function PHPStan\Testing\assertType;

class ArrayColumnPhp7Test
{

/** @param array<int, array{column: string, key: array}> $array */
public function testConstantArray1(array $array): void
{
assertType('array<int, string>', array_column($array, 'column', 'key'));
}

/** @param array<int, array{column: string, key: array|string}> $array */
public function testConstantArray2(array $array): void
{
assertType('array<int|string, string>', array_column($array, 'column', 'key'));
}

}
22 changes: 22 additions & 0 deletions tests/PHPStan/Analyser/data/array-column-php8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace ArrayColumn\Php8;

use function PHPStan\Testing\assertType;

class ArrayColumnPhp7Test
{

/** @param array<int, array{column: string, key: array}> $array */
public function testConstantArray1(array $array): void
{
assertType('array<*NEVER*, string>', array_column($array, 'column', 'key'));
}

/** @param array<int, array{column: string, key: array|string}> $array */
public function testConstantArray2(array $array): void
{
assertType('array<string, string>', array_column($array, 'column', 'key'));
}

}
Loading