1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-11 16:59:45 +01:00
psalm/tests/Php71Test.php

299 lines
9.2 KiB
PHP
Raw Normal View History

2016-12-04 05:03:51 +01:00
<?php
namespace Psalm\Tests;
class Php71Test extends TestCase
2016-12-04 05:03:51 +01:00
{
use Traits\FileCheckerInvalidCodeParseTestTrait;
use Traits\FileCheckerValidCodeParseTestTrait;
2016-12-04 19:35:38 +01:00
2017-01-13 20:07:23 +01:00
/**
* @return array
2017-01-13 20:07:23 +01:00
*/
public function providerFileCheckerValidCodeParse()
2016-12-04 19:35:38 +01:00
{
return [
'nullableReturnType' => [
'<?php
function a(): ?string
{
return rand(0, 10) ? "elePHPant" : null;
}
2017-06-29 16:22:49 +02:00
$a = a();',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$a' => 'string|null',
2017-05-27 02:05:57 +02:00
],
],
'nullableReturnTypeInDocblock' => [
'<?php
/** @return ?string */
function a() {
return rand(0, 10) ? "elePHPant" : null;
}
2017-06-29 16:22:49 +02:00
$a = a();',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$a' => 'null|string',
2017-05-27 02:05:57 +02:00
],
],
'nullableArgument' => [
'<?php
2018-01-11 21:50:45 +01:00
function test(?string $name): ?string
{
return $name;
}
2017-06-29 16:22:49 +02:00
test("elePHPant");
2017-05-27 02:05:57 +02:00
test(null);',
],
'protectedClassConst' => [
'<?php
class A
{
protected const IS_PROTECTED = 1;
}
2017-06-29 16:22:49 +02:00
class B extends A
{
2018-01-11 21:50:45 +01:00
function fooFoo(): int {
return A::IS_PROTECTED;
}
}',
],
'privateClassConst' => [
'<?php
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 {
return A::IS_PRIVATE;
}
2017-05-27 02:05:57 +02:00
}',
],
'publicClassConstFetch' => [
'<?php
class A
{
public const IS_PUBLIC = 1;
const IS_ALSO_PUBLIC = 2;
}
2017-06-29 16:22:49 +02:00
class B extends A
{
2018-01-11 21:50:45 +01:00
function fooFoo(): int {
echo A::IS_PUBLIC;
return A::IS_ALSO_PUBLIC;
}
}
2017-06-29 16:22:49 +02:00
echo A::IS_PUBLIC;
2017-05-27 02:05:57 +02:00
echo A::IS_ALSO_PUBLIC;',
],
'arrayDestructuring' => [
'<?php
$data = [
[1, "Tom"],
[2, "Fred"],
];
2017-06-29 16:22:49 +02:00
// list() style
list($id1, $name1) = $data[0];
2017-06-29 16:22:49 +02:00
// [] style
[$id2, $name2] = $data[1];',
'assertions' => [
'$id1' => 'int',
'$name1' => 'string',
'$id2' => 'int',
'$name2' => 'string',
2017-05-27 02:05:57 +02:00
],
],
'arrayDestructuringInForeach' => [
'<?php
$data = [
[1, "Tom"],
[2, "Fred"],
];
2017-06-29 16:22:49 +02:00
// [] style
foreach ($data as [$id, $name]) {
echo $id;
echo $name;
2017-05-27 02:05:57 +02:00
}',
],
'arrayDestructuringWithKeys' => [
'<?php
$data = [
["id" => 1, "name" => "Tom"],
["id" => 2, "name" => "Fred"],
];
2017-06-29 16:22:49 +02:00
// list() style
list("id" => $id1, "name" => $name1) = $data[0];
2017-06-29 16:22:49 +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
],
],
'arrayListDestructuringInForeachWithKeys' => [
'<?php
$data = [
["id" => 1, "name" => "Tom"],
["id" => 2, "name" => "Fred"],
];
2017-06-29 16:22:49 +02:00
$last_id = null;
$last_name = null;
2017-06-29 16:22:49 +02:00
// list() style
foreach ($data as list("id" => $id, "name" => $name)) {
$last_id = $id;
$last_name = $name;
}',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$last_id' => 'null|int',
'$last_name' => 'null|string',
2017-05-27 02:05:57 +02:00
],
],
'arrayDestructuringInForeachWithKeys' => [
'<?php
$data = [
["id" => 1, "name" => "Tom"],
["id" => 2, "name" => "Fred"],
];
2017-06-29 16:22:49 +02:00
$last_id = null;
$last_name = null;
2017-06-29 16:22:49 +02:00
// [] style
foreach ($data as ["id" => $id, "name" => $name]) {
$last_id = $id;
$last_name = $name;
}',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$last_id' => 'null|int',
'$last_name' => 'null|string',
2017-05-27 02:05:57 +02:00
],
],
'iterableArg' => [
'<?php
/**
* @param iterable<int, int> $iter
*/
2018-01-11 21:50:45 +01:00
function iterator(iterable $iter): void
{
foreach ($iter as $val) {
//
}
}
2017-06-29 16:22:49 +02:00
iterator([1, 2, 3, 4]);
2017-05-27 02:05:57 +02:00
iterator(new SplFixedArray(5));',
],
'traversableObject' => [
'<?php
class IteratorObj implements Iterator {
2018-01-11 21:50:45 +01:00
function rewind(): void {}
/** @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-06-29 16:22:49 +02:00
2018-01-11 21:50:45 +01:00
function foo(\Traversable $t): void {
}
2017-06-29 16:22:49 +02:00
2017-05-27 02:05:57 +02:00
foo(new IteratorObj);',
],
'iterableIsArrayOrTraversable' => [
'<?php
function castToArray(iterable $arr): array {
return $arr instanceof \Traversable ? iterator_to_array($arr, false) : $arr;
}
function castToArray2(iterable $arr): array {
return is_array($arr) ? $arr : iterator_to_array($arr, false);
}',
],
'substituteIterable' => [
'<?php
function foo(iterable $i): array {
if (!is_array($i)) {
$i = iterator_to_array($i, false);
}
return $i;
}',
],
];
}
2017-01-13 20:07:23 +01:00
/**
* @return array
2017-01-13 20:07:23 +01:00
*/
public function providerFileCheckerInvalidCodeParse()
{
return [
'invalidPrivateClassConstFetch' => [
'<?php
class A
{
private const IS_PRIVATE = 1;
}
2017-06-29 16:22:49 +02:00
echo A::IS_PRIVATE;',
2017-05-27 02:05:57 +02:00
'error_message' => 'InaccessibleClassConstant',
],
'invalidPrivateClassConstFetchFromSubclass' => [
'<?php
class A
{
private const IS_PRIVATE = 1;
}
2017-06-29 16:22:49 +02:00
class B extends A
{
2018-01-11 21:50:45 +01:00
function fooFoo(): int {
return A::IS_PRIVATE;
}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'InaccessibleClassConstant',
],
'invalidProtectedClassConstFetch' => [
'<?php
class A
{
protected const IS_PROTECTED = 1;
}
2017-06-29 16:22:49 +02:00
echo A::IS_PROTECTED;',
2017-05-27 02:05:57 +02:00
'error_message' => 'InaccessibleClassConstant',
],
'invalidIterableArg' => [
'<?php
/**
* @param iterable<string> $iter
*/
2018-01-11 21:50:45 +01:00
function iterator(iterable $iter): void
{
foreach ($iter as $val) {
//
}
}
2017-06-29 16:22:49 +02:00
class A {
}
2017-06-29 16:22:49 +02:00
iterator(new A());',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidArgument',
],
2016-12-04 19:35:38 +01:00
];
}
2016-12-04 05:03:51 +01:00
}