2022-01-16 21:45:58 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
|
|
|
class ArrayKeysTest extends TestCase
|
|
|
|
{
|
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
|
|
|
|
|
|
|
public function providerValidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'arrayKeysOfEmptyArrayReturnsListOfEmpty' => [
|
|
|
|
'code' => '<?php
|
|
|
|
$keys = array_keys([]);
|
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$keys' => 'list<never>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayKeysOfKeyedArrayReturnsNonEmptyListOfStrings' => [
|
|
|
|
'code' => '<?php
|
2022-01-20 22:41:33 +01:00
|
|
|
$keys = array_keys(["foo" => "bar"]);
|
2022-01-16 21:45:58 +01:00
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$keys' => 'non-empty-list<string>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayKeysOfListReturnsNonEmptyListOfInts' => [
|
|
|
|
'code' => '<?php
|
2022-01-20 22:41:33 +01:00
|
|
|
$keys = array_keys(["foo", "bar"]);
|
2022-01-16 21:45:58 +01:00
|
|
|
',
|
|
|
|
'assertions' => [
|
2022-12-13 21:40:19 +01:00
|
|
|
'$keys' => 'non-empty-list<int<0, 1>>',
|
2022-01-16 21:45:58 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayKeysOfKeyedStringIntArrayReturnsNonEmptyListOfIntsOrStrings' => [
|
|
|
|
'code' => '<?php
|
2022-01-20 22:41:33 +01:00
|
|
|
$keys = array_keys(["foo" => "bar", 42]);
|
2022-01-16 21:45:58 +01:00
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$keys' => 'non-empty-list<int|string>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayKeysOfArrayConformsToArrayKeys' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @return list<array-key>
|
|
|
|
*/
|
|
|
|
function getKeys(array $array) {
|
|
|
|
return array_keys($array);
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
',
|
2022-01-16 21:45:58 +01:00
|
|
|
],
|
|
|
|
'arrayKeysOfKeyedArrayConformsToCorrectLiteralStringList' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
2022-01-20 22:41:33 +01:00
|
|
|
* @return non-empty-list<"foo"|"bar">
|
2022-01-16 21:45:58 +01:00
|
|
|
*/
|
|
|
|
function getKeys() {
|
2022-01-20 22:41:33 +01:00
|
|
|
return array_keys(["foo" => 42, "bar" => 42]);
|
2022-01-16 21:45:58 +01:00
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
',
|
2022-01-16 21:45:58 +01:00
|
|
|
],
|
|
|
|
'arrayKeysOfLiteralListConformsToCorrectLiteralOffsets' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @return non-empty-list<0|1>
|
|
|
|
*/
|
|
|
|
function getKeys() {
|
2022-01-20 22:41:33 +01:00
|
|
|
return array_keys(["foo", "bar"]);
|
2022-01-16 21:45:58 +01:00
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
',
|
2022-01-16 21:45:58 +01:00
|
|
|
],
|
|
|
|
'arrayKeyFirstOfLiteralListConformsToCorrectLiteralOffsets' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @return 0|1
|
|
|
|
*/
|
|
|
|
function getKey() {
|
2022-01-20 22:41:33 +01:00
|
|
|
return array_key_first(["foo", "bar"]);
|
2022-01-16 21:45:58 +01:00
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
',
|
2022-01-16 21:45:58 +01:00
|
|
|
],
|
|
|
|
'arrayKeyLastOfLiteralListConformsToCorrectLiteralOffsets' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @return 0|1
|
|
|
|
*/
|
|
|
|
function getKey() {
|
2022-01-20 22:41:33 +01:00
|
|
|
return array_key_last(["foo", "bar"]);
|
2022-01-16 21:45:58 +01:00
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
',
|
2022-01-16 21:45:58 +01:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providerInvalidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'arrayKeysOfStringArrayDoesntConformsToIntList' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @param array<string, mixed> $array
|
|
|
|
* @return list<int>
|
|
|
|
*/
|
|
|
|
function getKeys(array $array) {
|
|
|
|
return array_keys($array);
|
|
|
|
}
|
|
|
|
',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'InvalidReturnStatement',
|
2022-01-16 21:45:58 +01:00
|
|
|
],
|
|
|
|
'arrayKeysOfStringKeyedArrayDoesntConformToIntList' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @return list<int>
|
|
|
|
*/
|
|
|
|
function getKeys() {
|
2022-01-20 22:41:33 +01:00
|
|
|
return array_keys(["foo" => 42, "bar" => 42]);
|
2022-01-16 21:45:58 +01:00
|
|
|
}
|
|
|
|
',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'InvalidReturnStatement',
|
|
|
|
],
|
2022-01-16 21:45:58 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|