1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00
psalm/tests/PureCallableTest.php

292 lines
9.3 KiB
PHP
Raw Normal View History

2020-08-26 22:51:22 +02:00
<?php
2020-08-26 22:51:22 +02:00
namespace Psalm\Tests;
2021-12-04 21:55:53 +01:00
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
2020-08-26 22:51:22 +02:00
class PureCallableTest extends TestCase
{
2021-12-04 21:55:53 +01:00
use ValidCodeAnalysisTestTrait;
use InvalidCodeAnalysisTestTrait;
2020-08-26 22:51:22 +02:00
/**
Add support for strict arrays, fix type alias intersection, fix array_is_list assertion on non-lists (#8395) * 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 :)
2022-11-05 22:34:42 +01:00
*
2020-08-26 22:51:22 +02:00
*/
public function providerValidCodeParse(): iterable
2020-08-26 22:51:22 +02:00
{
return [
'varCallableParamReturnType' => [
'code' => '<?php
2020-08-26 22:51:22 +02:00
$add_one =
/**
* @psalm-pure
*/
function(int $a): int {
return $a + 1;
};
/**
* @param pure-callable(int): int $c
*/
function bar(callable $c) : int {
return $c(1);
}
bar($add_one);',
],
'callableToClosure' => [
'code' => '<?php
2020-08-26 22:51:22 +02:00
/**
* @return pure-callable
*/
function foo() {
return
/**
* @psalm-pure
*/
function(string $a): string {
return $a . "blah";
};
}',
],
'callableToClosureArrow' => [
'code' => '<?php
2020-08-26 22:51:22 +02:00
/**
* @return pure-callable
*/
function foo() {
return
/**
* @psalm-pure
*/
fn(string $a): string => $a . "blah";
}',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '7.4',
2020-08-26 22:51:22 +02:00
],
'callableWithNonInvokable' => [
'code' => '<?php
2020-08-26 22:51:22 +02:00
/**
* @psalm-pure
*/
function asd(): void {}
class B {}
/**
* @param pure-callable|B $p
*/
function passes($p): void {}
passes("asd");',
],
'callableWithInvokableUnion' => [
'code' => '<?php
2020-08-26 22:51:22 +02:00
/**
* @psalm-pure
*/
function asd(): void {}
class A {public function __invoke(): void {} }
/**
* @param pure-callable|A $p
*/
function fails($p): void {}
fails("asd");',
],
'callableWithInvokable' => [
'code' => '<?php
2020-08-26 22:51:22 +02:00
/**
* @psalm-pure
*/
function asd(): void {}
class A {
/**
* @psalm-pure
*/
public function __invoke(): void {}
}
/**
* @param pure-callable $p
*/
function fails($p): void {}
fails(new A());
fails("asd");',
],
'allowVoidCallable' => [
'code' => '<?php
2020-08-26 22:51:22 +02:00
/**
* @param pure-callable():void $p
*/
function doSomething($p): void {}
doSomething(
/**
* @psalm-pure
*/
function(): bool { return false; }
);',
],
'callableProperties' => [
'code' => '<?php
2020-08-26 22:51:22 +02:00
class C {
/** @psalm-var pure-callable():bool */
private $callable;
/**
* @psalm-param pure-callable():bool $callable
*/
public function __construct(callable $callable) {
$this->callable = $callable;
}
public function callTheCallableDirectly(): bool {
return ($this->callable)();
}
public function callTheCallableIndirectly(): bool {
$r = $this->callable;
return $r();
}
}',
],
'nullableReturnTypeShorthand' => [
'code' => '<?php
2020-08-26 22:51:22 +02:00
class A {}
/** @param pure-callable(mixed):?A $a */
function foo(callable $a): void {}',
],
'callablesCanBeObjects' => [
'code' => '<?php
2020-08-26 22:51:22 +02:00
/**
* @param pure-callable $c
*/
function foo(callable $c) : void {
if (is_object($c)) {
$c();
}
}',
],
'goodCallableArgs' => [
'code' => '<?php
2020-08-26 22:51:22 +02:00
/**
* @param pure-callable(string,string):int $_p
*/
function f(callable $_p): void {}
class C {
/**
* @psalm-pure
*/
public static function m(string $a, string $b): int { return $a <=> $b; }
}
f("strcmp");
f([new C, "m"]);
f([C::class, "m"]);',
],
'callableWithSpaces' => [
'code' => '<?php
2020-08-26 22:51:22 +02:00
/**
* @param pure-callable(string, string) : int $p
*/
function f(callable $p): void {}',
],
'varCallableInNamespace' => [
'code' => '<?php
namespace Foo;
/**
* @param pure-callable $c
*/
function bar(callable $c) : callable {
return $c;
}',
],
'pureCallableArgument' => [
'code' => '<?php
/**
* @psalm-param array<int, int> $values
* @psalm-param pure-callable(int):int $num_func
*
* @psalm-pure
*/
function max_by(array $values, callable $num_func) : ?int {
$max = null;
$max_num = null;
foreach ($values as $value) {
$value_num = $num_func($value);
if (null === $max_num || $value_num >= $max_num) {
$max = $value;
$max_num = $value_num;
}
}
return $max;
}
$c = max_by([1, 2, 3], function(int $a): int {
return $a + 5;
});
echo $c;',
],
2020-08-26 22:51:22 +02:00
];
}
/**
Add support for strict arrays, fix type alias intersection, fix array_is_list assertion on non-lists (#8395) * 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 :)
2022-11-05 22:34:42 +01:00
*
2020-08-26 22:51:22 +02:00
*/
public function providerInvalidCodeParse(): iterable
2020-08-26 22:51:22 +02:00
{
return [
'impureCallableArgument' => [
'code' => '<?php
2020-08-26 22:51:22 +02:00
/**
* @psalm-param array<int, int> $values
* @psalm-param pure-callable(int):int $num_func
2020-08-26 22:51:22 +02:00
*
* @psalm-pure
*/
function max_by(array $values, callable $num_func) : ?int {
2020-08-26 22:51:22 +02:00
$max = null;
$max_num = null;
foreach ($values as $value) {
$value_num = $num_func($value);
if (null === $max_num || $value_num >= $max_num) {
$max = $value;
$max_num = $value_num;
}
}
return $max;
}
$c = max_by([1, 2, 3], function(int $a): int {
2020-08-26 22:51:22 +02:00
return $a + mt_rand(0, $a);
});
echo $c;',
'error_message' => 'InvalidArgument',
],
'impureCallableReturn' => [
'code' => '<?php
/**
* @psalm-pure
* @return pure-callable():int
*/
function foo(): callable {
return function() {
echo "bar";
return 1;
};
}',
'error_message' => 'InvalidReturnStatement',
],
2020-08-26 22:51:22 +02:00
];
}
}