1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Zenify |
5
|
|
|
* Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz) |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace ZenifyCodingStandard\Sniffs\WhiteSpace; |
9
|
|
|
|
10
|
|
|
use PHP_CodeSniffer_File; |
11
|
|
|
use PHP_CodeSniffer_Sniff; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Rules: |
16
|
|
|
* - Between properties and methods should be x empty line(s). |
17
|
|
|
*/ |
18
|
|
|
final class PropertiesMethodsMutualSpacingSniff implements PHP_CodeSniffer_Sniff |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
public $blankLinesInBetween = 2; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
1 |
|
public function register() |
31
|
|
|
{ |
32
|
1 |
|
return [T_VARIABLE]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
1 |
|
public function process(PHP_CodeSniffer_File $file, $position) |
40
|
|
|
{ |
41
|
|
|
// Fix type |
42
|
1 |
|
$this->blankLinesInBetween = (int) $this->blankLinesInBetween; |
43
|
|
|
|
44
|
1 |
|
if ($this->isLastProperty($file, $position) === FALSE) { |
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
if ($this->areMethodsPresent($file, $position) === FALSE) { |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
$tokens = $file->getTokens(); |
53
|
1 |
|
$next = $file->findNext([T_DOC_COMMENT_OPEN_TAG, T_FUNCTION], $position); |
54
|
|
|
|
55
|
1 |
|
$endOfProperty = $this->getEndOfProperty($file, $position); |
56
|
1 |
|
$blankLines = $tokens[$next]['line'] - $tokens[$endOfProperty]['line'] - 1; |
57
|
1 |
|
if ($blankLines !== $this->blankLinesInBetween) { |
58
|
1 |
|
$error = 'Between properties and methods should be %s empty line(s); %s found.'; |
59
|
|
|
$data = [ |
60
|
1 |
|
$this->blankLinesInBetween, |
61
|
1 |
|
$blankLines |
62
|
|
|
]; |
63
|
1 |
|
$file->addError($error, $position, NULL, $data); |
64
|
|
|
} |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param PHP_CodeSniffer_File $file |
70
|
|
|
* @param int $position |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
1 |
|
private function isLastProperty(PHP_CodeSniffer_File $file, $position) |
74
|
|
|
{ |
75
|
1 |
|
if ($this->isInsideMethod($file, $position)) { |
76
|
|
|
return FALSE; |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
$tokens = $file->getTokens(); |
80
|
1 |
|
$next = $file->findNext([T_VARIABLE, T_FUNCTION], $position + 1); |
81
|
1 |
|
if ($tokens[$next]['code'] === T_VARIABLE) { |
82
|
|
|
return FALSE; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return TRUE; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param PHP_CodeSniffer_File $file |
91
|
|
|
* @param int $position |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
1 |
View Code Duplication |
private function isInsideMethod(PHP_CodeSniffer_File $file, $position) |
95
|
|
|
{ |
96
|
1 |
|
$previousMethod = $file->findPrevious(T_FUNCTION, $position); |
97
|
1 |
|
$tokens = $file->getTokens(); |
98
|
1 |
|
if ($tokens[$previousMethod]['code'] === T_FUNCTION) { |
99
|
|
|
return TRUE; |
100
|
|
|
} |
101
|
1 |
|
return FALSE; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param PHP_CodeSniffer_File $file |
107
|
|
|
* @param int $position |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
1 |
View Code Duplication |
private function areMethodsPresent(PHP_CodeSniffer_File $file, $position) |
111
|
|
|
{ |
112
|
1 |
|
$next = $file->findNext(T_FUNCTION, $position + 1); |
113
|
1 |
|
$tokens = $file->getTokens(); |
114
|
1 |
|
if ($tokens[$next]['code'] === T_FUNCTION) { |
115
|
1 |
|
return TRUE; |
116
|
|
|
} |
117
|
|
|
return FALSE; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param PHP_CodeSniffer_File $file |
123
|
|
|
* @param int $position |
124
|
|
|
* @return int |
125
|
|
|
*/ |
126
|
1 |
|
private function getEndOfProperty(PHP_CodeSniffer_File $file, $position) |
127
|
|
|
{ |
128
|
1 |
|
$tokens = $file->getTokens(); |
129
|
|
|
|
130
|
1 |
|
$arrayPosition = $file->findNext(T_ARRAY, $position); |
131
|
1 |
|
if ($tokens[$arrayPosition]['line'] === $tokens[$position]['line']) { |
132
|
|
|
if ($tokens[$arrayPosition]['parenthesis_closer']) { |
133
|
|
|
return $tokens[$arrayPosition]['parenthesis_closer']; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
$openShortArrayPosition = $file->findNext(T_OPEN_SHORT_ARRAY, $position); |
138
|
1 |
|
if ($tokens[$openShortArrayPosition]['line'] === $tokens[$position]['line']) { |
139
|
1 |
|
return $tokens[$openShortArrayPosition]['bracket_closer']; |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
return $position; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|