-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathInterfaceNameSniff.php
46 lines (42 loc) · 1.29 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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sniffs\NamingConventions;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
class InterfaceNameSniff implements Sniff
{
const INTERFACE_SUFFIX = 'Interface';
/**
* {@inheritdoc}
*/
public function register()
{
return [T_INTERFACE];
}
/**
* {@inheritdoc}
*/
public function process(File $sourceFile, $stackPtr)
{
$tokens = $sourceFile->getTokens();
$declarationLine = $tokens[$stackPtr]['line'];
$suffixLength = strlen(self::INTERFACE_SUFFIX);
// 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) != self::INTERFACE_SUFFIX) {
$sourceFile->addError(
'Interface should have name that ends with "Interface" suffix.',
$stackPtr,
'WrongInterfaceName'
);
}
break;
}
$stackPtr++;
}
}
}