-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from unleashedtech/inheritdoc-comments
Require inheritdoc comments to follow a set style
- Loading branch information
Showing
10 changed files
with
241 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Unleashed\Sniffs\Commenting; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
|
||
final class InheritDocFormatSniff implements Sniff | ||
{ | ||
public const CODE_INVALID_INHERITDOC_STYLE = 'InvalidInheritDocStyle'; | ||
|
||
/** | ||
* The required style | ||
* | ||
* @var string | ||
*/ | ||
public $style = '{@inheritDoc}'; | ||
|
||
/** | ||
* @return array<int, (int|string)> | ||
*/ | ||
public function register(): array | ||
{ | ||
return [ | ||
T_DOC_COMMENT_OPEN_TAG, | ||
]; | ||
} | ||
|
||
/** | ||
* @param int $stackPtr | ||
* | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr): void | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
for ($i = $stackPtr + 1; $i < $tokens[$stackPtr]['comment_closer']; $i++) { | ||
if (\in_array($tokens[$i]['code'], [T_DOC_COMMENT_WHITESPACE, T_DOC_COMMENT_STAR], true)) { | ||
continue; | ||
} | ||
|
||
$content = $tokens[$i]['content']; | ||
|
||
if (\preg_match('~^(?:{@inheritDoc}|@inheritDoc)$~i', $content) === 0) { | ||
continue; | ||
} | ||
|
||
$fixed = \preg_replace('~({@inheritDoc}|@inheritDoc)~i', $this->style, $content); | ||
if ($content === $fixed) { | ||
continue; | ||
} | ||
|
||
$fix = $phpcsFile->addFixableError( | ||
\sprintf('Incorrect formatting of "%s"', $this->style), | ||
$i, | ||
self::CODE_INVALID_INHERITDOC_STYLE | ||
); | ||
|
||
if (! $fix) { | ||
return; | ||
} | ||
|
||
$phpcsFile->fixer->beginChangeset(); | ||
$phpcsFile->fixer->replaceToken($i, $fixed); | ||
$phpcsFile->fixer->endChangeset(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Test; | ||
|
||
interface Foo | ||
{ | ||
/** | ||
* @param array<int, string> $foo | ||
*/ | ||
public function foo(array $foo): void; | ||
} | ||
|
||
class A implements Foo | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function foo(array $foo): void | ||
{ | ||
} | ||
} | ||
|
||
class B implements Foo | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function foo(array $foo): void | ||
{ | ||
} | ||
} | ||
|
||
class C implements Foo | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function foo(array $foo): void | ||
{ | ||
} | ||
} | ||
|
||
class D implements Foo | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function foo(array $foo): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Test; | ||
|
||
interface Foo | ||
{ | ||
/** | ||
* @param array<int, string> $foo | ||
*/ | ||
public function foo(array $foo): void; | ||
} | ||
|
||
class A implements Foo | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function foo(array $foo): void | ||
{ | ||
} | ||
} | ||
|
||
class B implements Foo | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function foo(array $foo): void | ||
{ | ||
} | ||
} | ||
|
||
class C implements Foo | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function foo(array $foo): void | ||
{ | ||
} | ||
} | ||
|
||
class D implements Foo | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function foo(array $foo): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.