1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PSR2_Sniffs_Namespaces_UseDeclarationSniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHP_CodeSniffer |
9
|
|
|
* @author Greg Sherwood <[email protected]> |
10
|
|
|
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) |
11
|
|
|
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
12
|
|
|
* @link http://pear.php.net/package/PHP_CodeSniffer |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace PSR2R\Sniffs\Namespaces; |
16
|
|
|
|
17
|
|
|
use PHP_CodeSniffer_File; |
18
|
|
|
use PHP_CodeSniffer_Sniff; |
19
|
|
|
use PHP_CodeSniffer_Tokens; |
20
|
|
|
use PSR2R\Tools\Traits\CommentingTrait; |
21
|
|
|
use PSR2R\Tools\Traits\NamespaceTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* PSR2_Sniffs_Namespaces_UseDeclarationSniff. |
25
|
|
|
* |
26
|
|
|
* Ensures USE blocks are declared correctly. |
27
|
|
|
* |
28
|
|
|
* @author Greg Sherwood <[email protected]> |
29
|
|
|
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) |
30
|
|
|
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
31
|
|
|
* @version Release: @package_version@ |
32
|
|
|
* @link http://pear.php.net/package/PHP_CodeSniffer |
33
|
|
|
*/ |
34
|
|
|
class UseDeclarationSniff implements PHP_CodeSniffer_Sniff { |
35
|
|
|
|
36
|
|
|
use CommentingTrait; |
37
|
|
|
use NamespaceTrait; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritDoc |
41
|
|
|
*/ |
42
|
|
|
public function register() { |
43
|
|
|
return [T_USE]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritDoc |
48
|
|
|
*/ |
49
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
50
|
|
|
if ($this->shouldIgnoreUse($phpcsFile, $stackPtr) === true) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$tokens = $phpcsFile->getTokens(); |
55
|
|
|
|
56
|
|
|
// One space after the use keyword. |
57
|
|
View Code Duplication |
if ($tokens[($stackPtr + 1)]['content'] !== ' ') { |
|
|
|
|
58
|
|
|
$error = 'There must be a single space after the USE keyword'; |
59
|
|
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterUse'); |
60
|
|
|
if ($fix === true) { |
61
|
|
|
$phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Namespaces in use statements must not have a leading separator |
66
|
|
|
$next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
67
|
|
|
if ($tokens[$next]['code'] === T_NS_SEPARATOR) { |
68
|
|
|
$error = 'Namespaces in use statements should not start with a namespace separator'; |
69
|
|
|
$fix = $phpcsFile->addFixableError($error, $next, 'NamespaceStart'); |
|
|
|
|
70
|
|
|
if ($fix) { |
71
|
|
|
$phpcsFile->fixer->replaceToken($next, ''); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Only one USE declaration allowed per statement. |
76
|
|
|
$next = $phpcsFile->findNext([T_COMMA, T_SEMICOLON], ($stackPtr + 1)); |
77
|
|
|
if ($tokens[$next]['code'] === T_COMMA) { |
78
|
|
|
$error = 'There must be one USE keyword per declaration'; |
79
|
|
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultipleDeclarations'); |
80
|
|
|
if ($fix === true) { |
81
|
|
|
$phpcsFile->fixer->replaceToken($next, ';' . $phpcsFile->eolChar . 'use '); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} else { |
84
|
|
|
$nextUse = $phpcsFile->findNext(T_USE, $next + 1); |
85
|
|
|
if ($nextUse && !$this->shouldIgnoreUse($phpcsFile, $nextUse)) { |
|
|
|
|
86
|
|
|
if ($tokens[$nextUse]['line'] > $tokens[$next]['line'] + 1) { |
87
|
|
|
$error = 'There should not be newlines between use statements'; |
88
|
|
|
$fix = $phpcsFile->addFixableError($error, $nextUse, 'NewlineBetweenUse'); |
89
|
|
|
if ($fix) { |
90
|
|
|
$phpcsFile->fixer->replaceToken($nextUse - 1, ''); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// Make sure this USE comes after the first namespace declaration. |
97
|
|
|
$prev = $phpcsFile->findPrevious(T_NAMESPACE, ($stackPtr - 1)); |
98
|
|
View Code Duplication |
if ($prev !== false) { |
|
|
|
|
99
|
|
|
$first = $phpcsFile->findNext(T_NAMESPACE, 1); |
100
|
|
|
if ($prev !== $first) { |
101
|
|
|
$error = 'USE declarations must go after the first namespace declaration'; |
102
|
|
|
$phpcsFile->addError($error, $stackPtr, 'UseAfterNamespace'); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Only interested in the last USE statement from here onwards. |
107
|
|
|
$nextUse = $phpcsFile->findNext(T_USE, ($stackPtr + 1)); |
108
|
|
View Code Duplication |
while ($this->shouldIgnoreUse($phpcsFile, $nextUse)) { |
|
|
|
|
109
|
|
|
$nextUse = $phpcsFile->findNext(T_USE, ($nextUse + 1)); |
110
|
|
|
if ($nextUse === false) { |
111
|
|
|
break; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($nextUse !== false) { |
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1)); |
120
|
|
|
$next = $phpcsFile->findNext(T_WHITESPACE, ($end + 1), null, true); |
121
|
|
|
$diff = ($tokens[$next]['line'] - $tokens[$end]['line'] - 1); |
122
|
|
View Code Duplication |
if ($diff !== 1) { |
|
|
|
|
123
|
|
|
if ($diff < 0) { |
124
|
|
|
$diff = 0; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$error = 'There must be one blank line after the last USE statement; %s found;'; |
128
|
|
|
$data = [$diff]; |
129
|
|
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterLastUse', $data); |
130
|
|
|
if ($fix === true) { |
131
|
|
|
if ($diff === 0) { |
132
|
|
|
$phpcsFile->fixer->addNewline($end); |
133
|
|
|
} else { |
134
|
|
|
$phpcsFile->fixer->beginChangeset(); |
135
|
|
|
for ($i = ($end + 1); $i < $next; $i++) { |
136
|
|
|
if ($tokens[$i]['line'] === $tokens[$next]['line']) { |
137
|
|
|
break; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$phpcsFile->fixer->replaceToken($i, ''); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$phpcsFile->fixer->addNewline($end); |
144
|
|
|
$phpcsFile->fixer->endChangeset(); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.