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