• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

keradus / PHP-CS-Fixer / 17253322895

26 Aug 2025 11:52PM UTC coverage: 94.753% (+0.008%) from 94.745%
17253322895

push

github

keradus
add to git-blame-ignore-revs

28316 of 29884 relevant lines covered (94.75%)

45.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.43
/src/Runner/FileFilterIterator.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of PHP CS Fixer.
7
 *
8
 * (c) Fabien Potencier <fabien@symfony.com>
9
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14

15
namespace PhpCsFixer\Runner;
16

17
use PhpCsFixer\Cache\CacheManagerInterface;
18
use PhpCsFixer\FileReader;
19
use PhpCsFixer\Runner\Event\FileProcessed;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
use Symfony\Contracts\EventDispatcher\Event;
22

23
/**
24
 * @internal
25
 *
26
 * @extends \FilterIterator<mixed, \SplFileInfo, \Iterator<mixed, \SplFileInfo>>
27
 *
28
 * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
29
 *
30
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
31
 */
32
final class FileFilterIterator extends \FilterIterator
33
{
34
    private ?EventDispatcherInterface $eventDispatcher;
35

36
    private CacheManagerInterface $cacheManager;
37

38
    /**
39
     * @var array<string, bool>
40
     */
41
    private array $visitedElements = [];
42

43
    /**
44
     * @param \Traversable<\SplFileInfo> $iterator
45
     */
46
    public function __construct(
47
        \Traversable $iterator,
48
        ?EventDispatcherInterface $eventDispatcher,
49
        CacheManagerInterface $cacheManager
50
    ) {
51
        if (!$iterator instanceof \Iterator) {
9✔
52
            $iterator = new \IteratorIterator($iterator);
×
53
        }
54

55
        parent::__construct($iterator);
9✔
56

57
        $this->eventDispatcher = $eventDispatcher;
9✔
58
        $this->cacheManager = $cacheManager;
9✔
59
    }
60

61
    public function accept(): bool
62
    {
63
        $file = $this->current();
9✔
64
        if (!$file instanceof \SplFileInfo) {
9✔
65
            throw new \RuntimeException(
1✔
66
                \sprintf(
1✔
67
                    'Expected instance of "\SplFileInfo", got "%s".',
1✔
68
                    get_debug_type($file)
1✔
69
                )
1✔
70
            );
1✔
71
        }
72

73
        $path = $file->isLink() ? $file->getPathname() : $file->getRealPath();
8✔
74

75
        if (isset($this->visitedElements[$path])) {
8✔
76
            return false;
2✔
77
        }
78

79
        $this->visitedElements[$path] = true;
8✔
80

81
        if (!$file->isFile() || $file->isLink()) {
8✔
82
            return false;
2✔
83
        }
84

85
        $content = FileReader::createSingleton()->read($path);
7✔
86

87
        // mark as skipped:
88
        if (
89
            // empty file
90
            '' === $content
7✔
91
            // file that does not need fixing due to cache
92
            || !$this->cacheManager->needFixing($file->getPathname(), $content)
7✔
93
        ) {
94
            $this->dispatchEvent(FileProcessed::NAME, new FileProcessed(FileProcessed::STATUS_SKIPPED));
3✔
95

96
            return false;
3✔
97
        }
98

99
        return true;
4✔
100
    }
101

102
    private function dispatchEvent(string $name, Event $event): void
103
    {
104
        if (null === $this->eventDispatcher) {
3✔
105
            return;
1✔
106
        }
107

108
        $this->eventDispatcher->dispatch($event, $name);
2✔
109
    }
110
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc