2016-12-12 05:41:11 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class ForeachTest extends TestCase
|
2016-12-12 05:41:11 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
2017-11-24 18:57:00 +01:00
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerFileCheckerValidCodeParse()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'iteratorAggregateIteration' => [
|
|
|
|
'<?php
|
|
|
|
class C implements IteratorAggregate
|
|
|
|
{
|
|
|
|
public function getIterator(): Iterator
|
|
|
|
{
|
|
|
|
return new ArrayIterator([]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function loopT(Traversable $coll): void
|
|
|
|
{
|
|
|
|
foreach ($coll as $item) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
function loopI(IteratorAggregate $coll): void
|
|
|
|
{
|
|
|
|
foreach ($coll as $item) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
loopT(new C);
|
|
|
|
loopI(new C);',
|
|
|
|
'assignments' => [],
|
|
|
|
'error_levels' => [
|
|
|
|
'MixedAssignment', 'UndefinedThisPropertyAssignment',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-01-13 20:07:23 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
2016-12-12 05:41:11 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'continueOutsideLoop' => [
|
|
|
|
'<?php
|
|
|
|
continue;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'ContinueOutsideLoop',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'invalidIterator' => [
|
|
|
|
'<?php
|
|
|
|
foreach (5 as $a) {
|
2017-11-15 04:55:48 +01:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidIterator',
|
|
|
|
],
|
2017-11-15 04:55:48 +01:00
|
|
|
'rawObjectIteration' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var ?string */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
function bar(A $a) : void {}
|
|
|
|
|
|
|
|
$arr = [];
|
|
|
|
|
|
|
|
if (rand(0, 10) > 5) {
|
|
|
|
$arr[] = new A;
|
|
|
|
} else {
|
|
|
|
$arr = new B;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($arr as $a) {
|
|
|
|
bar($a);
|
|
|
|
}',
|
|
|
|
'error_message' => 'RawObjectIteration',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|
|
|
|
}
|