LintingFileIterator.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. declare(strict_types=1);
  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. namespace PhpCsFixer\Runner;
  13. use PhpCsFixer\Linter\LinterInterface;
  14. use PhpCsFixer\Linter\LintingResultInterface;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @extends \IteratorIterator<mixed, \SplFileInfo, \Traversable<\SplFileInfo>>
  21. */
  22. final class LintingFileIterator extends \IteratorIterator implements LintingResultAwareFileIteratorInterface
  23. {
  24. /**
  25. * @var null|LintingResultInterface
  26. */
  27. private $currentResult;
  28. private LinterInterface $linter;
  29. /**
  30. * @param \Iterator<mixed, \SplFileInfo> $iterator
  31. */
  32. public function __construct(\Iterator $iterator, LinterInterface $linter)
  33. {
  34. parent::__construct($iterator);
  35. $this->linter = $linter;
  36. }
  37. public function currentLintingResult(): ?LintingResultInterface
  38. {
  39. return $this->currentResult;
  40. }
  41. public function next(): void
  42. {
  43. parent::next();
  44. $this->currentResult = $this->valid() ? $this->handleItem($this->current()) : null;
  45. }
  46. public function rewind(): void
  47. {
  48. parent::rewind();
  49. $this->currentResult = $this->valid() ? $this->handleItem($this->current()) : null;
  50. }
  51. private function handleItem(\SplFileInfo $file): LintingResultInterface
  52. {
  53. return $this->linter->lintFile($file->getRealPath());
  54. }
  55. }