-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathDocBlockAlignmentSniff.php
More file actions
88 lines (82 loc) · 3.46 KB
/
DocBlockAlignmentSniff.php
File metadata and controls
88 lines (82 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://github.com/cakephp/cakephp-codesniffer
* @since CakePHP CodeSniffer 1.0.0
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace CakePHP\Sniffs\Commenting;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
/**
* Ensures doc block alignments.
*/
class DocBlockAlignmentSniff implements Sniff
{
/**
* @inheritDoc
*/
public function register()
{
return [T_DOC_COMMENT_OPEN_TAG];
}
/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$commentClose = $phpcsFile->findNext(T_DOC_COMMENT_CLOSE_TAG, $stackPtr);
$afterComment = $phpcsFile->findNext(T_WHITESPACE, $commentClose + 1, null, true);
$commentIndentation = $tokens[$stackPtr]['column'] - 1;
$codeIndentation = $tokens[$afterComment]['column'] - 1;
// Check for doc block opening being misaligned
if ($commentIndentation != $codeIndentation) {
$msg = 'Doc block not aligned with code; expected indentation of %s but found %s';
$data = [$codeIndentation, $commentIndentation];
$fix = $phpcsFile->addFixableError($msg, $stackPtr, 'DocBlockMisaligned', $data);
if ($fix === true) {
// Collect tokens to change indentation of
$tokensToIndent = [
$stackPtr => $codeIndentation,
];
$commentOpenLine = $tokens[$stackPtr]['line'];
$commentCloseLine = $tokens[$commentClose]['line'];
$lineBreaksInComment = $commentCloseLine - $commentOpenLine;
if ($lineBreaksInComment !== 0) {
$searchToken = $stackPtr + 1;
do {
$commentBorder = $phpcsFile->findNext(
[T_DOC_COMMENT_STAR, T_DOC_COMMENT_CLOSE_TAG],
$searchToken,
$commentClose + 1,
);
if ($commentBorder !== false) {
$tokensToIndent[$commentBorder] = $codeIndentation + 1;
$searchToken = $commentBorder + 1;
}
} while ($commentBorder !== false);
}
// Update indentation
$phpcsFile->fixer->beginChangeset();
foreach ($tokensToIndent as $searchToken => $indent) {
$indentString = str_repeat(' ', $indent);
$isOpenTag = $tokens[$searchToken]['code'] === T_DOC_COMMENT_OPEN_TAG;
if ($isOpenTag && $commentIndentation === 0) {
$phpcsFile->fixer->addContentBefore($searchToken, $indentString);
} else {
$phpcsFile->fixer->replaceToken($searchToken - 1, $indentString);
}
}
$phpcsFile->fixer->endChangeset();
}
}
}
}