1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/tests/Php56Test.php

316 lines
9.7 KiB
PHP
Raw Normal View History

2016-10-19 04:23:09 +02:00
<?php
namespace Psalm\Tests;
class Php56Test extends TestCase
2016-10-19 04:23:09 +02:00
{
2018-11-06 03:57:36 +01:00
use Traits\ValidCodeAnalysisTestTrait;
use Traits\InvalidCodeAnalysisTestTrait;
2016-10-19 04:23:09 +02:00
2017-01-13 20:07:23 +01:00
/**
2019-03-01 21:55:20 +01:00
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
2017-01-13 20:07:23 +01:00
*/
2018-11-06 03:57:36 +01:00
public function providerValidCodeParse()
2016-10-19 04:23:09 +02:00
{
return [
'constArray' => [
'<?php
const ARR = ["a", "b"];
$a = ARR[0];',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$a' => 'string',
2017-05-27 02:05:57 +02:00
],
],
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
'classConstFeatures' => [
'<?php
class C {
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
const ONE = 1;
const TWO = self::ONE * 2;
const THREE = self::TWO + 1;
const ONE_THIRD = self::ONE / self::THREE;
const SENTENCE = "The value of THREE is " . self::THREE;
2017-06-29 16:22:49 +02:00
2018-01-21 18:44:46 +01:00
/** @var int */
public $four = self::ONE + self::THREE;
/**
* @param int $a
* @return int
*/
public function f($a = self::ONE + self::THREE) {
return $a;
}
}
2017-06-29 16:22:49 +02:00
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
$c1 = C::ONE;
$c2 = C::TWO;
$c3 = C::THREE;
$c1_3rd = C::ONE_THIRD;
$c_sentence = C::SENTENCE;
2018-01-21 18:44:46 +01:00
$cf = (new C)->f();
2018-09-17 18:15:45 +02:00
$c4 = (new C)->four;',
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
'assertions' => [
'$c1' => 'int',
'$c2' => 'int',
'$c3' => 'int',
'$c1_3rd' => 'float|int',
'$c_sentence' => 'string',
'$cf' => 'int',
2018-01-21 18:44:46 +01:00
'$c4' => 'int',
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
],
],
'constFeatures' => [
'<?php
const ONE = 1;
const TWO = ONE * 2;
$one = ONE;
$two = TWO;',
'assertions' => [
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
'$one' => 'int',
'$two' => 'int',
2017-05-27 02:05:57 +02:00
],
],
'argumentUnpacking' => [
'<?php
/**
* @return int
* @param int $a
* @param int $b
* @param int $c
*/
function add($a, $b, $c) {
return $a + $b + $c;
}
2017-06-29 16:22:49 +02:00
$operators = [2, 3];
2017-05-27 02:05:57 +02:00
echo add(1, ...$operators);',
],
'arrayPushArgumentUnpackingWithGoodArg' => [
'<?php
$a = [];
$b = ["foo", "bar"];
$a[] = "foo";
array_push($a, ...$b);',
'assertions' => [
'$a' => 'non-empty-array<int, string>',
],
],
'arrayMergeArgumentUnpacking' => [
'<?php
$a = [[1, 2]];
$b = array_merge([], ...$a);',
'assertions' => [
'$b' => 'array{0:int, 1:int}',
],
],
'preserveTypesWhenUnpacking' => [
'<?php
/**
* @return array<int,array<int,string>>
*/
function getData(): array
{
return [
["a", "b"],
["c", "d"]
];
}
/**
* @return array<int,string>
*/
function f1(): array
{
$data = getData();
return array_merge($data[0], $data[1]);
}
/**
* @return array<int,string>
*/
function f2(): array
{
$data = getData();
return array_merge(...$data);
}
/**
* @return array<int,string>
*/
function f3(): array
{
$data = getData();
return array_merge([], ...$data);
}',
],
'unpackArg' => [
'<?php
function Foo(string $a, string ...$b) : void {}
/** @return array<int, string> */
function Baz(string ...$c) {
Foo(...$c);
return $c;
}',
],
'unpackByRefArg' => [
'<?php
function example (int &...$x): void {}
$y = 0;
example($y);
$z = [0];
example(...$z);',
'assertions' => [
'$y' => 'int',
'$z' => 'array<int, int>',
],
],
'exponentiation' => [
'<?php
$a = 2;
2017-05-27 02:05:57 +02:00
$a **= 3;',
],
'constantAliasInNamespace' => [
'<?php
namespace Name\Space {
const FOO = 42;
}
2017-06-29 16:22:49 +02:00
namespace Noom\Spice {
use const Name\Space\FOO;
2017-06-29 16:22:49 +02:00
echo FOO . "\n";
echo \Name\Space\FOO;
2017-05-27 02:05:57 +02:00
}',
],
'constantAliasInClass' => [
'<?php
namespace Name\Space {
const FOO = 42;
}
2017-06-29 16:22:49 +02:00
namespace Noom\Spice {
use const Name\Space\FOO;
2017-06-29 16:22:49 +02:00
class A {
/** @return void */
public function fooFoo() {
echo FOO . "\n";
echo \Name\Space\FOO;
}
}
2017-05-27 02:05:57 +02:00
}',
],
'functionAliasInNamespace' => [
'<?php
namespace Name\Space {
/**
* @return void
*/
function f() { echo __FUNCTION__."\n"; }
}
2017-06-29 16:22:49 +02:00
namespace Noom\Spice {
use function Name\Space\f;
2017-06-29 16:22:49 +02:00
f();
\Name\Space\f();
2017-05-27 02:05:57 +02:00
}',
],
'functionAliasInClass' => [
'<?php
namespace Name\Space {
/**
* @return void
*/
function f() { echo __FUNCTION__."\n"; }
}
2017-06-29 16:22:49 +02:00
namespace Noom\Spice {
use function Name\Space\f;
2017-06-29 16:22:49 +02:00
class A {
/** @return void */
public function fooFoo() {
f();
\Name\Space\f();
}
}
2017-05-27 02:05:57 +02:00
}',
],
2018-06-28 23:05:50 +02:00
'argumentUnpackingWithoutChangingRef' => [
'<?php
function foo(int ...$is) : void {}
$arr = [1, 2, 3, 4];
foo(...$arr);',
'assertions' => [
'$arr' => 'array<int, int>'
],
2018-06-28 23:05:50 +02:00
],
'iterableSplat' => [
'<?php
function foo(iterable $args): int {
return intval(...$args);
}
2019-01-06 22:40:44 +01:00
function bar(ArrayIterator $args): int {
return intval(...$args);
}',
],
2019-01-31 23:48:48 +01:00
'yieldReturn' => [
'<?php
function foo() : Traversable {
if (rand(0, 1)) {
yield "hello";
return;
}
yield "goodbye";
}',
],
];
2016-10-19 04:23:09 +02:00
}
/**
2019-03-01 21:55:20 +01:00
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
*/
2018-11-06 03:57:36 +01:00
public function providerInvalidCodeParse()
{
return [
'arrayPushArgumentUnpackingWithBadArg' => [
'<?php
$a = [];
$b = "hello";
$a[] = "foo";
array_push($a, ...$b);',
'error_message' => 'InvalidArgument',
],
'shouldWarnAboutNoGeneratorReturn' => [
'<?php
function generator2() : Generator {
if (rand(0,1)) {
return;
}
yield 2;
}
/**
* @psalm-suppress InvalidNullableReturnType
*/
function notagenerator() : Generator {
if (rand(0, 1)) {
return;
}
return generator2();
}',
'error_message' => 'InvalidReturnStatement',
],
];
}
2016-10-19 04:23:09 +02:00
}