Skip to content
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
32 changes: 26 additions & 6 deletions PhpCollective/Sniffs/Commenting/InlineDocBlockSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ protected function checkInlineComments(File $phpcsFile, int $startIndex, int $en

$comment = $tokens[$contentTag]['content'];

if (isset($errors['space-before-end']) || isset($errors['end'])) {
$comment .= ' ';
if (isset($errors['space-before-end'])) {
$comment = rtrim($comment) . ' ';
}

if (isset($errors['order'])) {
$comment = preg_replace('|^(.+?)\s+(.+?)\s*$|', '\2 \1 ', $comment);
// Swap only `$var Type`, a trailing description stays where it is
$comment = (string)preg_replace('|^(\S+)(\s+)(\S+)|', '\3\2\1', $comment, 1);
}

$phpcsFile->fixer->replaceToken($contentTag, $comment);
Expand Down Expand Up @@ -217,17 +218,36 @@ protected function findErrors(File $phpcsFile, int $contentIndex, bool $isSingle

$errors = [];

if ($isSingleLine && !preg_match('|([a-z0-9]) $|i', $comment)) {
if ($isSingleLine && !preg_match('|\S $|', $comment)) {
$errors['space-before-end'] = 'Expected single space before ´*/´';
}

if (!preg_match('|^\$[a-z0-9_]+$|i', $contentMatches[3])) {
$errors['order'] = 'Expected `{Type} ${var}`, got `' . $contentMatches[1] . $contentMatches[2] . $contentMatches[3] . '`';
[$first, $second] = preg_split('|\s+|', trim($comment), 3) ?: ['', ''];
if ($this->isVariable($second)) {
return $errors;
}

if ($this->isVariable($first)) {
$errors['order'] = 'Expected `{Type} ${var}`, got `' . $first . ' ' . $second . '`';

return $errors;
}

$phpcsFile->addError('Invalid Inline Doc Block content, expected `{Type} ${var}` style', $contentIndex, 'ContentInvalid');

return $errors;
}

/**
* @param string $word
*
* @return bool
*/
protected function isVariable(string $word): bool
{
return (bool)preg_match('|^\$[a-z0-9_]+$|i', $word);
}

/**
* @param \PHP_CodeSniffer\Files\File $phpcsFile
* @param int $contentIndex
Expand Down
5 changes: 5 additions & 0 deletions PhpCollective/Sniffs/WhiteSpace/ConsistentIndentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,11 @@ protected function isPreviousLineComplete(int $prevToken, array $tokens): bool
*/
protected function isInMultiLineConstruct(File $phpcsFile, int $stackPtr, array $tokens): bool
{
// The backward scan below stops at `;`, which would miss the parts of a `for (...)` header
if (!empty($tokens[$stackPtr]['nested_parenthesis'])) {
return true;
}

// Check if we have unmatched parentheses on previous lines
$currentLine = $tokens[$stackPtr]['line'];
$openParens = 0;
Expand Down
6 changes: 5 additions & 1 deletion PhpCollective/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@
<rule ref="SlevomatCodingStandard.TypeHints.NullableTypeForNullDefaultValue"/>
<rule ref="SlevomatCodingStandard.TypeHints.LongTypeHints"/>

<rule ref="SlevomatCodingStandard.Attributes.AttributeAndTargetSpacing"/>
<rule ref="SlevomatCodingStandard.Attributes.AttributeAndTargetSpacing">
<properties>
<property name="allowOnSameLine" value="true"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment"/>

<rule ref="SlevomatCodingStandard.Classes.BackedEnumTypeSpacing"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class InlineDocBlockSniffTest extends TestCase
*/
public function testDocBlockConstSniffer(): void
{
$this->assertSnifferFindsErrors(new InlineDocBlockSniff(), 1);
$this->assertSnifferFindsErrors(new InlineDocBlockSniff(), 2);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/_data/ConsistentIndent/after.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,15 @@ public function defaultProvider(): string
},
);
}

public function forHeader(array $list, int $lastIndex): void
{
for (
$changeIndex = $lastIndex;
$list[$changeIndex] === 0 && $changeIndex > 0;
$changeIndex--
) {
$list[$changeIndex] = 1;
}
}
}
11 changes: 11 additions & 0 deletions tests/_data/ConsistentIndent/before.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,15 @@ public function defaultProvider(): string
},
);
}

public function forHeader(array $list, int $lastIndex): void
{
for (
$changeIndex = $lastIndex;
$list[$changeIndex] === 0 && $changeIndex > 0;
$changeIndex--
) {
$list[$changeIndex] = 1;
}
}
}
9 changes: 9 additions & 0 deletions tests/_data/InlineDocBlock/after.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,14 @@ public function test()
/** @var object{user_id: int, home_id: int} $row */
$row = new \stdClass();
$user_id = $row->user_id;

/** @var bool $deprecation Expand type for psalm */
$deprecation = false;

/** @var string $dotted Ends with a dot. */
$dotted = '';

/** @var string $reversed Some description */
$reversed = '';
}
}
9 changes: 9 additions & 0 deletions tests/_data/InlineDocBlock/before.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,14 @@ public function test()
/** @var object{user_id: int, home_id: int} $row */
$row = new \stdClass();
$user_id = $row->user_id;

/** @var bool $deprecation Expand type for psalm */
$deprecation = false;

/** @var string $dotted Ends with a dot. */
$dotted = '';

/** @var $reversed string Some description */
$reversed = '';
}
}
Loading