1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00
psalm/tests/ArrayAccessTest.php

116 lines
3.4 KiB
PHP
Raw Normal View History

2016-12-12 05:41:11 +01:00
<?php
namespace Psalm\Tests;
class ArrayAccessTest extends TestCase
2016-12-12 05:41:11 +01:00
{
use Traits\FileCheckerInvalidCodeParseTestTrait;
use Traits\FileCheckerValidCodeParseTestTrait;
2016-12-12 05:41:11 +01:00
/**
* @return array
2016-12-12 05:41:11 +01:00
*/
public function providerFileCheckerValidCodeParse()
2016-12-12 05:41:11 +01: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
}',
],
'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
}',
],
'notEmptyStringOffset' => [
'<?php
/**
* @param array<string> $a
*/
function bar (array $a) : string {
if ($a["bat"]) {
return $a["bat"];
}
return "blah";
2017-05-27 02:05:57 +02:00
}',
],
'notEmptyIntOffset' => [
'<?php
/**
* @param array<string> $a
*/
function bar (array $a) : string {
if ($a[0]) {
return $a[0];
}
return "blah";
2017-05-27 02:05:57 +02:00
}',
],
'ignorePossiblyNullArrayAccess' => [
'<?php
$a = rand(0, 1) ? [1, 2] : null;
echo $a[0];',
'assertions' => [],
2017-05-27 02:05:57 +02:00
'error_levels' => ['PossiblyNullArrayAccess'],
],
];
2016-12-12 05:41:11 +01:00
}
/**
* @return array
*/
public function providerFileCheckerInvalidCodeParse()
{
return [
'invalidArrayAccess' => [
'<?php
$a = 5;
echo $a[0];',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidArrayAccess',
],
'mixedArrayAccess' => [
'<?php
/** @var mixed */
$a = [];
echo $a[0];',
'error_message' => 'MixedArrayAccess',
2017-05-27 02:05:57 +02:00
'error_level' => ['MixedAssignment'],
],
'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'],
],
'nullArrayAccess' => [
'<?php
$a = null;
echo $a[0];',
2017-05-27 02:05:57 +02:00
'error_message' => 'NullArrayAccess',
],
'possiblyNullArrayAccess' => [
'<?php
$a = rand(0, 1) ? [1, 2] : null;
echo $a[0];',
2017-05-27 02:05:57 +02:00
'error_message' => 'PossiblyNullArrayAccess',
],
];
}
2016-12-12 05:41:11 +01:00
}