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

105 lines
3.3 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests;
class IssetTest extends TestCase
{
use Traits\FileCheckerValidCodeParseTestTrait;
/**
* @return array
*/
public function providerFileCheckerValidCodeParse()
{
return [
'isset' => [
'<?php
$a = isset($b) ? $b : null;',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$a' => 'mixed',
],
2017-05-27 02:05:57 +02:00
'error_levels' => ['MixedAssignment'],
],
'nullCoalesce' => [
'<?php
$a = $b ?? null;',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$a' => 'mixed',
],
2017-05-27 02:05:57 +02:00
'error_levels' => ['MixedAssignment'],
],
'nullCoalesceWithGoodVariable' => [
'<?php
$b = rand(0, 10) > 5 ? "hello" : null;
$a = $b ?? null;',
'assertions' => [
'$a' => 'string|null',
2017-05-27 02:05:57 +02:00
],
],
'issetKeyedOffset' => [
'<?php
if (!isset($foo["a"])) {
$foo["a"] = "hello";
}',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$foo[\'a\']' => 'mixed',
],
'error_levels' => [],
'scope_vars' => [
2017-05-27 02:05:57 +02:00
'$foo' => \Psalm\Type::getArray(),
],
],
'issetKeyedOffsetORFalse' => [
'<?php
/** @return void */
function takesString(string $str) {}
2017-06-29 16:22:49 +02:00
$bar = rand(0, 1) ? ["foo" => "bar"] : false;
2017-06-29 16:22:49 +02:00
if (isset($bar["foo"])) {
takesString($bar["foo"]);
}',
'assertions' => [],
'error_levels' => [],
'scope_vars' => [
2017-05-27 02:05:57 +02:00
'$foo' => \Psalm\Type::getArray(),
],
],
'nullCoalesceKeyedOffset' => [
'<?php
$foo["a"] = $foo["a"] ?? "hello";',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$foo[\'a\']' => 'mixed',
],
'error_levels' => ['MixedAssignment'],
'scope_vars' => [
2017-05-27 02:05:57 +02:00
'$foo' => \Psalm\Type::getArray(),
],
],
'noRedundantConditionOnMixed' => [
'<?php
function testarray(array $data) : void {
foreach ($data as $item) {
if (isset($item["a"]) && isset($item["b"]) && isset($item["b"]["c"])) {
echo "Found\n";
}
}
}',
'assertions' => [],
'error_levels' => ['MixedAssignment'],
],
'testUnset' => [
'<?php
$foo = ["a", "b", "c"];
foreach ($foo as $bar) {}
unset($foo, $bar);
function foo() : void {
$foo = ["a", "b", "c"];
foreach ($foo as $bar) {}
unset($foo, $bar);
}',
],
];
}
}