forked from magento/magento-coding-standard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterfaceNameSniff.php
64 lines (58 loc) · 1.59 KB
/
InterfaceNameSniff.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
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Sniffs\NamingConvention;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
/**
* Detects possible interface declaration without 'Interface' suffix.
*/
class InterfaceNameSniff implements Sniff
{
/**
* String representation of warning.
*
* @var string
*/
protected $warningMessage = 'Interface should have name that ends with "Interface" suffix.';
/**
* Warning violation code.
*
* @var string
*/
protected $warningCode = 'WrongInterfaceName';
/**
* Interface suffix.
*
* @var string
*/
private $interfaceSuffix = 'Interface';
/**
* @inheritdoc
*/
public function register()
{
return [T_INTERFACE];
}
/**
* @inheritdoc
*/
public function process(File $sourceFile, $stackPtr)
{
$tokens = $sourceFile->getTokens();
$declarationLine = $tokens[$stackPtr]['line'];
$suffixLength = strlen($this->interfaceSuffix);
// Find first T_STRING after 'interface' keyword in the line and verify it
while ($tokens[$stackPtr]['line'] === $declarationLine) {
if ($tokens[$stackPtr]['type'] === 'T_STRING') {
if (substr($tokens[$stackPtr]['content'], 0 - $suffixLength) !== $this->interfaceSuffix) {
$sourceFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode);
}
break;
}
$stackPtr++;
}
}
}