2016-12-04 05:03:51 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2016-12-04 05:03:51 +01:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class Php71Test extends TestCase
|
2016-12-04 05:03:51 +01:00
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2016-12-04 19:35:38 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2022-11-05 22:34:42 +01:00
|
|
|
*
|
2017-01-13 20:07:23 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2016-12-04 19:35:38 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'nullableReturnType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
function a(): ?string
|
|
|
|
{
|
|
|
|
return rand(0, 10) ? "elePHPant" : null;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = a();',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$a' => 'null|string',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableReturnTypeInDocblock' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return ?string */
|
|
|
|
function a() {
|
|
|
|
return rand(0, 10) ? "elePHPant" : null;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = a();',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$a' => 'null|string',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableArgument' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function test(?string $name): ?string
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return $name;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
test("elePHPant");
|
2017-05-27 02:05:57 +02:00
|
|
|
test(null);',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'protectedClassConst' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A
|
|
|
|
{
|
|
|
|
protected const IS_PROTECTED = 1;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A
|
|
|
|
{
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(): int {
|
2017-04-25 05:45:02 +02:00
|
|
|
return A::IS_PROTECTED;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'privateClassConst' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A
|
|
|
|
{
|
|
|
|
private const IS_PRIVATE = 1;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(): int {
|
2017-04-25 05:45:02 +02:00
|
|
|
return A::IS_PRIVATE;
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'publicClassConstFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A
|
|
|
|
{
|
|
|
|
public const IS_PUBLIC = 1;
|
|
|
|
const IS_ALSO_PUBLIC = 2;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A
|
|
|
|
{
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(): int {
|
2017-04-25 05:45:02 +02:00
|
|
|
echo A::IS_PUBLIC;
|
|
|
|
return A::IS_ALSO_PUBLIC;
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo A::IS_PUBLIC;
|
2017-05-27 02:05:57 +02:00
|
|
|
echo A::IS_ALSO_PUBLIC;',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2019-10-22 17:17:42 +02:00
|
|
|
'arrayDestructuringList' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$data = [
|
|
|
|
[1, "Tom"],
|
|
|
|
[2, "Fred"],
|
|
|
|
];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
// list() style
|
|
|
|
list($id1, $name1) = $data[0];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
// [] style
|
|
|
|
[$id2, $name2] = $data[1];',
|
|
|
|
'assertions' => [
|
2017-12-19 00:47:17 +01:00
|
|
|
'$id1' => 'int',
|
|
|
|
'$name1' => 'string',
|
|
|
|
'$id2' => 'int',
|
|
|
|
'$name2' => 'string',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayDestructuringInForeach' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$data = [
|
|
|
|
[1, "Tom"],
|
|
|
|
[2, "Fred"],
|
|
|
|
];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
// [] style
|
|
|
|
foreach ($data as [$id, $name]) {
|
|
|
|
echo $id;
|
|
|
|
echo $name;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayDestructuringWithKeys' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$data = [
|
|
|
|
["id" => 1, "name" => "Tom"],
|
|
|
|
["id" => 2, "name" => "Fred"],
|
|
|
|
];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
// list() style
|
|
|
|
list("id" => $id1, "name" => $name1) = $data[0];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
// [] style
|
|
|
|
["id" => $id2, "name" => $name2] = $data[1];',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$id1' => 'int',
|
|
|
|
'$name1' => 'string',
|
|
|
|
'$id2' => 'int',
|
|
|
|
'$name2' => 'string',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayListDestructuringInForeachWithKeys' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$data = [
|
|
|
|
["id" => 1, "name" => "Tom"],
|
|
|
|
["id" => 2, "name" => "Fred"],
|
|
|
|
];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
// list() style
|
|
|
|
foreach ($data as list("id" => $id, "name" => $name)) {
|
|
|
|
$last_id = $id;
|
|
|
|
$last_name = $name;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2018-11-10 22:10:59 +01:00
|
|
|
'$last_id' => 'int',
|
|
|
|
'$last_name' => 'string',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayDestructuringInForeachWithKeys' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$data = [
|
|
|
|
["id" => 1, "name" => "Tom"],
|
|
|
|
["id" => 2, "name" => "Fred"],
|
|
|
|
];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
// [] style
|
|
|
|
foreach ($data as ["id" => $id, "name" => $name]) {
|
|
|
|
$last_id = $id;
|
|
|
|
$last_name = $name;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2018-11-10 22:10:59 +01:00
|
|
|
'$last_id' => 'int',
|
|
|
|
'$last_name' => 'string',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'iterableArg' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @param iterable<int, int> $iter
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function iterator(iterable $iter): void
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
foreach ($iter as $val) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
iterator([1, 2, 3, 4]);
|
2020-04-19 15:28:53 +02:00
|
|
|
/** @psalm-suppress MixedArgumentTypeCoercion */
|
2017-05-27 02:05:57 +02:00
|
|
|
iterator(new SplFixedArray(5));',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'traversableObject' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2022-01-26 18:46:02 +01:00
|
|
|
/**
|
|
|
|
* @implements Iterator<0, mixed>
|
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
class IteratorObj implements Iterator {
|
2018-01-11 21:50:45 +01:00
|
|
|
function rewind(): void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return mixed */
|
|
|
|
function current() { return null; }
|
2018-01-11 21:50:45 +01:00
|
|
|
function key(): int { return 0; }
|
|
|
|
function next(): void {}
|
|
|
|
function valid(): bool { return false; }
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(\Traversable $t): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
foo(new IteratorObj);',
|
|
|
|
],
|
2018-03-14 22:50:48 +01:00
|
|
|
'iterableIsArrayOrTraversable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-03-14 22:50:48 +01:00
|
|
|
function castToArray(iterable $arr): array {
|
2019-01-05 06:15:53 +01:00
|
|
|
if ($arr instanceof \Traversable) {
|
|
|
|
return iterator_to_array($arr, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $arr;
|
2018-03-14 22:50:48 +01:00
|
|
|
}
|
2019-01-05 06:15:53 +01:00
|
|
|
|
2018-03-14 22:50:48 +01:00
|
|
|
function castToArray2(iterable $arr): array {
|
2019-01-05 06:15:53 +01:00
|
|
|
if (is_array($arr)) {
|
|
|
|
return $arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return iterator_to_array($arr, false);
|
2018-03-14 22:50:48 +01:00
|
|
|
}',
|
|
|
|
],
|
2018-03-15 19:25:04 +01:00
|
|
|
'substituteIterable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-03-15 19:25:04 +01:00
|
|
|
function foo(iterable $i): array {
|
|
|
|
if (!is_array($i)) {
|
|
|
|
$i = iterator_to_array($i, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $i;
|
|
|
|
}',
|
|
|
|
],
|
2021-03-16 18:45:34 +01:00
|
|
|
'iterator_to_arrayMixedKey' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-03-16 18:45:34 +01:00
|
|
|
/**
|
|
|
|
* @template TKey
|
|
|
|
* @template TValue
|
|
|
|
* @param Traversable<TKey, TValue> $traversable
|
|
|
|
* @return array<TValue>
|
|
|
|
*/
|
|
|
|
function toArray(Traversable $traversable): array
|
|
|
|
{
|
|
|
|
return iterator_to_array($traversable);
|
|
|
|
}',
|
|
|
|
],
|
2019-02-27 20:52:51 +01:00
|
|
|
'noReservedWordInDocblock' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-27 20:52:51 +01:00
|
|
|
/**
|
|
|
|
* @param Closure():(resource|false) $op
|
|
|
|
* @return resource|false
|
|
|
|
*/
|
|
|
|
function create_resource($op) {
|
|
|
|
return $op();
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-02-27 20:52:51 +01:00
|
|
|
],
|
2019-10-22 17:17:42 +02:00
|
|
|
'arrayDestructuringOnArrayObject' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-10-22 17:17:42 +02:00
|
|
|
$var = new ArrayObject([0 => "first", "dos" => "second"]);
|
|
|
|
[0 => $first, "dos" => $second] = $var;
|
|
|
|
echo $first;
|
|
|
|
echo $second;',
|
|
|
|
],
|
2016-12-04 20:14:00 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2022-11-05 22:34:42 +01:00
|
|
|
*
|
2017-01-13 20:07:23 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2016-12-04 20:14:00 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'invalidPrivateClassConstFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A
|
|
|
|
{
|
|
|
|
private const IS_PRIVATE = 1;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo A::IS_PRIVATE;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InaccessibleClassConstant',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'invalidPrivateClassConstFetchFromSubclass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A
|
|
|
|
{
|
|
|
|
private const IS_PRIVATE = 1;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A
|
|
|
|
{
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(): int {
|
2017-04-25 05:45:02 +02:00
|
|
|
return A::IS_PRIVATE;
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InaccessibleClassConstant',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'invalidProtectedClassConstFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A
|
|
|
|
{
|
|
|
|
protected const IS_PROTECTED = 1;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo A::IS_PROTECTED;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InaccessibleClassConstant',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'invalidIterableArg' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @param iterable<string> $iter
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function iterator(iterable $iter): void
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
foreach ($iter as $val) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
iterator(new A());',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2019-02-07 18:25:57 +01:00
|
|
|
'voidDoesntWorkIn70' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-07 18:25:57 +01:00
|
|
|
function foo(): void {
|
|
|
|
|
|
|
|
}',
|
|
|
|
'error_message' => 'ReservedWord',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '7.0',
|
2019-02-07 18:25:57 +01:00
|
|
|
],
|
|
|
|
'objectDoesntWorkIn71' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-07 18:25:57 +01:00
|
|
|
function foo(): object {
|
|
|
|
return new stdClass();
|
|
|
|
}',
|
|
|
|
'error_message' => 'ReservedWord',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '7.0',
|
2019-02-07 18:25:57 +01:00
|
|
|
],
|
2019-10-15 19:55:30 +02:00
|
|
|
'arrayDestructuringInvalidList' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-10-15 19:55:30 +02:00
|
|
|
$a = 42;
|
|
|
|
|
|
|
|
list($id1, $name1) = $a;',
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
],
|
|
|
|
'arrayDestructuringInvalidArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-10-15 19:55:30 +02:00
|
|
|
$a = 42;
|
|
|
|
|
|
|
|
[$id2, $name2] = $a;',
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
],
|
2016-12-04 19:35:38 +01:00
|
|
|
];
|
2017-01-17 02:47:23 +01:00
|
|
|
}
|
2016-12-04 05:03:51 +01:00
|
|
|
}
|