PhpdocSummaryFixer.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\Fixer\Phpdoc;
  13. use PhpCsFixer\AbstractFixer;
  14. use PhpCsFixer\DocBlock\DocBlock;
  15. use PhpCsFixer\DocBlock\ShortDescription;
  16. use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
  17. use PhpCsFixer\FixerDefinition\CodeSample;
  18. use PhpCsFixer\FixerDefinition\FixerDefinition;
  19. use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
  20. use PhpCsFixer\Tokenizer\Token;
  21. use PhpCsFixer\Tokenizer\Tokens;
  22. /**
  23. * @author Graham Campbell <hello@gjcampbell.co.uk>
  24. */
  25. final class PhpdocSummaryFixer extends AbstractFixer implements WhitespacesAwareFixerInterface
  26. {
  27. public function getDefinition(): FixerDefinitionInterface
  28. {
  29. return new FixerDefinition(
  30. 'PHPDoc summary should end in either a full stop, exclamation mark, or question mark.',
  31. [new CodeSample('<?php
  32. /**
  33. * Foo function is great
  34. */
  35. function foo () {}
  36. ')]
  37. );
  38. }
  39. /**
  40. * {@inheritdoc}
  41. *
  42. * Must run before PhpdocAlignFixer.
  43. * Must run after AlignMultilineCommentFixer, CommentToPhpdocFixer, PhpdocIndentFixer, PhpdocScalarFixer, PhpdocToCommentFixer, PhpdocTypesFixer.
  44. */
  45. public function getPriority(): int
  46. {
  47. return 0;
  48. }
  49. public function isCandidate(Tokens $tokens): bool
  50. {
  51. return $tokens->isTokenKindFound(T_DOC_COMMENT);
  52. }
  53. protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
  54. {
  55. foreach ($tokens as $index => $token) {
  56. if (!$token->isGivenKind(T_DOC_COMMENT)) {
  57. continue;
  58. }
  59. $doc = new DocBlock($token->getContent());
  60. $end = (new ShortDescription($doc))->getEnd();
  61. if (null !== $end) {
  62. $line = $doc->getLine($end);
  63. $content = rtrim($line->getContent());
  64. if (
  65. // final line of Description is NOT properly formatted
  66. !$this->isCorrectlyFormatted($content)
  67. // and first line of Description, if different than final line, does NOT indicate a list
  68. && (1 === $end || ($doc->isMultiLine() && ':' !== substr(rtrim($doc->getLine(1)->getContent()), -1)))
  69. ) {
  70. $line->setContent($content.'.'.$this->whitespacesConfig->getLineEnding());
  71. $tokens[$index] = new Token([T_DOC_COMMENT, $doc->getContent()]);
  72. }
  73. }
  74. }
  75. }
  76. /**
  77. * Is the last line of the short description correctly formatted?
  78. */
  79. private function isCorrectlyFormatted(string $content): bool
  80. {
  81. if (false !== stripos($content, '{@inheritdoc}')) {
  82. return true;
  83. }
  84. return $content !== rtrim($content, '.:。!?¡¿!?');
  85. }
  86. }