2017-02-17 20:50:47 -05:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
class IssetTest extends TestCase
|
2017-02-17 20:50:47 -05:00
|
|
|
{
|
2017-04-24 23:45:02 -04:00
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2017-02-17 20:50:47 -05:00
|
|
|
|
|
|
|
/**
|
2017-04-24 23:45:02 -04:00
|
|
|
* @return array
|
2017-02-17 20:50:47 -05:00
|
|
|
*/
|
2017-04-24 23:45:02 -04:00
|
|
|
public function providerFileCheckerValidCodeParse()
|
2017-02-17 20:50:47 -05:00
|
|
|
{
|
2017-04-24 23:45:02 -04:00
|
|
|
return [
|
|
|
|
'isset' => [
|
|
|
|
'<?php
|
|
|
|
$a = isset($b) ? $b : null;',
|
|
|
|
'assertions' => [
|
2017-06-29 10:22:49 -04:00
|
|
|
'$a' => 'mixed',
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
2017-05-26 20:05:57 -04:00
|
|
|
'error_levels' => ['MixedAssignment'],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'nullCoalesce' => [
|
|
|
|
'<?php
|
|
|
|
$a = $b ?? null;',
|
|
|
|
'assertions' => [
|
2017-06-29 10:22:49 -04:00
|
|
|
'$a' => 'mixed',
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
2017-05-26 20:05:57 -04:00
|
|
|
'error_levels' => ['MixedAssignment'],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'nullCoalesceWithGoodVariable' => [
|
|
|
|
'<?php
|
2017-11-28 00:46:41 -05:00
|
|
|
$b = rand(0, 10) > 5 ? "hello" : null;
|
2017-04-24 23:45:02 -04:00
|
|
|
$a = $b ?? null;',
|
|
|
|
'assertions' => [
|
2017-11-28 00:46:41 -05:00
|
|
|
'$a' => 'string|null',
|
2017-05-26 20:05:57 -04:00
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'issetKeyedOffset' => [
|
|
|
|
'<?php
|
|
|
|
if (!isset($foo["a"])) {
|
|
|
|
$foo["a"] = "hello";
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2017-06-29 10:22:49 -04:00
|
|
|
'$foo[\'a\']' => 'mixed',
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'error_levels' => [],
|
|
|
|
'scope_vars' => [
|
2017-05-26 20:05:57 -04:00
|
|
|
'$foo' => \Psalm\Type::getArray(),
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'issetKeyedOffsetORFalse' => [
|
|
|
|
'<?php
|
|
|
|
/** @return void */
|
|
|
|
function takesString(string $str) {}
|
2017-06-29 10:22:49 -04:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
$bar = rand(0, 1) ? ["foo" => "bar"] : false;
|
2017-06-29 10:22:49 -04:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
if (isset($bar["foo"])) {
|
|
|
|
takesString($bar["foo"]);
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => [],
|
|
|
|
'scope_vars' => [
|
2017-05-26 20:05:57 -04:00
|
|
|
'$foo' => \Psalm\Type::getArray(),
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'nullCoalesceKeyedOffset' => [
|
|
|
|
'<?php
|
|
|
|
$foo["a"] = $foo["a"] ?? "hello";',
|
|
|
|
'assertions' => [
|
2017-06-29 10:22:49 -04:00
|
|
|
'$foo[\'a\']' => 'mixed',
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'error_levels' => ['MixedAssignment'],
|
|
|
|
'scope_vars' => [
|
2017-05-26 20:05:57 -04:00
|
|
|
'$foo' => \Psalm\Type::getArray(),
|
|
|
|
],
|
|
|
|
],
|
2017-11-28 16:52:52 -05:00
|
|
|
'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'],
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
];
|
2017-02-17 20:50:47 -05:00
|
|
|
}
|
|
|
|
}
|