CachingLinter.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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\Linter;
  13. /**
  14. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  15. *
  16. * @internal
  17. */
  18. final class CachingLinter implements LinterInterface
  19. {
  20. private LinterInterface $sublinter;
  21. /**
  22. * @var array<string, LintingResultInterface>
  23. */
  24. private array $cache = [];
  25. public function __construct(LinterInterface $linter)
  26. {
  27. $this->sublinter = $linter;
  28. }
  29. public function isAsync(): bool
  30. {
  31. return $this->sublinter->isAsync();
  32. }
  33. public function lintFile(string $path): LintingResultInterface
  34. {
  35. $checksum = md5(file_get_contents($path));
  36. if (!isset($this->cache[$checksum])) {
  37. $this->cache[$checksum] = $this->sublinter->lintFile($path);
  38. }
  39. return $this->cache[$checksum];
  40. }
  41. public function lintSource(string $source): LintingResultInterface
  42. {
  43. $checksum = md5($source);
  44. if (!isset($this->cache[$checksum])) {
  45. $this->cache[$checksum] = $this->sublinter->lintSource($source);
  46. }
  47. return $this->cache[$checksum];
  48. }
  49. }