2016-12-11 23:41:11 -05:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
class ArrayAccessTest extends TestCase
|
2016-12-11 23:41:11 -05:00
|
|
|
{
|
2017-04-24 23:45:02 -04:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2016-12-11 23:41:11 -05:00
|
|
|
|
|
|
|
/**
|
2017-04-24 23:45:02 -04:00
|
|
|
* @return array
|
2016-12-11 23:41:11 -05:00
|
|
|
*/
|
2017-04-24 23:45:02 -04:00
|
|
|
public function providerFileCheckerValidCodeParse()
|
2016-12-11 23:41:11 -05:00
|
|
|
{
|
2017-04-24 23:45:02 -04:00
|
|
|
return [
|
|
|
|
'instanceOfStringOffset' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function fooFoo() : void { }
|
|
|
|
}
|
|
|
|
function bar (array $a) : void {
|
|
|
|
if ($a["a"] instanceof A) {
|
|
|
|
$a["a"]->fooFoo();
|
|
|
|
}
|
2017-05-26 20:05:57 -04:00
|
|
|
}',
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'instanceOfIntOffset' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function fooFoo() : void { }
|
|
|
|
}
|
|
|
|
function bar (array $a) : void {
|
|
|
|
if ($a[0] instanceof A) {
|
|
|
|
$a[0]->fooFoo();
|
|
|
|
}
|
2017-05-26 20:05:57 -04:00
|
|
|
}',
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'notEmptyStringOffset' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<string> $a
|
|
|
|
*/
|
|
|
|
function bar (array $a) : string {
|
|
|
|
if ($a["bat"]) {
|
|
|
|
return $a["bat"];
|
|
|
|
}
|
2017-06-28 23:37:02 -04:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
return "blah";
|
2017-05-26 20:05:57 -04:00
|
|
|
}',
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
2017-06-29 17:39:46 -04:00
|
|
|
'issetPropertyStringOffset' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var array<string, string> */
|
|
|
|
public $arr = [];
|
|
|
|
}
|
|
|
|
$a = new A();
|
|
|
|
if (!isset($a->arr["bat"]) || strlen($a->arr["bat"])) { }',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
'notEmptyIntOffset' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<string> $a
|
|
|
|
*/
|
|
|
|
function bar (array $a) : string {
|
|
|
|
if ($a[0]) {
|
|
|
|
return $a[0];
|
|
|
|
}
|
2017-06-28 23:37:02 -04:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
return "blah";
|
2017-05-26 20:05:57 -04:00
|
|
|
}',
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'ignorePossiblyNullArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0, 1) ? [1, 2] : null;
|
|
|
|
echo $a[0];',
|
2017-11-15 20:45:53 -05:00
|
|
|
'assertions' => [],
|
2017-05-26 20:05:57 -04:00
|
|
|
'error_levels' => ['PossiblyNullArrayAccess'],
|
|
|
|
],
|
2017-11-15 20:45:53 -05:00
|
|
|
'ignoreEmptyArrayAccess' => [
|
2017-06-28 23:37:02 -04:00
|
|
|
'<?php
|
|
|
|
$arr = [];
|
|
|
|
$x = $arr[0];
|
|
|
|
if (isset($arr[0]) && $arr[0]) { }',
|
|
|
|
'assertions' => [
|
2017-06-29 10:22:49 -04:00
|
|
|
'$x' => 'mixed',
|
2017-06-28 23:37:02 -04:00
|
|
|
],
|
|
|
|
'error_levels' => ['EmptyArrayAccess', 'MixedAssignment'],
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
];
|
2016-12-11 23:41:11 -05:00
|
|
|
}
|
2016-12-15 01:28:36 -05:00
|
|
|
|
|
|
|
/**
|
2017-04-24 23:45:02 -04:00
|
|
|
* @return array
|
2016-12-15 01:28:36 -05:00
|
|
|
*/
|
2017-04-24 23:45:02 -04:00
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
2016-12-15 01:28:36 -05:00
|
|
|
{
|
2017-04-24 23:45:02 -04:00
|
|
|
return [
|
|
|
|
'invalidArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5;
|
|
|
|
echo $a[0];',
|
2017-05-26 20:05:57 -04:00
|
|
|
'error_message' => 'InvalidArrayAccess',
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
2017-11-15 21:04:25 -05:00
|
|
|
'invalidArrayAccess2' => [
|
|
|
|
'<?php
|
|
|
|
$x = ["a"];
|
|
|
|
$y = $x["b"];',
|
|
|
|
'error_message' => 'InvalidArrayAccess',
|
|
|
|
],
|
2017-11-11 21:22:11 -05:00
|
|
|
'possiblyInvalidArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0, 10) > 5 ? 5 : ["hello"];
|
|
|
|
echo $a[0];',
|
|
|
|
'error_message' => 'PossiblyInvalidArrayAccess',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
'mixedArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
/** @var mixed */
|
|
|
|
$a = [];
|
|
|
|
echo $a[0];',
|
|
|
|
'error_message' => 'MixedArrayAccess',
|
2017-05-26 20:05:57 -04:00
|
|
|
'error_level' => ['MixedAssignment'],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'mixedArrayOffset' => [
|
|
|
|
'<?php
|
|
|
|
/** @var mixed */
|
|
|
|
$a = 5;
|
|
|
|
echo [1, 2, 3, 4][$a];',
|
|
|
|
'error_message' => 'MixedArrayOffset',
|
2017-05-26 20:05:57 -04:00
|
|
|
'error_level' => ['MixedAssignment'],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'nullArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
$a = null;
|
|
|
|
echo $a[0];',
|
2017-05-26 20:05:57 -04:00
|
|
|
'error_message' => 'NullArrayAccess',
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'possiblyNullArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0, 1) ? [1, 2] : null;
|
|
|
|
echo $a[0];',
|
2017-05-26 20:05:57 -04:00
|
|
|
'error_message' => 'PossiblyNullArrayAccess',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
];
|
2017-02-12 17:13:03 -05:00
|
|
|
}
|
2016-12-11 23:41:11 -05:00
|
|
|
}
|