mirror of
https://github.com/danog/psalm.git
synced 2024-12-11 16:59:45 +01:00
Daniil Gentili
1986c8b4a8
* Immutable CodeLocation * Remove excess clones * Remove external clones * Remove leftover clones * Fix final clone issue * Immutable storages * Refactoring * Fixes * Fixes * Fix * Fix * Fixes * Simplify * Fixes * Fix * Fixes * Update * Fix * Cache global types * Fix * Update * Update * Fixes * Fixes * Refactor * Fixes * Fix * Fix * More caching * Fix * Fix * Update * Update * Fix * Fixes * Update * Refactor * Update * Fixes * Break one more test * Fix * FIx * Fix * Fix * Fix * Fix * Improve performance and readability * Equivalent logic * Fixes * Revert * Revert "Revert" This reverts commit f9175100c8452c80559234200663fd4c4f4dd889. * Fix * Fix reference bug * Make default TypeVisitor immutable * Bugfix * Remove clones * Partial refactoring * Refactoring * Fixes * Fix * Fixes * Fixes * cs-fix * Fix final bugs * Add test * Misc fixes * Update * Fixes * Experiment with removing different property * revert "Experiment with removing different property" This reverts commit ac1156e077fc4ea633530d51096d27b6e88bfdf9. * Uniform naming * Uniform naming * Hack hotfix * Clean up $_FILES ref #8621 * Undo hack, try fixing properly * Helper method * Remove redundant call * Partially fix bugs * Cleanup * Change defaults * Fix bug * Fix (?, hope this doesn't break anything else) * cs-fix * Review fixes * Bugfix * Bugfix * Improve logic * Add support for list{} and callable-list{} types, properly implement array_is_list assertions (fixes #8389) * Default to sealed arrays * Fix array_merge bug * Fixes * Fix * Sealed type checks * Properly infer properties-of and get_object_vars on final classes * Fix array_map zipping * Fix tests * Fixes * Fixes * Fix more stuff * Recursively resolve type aliases * Fix typo * Fixes * Fix array_is_list assertion on keyed array * Add BC docs * Fixes * fix * Update * Update * Update * Update * Seal arrays with count assertions * Fix #8528 * Fix * Update * Improve sealed array foreach logic * get_object_vars on template properties * Fix sealed array assertion reconciler logic * Improved reconciler * Add tests * Single source of truth for test types * Fix tests * Fixup tests * Fixup tests * Fixup tests * Update * Fix tests * Fix tests * Final fixes * Fixes * Use list syntax only when needed * Fix tests * Cs-fix * Update docs * Update docs * Update docs * Update docs * Update docs * Document missing types * Update docs * Improve class-string-map docs * Update * Update * I love working on psalm :) * Keep arrays unsealed by default * Fixup tests * Fix syntax mistake * cs-fix * Fix typo * Re-import missing types * Keep strict types only in return types * argc/argv fixes * argc/argv fixes * Fix test * Comment-out valinor code, pinging @romm pls merge https://github.com/CuyZ/Valinor/pull/246 so we can add valinor to the psalm docs :)
372 lines
12 KiB
PHP
372 lines
12 KiB
PHP
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
class Php71Test extends TestCase
|
|
{
|
|
use InvalidCodeAnalysisTestTrait;
|
|
use ValidCodeAnalysisTestTrait;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public function providerValidCodeParse(): iterable
|
|
{
|
|
return [
|
|
'nullableReturnType' => [
|
|
'code' => '<?php
|
|
function a(): ?string
|
|
{
|
|
return rand(0, 10) ? "elePHPant" : null;
|
|
}
|
|
|
|
$a = a();',
|
|
'assertions' => [
|
|
'$a' => 'null|string',
|
|
],
|
|
],
|
|
'nullableReturnTypeInDocblock' => [
|
|
'code' => '<?php
|
|
/** @return ?string */
|
|
function a() {
|
|
return rand(0, 10) ? "elePHPant" : null;
|
|
}
|
|
|
|
$a = a();',
|
|
'assertions' => [
|
|
'$a' => 'null|string',
|
|
],
|
|
],
|
|
'nullableArgument' => [
|
|
'code' => '<?php
|
|
function test(?string $name): ?string
|
|
{
|
|
return $name;
|
|
}
|
|
|
|
test("elePHPant");
|
|
test(null);',
|
|
],
|
|
'protectedClassConst' => [
|
|
'code' => '<?php
|
|
class A
|
|
{
|
|
protected const IS_PROTECTED = 1;
|
|
}
|
|
|
|
class B extends A
|
|
{
|
|
function fooFoo(): int {
|
|
return A::IS_PROTECTED;
|
|
}
|
|
}',
|
|
],
|
|
'privateClassConst' => [
|
|
'code' => '<?php
|
|
class A
|
|
{
|
|
private const IS_PRIVATE = 1;
|
|
|
|
function fooFoo(): int {
|
|
return A::IS_PRIVATE;
|
|
}
|
|
}',
|
|
],
|
|
'publicClassConstFetch' => [
|
|
'code' => '<?php
|
|
class A
|
|
{
|
|
public const IS_PUBLIC = 1;
|
|
const IS_ALSO_PUBLIC = 2;
|
|
}
|
|
|
|
class B extends A
|
|
{
|
|
function fooFoo(): int {
|
|
echo A::IS_PUBLIC;
|
|
return A::IS_ALSO_PUBLIC;
|
|
}
|
|
}
|
|
|
|
echo A::IS_PUBLIC;
|
|
echo A::IS_ALSO_PUBLIC;',
|
|
],
|
|
'arrayDestructuringList' => [
|
|
'code' => '<?php
|
|
$data = [
|
|
[1, "Tom"],
|
|
[2, "Fred"],
|
|
];
|
|
|
|
// list() style
|
|
list($id1, $name1) = $data[0];
|
|
|
|
// [] style
|
|
[$id2, $name2] = $data[1];',
|
|
'assertions' => [
|
|
'$id1' => 'int',
|
|
'$name1' => 'string',
|
|
'$id2' => 'int',
|
|
'$name2' => 'string',
|
|
],
|
|
],
|
|
'arrayDestructuringInForeach' => [
|
|
'code' => '<?php
|
|
$data = [
|
|
[1, "Tom"],
|
|
[2, "Fred"],
|
|
];
|
|
|
|
// [] style
|
|
foreach ($data as [$id, $name]) {
|
|
echo $id;
|
|
echo $name;
|
|
}',
|
|
],
|
|
'arrayDestructuringWithKeys' => [
|
|
'code' => '<?php
|
|
$data = [
|
|
["id" => 1, "name" => "Tom"],
|
|
["id" => 2, "name" => "Fred"],
|
|
];
|
|
|
|
// list() style
|
|
list("id" => $id1, "name" => $name1) = $data[0];
|
|
|
|
// [] style
|
|
["id" => $id2, "name" => $name2] = $data[1];',
|
|
'assertions' => [
|
|
'$id1' => 'int',
|
|
'$name1' => 'string',
|
|
'$id2' => 'int',
|
|
'$name2' => 'string',
|
|
],
|
|
],
|
|
'arrayListDestructuringInForeachWithKeys' => [
|
|
'code' => '<?php
|
|
$data = [
|
|
["id" => 1, "name" => "Tom"],
|
|
["id" => 2, "name" => "Fred"],
|
|
];
|
|
|
|
// list() style
|
|
foreach ($data as list("id" => $id, "name" => $name)) {
|
|
$last_id = $id;
|
|
$last_name = $name;
|
|
}',
|
|
'assertions' => [
|
|
'$last_id' => 'int',
|
|
'$last_name' => 'string',
|
|
],
|
|
],
|
|
'arrayDestructuringInForeachWithKeys' => [
|
|
'code' => '<?php
|
|
$data = [
|
|
["id" => 1, "name" => "Tom"],
|
|
["id" => 2, "name" => "Fred"],
|
|
];
|
|
|
|
// [] style
|
|
foreach ($data as ["id" => $id, "name" => $name]) {
|
|
$last_id = $id;
|
|
$last_name = $name;
|
|
}',
|
|
'assertions' => [
|
|
'$last_id' => 'int',
|
|
'$last_name' => 'string',
|
|
],
|
|
],
|
|
'iterableArg' => [
|
|
'code' => '<?php
|
|
/**
|
|
* @param iterable<int, int> $iter
|
|
*/
|
|
function iterator(iterable $iter): void
|
|
{
|
|
foreach ($iter as $val) {
|
|
//
|
|
}
|
|
}
|
|
|
|
iterator([1, 2, 3, 4]);
|
|
/** @psalm-suppress MixedArgumentTypeCoercion */
|
|
iterator(new SplFixedArray(5));',
|
|
],
|
|
'traversableObject' => [
|
|
'code' => '<?php
|
|
/**
|
|
* @implements Iterator<0, mixed>
|
|
*/
|
|
class IteratorObj implements Iterator {
|
|
function rewind(): void {}
|
|
/** @return mixed */
|
|
function current() { return null; }
|
|
function key(): int { return 0; }
|
|
function next(): void {}
|
|
function valid(): bool { return false; }
|
|
}
|
|
|
|
function foo(\Traversable $t): void {
|
|
}
|
|
|
|
foo(new IteratorObj);',
|
|
],
|
|
'iterableIsArrayOrTraversable' => [
|
|
'code' => '<?php
|
|
function castToArray(iterable $arr): array {
|
|
if ($arr instanceof \Traversable) {
|
|
return iterator_to_array($arr, false);
|
|
}
|
|
|
|
return $arr;
|
|
}
|
|
|
|
function castToArray2(iterable $arr): array {
|
|
if (is_array($arr)) {
|
|
return $arr;
|
|
}
|
|
|
|
return iterator_to_array($arr, false);
|
|
}',
|
|
],
|
|
'substituteIterable' => [
|
|
'code' => '<?php
|
|
function foo(iterable $i): array {
|
|
if (!is_array($i)) {
|
|
$i = iterator_to_array($i, false);
|
|
}
|
|
|
|
return $i;
|
|
}',
|
|
],
|
|
'iterator_to_arrayMixedKey' => [
|
|
'code' => '<?php
|
|
/**
|
|
* @template TKey
|
|
* @template TValue
|
|
* @param Traversable<TKey, TValue> $traversable
|
|
* @return array<TValue>
|
|
*/
|
|
function toArray(Traversable $traversable): array
|
|
{
|
|
return iterator_to_array($traversable);
|
|
}',
|
|
],
|
|
'noReservedWordInDocblock' => [
|
|
'code' => '<?php
|
|
/**
|
|
* @param Closure():(resource|false) $op
|
|
* @return resource|false
|
|
*/
|
|
function create_resource($op) {
|
|
return $op();
|
|
}',
|
|
],
|
|
'arrayDestructuringOnArrayObject' => [
|
|
'code' => '<?php
|
|
$var = new ArrayObject([0 => "first", "dos" => "second"]);
|
|
[0 => $first, "dos" => $second] = $var;
|
|
echo $first;
|
|
echo $second;',
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public function providerInvalidCodeParse(): iterable
|
|
{
|
|
return [
|
|
'invalidPrivateClassConstFetch' => [
|
|
'code' => '<?php
|
|
class A
|
|
{
|
|
private const IS_PRIVATE = 1;
|
|
}
|
|
|
|
echo A::IS_PRIVATE;',
|
|
'error_message' => 'InaccessibleClassConstant',
|
|
],
|
|
'invalidPrivateClassConstFetchFromSubclass' => [
|
|
'code' => '<?php
|
|
class A
|
|
{
|
|
private const IS_PRIVATE = 1;
|
|
}
|
|
|
|
class B extends A
|
|
{
|
|
function fooFoo(): int {
|
|
return A::IS_PRIVATE;
|
|
}
|
|
}',
|
|
'error_message' => 'InaccessibleClassConstant',
|
|
],
|
|
'invalidProtectedClassConstFetch' => [
|
|
'code' => '<?php
|
|
class A
|
|
{
|
|
protected const IS_PROTECTED = 1;
|
|
}
|
|
|
|
echo A::IS_PROTECTED;',
|
|
'error_message' => 'InaccessibleClassConstant',
|
|
],
|
|
'invalidIterableArg' => [
|
|
'code' => '<?php
|
|
/**
|
|
* @param iterable<string> $iter
|
|
*/
|
|
function iterator(iterable $iter): void
|
|
{
|
|
foreach ($iter as $val) {
|
|
//
|
|
}
|
|
}
|
|
|
|
class A {
|
|
}
|
|
|
|
iterator(new A());',
|
|
'error_message' => 'InvalidArgument',
|
|
],
|
|
'voidDoesntWorkIn70' => [
|
|
'code' => '<?php
|
|
function foo(): void {
|
|
|
|
}',
|
|
'error_message' => 'ReservedWord',
|
|
'ignored_issues' => [],
|
|
'php_version' => '7.0',
|
|
],
|
|
'objectDoesntWorkIn71' => [
|
|
'code' => '<?php
|
|
function foo(): object {
|
|
return new stdClass();
|
|
}',
|
|
'error_message' => 'ReservedWord',
|
|
'ignored_issues' => [],
|
|
'php_version' => '7.0',
|
|
],
|
|
'arrayDestructuringInvalidList' => [
|
|
'code' => '<?php
|
|
$a = 42;
|
|
|
|
list($id1, $name1) = $a;',
|
|
'error_message' => 'InvalidArrayOffset',
|
|
],
|
|
'arrayDestructuringInvalidArray' => [
|
|
'code' => '<?php
|
|
$a = 42;
|
|
|
|
[$id2, $name2] = $a;',
|
|
'error_message' => 'InvalidArrayOffset',
|
|
],
|
|
];
|
|
}
|
|
}
|