PhpdocAnnotationWithoutDotFixer.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Fixer\Phpdoc;
  12. use PhpCsFixer\AbstractFixer;
  13. use PhpCsFixer\DocBlock\DocBlock;
  14. use PhpCsFixer\FixerDefinition\CodeSample;
  15. use PhpCsFixer\FixerDefinition\FixerDefinition;
  16. use PhpCsFixer\Tokenizer\Token;
  17. use PhpCsFixer\Tokenizer\Tokens;
  18. /**
  19. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  20. */
  21. final class PhpdocAnnotationWithoutDotFixer extends AbstractFixer
  22. {
  23. private $tags = ['throws', 'return', 'param', 'internal', 'deprecated', 'var', 'type'];
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function getDefinition()
  28. {
  29. return new FixerDefinition(
  30. 'Phpdocs annotation descriptions should not be a sentence.',
  31. [new CodeSample('<?php
  32. /**
  33. * @param string $bar Some string.
  34. */
  35. function foo ($bar) {}
  36. ')]
  37. );
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function isCandidate(Tokens $tokens)
  43. {
  44. return $tokens->isTokenKindFound(T_DOC_COMMENT);
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function applyFix(\SplFileInfo $file, Tokens $tokens)
  50. {
  51. foreach ($tokens as $index => $token) {
  52. if (!$token->isGivenKind(T_DOC_COMMENT)) {
  53. continue;
  54. }
  55. $doc = new DocBlock($token->getContent());
  56. $annotations = $doc->getAnnotations();
  57. if (empty($annotations)) {
  58. continue;
  59. }
  60. foreach ($annotations as $annotation) {
  61. if (
  62. !$annotation->getTag()->valid() || !in_array($annotation->getTag()->getName(), $this->tags, true)
  63. ) {
  64. continue;
  65. }
  66. $content = $annotation->getContent();
  67. if (
  68. 1 !== preg_match('/[.。]$/u', $content)
  69. || 0 !== preg_match('/[.。](?!$)/u', $content, $matches)
  70. ) {
  71. continue;
  72. }
  73. $endLine = $doc->getLine($annotation->getEnd());
  74. $endLine->setContent(preg_replace('/(?<![.。])[.。](\s+)$/u', '\1', $endLine->getContent()));
  75. $startLine = $doc->getLine($annotation->getStart());
  76. $optionalTypeRegEx = $annotation->supportTypes()
  77. ? sprintf('(?:%s\s+(?:\$\w+\s+)?)?', preg_quote(implode('|', $annotation->getTypes()), '/'))
  78. : '';
  79. $content = preg_replace_callback(
  80. '/^(\s*\*\s*@\w+\s+'.$optionalTypeRegEx.')(\p{Lu}?(?=\p{Ll}|\p{Zs}))(.*)$/',
  81. static function (array $matches) {
  82. return $matches[1].strtolower($matches[2]).$matches[3];
  83. },
  84. $startLine->getContent(),
  85. 1
  86. );
  87. $startLine->setContent($content);
  88. }
  89. $tokens[$index] = new Token([T_DOC_COMMENT, $doc->getContent()]);
  90. }
  91. }
  92. }