Skip to content

Commit

Permalink
bug #5791 NoBlankLinesAfterPhpdoc - Add T_NAMESPACE in array of forbi…
Browse files Browse the repository at this point in the history
…dden successors (paulbalandan)

This PR was merged into the 2.19 branch.

Discussion
----------

NoBlankLinesAfterPhpdoc - Add T_NAMESPACE in array of forbidden successors

Fixes #5111

When used, `no_blank_lines_after_phpdoc` removes the whitespace between the file-level doc-block and the namespace declaration. This should not be the case as namespaces are not considered structural elements that can be documented with doc-blocks. Besides, the file-level doc-block belongs to the file and NOT on the namespace.

See more: https://docs.phpdoc.org/3.0/guide/getting-started/what-is-a-docblock.html

Commits
-------

e4dd5a6 Add T_NAMESPACE in array of forbidden successors
  • Loading branch information
keradus committed Aug 2, 2021
2 parents d7c4564 + e4dd5a6 commit 7b18f04
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public function isCandidate(Tokens $tokens)

/**
* {@inheritdoc}
*
* Must run after NoBlankLinesAfterPhpdocFixer.
*/
public function getPriority()
{
Expand Down
15 changes: 8 additions & 7 deletions src/Fixer/Phpdoc/NoBlankLinesAfterPhpdocFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Bar {}
/**
* {@inheritdoc}
*
* Must run before HeaderCommentFixer, PhpdocAlignFixer, SingleBlankLineBeforeNamespaceFixer.
* Must run before HeaderCommentFixer, PhpdocAlignFixer.
* Must run after AlignMultilineCommentFixer, CommentToPhpdocFixer, PhpdocIndentFixer, PhpdocScalarFixer, PhpdocToCommentFixer, PhpdocTypesFixer.
*/
public function getPriority()
Expand All @@ -71,16 +71,17 @@ public function getPriority()
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
static $forbiddenSuccessors = [
T_DOC_COMMENT,
T_BREAK,
T_COMMENT,
T_WHITESPACE,
T_RETURN,
T_THROW,
T_GOTO,
T_CONTINUE,
T_BREAK,
T_DECLARE,
T_DOC_COMMENT,
T_GOTO,
T_NAMESPACE,
T_RETURN,
T_THROW,
T_USE,
T_WHITESPACE,
];

foreach ($tokens as $index => $token) {
Expand Down
1 change: 0 additions & 1 deletion tests/AutoReview/FixerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ public function provideFixersPriorityCases()
[$fixers['no_alternative_syntax'], $fixers['no_useless_else']],
[$fixers['no_alternative_syntax'], $fixers['switch_continue_to_break']],
[$fixers['no_blank_lines_after_phpdoc'], $fixers['header_comment']],
[$fixers['no_blank_lines_after_phpdoc'], $fixers['single_blank_line_before_namespace']],
[$fixers['no_empty_comment'], $fixers['no_extra_blank_lines']],
[$fixers['no_empty_comment'], $fixers['no_trailing_whitespace']],
[$fixers['no_empty_comment'], $fixers['no_whitespace_in_blank_line']],
Expand Down
16 changes: 16 additions & 0 deletions tests/Fixer/Phpdoc/NoBlankLinesAfterPhpdocFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,22 @@ public function testFixesOthers()
$this->doTest($expected, $input);
}

public function testWhitespaceInDocBlockAboveNamespaceIsNotTouched()
{
$expected = <<<'EOF'
<?php
/**
* This is a file-level docblock.
*/
namespace Foo\Bar\Baz;
EOF;

$this->doTest($expected);
}

public function testFixesWindowsStyle()
{
$expected = "<?php\r\n /** * Constant! */\n \$foo = 123;";
Expand Down

0 comments on commit 7b18f04

Please sign in to comment.