FileCachingLintingFileIterator.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 \CachingIterator<mixed, \SplFileInfo, \Iterator<mixed, \SplFileInfo>>
  21. */
  22. final class FileCachingLintingFileIterator extends \CachingIterator implements LintingResultAwareFileIteratorInterface
  23. {
  24. private LinterInterface $linter;
  25. private ?LintingResultInterface $currentResult = null;
  26. private ?LintingResultInterface $nextResult = null;
  27. /**
  28. * @param \Iterator<mixed, \SplFileInfo> $iterator
  29. */
  30. public function __construct(\Iterator $iterator, LinterInterface $linter)
  31. {
  32. parent::__construct($iterator);
  33. $this->linter = $linter;
  34. }
  35. public function currentLintingResult(): ?LintingResultInterface
  36. {
  37. return $this->currentResult;
  38. }
  39. public function next(): void
  40. {
  41. parent::next();
  42. $this->currentResult = $this->nextResult;
  43. if ($this->hasNext()) {
  44. $this->nextResult = $this->handleItem($this->getInnerIterator()->current());
  45. }
  46. }
  47. public function rewind(): void
  48. {
  49. parent::rewind();
  50. if ($this->valid()) {
  51. $this->currentResult = $this->handleItem($this->current());
  52. }
  53. if ($this->hasNext()) {
  54. $this->nextResult = $this->handleItem($this->getInnerIterator()->current());
  55. }
  56. }
  57. private function handleItem(\SplFileInfo $file): LintingResultInterface
  58. {
  59. return $this->linter->lintFile($file->getRealPath());
  60. }
  61. }