Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better detection expected indent in doc-block comment - closing brackets #26

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions src/WebimpressCodingStandard/Sniffs/Commenting/DocCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ private function checkSpacesAfterStar(File $phpcsFile, int $commentStart, int $c
$tokens = $phpcsFile->getTokens();
$firstTag = $tokens[$commentStart]['comment_tags'][0] ?? null;

$tagSpaces = [];

$replaces = [];
$next = $commentStart;
$search = [T_DOC_COMMENT_STAR, T_DOC_COMMENT_CLOSE_TAG];
Expand Down Expand Up @@ -402,6 +404,13 @@ private function checkSpacesAfterStar(File $phpcsFile, int $commentStart, int $c
}
}

$lastInLine = $next + 1;
while ($tokens[$lastInLine + 1]['line'] === $tokens[$lastInLine]['line']
&& strpos($tokens[$lastInLine + 1]['content'], $phpcsFile->eolChar) === false
) {
++$lastInLine;
}

$expectedSpaces = 1 + $this->indent * $nested;
$expected = str_repeat(' ', $expectedSpaces);

Expand Down Expand Up @@ -453,8 +462,34 @@ private function checkSpacesAfterStar(File $phpcsFile, int $commentStart, int $c
true
);

$closingBracket = false;
if (in_array($tokens[$next + 2]['content'][0], ['}', ')'], true)) {
$expectedSpaces -= $this->indent;
$expectedBracket = $tokens[$next + 2]['content'][0] === '}' ? '{' : '(';

$lvl = -1;
$token = $next;
$expectedTokens = [T_DOC_COMMENT_STRING, T_DOC_COMMENT_TAG];
while ($token = $phpcsFile->findPrevious($expectedTokens, $token - 1, $firstTag)) {
$last = substr(trim($tokens[$token]['content']), -1);
if (in_array($last, ['{', '('], true)) {
++$lvl;
}

if ($lvl === 0) {
break;
}

if (in_array($tokens[$token]['content'][0], ['}', ')'], true)) {
--$lvl;
}
}

if (isset($tagSpaces[$token]) && $last === $expectedBracket) {
$closingBracket = true;
$expectedSpaces = $tagSpaces[$token];
} else {
$expectedSpaces -= $this->indent;
}
} elseif (! in_array($prev2, $openNestingTokens, true)
&& ! in_array($prev, $openNestingTokens, true)
) {
Expand All @@ -470,12 +505,16 @@ private function checkSpacesAfterStar(File $phpcsFile, int $commentStart, int $c
&& ($spaces < $expectedSpaces
|| (($spaces - 1) % $this->indent) !== 0
|| ($spaces > $expectedSpaces
&& $tokens[$prev]['line'] === $tokens[$next + 1]['line'] - 1))
&& $tokens[$prev]['line'] === $tokens[$next + 1]['line'] - 1)
|| ($spaces > $expectedSpaces
&& $closingBracket))
) {
if ($tokens[$prev2]['line'] === $tokens[$next]['line'] - 1) {
if (isset($replaces[$prev][$spaces]) && $replaces[$prev][$spaces] !== $spaces) {
$expectedSpaces = $replaces[$prev][$spaces];
} elseif ($tokens[$prev]['line'] !== $tokens[$next + 1]['line'] - 1) {
} elseif (! $closingBracket
&& $tokens[$prev]['line'] !== $tokens[$next + 1]['line'] - 1
) {
$expectedSpaces = 1 + (int) max(
round(($spaces - 1) / $this->indent) * $this->indent,
$this->indent
Expand Down Expand Up @@ -506,8 +545,12 @@ private function checkSpacesAfterStar(File $phpcsFile, int $commentStart, int $c
. ' or remove empty line above if it is description for the tag';
$phpcsFile->addError($error, $next + 1, 'AdditionalDescription');
}
} else {
$expectedSpaces = $spaces;
}
}

$tagSpaces[$lastInLine] = $expectedSpaces;
}
}
}
Expand Down
28 changes: 25 additions & 3 deletions test/Sniffs/Commenting/DocCommentUnitTest.10.inc
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ namespace MyApp\MyNamespace;
* name="foo",
* indexes={
* @ORM\Index(name="foo_idx", columns={"foo"})
* },
* },
* uniqueConstraints={
* @ORM\UniqueConstraint(name="unique_idx", columns={"unique"})
* }
* )
* }
* )
*/
class Foo
{
Expand All @@ -52,4 +52,26 @@ class Foo
* )
*/
public $b;

/**
* @var int
*
* @ORM\Column({
* "default": 0
* })
*/
public $c;

/**
* @var string Description, for example:
* function() {
* if ($a
* && $b
* ) {
* return 0;
* }
* return null;
* }
*/
public $d;
}
77 changes: 77 additions & 0 deletions test/Sniffs/Commenting/DocCommentUnitTest.10.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace MyApp\MyNamespace;

/**
* @ORM\Table(
* name="foo",
* indexes={
* @ORM\Index(
* name="foo_idx",
* columns={"foo"}
* )
* },
* uniqueConstraints={
* @ORM\UniqueConstraint(
* name="unique_idx",
* columns={"unique"}
* )
* }
* )
* @ORM\Table(
* name="foo",
* indexes={
* @ORM\Index(name="foo_idx", columns={"foo"})
* },
* uniqueConstraints={
* @ORM\UniqueConstraint(name="unique_idx", columns={"unique"})
* }
* )
*/
class Foo
{
/**
* @var bool
*
* @ORM\Column(name="required", type="boolean", nullable=false, options={
* "default": "false"
* })
*/
public $a;

/**
* @var string
*
* @ORM\Column(
* name="required",
* type="boolean",
* nullable=false,
* options={
* "default": "false"
* }
* )
*/
public $b;

/**
* @var int
*
* @ORM\Column({
* "default": 0
* })
*/
public $c;

/**
* @var string Description, for example:
* function() {
* if ($a
* && $b
* ) {
* return 0;
* }
* return null;
* }
*/
public $d;
}
8 changes: 7 additions & 1 deletion test/Sniffs/Commenting/DocCommentUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ protected function getErrorList(string $testFile = '') : array
32 => 1,
];
case 'DocCommentUnitTest.10.inc':
return [];
return [
25 => 1,
28 => 1,
29 => 1,
60 => 1,
61 => 1,
];
}

return [
Expand Down