2022-01-25 22:08:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
|
|
|
class ValueOfTemplateTest extends TestCase
|
|
|
|
{
|
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return iterable<string,array{code:string,assertions?:array<string,string>,ignored_issues?:list<string>,php_version?:string}>
|
|
|
|
*/
|
|
|
|
public function providerValidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'acceptsArrayValuesFn' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @template T of array
|
|
|
|
* @param T $array
|
|
|
|
* @return value-of<T>[]
|
|
|
|
*/
|
|
|
|
function getValues($array) {
|
|
|
|
return array_values($array);
|
|
|
|
}
|
|
|
|
'
|
|
|
|
],
|
|
|
|
'SKIPPED-acceptsIfInArrayFn' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @template T of array
|
|
|
|
* @param T $array
|
|
|
|
* @return value-of<T>|null
|
|
|
|
*/
|
|
|
|
function getValue(string $value, $array) {
|
|
|
|
if (in_array($value, $array)) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
'
|
|
|
|
],
|
2022-01-27 21:59:26 +01:00
|
|
|
'valueOfUnreplacedTemplateParam' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @template T as array<bool>
|
|
|
|
*/
|
|
|
|
abstract class Foo {
|
|
|
|
/**
|
|
|
|
* @return value-of<T>
|
|
|
|
*/
|
|
|
|
abstract public function getRandomValue(): bool;
|
2022-01-28 00:04:21 +01:00
|
|
|
}
|
|
|
|
',
|
|
|
|
],
|
|
|
|
'valueOfNestedTemplates' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @template TValue
|
|
|
|
* @template TArray of array<TValue>
|
|
|
|
* @param TArray $array
|
|
|
|
* @return list<TValue>
|
|
|
|
*/
|
|
|
|
function toList(array $array): array {
|
|
|
|
return array_values($array);
|
|
|
|
}'
|
2022-01-27 21:59:26 +01:00
|
|
|
],
|
2022-01-25 22:08:44 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return iterable<string,array{code:string,error_message:string,ignored_issues?:list<string>,php_version?:string}>
|
|
|
|
*/
|
|
|
|
public function providerInvalidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'valueOfTemplateNotIncludesString' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @template T of array
|
|
|
|
* @param T $array
|
|
|
|
* @return value-of<T>
|
|
|
|
*/
|
|
|
|
function getValue($array) {
|
|
|
|
return "foo";
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'InvalidReturnStatement'
|
|
|
|
],
|
|
|
|
'valueOfTemplateNotIncludesInt' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @template T of array
|
|
|
|
* @param T $array
|
|
|
|
* @return value-of<T>
|
|
|
|
*/
|
|
|
|
function getValue($array) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'InvalidReturnStatement'
|
|
|
|
],
|
2022-01-27 21:59:26 +01:00
|
|
|
'valueOfUnresolvedTemplateParamIsStillChecked' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @template T as array<bool>
|
|
|
|
*/
|
|
|
|
abstract class Foo {
|
|
|
|
/**
|
|
|
|
* @return value-of<T>
|
|
|
|
*/
|
|
|
|
abstract public function getRandomValue(): string;
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'MismatchingDocblockReturnType'
|
|
|
|
],
|
2022-01-25 22:08:44 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|