Skip to content

Commit fcda944

Browse files
committed
refactor(PropertyDeclaration): Use upstream PSR2 sniff to avoid maintaining the code
1 parent fcbab3b commit fcda944

File tree

3 files changed

+44
-93
lines changed

3 files changed

+44
-93
lines changed
Lines changed: 24 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Verifies that properties are declared correctly.
3+
* Verifies that the "var" keyword is not used for class properties.
44
*
55
* @category PHP
66
* @package PHP_CodeSniffer
@@ -10,106 +10,53 @@
1010
namespace Drupal\Sniffs\Classes;
1111

1212
use PHP_CodeSniffer\Files\File;
13-
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
14-
use PHP_CodeSniffer\Util\Tokens;
13+
use PHP_CodeSniffer\Sniffs\Sniff;
1514

1615
/**
17-
* Largely copied from
18-
* \PHP_CodeSniffer\Standards\PSR2\Sniffs\Classes\PropertyDeclarationSniff to have a fixer
19-
* for the var keyword.
16+
* Originally this was a fork of the PSR2 PropertyDeclarationSniff to have a fixer
17+
* for the var keyword. Since we don't want to maintain all the forked code, this
18+
* class was changed to only target the var keyword and provide a fixer for it.
19+
*
20+
* As a replacement PSR2.Classes.PropertyDeclaration is now included in ruleset.xml.
2021
*
2122
* @category PHP
2223
* @package PHP_CodeSniffer
2324
* @link http://pear.php.net/package/PHP_CodeSniffer
2425
*/
25-
class PropertyDeclarationSniff extends AbstractVariableSniff
26+
class PropertyDeclarationSniff implements Sniff
2627
{
2728

2829

2930
/**
30-
* Processes the function tokens within the class.
31-
*
32-
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
33-
* @param int $stackPtr The position where the token was found.
31+
* Returns an array of tokens this test wants to listen for.
3432
*
35-
* @return void
33+
* @return array<int|string>
3634
*/
37-
protected function processMemberVar(File $phpcsFile, $stackPtr)
35+
public function register()
3836
{
39-
$tokens = $phpcsFile->getTokens();
40-
41-
if ($tokens[$stackPtr]['content'][1] === '_') {
42-
$error = 'Property name "%s" should not be prefixed with an underscore to indicate visibility';
43-
$data = [$tokens[$stackPtr]['content']];
44-
$phpcsFile->addWarning($error, $stackPtr, 'Underscore', $data);
45-
}
46-
47-
// Detect multiple properties defined at the same time. Throw an error
48-
// for this, but also only process the first property in the list so we don't
49-
// repeat errors.
50-
$find = Tokens::$scopeModifiers;
51-
$find = array_merge($find, [T_VARIABLE, T_VAR, T_SEMICOLON]);
52-
$prev = $phpcsFile->findPrevious($find, ($stackPtr - 1));
53-
if ($tokens[$prev]['code'] === T_VARIABLE) {
54-
return;
55-
}
56-
57-
if ($tokens[$prev]['code'] === T_VAR) {
58-
$error = 'The var keyword must not be used to declare a property';
59-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'VarUsed');
60-
if ($fix === true) {
61-
$phpcsFile->fixer->replaceToken($prev, 'public');
62-
}
63-
}
37+
return [T_VAR];
6438

65-
$next = $phpcsFile->findNext([T_VARIABLE, T_SEMICOLON], ($stackPtr + 1));
66-
if ($tokens[$next]['code'] === T_VARIABLE) {
67-
$error = 'There must not be more than one property declared per statement';
68-
$phpcsFile->addError($error, $stackPtr, 'Multiple');
69-
}
70-
71-
$modifier = $phpcsFile->findPrevious(Tokens::$scopeModifiers, $stackPtr);
72-
if (($modifier === false) || ($tokens[$modifier]['line'] !== $tokens[$stackPtr]['line'])) {
73-
$error = 'Visibility must be declared on property "%s"';
74-
$data = [$tokens[$stackPtr]['content']];
75-
$phpcsFile->addError($error, $stackPtr, 'ScopeMissing', $data);
76-
}
77-
78-
}//end processMemberVar()
39+
}//end register()
7940

8041

8142
/**
82-
* Processes normal variables.
43+
* Processes this test, when one of its tokens is encountered.
8344
*
84-
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
85-
* @param int $stackPtr The position where the token was found.
45+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
46+
* @param int $stackPtr The position of the current token in
47+
* the stack passed in $tokens.
8648
*
8749
* @return void
8850
*/
89-
protected function processVariable(File $phpcsFile, $stackPtr)
51+
public function process(File $phpcsFile, $stackPtr)
9052
{
91-
/*
92-
We don't care about normal variables.
93-
*/
94-
95-
}//end processVariable()
96-
97-
98-
/**
99-
* Processes variables in double quoted strings.
100-
*
101-
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
102-
* @param int $stackPtr The position where the token was found.
103-
*
104-
* @return void
105-
*/
106-
protected function processVariableInString(File $phpcsFile, $stackPtr)
107-
{
108-
/*
109-
We don't care about normal variables.
110-
*/
53+
$error = 'The var keyword must not be used to declare a property';
54+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'VarUsed');
55+
if ($fix === true) {
56+
$phpcsFile->fixer->replaceToken($stackPtr, 'public');
57+
}
11158

112-
}//end processVariableInString()
59+
}//end process()
11360

11461

11562
}//end class

coder_sniffer/Drupal/ruleset.xml

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,48 +68,52 @@
6868
<rule ref="Generic.WhiteSpace.DisallowTabIndent"/>
6969

7070
<rule ref="MySource.Debug.DebugCode"/>
71+
72+
<!-- PEAR sniffs -->
7173
<rule ref="PEAR.Files.IncludingFile"/>
7274
<!-- Disable some error messages that we do not want. -->
73-
<rule ref="PEAR.Files.IncludingFile.UseIncludeOnce">
74-
<severity>0</severity>
75-
</rule>
7675
<rule ref="PEAR.Files.IncludingFile.UseInclude">
7776
<severity>0</severity>
7877
</rule>
79-
<rule ref="PEAR.Files.IncludingFile.UseRequireOnce">
78+
<rule ref="PEAR.Files.IncludingFile.UseIncludeOnce">
8079
<severity>0</severity>
8180
</rule>
8281
<rule ref="PEAR.Files.IncludingFile.UseRequire">
8382
<severity>0</severity>
8483
</rule>
85-
84+
<rule ref="PEAR.Files.IncludingFile.UseRequireOnce">
85+
<severity>0</severity>
86+
</rule>
8687
<rule ref="PEAR.Functions.FunctionCallSignature"/>
87-
<!-- Disable some error messages that we already cover. -->
88-
<rule ref="PEAR.Functions.FunctionCallSignature.SpaceAfterOpenBracket">
88+
<rule ref="PEAR.Functions.FunctionCallSignature.CloseBracketLine">
8989
<severity>0</severity>
9090
</rule>
91-
<rule ref="PEAR.Functions.FunctionCallSignature.SpaceBeforeCloseBracket">
91+
<rule ref="PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket">
9292
<severity>0</severity>
9393
</rule>
94-
<!-- Disable some error messages that we do not want. -->
95-
<rule ref="PEAR.Functions.FunctionCallSignature.Indent">
94+
<rule ref="PEAR.Functions.FunctionCallSignature.EmptyLine">
9695
<severity>0</severity>
9796
</rule>
98-
<rule ref="PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket">
97+
<rule ref="PEAR.Functions.FunctionCallSignature.Indent">
9998
<severity>0</severity>
10099
</rule>
101-
<rule ref="PEAR.Functions.FunctionCallSignature.CloseBracketLine">
100+
<rule ref="PEAR.Functions.FunctionCallSignature.OpeningIndent">
102101
<severity>0</severity>
103102
</rule>
104-
<rule ref="PEAR.Functions.FunctionCallSignature.EmptyLine">
103+
<!-- Disable some error messages that we already cover. -->
104+
<rule ref="PEAR.Functions.FunctionCallSignature.SpaceAfterOpenBracket">
105105
<severity>0</severity>
106106
</rule>
107-
<rule ref="PEAR.Functions.FunctionCallSignature.OpeningIndent">
107+
<rule ref="PEAR.Functions.FunctionCallSignature.SpaceBeforeCloseBracket">
108108
<severity>0</severity>
109109
</rule>
110-
111110
<rule ref="PEAR.Functions.ValidDefaultValue"/>
112111

112+
<!-- PSR-2 sniffs -->
113+
<rule ref="PSR2.Classes.PropertyDeclaration">
114+
<!-- Already covered by Drupal.Classes.PropertyDeclaration.VarUsed. -->
115+
<exclude name="PSR2.Classes.PropertyDeclaration.VarUsed"/>
116+
</rule>
113117
<rule ref="PSR2.Namespaces.NamespaceDeclaration"/>
114118
<rule ref="PSR2.Namespaces.UseDeclaration"/>
115119

tests/Drupal/Classes/PropertyDeclarationUnitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class PropertyDeclarationUnitTest extends CoderSniffUnitTest
2020
*/
2121
protected function getErrorList(string $testFile): array
2222
{
23-
return [16 => 2];
23+
return [16 => 1];
2424

2525
}//end getErrorList()
2626

0 commit comments

Comments
 (0)