-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathEmptyLineMissedSniff.php
66 lines (59 loc) · 1.69 KB
/
EmptyLineMissedSniff.php
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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sniffs\Whitespace;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
/**
* Class EmptyLineMissedSniff
*/
class EmptyLineMissedSniff implements Sniff
{
/**
* {@inheritdoc}
*/
public function register()
{
return [T_DOC_COMMENT];
}
/**
* {@inheritdoc}
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if ($this->doCheck($phpcsFile, $stackPtr, $tokens)) {
$previous = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
if ($tokens[$stackPtr]['line'] - $tokens[$previous]['line'] < 2) {
$error = 'Empty line missed';
$phpcsFile->addError($error, $stackPtr, '', null);
}
}
}
/**
* @param File $phpcsFile
* @param int $stackPtr
* @param array $tokens
* @return bool
*/
private function doCheck(File $phpcsFile, $stackPtr, $tokens)
{
$result = false;
if ($phpcsFile->hasCondition($stackPtr, T_CLASS) || $phpcsFile->hasCondition($stackPtr, T_INTERFACE)) {
$result = true;
}
if ($phpcsFile->hasCondition($stackPtr, T_FUNCTION)) {
$result = false;
}
$previous = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
if ($tokens[$previous]['type'] === 'T_OPEN_CURLY_BRACKET') {
$result = false;
}
if (strpos($tokens[$stackPtr]['content'], '/**') === false) {
$result = false;
}
return $result;
}
}