Skip to content

Commit 74666dc

Browse files
committed
bug #4404 FileLintingIterator - fix current value on end/invalid (SpacePossum)
This PR was merged into the 2.12 branch. Discussion ---------- FileLintingIterator - fix current value on end/invalid Current implementation keeps the last value and does not reset the value to `null` at the end of an iteration or when an invalid value has been encountered. Commits ------- 747d3d5 fix FileLintingIterator current value on end/invalid
2 parents 82ed7d1 + 747d3d5 commit 74666dc

File tree

3 files changed

+101
-8
lines changed

3 files changed

+101
-8
lines changed

src/Runner/FileLintingIterator.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class FileLintingIterator extends \IteratorIterator
2828
private $currentResult;
2929

3030
/**
31-
* @var LinterInterface
31+
* @var null|LinterInterface
3232
*/
3333
private $linter;
3434

@@ -39,6 +39,9 @@ public function __construct(\Iterator $iterator, LinterInterface $linter)
3939
$this->linter = $linter;
4040
}
4141

42+
/**
43+
* @return null|LinterInterface
44+
*/
4245
public function currentLintingResult()
4346
{
4447
return $this->currentResult;
@@ -48,18 +51,14 @@ public function next()
4851
{
4952
parent::next();
5053

51-
if ($this->valid()) {
52-
$this->currentResult = $this->handleItem($this->current());
53-
}
54+
$this->currentResult = $this->valid() ? $this->handleItem($this->current()) : null;
5455
}
5556

5657
public function rewind()
5758
{
5859
parent::rewind();
5960

60-
if ($this->valid()) {
61-
$this->currentResult = $this->handleItem($this->current());
62-
}
61+
$this->currentResult = $this->valid() ? $this->handleItem($this->current()) : null;
6362
}
6463

6564
private function handleItem(\SplFileInfo $file)

tests/AutoReview/ProjectCodeTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ final class ProjectCodeTest extends TestCase
4848
\PhpCsFixer\Fixer\Operator\AlignEqualsFixerHelper::class,
4949
\PhpCsFixer\Fixer\Whitespace\NoExtraConsecutiveBlankLinesFixer::class,
5050
\PhpCsFixer\Runner\FileCachingLintingIterator::class,
51-
\PhpCsFixer\Runner\FileLintingIterator::class,
5251
\PhpCsFixer\Test\AccessibleObject::class,
5352
];
5453

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHP CS Fixer.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
8+
*
9+
* This source file is subject to the MIT license that is bundled
10+
* with this source code in the file LICENSE.
11+
*/
12+
13+
namespace PhpCsFixer\Tests\Runner;
14+
15+
use PhpCsFixer\Linter\LinterInterface;
16+
use PhpCsFixer\Linter\LintingResultInterface;
17+
use PhpCsFixer\Runner\FileLintingIterator;
18+
use PhpCsFixer\Tests\TestCase;
19+
20+
/**
21+
* @author SpacePossum
22+
*
23+
* @internal
24+
*
25+
* @covers \PhpCsFixer\Runner\FileLintingIterator
26+
*/
27+
final class FileLintingIteratorTest extends TestCase
28+
{
29+
public function testFileLintingIteratorEmpty()
30+
{
31+
$fileLintingIteratorProphecy = $this->prophesize(LinterInterface::class);
32+
33+
$iterator = new \ArrayIterator([]);
34+
35+
$fileLintingIterator = new FileLintingIterator(
36+
$iterator,
37+
$fileLintingIteratorProphecy->reveal()
38+
);
39+
40+
static::assertNull($fileLintingIterator->current());
41+
static::assertNull($fileLintingIterator->currentLintingResult());
42+
static::assertSame($iterator, $fileLintingIterator->getInnerIterator());
43+
static::assertFalse($fileLintingIterator->valid());
44+
}
45+
46+
public function testFileLintingIterator()
47+
{
48+
$file = new \SplFileInfo(__FILE__);
49+
$fileLintingIteratorProphecy = $this->prophesize(LinterInterface::class);
50+
51+
$lintingResultInterfaceProphecy = $this->prophesize(LintingResultInterface::class)->reveal();
52+
$fileLintingIteratorProphecy->lintFile($file)->willReturn($lintingResultInterfaceProphecy);
53+
54+
$iterator = new \ArrayIterator([$file]);
55+
56+
$fileLintingIterator = new FileLintingIterator(
57+
$iterator,
58+
$fileLintingIteratorProphecy->reveal()
59+
);
60+
61+
// test when not touched current is null
62+
63+
static::assertNull($fileLintingIterator->currentLintingResult());
64+
65+
// test iterating
66+
67+
$this->fileLintingIteratorIterationTest($fileLintingIterator, $file, $lintingResultInterfaceProphecy);
68+
69+
// rewind and test again
70+
71+
$fileLintingIterator->rewind();
72+
73+
$this->fileLintingIteratorIterationTest($fileLintingIterator, $file, $lintingResultInterfaceProphecy);
74+
}
75+
76+
private function fileLintingIteratorIterationTest(
77+
FileLintingIterator $fileLintingIterator,
78+
\SplFileInfo $file,
79+
LintingResultInterface $lintingResultInterface
80+
) {
81+
$iterations = 0;
82+
83+
foreach ($fileLintingIterator as $lintedFile) {
84+
static::assertSame($file, $lintedFile);
85+
static::assertSame($lintingResultInterface, $fileLintingIterator->currentLintingResult());
86+
++$iterations;
87+
}
88+
89+
static::assertSame(1, $iterations);
90+
91+
$fileLintingIterator->next();
92+
93+
static::assertNull($fileLintingIterator->currentLintingResult());
94+
}
95+
}

0 commit comments

Comments
 (0)