2017-10-27 00:19:19 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
class AssertTest extends TestCase
|
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
2017-10-27 00:19:19 +02:00
|
|
|
|
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
2017-10-27 00:19:19 +02:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerValidCodeParse()
|
2017-10-27 00:19:19 +02:00
|
|
|
{
|
|
|
|
return [
|
2019-03-11 04:38:30 +01:00
|
|
|
'assertArrayReturnTypeNarrowed' => [
|
|
|
|
'<?php
|
|
|
|
/** @return array{0:Exception} */
|
|
|
|
function f(array $a): array {
|
|
|
|
if ($a[0] instanceof Exception) {
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [new Exception("bad")];
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-03-11 04:38:30 +01:00
|
|
|
],
|
|
|
|
'assertTypeNarrowedByAssert' => [
|
|
|
|
'<?php
|
|
|
|
/** @return array{0:Exception,1:Exception} */
|
|
|
|
function f(array $ret): array {
|
|
|
|
assert($ret[0] instanceof Exception);
|
|
|
|
assert($ret[1] instanceof Exception);
|
|
|
|
return $ret;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'assertTypeNarrowedByButOtherFetchesAreMixed' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return array{0:Exception}
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
*/
|
|
|
|
function f(array $ret): array {
|
|
|
|
assert($ret[0] instanceof Exception);
|
|
|
|
echo strlen($ret[1]);
|
|
|
|
return $ret;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'assertTypeNarrowedByNestedIsset' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedMethodCall
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
*/
|
|
|
|
function foo(array $array = []): void {
|
|
|
|
if (array_key_exists("a", $array)) {
|
|
|
|
echo $array["a"];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists("b", $array)) {
|
|
|
|
echo $array["b"]->format("Y-m-d");
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-03-10 22:54:03 +01:00
|
|
|
'assertCheckOnNonZeroArrayOffset' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array{string,array|null} $a
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function f(array $a) {
|
|
|
|
assert(is_array($a[1]));
|
|
|
|
return $a[0];
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-03-10 22:54:03 +01:00
|
|
|
],
|
2019-03-11 04:38:30 +01:00
|
|
|
'assertOnParseUrlOutput' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<"a"|"b"|"c", mixed> $arr
|
|
|
|
*/
|
|
|
|
function uriToPath(array $arr) : string {
|
|
|
|
if (!isset($arr["a"]) || $arr["b"] !== "foo") {
|
|
|
|
throw new \InvalidArgumentException("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (string) $arr["c"];
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-03-11 04:38:30 +01:00
|
|
|
],
|
|
|
|
'combineAfterLoopAssert' => [
|
|
|
|
'<?php
|
|
|
|
function foo(array $array) : void {
|
|
|
|
$c = 0;
|
|
|
|
|
|
|
|
if ($array["a"] === "a") {
|
|
|
|
foreach ([rand(0, 1), rand(0, 1)] as $i) {
|
|
|
|
if ($array["b"] === "c") {}
|
|
|
|
$c++;
|
|
|
|
}
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-03-11 04:38:30 +01:00
|
|
|
],
|
|
|
|
'assertOnXml' => [
|
|
|
|
'<?php
|
|
|
|
function f(array $array) : void {
|
|
|
|
if ($array["foo"] === "ok") {
|
|
|
|
if ($array["bar"] === "a") {}
|
|
|
|
if ($array["bar"] === "b") {}
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-03-11 04:38:30 +01:00
|
|
|
],
|
|
|
|
'assertOnBacktrace' => [
|
|
|
|
'<?php
|
|
|
|
function _validProperty(array $c, array $arr) : void {
|
|
|
|
if (empty($arr["a"])) {}
|
|
|
|
|
|
|
|
if ($c && $c["a"] !== "b") {}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-03-11 04:38:30 +01:00
|
|
|
],
|
|
|
|
'assertOnRemainderOfArray' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedInferredReturnType
|
|
|
|
* @psalm-suppress MixedReturnStatement
|
|
|
|
*/
|
|
|
|
function foo(string $file_name) : int {
|
|
|
|
while ($data = getData()) {
|
|
|
|
if (is_numeric($data[0])) {
|
|
|
|
for ($i = 1; $i < count($data); $i++) {
|
|
|
|
return $data[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getData() : ?array {
|
|
|
|
return rand(0, 1) ? ["a", "b", "c"] : null;
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-03-11 06:35:09 +01:00
|
|
|
],
|
|
|
|
'notEmptyCheck' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedAssignment
|
|
|
|
*/
|
|
|
|
function load(string $objectName, array $config = []) : void {
|
|
|
|
if (isset($config["className"])) {
|
|
|
|
$name = $objectName;
|
|
|
|
$objectName = $config["className"];
|
|
|
|
}
|
|
|
|
if (!empty($config)) {}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-03-11 06:54:32 +01:00
|
|
|
],
|
|
|
|
'unsetAfterIssetCheck' => [
|
|
|
|
'<?php
|
|
|
|
function checkbox(array $options = []) : void {
|
|
|
|
if ($options["a"]) {}
|
|
|
|
|
|
|
|
unset($options["a"], $options["b"]);
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-03-11 06:54:32 +01:00
|
|
|
],
|
2019-09-19 21:07:49 +02:00
|
|
|
'dontCrashWhenGettingEmptyCountAssertions' => [
|
|
|
|
'<?php
|
|
|
|
function foo() : bool {
|
|
|
|
/** @psalm-suppress TooFewArguments */
|
|
|
|
return count() > 0;
|
|
|
|
}',
|
|
|
|
],
|
2019-10-02 01:31:08 +02:00
|
|
|
'assertHasArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return array|ArrayAccess
|
|
|
|
*/
|
|
|
|
function getBar(array $array) {
|
|
|
|
if (isset($array[\'foo\'][\'bar\'])) {
|
|
|
|
return $array[\'foo\'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'assertHasArrayAccessWithType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<string, array<string, string>> $array
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
|
|
|
function getBar(array $array) : array {
|
|
|
|
if (isset($array[\'foo\'][\'bar\'])) {
|
|
|
|
return $array[\'foo\'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'assertHasArrayAccessOnSimpleXMLElement' => [
|
|
|
|
'<?php
|
|
|
|
function getBar(SimpleXMLElement $e, string $s) : void {
|
|
|
|
if (isset($e[$s])) {
|
|
|
|
echo (string) $e[$s];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($e[\'foo\'])) {
|
|
|
|
echo (string) $e[\'foo\'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($e->bar)) {}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'assertArrayOffsetToTraversable' => [
|
|
|
|
'<?php
|
|
|
|
function render(array $data): ?Traversable {
|
|
|
|
if ($data["o"] instanceof Traversable) {
|
|
|
|
return $data["o"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'assertOnArrayShouldNotChangeType' => [
|
|
|
|
'<?php
|
|
|
|
/** @return array|string|false */
|
|
|
|
function foo(string $a, string $b) {
|
|
|
|
$options = getopt($a, [$b]);
|
|
|
|
|
|
|
|
if (isset($options["config"])) {
|
|
|
|
$options["c"] = $options["config"];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options["root"])) {
|
|
|
|
return $options["root"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'assertOnArrayInTernary' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $a, string $b) : void {
|
|
|
|
$o = getopt($a, [$b]);
|
|
|
|
|
|
|
|
$a = isset($o["a"]) && is_string($o["a"]) ? $o["a"] : "foo";
|
|
|
|
$a = isset($o["a"]) && is_string($o["a"]) ? $o["a"] : "foo";
|
|
|
|
echo $a;
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'nonEmptyArrayAfterIsset' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<string, int> $arr
|
|
|
|
* @return non-empty-array<string, int>
|
|
|
|
*/
|
|
|
|
function foo(array $arr) : array {
|
|
|
|
if (isset($arr["a"])) {
|
|
|
|
return $arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ["b" => 1];
|
|
|
|
}'
|
|
|
|
],
|
2019-10-02 07:17:45 +02:00
|
|
|
'setArrayConstantOffset' => [
|
|
|
|
'<?php
|
|
|
|
class S {
|
|
|
|
const A = 0;
|
|
|
|
const B = 1;
|
|
|
|
const C = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
switch ($arr[S::A]) {
|
|
|
|
case S::B:
|
|
|
|
case S::C:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-02-25 17:30:45 +01:00
|
|
|
];
|
|
|
|
}
|
2017-10-27 00:19:19 +02:00
|
|
|
}
|