DocumentationLocator.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\Documentation;
  13. use PhpCsFixer\Fixer\FixerInterface;
  14. use PhpCsFixer\Preg;
  15. use PhpCsFixer\Utils;
  16. /**
  17. * @internal
  18. */
  19. final class DocumentationLocator
  20. {
  21. private string $path;
  22. public function __construct()
  23. {
  24. $this->path = \dirname(__DIR__, 2).'/doc';
  25. }
  26. public function getFixersDocumentationDirectoryPath(): string
  27. {
  28. return $this->path.'/rules';
  29. }
  30. public function getFixersDocumentationIndexFilePath(): string
  31. {
  32. return $this->getFixersDocumentationDirectoryPath().'/index.rst';
  33. }
  34. public function getFixerDocumentationFilePath(FixerInterface $fixer): string
  35. {
  36. return $this->getFixersDocumentationDirectoryPath().'/'.Preg::replaceCallback(
  37. '/^.*\\\(.+)\\\(.+)Fixer$/',
  38. static fn (array $matches): string => Utils::camelCaseToUnderscore($matches[1]).'/'.Utils::camelCaseToUnderscore($matches[2]),
  39. \get_class($fixer)
  40. ).'.rst';
  41. }
  42. public function getFixerDocumentationFileRelativePath(FixerInterface $fixer): string
  43. {
  44. return Preg::replace(
  45. '#^'.preg_quote($this->getFixersDocumentationDirectoryPath(), '#').'/#',
  46. '',
  47. $this->getFixerDocumentationFilePath($fixer)
  48. );
  49. }
  50. public function getRuleSetsDocumentationDirectoryPath(): string
  51. {
  52. return $this->path.'/ruleSets';
  53. }
  54. public function getRuleSetsDocumentationIndexFilePath(): string
  55. {
  56. return $this->getRuleSetsDocumentationDirectoryPath().'/index.rst';
  57. }
  58. public function getRuleSetsDocumentationFilePath(string $name): string
  59. {
  60. return $this->getRuleSetsDocumentationDirectoryPath().'/'.str_replace(':risky', 'Risky', ucfirst(substr($name, 1))).'.rst';
  61. }
  62. public function getUsageFilePath(): string
  63. {
  64. return $this->path.'/usage.rst';
  65. }
  66. }