1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00
psalm/tests/IssetTest.php
Matthew Brown f27bd50abe
Add RedundantCondition issue, replacing FailedTypeResolution (#344)
* Group changes

* Don’t worry about vars defined before exiting

* Fix issues with vars defined in conditionals

* Add failing test

* Only add failed reconciliation flag if nothing could be salvaged

* Avoid notice when removing clauses

* Improve handling of loops

* Fix evaluation of binary op expressions

* Remove unset vars from outer context after loop

* Ignore RedundantCondition in some more configs
2017-11-28 00:46:41 -05:00

81 lines
2.4 KiB
PHP

<?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' => [
'$a' => 'mixed',
],
'error_levels' => ['MixedAssignment'],
],
'nullCoalesce' => [
'<?php
$a = $b ?? null;',
'assertions' => [
'$a' => 'mixed',
],
'error_levels' => ['MixedAssignment'],
],
'nullCoalesceWithGoodVariable' => [
'<?php
$b = rand(0, 10) > 5 ? "hello" : null;
$a = $b ?? null;',
'assertions' => [
'$a' => 'string|null',
],
],
'issetKeyedOffset' => [
'<?php
if (!isset($foo["a"])) {
$foo["a"] = "hello";
}',
'assertions' => [
'$foo[\'a\']' => 'mixed',
],
'error_levels' => [],
'scope_vars' => [
'$foo' => \Psalm\Type::getArray(),
],
],
'issetKeyedOffsetORFalse' => [
'<?php
/** @return void */
function takesString(string $str) {}
$bar = rand(0, 1) ? ["foo" => "bar"] : false;
if (isset($bar["foo"])) {
takesString($bar["foo"]);
}',
'assertions' => [],
'error_levels' => [],
'scope_vars' => [
'$foo' => \Psalm\Type::getArray(),
],
],
'nullCoalesceKeyedOffset' => [
'<?php
$foo["a"] = $foo["a"] ?? "hello";',
'assertions' => [
'$foo[\'a\']' => 'mixed',
],
'error_levels' => ['MixedAssignment'],
'scope_vars' => [
'$foo' => \Psalm\Type::getArray(),
],
],
];
}
}