Skip to content

Commit 187cc11

Browse files
✨ Declare const before property
1 parent e08aca0 commit 187cc11

File tree

3 files changed

+83
-8
lines changed

3 files changed

+83
-8
lines changed

SymfonyCustom/Sniffs/Classes/PropertyDeclarationSniff.php

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,27 @@ public function register(): array
2828
*/
2929
public function process(File $phpcsFile, $stackPtr): void
3030
{
31-
$tokens = $phpcsFile->getTokens();
31+
$this->processProperty($phpcsFile, $stackPtr);
32+
$this->processFunction($phpcsFile, $stackPtr);
33+
}
3234

33-
$end = null;
34-
if (isset($tokens[$stackPtr]['scope_closer'])) {
35-
$end = $tokens[$stackPtr]['scope_closer'];
36-
}
35+
/**
36+
* @param File $phpcsFile
37+
* @param int $stackPtr
38+
*
39+
* @return void
40+
*/
41+
private function processFunction(File $phpcsFile, int $stackPtr): void
42+
{
43+
$tokens = $phpcsFile->getTokens();
44+
$end = $tokens[$stackPtr]['scope_closer'] ?? null;
3745

3846
$function = $phpcsFile->findNext(T_FUNCTION, $stackPtr, $end);
3947
if (false === $function) {
4048
return;
4149
}
4250

43-
$wantedTokens = [T_PUBLIC, T_PROTECTED, T_PRIVATE, T_ANON_CLASS];
51+
$wantedTokens = [T_CONST, T_PUBLIC, T_PROTECTED, T_PRIVATE, T_ANON_CLASS];
4452
$scope = $phpcsFile->findNext($wantedTokens, $function + 1, $end);
4553

4654
while (false !== $scope) {
@@ -50,8 +58,57 @@ public function process(File $phpcsFile, $stackPtr): void
5058
continue;
5159
}
5260

53-
if (T_VARIABLE === $tokens[$scope + 2]['code']) {
54-
$phpcsFile->addError('Declare class properties before methods', $scope, 'Invalid');
61+
if (T_CONST === $tokens[$scope]['code']) {
62+
$phpcsFile->addError('Declare class constants before methods', $scope, 'ConstBeforeFunction');
63+
} elseif (T_VARIABLE === $tokens[$scope + 2]['code']) {
64+
$phpcsFile->addError('Declare class properties before methods', $scope, 'PropertyBeforeFunction');
65+
}
66+
67+
$scope = $phpcsFile->findNext($wantedTokens, $scope + 1, $end);
68+
}
69+
}
70+
71+
/**
72+
* @param File $phpcsFile
73+
* @param int $stackPtr
74+
*
75+
* @return void
76+
*/
77+
private function processProperty(File $phpcsFile, int $stackPtr): void
78+
{
79+
$tokens = $phpcsFile->getTokens();
80+
$end = $tokens[$stackPtr]['scope_closer'] ?? null;
81+
82+
$wantedTokens = [T_PUBLIC, T_PROTECTED, T_PRIVATE, T_ANON_CLASS];
83+
$scope = $phpcsFile->findNext($wantedTokens, $stackPtr + 1, $end);
84+
85+
while (false !== $scope && T_VARIABLE !== $tokens[$scope + 2]['code']) {
86+
if (T_ANON_CLASS === $tokens[$scope]['code']) {
87+
$scope = $tokens[$scope]['scope_closer'];
88+
89+
continue;
90+
}
91+
92+
$scope = $phpcsFile->findNext($wantedTokens, $scope + 1, $end);
93+
}
94+
95+
if (false === $scope) {
96+
return;
97+
}
98+
$property = $scope + 2;
99+
100+
$wantedTokens = [T_CONST, T_ANON_CLASS];
101+
$scope = $phpcsFile->findNext($wantedTokens, $property + 1, $end);
102+
103+
while (false !== $scope) {
104+
if (T_ANON_CLASS === $tokens[$scope]['code']) {
105+
$scope = $tokens[$scope]['scope_closer'];
106+
107+
continue;
108+
}
109+
110+
if (T_CONST === $tokens[$scope]['code']) {
111+
$phpcsFile->addError('Declare class property before const', $scope, 'ConstBeforeProperty');
55112
}
56113

57114
$scope = $phpcsFile->findNext($wantedTokens, $scope + 1, $end);

SymfonyCustom/Tests/Classes/PropertyDeclarationUnitTest.inc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,19 @@ class MoreBar
4444
};
4545
}
4646
}
47+
48+
class BarWithConst
49+
{
50+
public function functionTest() {
51+
52+
}
53+
54+
const FOO = 1;
55+
}
56+
57+
class BarWithConst2
58+
{
59+
public $foo;
60+
61+
const FOO = 1;
62+
}

SymfonyCustom/Tests/Classes/PropertyDeclarationUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ protected function getErrorList(): array
1919
return [
2020
9 => 1,
2121
43 => 1,
22+
54 => 1,
23+
61 => 1,
2224
];
2325
}
2426

0 commit comments

Comments
 (0)