Skip to content

Commit 1870844

Browse files
authored
Merge pull request #991 from PHPCSStandards/phpcs-4.0/feature/sq-2455-file-getmemberproperties-remove-parse-error-warnings
File::getMemberProperties(): removed parse error warning
2 parents c0bd2bb + ba47d74 commit 1870844

28 files changed

+427
-303
lines changed

src/Files/File.php

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,34 +1791,15 @@ public function getMemberProperties($stackPtr)
17911791
throw new RuntimeException('$stackPtr must be of type T_VARIABLE');
17921792
}
17931793

1794-
$conditions = array_keys($this->tokens[$stackPtr]['conditions']);
1794+
$conditions = $this->tokens[$stackPtr]['conditions'];
1795+
$conditions = array_keys($conditions);
17951796
$ptr = array_pop($conditions);
17961797
if (isset($this->tokens[$ptr]) === false
1797-
|| ($this->tokens[$ptr]['code'] !== T_CLASS
1798-
&& $this->tokens[$ptr]['code'] !== T_ANON_CLASS
1799-
&& $this->tokens[$ptr]['code'] !== T_TRAIT)
1798+
|| isset(Tokens::$ooScopeTokens[$this->tokens[$ptr]['code']]) === false
1799+
|| $this->tokens[$ptr]['code'] === T_ENUM
18001800
) {
1801-
if (isset($this->tokens[$ptr]) === true
1802-
&& ($this->tokens[$ptr]['code'] === T_INTERFACE
1803-
|| $this->tokens[$ptr]['code'] === T_ENUM)
1804-
) {
1805-
// T_VARIABLEs in interfaces/enums can actually be method arguments
1806-
// but they won't be seen as being inside the method because there
1807-
// are no scope openers and closers for abstract methods. If it is in
1808-
// parentheses, we can be pretty sure it is a method argument.
1809-
if (isset($this->tokens[$stackPtr]['nested_parenthesis']) === false
1810-
|| empty($this->tokens[$stackPtr]['nested_parenthesis']) === true
1811-
) {
1812-
$error = 'Possible parse error: %ss may not include member vars';
1813-
$code = sprintf('Internal.ParseError.%sHasMemberVar', ucfirst($this->tokens[$ptr]['content']));
1814-
$data = [strtolower($this->tokens[$ptr]['content'])];
1815-
$this->addWarning($error, $stackPtr, $code, $data);
1816-
return [];
1817-
}
1818-
} else {
1819-
throw new RuntimeException('$stackPtr is not a class member var');
1820-
}
1821-
}//end if
1801+
throw new RuntimeException('$stackPtr is not a class member var');
1802+
}
18221803

18231804
// Make sure it's not a method parameter.
18241805
if (empty($this->tokens[$stackPtr]['nested_parenthesis']) === false) {

src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,10 @@ public function process(File $phpcsFile, $stackPtr)
162162
try {
163163
$props = $phpcsFile->getMemberProperties($i);
164164
} catch (RuntimeException $e) {
165-
// Not an OO property.
165+
// Parse error: property in enum. Ignore.
166166
continue;
167167
}
168168

169-
if (empty($props) === true) {
170-
// Parse error - property in interface or enum. Ignore.
171-
return;
172-
}
173-
174169
// Strip off potential nullable indication.
175170
$type = ltrim($props['type'], '?');
176171

src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc renamed to src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ class DNFTypes {
139139
function DNFReturnTypes ($var): object|(Self&\Package\Other_Class)|sTRINg|false {}
140140
}
141141

142-
// Intentional error, should be ignored by the sniff.
143-
interface PropertiesNotAllowed {
144-
public $notAllowed;
142+
interface PHP84HookedProperty {
143+
public String $readable { get; }
145144
}

src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc.fixed renamed to src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc.fixed

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ class DNFTypes {
139139
function DNFReturnTypes ($var): object|(self&\Package\Other_Class)|string|false {}
140140
}
141141

142-
// Intentional error, should be ignored by the sniff.
143-
interface PropertiesNotAllowed {
144-
public $notAllowed;
142+
interface PHP84HookedProperty {
143+
public string $readable { get; }
145144
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
// Intentional parse error, should be ignored by the sniff.
4+
// This should be the only test in this test case file.
5+
6+
enum PropertiesNotAllowed {
7+
public STRING $notAllowed;
8+
}

src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php

Lines changed: 75 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -26,74 +26,83 @@ final class LowerCaseTypeUnitTest extends AbstractSniffUnitTest
2626
* The key of the array should represent the line number and the value
2727
* should represent the number of errors that should occur on that line.
2828
*
29+
* @param string $testFile The name of the file being tested.
30+
*
2931
* @return array<int, int>
3032
*/
31-
public function getErrorList()
33+
public function getErrorList($testFile='')
3234
{
33-
return [
34-
14 => 1,
35-
15 => 1,
36-
16 => 1,
37-
17 => 1,
38-
18 => 1,
39-
21 => 4,
40-
22 => 3,
41-
23 => 3,
42-
25 => 1,
43-
26 => 2,
44-
27 => 2,
45-
32 => 4,
46-
36 => 1,
47-
37 => 1,
48-
38 => 1,
49-
39 => 1,
50-
43 => 2,
51-
44 => 1,
52-
46 => 1,
53-
49 => 1,
54-
51 => 2,
55-
53 => 1,
56-
55 => 2,
57-
60 => 1,
58-
61 => 1,
59-
62 => 1,
60-
63 => 1,
61-
64 => 1,
62-
65 => 1,
63-
66 => 1,
64-
67 => 1,
65-
68 => 1,
66-
69 => 1,
67-
71 => 3,
68-
72 => 2,
69-
73 => 3,
70-
74 => 3,
71-
78 => 3,
72-
82 => 2,
73-
85 => 1,
74-
94 => 5,
75-
96 => 4,
76-
105 => 1,
77-
106 => 1,
78-
107 => 1,
79-
111 => 1,
80-
112 => 1,
81-
113 => 1,
82-
114 => 1,
83-
117 => 1,
84-
118 => 1,
85-
119 => 1,
86-
122 => 3,
87-
123 => 2,
88-
124 => 3,
89-
125 => 3,
90-
129 => 2,
91-
131 => 1,
92-
134 => 1,
93-
135 => 1,
94-
136 => 1,
95-
139 => 2,
96-
];
35+
switch ($testFile) {
36+
case 'LowerCaseTypeUnitTest.1.inc':
37+
return [
38+
14 => 1,
39+
15 => 1,
40+
16 => 1,
41+
17 => 1,
42+
18 => 1,
43+
21 => 4,
44+
22 => 3,
45+
23 => 3,
46+
25 => 1,
47+
26 => 2,
48+
27 => 2,
49+
32 => 4,
50+
36 => 1,
51+
37 => 1,
52+
38 => 1,
53+
39 => 1,
54+
43 => 2,
55+
44 => 1,
56+
46 => 1,
57+
49 => 1,
58+
51 => 2,
59+
53 => 1,
60+
55 => 2,
61+
60 => 1,
62+
61 => 1,
63+
62 => 1,
64+
63 => 1,
65+
64 => 1,
66+
65 => 1,
67+
66 => 1,
68+
67 => 1,
69+
68 => 1,
70+
69 => 1,
71+
71 => 3,
72+
72 => 2,
73+
73 => 3,
74+
74 => 3,
75+
78 => 3,
76+
82 => 2,
77+
85 => 1,
78+
94 => 5,
79+
96 => 4,
80+
105 => 1,
81+
106 => 1,
82+
107 => 1,
83+
111 => 1,
84+
112 => 1,
85+
113 => 1,
86+
114 => 1,
87+
117 => 1,
88+
118 => 1,
89+
119 => 1,
90+
122 => 3,
91+
123 => 2,
92+
124 => 3,
93+
125 => 3,
94+
129 => 2,
95+
131 => 1,
96+
134 => 1,
97+
135 => 1,
98+
136 => 1,
99+
139 => 2,
100+
143 => 1,
101+
];
102+
103+
default:
104+
return [];
105+
}//end switch
97106

98107
}//end getErrorList()
99108

@@ -108,8 +117,7 @@ public function getErrorList()
108117
*/
109118
public function getWarningList()
110119
{
111-
// Warning from getMemberProperties() about parse error.
112-
return [144 => 1];
120+
return [];
113121

114122
}//end getWarningList()
115123

src/Standards/PEAR/Sniffs/NamingConventions/ValidVariableNameSniff.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventions;
1111

12+
use PHP_CodeSniffer\Exceptions\RuntimeException;
1213
use PHP_CodeSniffer\Files\File;
1314
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
1415

@@ -27,13 +28,14 @@ class ValidVariableNameSniff extends AbstractVariableSniff
2728
*/
2829
protected function processMemberVar(File $phpcsFile, $stackPtr)
2930
{
30-
$tokens = $phpcsFile->getTokens();
31-
32-
$memberProps = $phpcsFile->getMemberProperties($stackPtr);
33-
if (empty($memberProps) === true) {
31+
try {
32+
$memberProps = $phpcsFile->getMemberProperties($stackPtr);
33+
} catch (RuntimeException $e) {
34+
// Parse error: property in enum. Ignore.
3435
return;
3536
}
3637

38+
$tokens = $phpcsFile->getTokens();
3739
$memberName = ltrim($tokens[$stackPtr]['content'], '$');
3840
$scope = $memberProps['scope'];
3941
$scopeSpecified = $memberProps['scope_specified'];

src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.inc renamed to src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,8 @@ $util->setLogger(
9999
private $varName = 'hello';
100100
private $_varName = 'hello';
101101
});
102+
103+
interface PHP84HookedProperty {
104+
public $thisisfine { get; }
105+
public $_underscore { get; }
106+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
// Intentional parse error, should be ignored by the sniff.
4+
// This should be the only test in this test case file.
5+
6+
enum PropertiesNotAllowed {
7+
public $_notAllowed;
8+
}

src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,28 @@ final class ValidVariableNameUnitTest extends AbstractSniffUnitTest
2626
* The key of the array should represent the line number and the value
2727
* should represent the number of errors that should occur on that line.
2828
*
29+
* @param string $testFile The name of the file being tested.
30+
*
2931
* @return array<int, int>
3032
*/
31-
public function getErrorList()
33+
public function getErrorList($testFile='')
3234
{
33-
return [
34-
12 => 1,
35-
17 => 1,
36-
22 => 1,
37-
92 => 1,
38-
93 => 1,
39-
94 => 1,
40-
99 => 1,
41-
];
35+
switch ($testFile) {
36+
case 'ValidVariableNameUnitTest.1.inc':
37+
return [
38+
12 => 1,
39+
17 => 1,
40+
22 => 1,
41+
92 => 1,
42+
93 => 1,
43+
94 => 1,
44+
99 => 1,
45+
105 => 1,
46+
];
47+
48+
default:
49+
return [];
50+
}//end switch
4251

4352
}//end getErrorList()
4453

src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ class PropertyDeclarationSniff extends AbstractVariableSniff
2828
*/
2929
protected function processMemberVar(File $phpcsFile, $stackPtr)
3030
{
31+
try {
32+
$propertyInfo = $phpcsFile->getMemberProperties($stackPtr);
33+
} catch (Exception $e) {
34+
// Parse error: property in enum. Ignore.
35+
return;
36+
}
37+
3138
$tokens = $phpcsFile->getTokens();
3239

3340
if ($tokens[$stackPtr]['content'][1] === '_') {
@@ -63,16 +70,6 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
6370
$phpcsFile->addError($error, $stackPtr, 'Multiple');
6471
}
6572

66-
try {
67-
$propertyInfo = $phpcsFile->getMemberProperties($stackPtr);
68-
if (empty($propertyInfo) === true) {
69-
return;
70-
}
71-
} catch (Exception $e) {
72-
// Turns out not to be a property after all.
73-
return;
74-
}
75-
7673
if ($propertyInfo['type'] !== '') {
7774
$typeToken = $propertyInfo['type_end_token'];
7875
$error = 'There must be 1 space after the property type declaration; %s found';

src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc renamed to src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.1.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,8 @@ class FinalProperties {
9696
public FINAL ?int $wrongOrder1;
9797
static protected final ?string $wrongOrder2;
9898
}
99+
100+
interface PHP84HookedProperty {
101+
public $thisisfine { get; }
102+
public $_underscore { get; }
103+
}

src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc.fixed renamed to src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.1.inc.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,8 @@ class FinalProperties {
9393
FINAL public ?int $wrongOrder1;
9494
final protected static ?string $wrongOrder2;
9595
}
96+
97+
interface PHP84HookedProperty {
98+
public $thisisfine { get; }
99+
public $_underscore { get; }
100+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
// Intentional parse error, should be ignored by the sniff.
4+
// This should be the only test in this test case file.
5+
6+
enum PropertiesNotAllowed {
7+
public $_notAllowed;
8+
}

0 commit comments

Comments
 (0)