1 | <?php |
||
22 | final class UseDeclarationSniff extends PSR2_Sniffs_Namespaces_UseDeclarationSniff |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * @var int |
||
27 | */ |
||
28 | public $blankLinesAfterUseStatement = 2; |
||
29 | |||
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | 1 | public function register() |
|
35 | { |
||
36 | 1 | return [T_USE]; |
|
37 | } |
||
38 | |||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | 1 | public function process(PHP_CodeSniffer_File $file, $position) |
|
44 | { |
||
45 | // Fix types |
||
46 | 1 | $this->blankLinesAfterUseStatement = (int) $this->blankLinesAfterUseStatement; |
|
47 | |||
48 | 1 | if ($this->shouldIgnoreUse($file, $position) === TRUE) { |
|
49 | return; |
||
50 | } |
||
51 | |||
52 | 1 | $this->checkIfSingleSpaceAfterUseKeyword($file, $position); |
|
53 | 1 | $this->checkIfOneUseDeclarationPerStatement($file, $position); |
|
54 | 1 | $this->checkIfUseComesAfterNamespaceDeclaration($file, $position); |
|
55 | |||
56 | // Only interested in the last USE statement from here onwards. |
||
57 | 1 | $nextUse = $file->findNext(T_USE, ($position + 1)); |
|
58 | 1 | while ($this->shouldIgnoreUse($file, $nextUse) === TRUE) { |
|
59 | $nextUse = $file->findNext(T_USE, ($nextUse + 1)); |
||
60 | if ($nextUse === FALSE) { |
||
61 | break; |
||
62 | } |
||
63 | } |
||
64 | |||
65 | 1 | if ($nextUse !== FALSE) { |
|
66 | 1 | return; |
|
67 | } |
||
68 | |||
69 | 1 | $this->checkBlankLineAfterLastUseStatement($file, $position); |
|
70 | 1 | } |
|
71 | |||
72 | |||
73 | /** |
||
74 | * Check if this use statement is part of the namespace block. |
||
75 | * @param PHP_CodeSniffer_File $file |
||
76 | * @param int|bool $position |
||
77 | */ |
||
78 | 1 | private function shouldIgnoreUse(PHP_CodeSniffer_File $file, $position) : bool |
|
95 | |||
96 | |||
97 | 1 | private function checkIfSingleSpaceAfterUseKeyword(PHP_CodeSniffer_File $file, int $position) |
|
105 | |||
106 | |||
107 | 1 | private function checkIfOneUseDeclarationPerStatement(PHP_CodeSniffer_File $file, int $position) |
|
116 | |||
117 | |||
118 | 1 | private function checkIfUseComesAfterNamespaceDeclaration(PHP_CodeSniffer_File $file, int $position) |
|
129 | |||
130 | |||
131 | 1 | private function checkBlankLineAfterLastUseStatement(PHP_CodeSniffer_File $file, int $position) |
|
147 | |||
148 | } |
||
149 |