1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-11 08:49:52 +01:00
psalm/tests/CoreStubsTest.php

477 lines
22 KiB
PHP
Raw Normal View History

2021-09-09 16:04:12 +02:00
<?php
namespace Psalm\Tests;
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
2021-12-04 21:55:53 +01:00
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
2021-09-09 16:04:12 +02:00
class CoreStubsTest extends TestCase
{
2021-12-04 21:55:53 +01:00
use ValidCodeAnalysisTestTrait;
use InvalidCodeAnalysisTestTrait;
2021-09-09 16:04:12 +02:00
public function providerValidCodeParse(): iterable
{
yield 'RecursiveArrayIterator::CHILD_ARRAYS_ONLY (#6464)' => [
'code' => '<?php
2021-09-09 16:04:12 +02:00
2022-12-18 17:15:15 +01:00
new RecursiveArrayIterator([], RecursiveArrayIterator::CHILD_ARRAYS_ONLY);',
2021-09-09 16:04:12 +02:00
];
yield 'proc_open() named arguments' => [
2022-01-22 18:05:32 +01:00
'code' => '<?php
proc_open(
command: "ls",
descriptor_spec: [],
pipes: $pipes,
cwd: null,
env_vars: null,
options: null
);',
'assertions' => [],
2022-01-22 18:05:32 +01:00
'ignored_issues' => [],
'php_version' => '8.0',
];
yield 'Iterating over \DatePeriod (#5954) PHP7 Traversable' => [
2022-10-17 10:21:26 +02:00
'code' => '<?php
$period = new DatePeriod(
new DateTimeImmutable("now"),
DateInterval::createFromDateString("1 day"),
new DateTime("+1 week")
);
$dt = null;
foreach ($period as $dt) {
echo $dt->format("Y-m-d");
}',
'assertions' => [
'$period' => 'DatePeriod<DateTimeImmutable>',
2022-12-18 17:15:15 +01:00
'$dt' => 'DateTimeInterface|null',
],
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
'ignored_issues' => [],
'php_version' => '7.3',
];
yield 'Iterating over \DatePeriod (#5954) PHP8 IteratorAggregate' => [
2022-10-17 10:21:26 +02:00
'code' => '<?php
2022-07-22 15:03:45 +02:00
$period = new DatePeriod(
new DateTimeImmutable("now"),
DateInterval::createFromDateString("1 day"),
new DateTime("+1 week")
);
$dt = null;
foreach ($period as $dt) {
echo $dt->format("Y-m-d");
}',
'assertions' => [
'$period' => 'DatePeriod<DateTimeImmutable>',
2022-12-18 17:15:15 +01:00
'$dt' => 'DateTimeImmutable|null',
2022-07-22 15:03:45 +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
'ignored_issues' => [],
'php_version' => '8.0',
2022-07-22 15:03:45 +02:00
];
yield 'Iterating over \DatePeriod (#5954), ISO string' => [
2022-10-17 10:21:26 +02:00
'code' => '<?php
2022-07-22 15:03:45 +02:00
$period = new DatePeriod("R4/2012-07-01T00:00:00Z/P7D");
$dt = null;
foreach ($period as $dt) {
echo $dt->format("Y-m-d");
}',
'assertions' => [
'$period' => 'DatePeriod<string>',
2022-12-18 17:15:15 +01:00
'$dt' => 'DateTime|null',
2022-07-22 15:03:45 +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
'ignored_issues' => [],
'php_version' => '8.0',
];
yield 'DatePeriod implements only Traversable on PHP 7' => [
2022-10-17 10:21:26 +02:00
'code' => '<?php
$period = new DatePeriod("R4/2012-07-01T00:00:00Z/P7D");
if ($period instanceof IteratorAggregate) {}',
'assertions' => [],
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
'ignored_issues' => [],
'php_version' => '7.3',
];
yield 'DatePeriod implements IteratorAggregate on PHP 8' => [
2022-10-17 10:21:26 +02:00
'code' => '<?php
$period = new DatePeriod("R4/2012-07-01T00:00:00Z/P7D");
if ($period instanceof IteratorAggregate) {}',
'assertions' => [],
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
'ignored_issues' => ['RedundantCondition'],
'php_version' => '8.0',
2022-07-22 15:03:45 +02:00
];
2022-12-18 11:04:16 +01:00
yield 'sprintf yields a non-empty-string for non-empty-string value' => [
2022-12-18 11:26:43 +01:00
'code' => '<?php
/**
* @param non-empty-string $foo
* @return non-empty-string
*/
function foo(string $foo): string
{
return sprintf("%s", $foo);
}
',
];
2022-12-18 11:04:16 +01:00
yield 'sprintf yields a string for possible empty string param' => [
2022-12-18 11:26:43 +01:00
'code' => '<?php
2022-12-18 11:04:16 +01:00
$a = sprintf("%s", "");
',
'assertions' => [
'$a===' => 'string',
2022-12-18 11:26:43 +01:00
],
2022-12-18 11:04:16 +01:00
];
2023-09-26 08:51:21 +02:00
yield 'sprintf accepts Stringable values' => [
'code' => '<?php
$a = sprintf(
"%s",
new class implements Stringable { public function __toString(): string { return "hello"; } },
);
',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.0',
];
yield 'json_encode returns a non-empty-string provided JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE' => [
'code' => '<?php
2023-03-10 06:44:53 +01:00
$a = json_encode([], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
',
2023-03-10 06:44:53 +01:00
'assertions' => [
'$a===' => 'non-empty-string',
],
];
2023-03-16 07:08:07 +01:00
yield 'json_encode returns a non-empty-string with JSON_THROW_ON_ERROR' => [
'code' => '<?php
$a = json_encode([], JSON_THROW_ON_ERROR | JSON_HEX_TAG);
$b = json_encode([], JSON_THROW_ON_ERROR | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
$c = json_encode([], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$d = json_encode([], JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION);
2023-03-16 07:42:36 +01:00
$e = json_encode([], JSON_PRESERVE_ZERO_FRACTION);
$f = json_encode([], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
2023-03-16 07:08:07 +01:00
',
'assertions' => [
'$a===' => 'non-empty-string',
'$b===' => 'non-empty-string',
'$c===' => 'non-empty-string',
'$d===' => 'non-empty-string',
2023-03-16 07:42:36 +01:00
'$e===' => 'false|non-empty-string',
'$f===' => 'false|non-empty-string',
2023-03-16 07:08:07 +01:00
],
];
2023-03-17 11:39:04 +01:00
yield 'str_starts_with/str_ends_with/str_contains redundant condition detection' => [
'code' => '<?php
$a1 = str_starts_with(uniqid(), "");
/** @psalm-suppress InvalidLiteralArgument */
$b1 = str_starts_with("", "random string");
$c1 = str_starts_with(uniqid(), "random string");
$a2 = str_ends_with(uniqid(), "");
/** @psalm-suppress InvalidLiteralArgument */
$b2 = str_ends_with("", "random string");
$c2 = str_ends_with(uniqid(), "random string");
$a3 = str_contains(uniqid(), "");
/** @psalm-suppress InvalidLiteralArgument */
$b3 = str_contains("", "random string");
$c3 = str_contains(uniqid(), "random string");
',
'assertions' => [
'$a1===' => 'true',
'$b1===' => 'false',
'$c1===' => 'bool',
'$a2===' => 'true',
'$b2===' => 'false',
'$c2===' => 'bool',
'$a3===' => 'true',
'$b3===' => 'false',
'$c3===' => 'bool',
],
];
yield 'PHP80-str_* function assert non-empty-string' => [
'code' => '<?php
/** @return non-empty-string */
function after_str_contains(): string
{
$string = file_get_contents("");
if (str_contains($string, "foo")) {
return $string;
}
throw new RuntimeException();
}
/** @return non-empty-string */
function after_str_starts_with(): string
{
$string = file_get_contents("");
if (str_starts_with($string, "foo")) {
return $string;
}
throw new RuntimeException();
}
/** @return non-empty-string */
function after_str_ends_with(): string
{
$string = file_get_contents("");
if (str_ends_with($string, "foo")) {
return $string;
}
throw new RuntimeException();
}
/** @return non-empty-string */
function after_strpos(): string
{
$string = uniqid();
if (strpos($string, "foo") !== false) {
return $string;
}
throw new RuntimeException();
}
/** @return non-empty-string */
function after_stripos(): string
{
$string = uniqid();
if (stripos($string, "foo") !== false) {
return $string;
}
throw new RuntimeException();
}
$a = after_str_contains();
$b = after_str_starts_with();
$c = after_str_ends_with();
$d = after_strpos();
$e = after_stripos();
',
'assertions' => [
'$a===' => 'non-empty-string',
'$b===' => 'non-empty-string',
'$c===' => 'non-empty-string',
'$d===' => 'non-empty-string',
'$e===' => 'non-empty-string',
],
];
yield "PHP80-str_* function doesn't subtract string after assertion" => [
'code' => '<?php
/** @return false|string */
function after_str_contains()
{
$string = file_get_contents("");
if (!str_contains($string, "foo")) {
return $string;
}
throw new RuntimeException();
}
/** @return false|string */
function after_str_starts_with()
{
$string = file_get_contents("");
if (!str_starts_with($string, "foo")) {
return $string;
}
throw new RuntimeException();
}
/** @return false|string */
function after_str_ends_with()
{
$string = file_get_contents("");
if (!str_ends_with($string, "foo")) {
return $string;
}
throw new RuntimeException();
}
$a = after_str_contains();
$b = after_str_starts_with();
$c = after_str_ends_with();
',
'assertions' => [
'$a===' => 'false|string',
'$b===' => 'false|string',
'$c===' => 'false|string',
],
];
yield "str_contains doesn't yield InvalidLiteralArgument for __DIR__" => [
'code' => '<?php
$d = __DIR__;
echo str_contains($d, "psalm");
',
];
2023-06-06 17:02:28 +02:00
yield 'glob return types' => [
'code' => <<<'PHP'
<?php
/** @var int-mask<GLOB_NOCHECK> */
$maybeNocheckFlag = 0;
/** @var int-mask<GLOB_ONLYDIR> */
$maybeOnlydirFlag = 0;
/** @var string */
$string = '';
$emptyPatternNoFlags = glob( '' );
$emptyPatternWithoutNocheckFlag1 = glob( '', GLOB_MARK );
$emptyPatternWithoutNocheckFlag2 = glob( '' , GLOB_NOSORT | GLOB_NOESCAPE);
$emptyPatternWithoutNocheckFlag3 = glob( '' , GLOB_MARK | GLOB_NOSORT | GLOB_NOESCAPE | GLOB_BRACE | GLOB_ONLYDIR | GLOB_ERR);
$emptyPatternWithNocheckFlag1 = glob( '' , GLOB_NOCHECK);
$emptyPatternWithNocheckFlag2 = glob( '' , GLOB_NOCHECK | GLOB_MARK);
$emptyPatternWithNocheckFlag3 = glob( '' , GLOB_NOCHECK | GLOB_MARK | GLOB_NOSORT | GLOB_NOESCAPE | GLOB_BRACE | GLOB_ERR);
$emptyPatternWithNocheckAndOnlydirFlag1 = glob( '' , GLOB_NOCHECK | GLOB_ONLYDIR);
$emptyPatternWithNocheckAndOnlydirFlag2 = glob( '' , GLOB_NOCHECK | GLOB_ONLYDIR | GLOB_MARK);
$emptyPatternWithNocheckAndOnlydirFlag3 = glob( '' , GLOB_NOCHECK | GLOB_ONLYDIR | GLOB_MARK | GLOB_NOSORT | GLOB_NOESCAPE | GLOB_BRACE | GLOB_ERR);
$emptyPatternWithNocheckFlagAndMaybeOnlydir = glob( '' , GLOB_NOCHECK | $maybeOnlydirFlag);
$emptyPatternMaybeWithNocheckFlag = glob( '' , $maybeNocheckFlag);
$emptyPatternMaybeWithNocheckFlagAndOnlydir = glob( '' , $maybeNocheckFlag | GLOB_ONLYDIR);
$emptyPatternMaybeWithNocheckFlagAndMaybeOnlydir = glob( '' , $maybeNocheckFlag | $maybeOnlydirFlag);
$nonEmptyPatternNoFlags = glob( 'pattern' );
$nonEmptyPatternWithoutNocheckFlag1 = glob( 'pattern', GLOB_MARK );
$nonEmptyPatternWithoutNocheckFlag2 = glob( 'pattern' , GLOB_NOSORT | GLOB_NOESCAPE);
$nonEmptyPatternWithoutNocheckFlag3 = glob( 'pattern' , GLOB_MARK | GLOB_NOSORT | GLOB_NOESCAPE | GLOB_BRACE | GLOB_ONLYDIR | GLOB_ERR);
$nonEmptyPatternWithNocheckFlag1 = glob( 'pattern' , GLOB_NOCHECK);
$nonEmptyPatternWithNocheckFlag2 = glob( 'pattern' , GLOB_NOCHECK | GLOB_MARK);
$nonEmptyPatternWithNocheckFlag3 = glob( 'pattern' , GLOB_NOCHECK | GLOB_MARK | GLOB_NOSORT | GLOB_NOESCAPE | GLOB_BRACE | GLOB_ERR);
$nonEmptyPatternWithNocheckAndOnlydirFlag1 = glob( 'pattern' , GLOB_NOCHECK | GLOB_ONLYDIR);
$nonEmptyPatternWithNocheckAndOnlydirFlag2 = glob( 'pattern' , GLOB_NOCHECK | GLOB_ONLYDIR | GLOB_MARK);
$nonEmptyPatternWithNocheckAndOnlydirFlag3 = glob( 'pattern' , GLOB_NOCHECK | GLOB_ONLYDIR | GLOB_MARK | GLOB_NOSORT | GLOB_NOESCAPE | GLOB_BRACE | GLOB_ERR);
$nonEmptyPatternWithNocheckFlagAndMaybeOnlydir = glob( 'pattern' , GLOB_NOCHECK | $maybeOnlydirFlag);
$nonEmptyPatternMaybeWithNocheckFlag = glob( 'pattern' , $maybeNocheckFlag);
$nonEmptyPatternMaybeWithNocheckFlagAndOnlydir = glob( 'pattern' , $maybeNocheckFlag | GLOB_ONLYDIR);
$nonEmptyPatternMaybeWithNocheckFlagAndMaybeOnlydir = glob( 'pattern' , $maybeNocheckFlag | $maybeOnlydirFlag);
$stringPatternNoFlags = glob( $string );
$stringPatternWithoutNocheckFlag1 = glob( $string, GLOB_MARK );
$stringPatternWithoutNocheckFlag2 = glob( $string , GLOB_NOSORT | GLOB_NOESCAPE);
$stringPatternWithoutNocheckFlag3 = glob( $string , GLOB_MARK | GLOB_NOSORT | GLOB_NOESCAPE | GLOB_BRACE | GLOB_ONLYDIR | GLOB_ERR);
$stringPatternWithNocheckFlag1 = glob( $string , GLOB_NOCHECK);
$stringPatternWithNocheckFlag2 = glob( $string , GLOB_NOCHECK | GLOB_MARK);
$stringPatternWithNocheckFlag3 = glob( $string , GLOB_NOCHECK | GLOB_MARK | GLOB_NOSORT | GLOB_NOESCAPE | GLOB_BRACE | GLOB_ERR);
$stringPatternWithNocheckAndOnlydirFlag1 = glob( $string , GLOB_NOCHECK | GLOB_ONLYDIR);
$stringPatternWithNocheckAndOnlydirFlag2 = glob( $string , GLOB_NOCHECK | GLOB_ONLYDIR | GLOB_MARK);
$stringPatternWithNocheckAndOnlydirFlag3 = glob( $string , GLOB_NOCHECK | GLOB_ONLYDIR | GLOB_MARK | GLOB_NOSORT | GLOB_NOESCAPE | GLOB_BRACE | GLOB_ERR);
$stringPatternWithNocheckFlagAndMaybeOnlydir = glob( $string , GLOB_NOCHECK | $maybeOnlydirFlag);
$stringPatternMaybeWithNocheckFlag = glob( $string , $maybeNocheckFlag);
$stringPatternMaybeWithNocheckFlagAndOnlydir = glob( $string , $maybeNocheckFlag | GLOB_ONLYDIR);
$stringPatternMaybeWithNocheckFlagAndMaybeOnlydir = glob( $string , $maybeNocheckFlag | $maybeOnlydirFlag);
PHP,
'assertions' => [
'$emptyPatternNoFlags===' => 'false|list<never>',
'$emptyPatternWithoutNocheckFlag1===' => 'false|list<never>',
'$emptyPatternWithoutNocheckFlag2===' => 'false|list<never>',
'$emptyPatternWithoutNocheckFlag3===' => 'false|list<never>',
'$emptyPatternWithNocheckFlag1===' => 'false|list{\'\'}',
'$emptyPatternWithNocheckFlag2===' => 'false|list{\'\'}',
'$emptyPatternWithNocheckFlag3===' => 'false|list{\'\'}',
'$emptyPatternWithNocheckAndOnlydirFlag1===' => 'false|list<never>',
'$emptyPatternWithNocheckAndOnlydirFlag2===' => 'false|list<never>',
'$emptyPatternWithNocheckAndOnlydirFlag3===' => 'false|list<never>',
'$emptyPatternWithNocheckFlagAndMaybeOnlydir===' => 'false|list{0?: \'\', ...<never>}',
'$emptyPatternMaybeWithNocheckFlag===' => 'false|list{0?: \'\', ...<never>}',
'$emptyPatternMaybeWithNocheckFlagAndOnlydir===' => 'false|list<never>',
'$emptyPatternMaybeWithNocheckFlagAndMaybeOnlydir===' => 'false|list{0?: \'\', ...<never>}',
'$nonEmptyPatternNoFlags===' => 'false|list<non-empty-string>',
'$nonEmptyPatternWithoutNocheckFlag1===' => 'false|list<non-empty-string>',
'$nonEmptyPatternWithoutNocheckFlag2===' => 'false|list<non-empty-string>',
'$nonEmptyPatternWithoutNocheckFlag3===' => 'false|list<non-empty-string>',
'$nonEmptyPatternWithNocheckFlag1===' => 'false|non-empty-list<non-empty-string>',
'$nonEmptyPatternWithNocheckFlag2===' => 'false|non-empty-list<non-empty-string>',
'$nonEmptyPatternWithNocheckFlag3===' => 'false|non-empty-list<non-empty-string>',
'$nonEmptyPatternWithNocheckAndOnlydirFlag1===' => 'false|list<non-empty-string>',
'$nonEmptyPatternWithNocheckAndOnlydirFlag2===' => 'false|list<non-empty-string>',
'$nonEmptyPatternWithNocheckAndOnlydirFlag3===' => 'false|list<non-empty-string>',
'$nonEmptyPatternWithNocheckFlagAndMaybeOnlydir===' => 'false|list<non-empty-string>',
'$nonEmptyPatternMaybeWithNocheckFlag===' => 'false|list<non-empty-string>',
'$nonEmptyPatternMaybeWithNocheckFlagAndOnlydir===' => 'false|list<non-empty-string>',
'$nonEmptyPatternMaybeWithNocheckFlagAndMaybeOnlydir===' => 'false|list<non-empty-string>',
'$stringPatternNoFlags===' => 'false|list<non-empty-string>',
'$stringPatternWithoutNocheckFlag1===' => 'false|list<non-empty-string>',
'$stringPatternWithoutNocheckFlag2===' => 'false|list<non-empty-string>',
'$stringPatternWithoutNocheckFlag3===' => 'false|list<non-empty-string>',
'$stringPatternWithNocheckFlag1===' => 'false|list{string, ...<non-empty-string>}',
'$stringPatternWithNocheckFlag2===' => 'false|list{string, ...<non-empty-string>}',
'$stringPatternWithNocheckFlag3===' => 'false|list{string, ...<non-empty-string>}',
'$stringPatternWithNocheckAndOnlydirFlag1===' => 'false|list<non-empty-string>',
'$stringPatternWithNocheckAndOnlydirFlag2===' => 'false|list<non-empty-string>',
'$stringPatternWithNocheckAndOnlydirFlag3===' => 'false|list<non-empty-string>',
'$stringPatternWithNocheckFlagAndMaybeOnlydir===' => 'false|list{0?: string, ...<non-empty-string>}',
'$stringPatternMaybeWithNocheckFlag===' => 'false|list{0?: string, ...<non-empty-string>}',
'$stringPatternMaybeWithNocheckFlagAndOnlydir===' => 'false|list<non-empty-string>',
2023-06-07 08:52:54 +02:00
'$stringPatternMaybeWithNocheckFlagAndMaybeOnlydir===' => 'false|list{0?: string, ...<non-empty-string>}',
],
2023-06-06 17:02:28 +02:00
];
yield 'glob return ignores false' => [
'code' => <<<'PHP'
<?php
/**
* @param list $list
*/
function takesList(array $list): void {}
takesList(glob( '' ));
PHP,
];
yield 'glob accepts GLOB_BRACE' => [
'code' => <<<'PHP'
<?php
$globBrace = glob('abc', GLOB_BRACE);
PHP,
];
2024-01-31 10:39:54 +01:00
yield "ownerDocument's type is non-nullable DOMDocument and always null on DOMDocument itself" => [
'code' => '<?php
$a = (new DOMDocument())->ownerDocument;
$b = (new DOMNode())->ownerDocument;
$c = (new DOMElement("p"))->ownerDocument;
$d = (new DOMNameSpaceNode())->ownerDocument;
',
'assertions' => [
'$a===' => 'null',
'$b===' => 'DOMDocument',
'$c===' => 'DOMDocument',
'$d===' => 'DOMDocument',
],
];
2021-09-09 16:04:12 +02:00
}
public function providerInvalidCodeParse(): iterable
{
yield 'json_decode invalid depth' => [
'code' => '<?php
json_decode("true", depth: -1);
',
'error_message' => 'InvalidArgument',
];
yield 'json_encode invalid depth' => [
'code' => '<?php
json_encode([], depth: 439877348953739);
',
'error_message' => 'InvalidArgument',
];
2023-03-17 11:41:36 +01:00
yield 'str_contains literal haystack' => [
'code' => '<?php
str_contains("literal", "");
',
'error_message' => 'InvalidLiteralArgument',
];
yield 'str_starts_with literal haystack' => [
'code' => '<?php
str_starts_with("literal", "");
',
'error_message' => 'InvalidLiteralArgument',
];
yield 'str_ends_with literal haystack' => [
'code' => '<?php
str_ends_with("literal", "");
',
'error_message' => 'InvalidLiteralArgument',
];
}
2021-09-09 16:04:12 +02:00
}