-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathAbstractApiSniff.php
72 lines (60 loc) · 1.62 KB
/
AbstractApiSniff.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
67
68
69
70
71
72
<?php
/**
* Copyright 2019 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
namespace Magento2\Sniffs\Classes;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
/**
* Detects api annotation for an abstract class.
*/
class AbstractApiSniff implements Sniff
{
/**
* String representation of warning.
*
* @var string
*/
protected $warningMessage = 'Abstract classes MUST NOT be marked as public @api.';
/**
* Warning violation code.
*
* @var string
*/
protected $warningCode = 'AbstractApi';
/**
* @inheritDoc
*/
public function register()
{
return [T_CLASS];
}
/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
if ($prev !== false && $tokens[$prev]['code'] !== T_ABSTRACT) {
return;
}
$commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1, 0);
if ($commentStartPtr === false) {
return;
}
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {
$token = $tokens[$i];
if ($token['code'] !== T_DOC_COMMENT_TAG) {
continue;
}
if (strpos($token['content'], '@api') === false) {
continue;
}
$phpcsFile->addWarning($this->warningMessage, $i, $this->warningCode);
}
}
}