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
73 changes: 73 additions & 0 deletions src/Type/Php/ArrayCountValuesDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\ErrorType;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use function count;

#[AutowiredService]
final class ArrayCountValuesDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

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

public function getTypeFromFunctionCall(
FunctionReflection $functionReflection,
FuncCall $functionCall,
Scope $scope,
): ?Type
{
$args = $functionCall->getArgs();

if (!isset($args[0])) {
return null;
}

$inputType = $scope->getType($args[0]->value);

$arrayTypes = $inputType->getArrays();

$outputTypes = [];

foreach ($arrayTypes as $arrayType) {
$itemType = $arrayType->getItemType();

if ($itemType instanceof UnionType) {
$itemType = $itemType->filterTypes(
static fn ($type) => !$type->toArrayKey() instanceof ErrorType,
);
}

if ($itemType->toArrayKey() instanceof ErrorType) {
continue;
}

$outputTypes[] = TypeCombinator::intersect(
new ArrayType($itemType, IntegerRangeType::fromInterval(1, null)),
new NonEmptyArrayType(),
);
}

if (count($outputTypes) === 0) {
return new ConstantArrayType([], []);
}

return TypeCombinator::union(...$outputTypes);
}

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

namespace ArrayCountValues;

use function PHPStan\Testing\assertType;

$ints = array_count_values([1, 2, 2, 3]);

assertType('non-empty-array<1|2|3, int<1, max>>', $ints);

$strings = array_count_values(['one', 'two', 'two', 'three']);

assertType('non-empty-array<\'one\'|\'three\'|\'two\', int<1, max>>', $strings);

$objects = array_count_values([new \stdClass()]);

assertType('array{}', $objects);

/**
* @return array<int, string|object>
*/
function returnsStringOrObjectArray(): array
{

}

// Objects are ignored by array_count_values, with a warning emitted.
assertType('non-empty-array<string, int<1, max>>', array_count_values(returnsStringOrObjectArray()));

class StringableObject
{

public function __toString(): string
{
return 'string';
}

}

// Stringable objects are ignored by array_count_values, with a warning emitted.
$stringable = array_count_values([new StringableObject(), 'string', 1]);

assertType('non-empty-array<1|\'string\', int<1, max>>', $stringable);
Loading