Skip to content

Commit 1c82e07

Browse files
committed
Merge branch 'feature/file-getmemberprops-throw-exception-for-enums' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 7109f0c + 152fa05 commit 1c82e07

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/Files/File.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,23 +1791,26 @@ public function getMemberProperties($stackPtr)
17911791
&& $this->tokens[$ptr]['code'] !== T_TRAIT)
17921792
) {
17931793
if (isset($this->tokens[$ptr]) === true
1794-
&& $this->tokens[$ptr]['code'] === T_INTERFACE
1794+
&& ($this->tokens[$ptr]['code'] === T_INTERFACE
1795+
|| $this->tokens[$ptr]['code'] === T_ENUM)
17951796
) {
1796-
// T_VARIABLEs in interfaces can actually be method arguments
1797+
// T_VARIABLEs in interfaces/enums can actually be method arguments
17971798
// but they wont be seen as being inside the method because there
17981799
// are no scope openers and closers for abstract methods. If it is in
17991800
// parentheses, we can be pretty sure it is a method argument.
18001801
if (isset($this->tokens[$stackPtr]['nested_parenthesis']) === false
18011802
|| empty($this->tokens[$stackPtr]['nested_parenthesis']) === true
18021803
) {
1803-
$error = 'Possible parse error: interfaces may not include member vars';
1804-
$this->addWarning($error, $stackPtr, 'Internal.ParseError.InterfaceHasMemberVar');
1804+
$error = 'Possible parse error: %ss may not include member vars';
1805+
$code = sprintf('Internal.ParseError.%sHasMemberVar', ucfirst($this->tokens[$ptr]['content']));
1806+
$data = [strtolower($this->tokens[$ptr]['content'])];
1807+
$this->addWarning($error, $stackPtr, $code, $data);
18051808
return [];
18061809
}
18071810
} else {
18081811
throw new RuntimeException('$stackPtr is not a class member var');
18091812
}
1810-
}
1813+
}//end if
18111814

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

tests/Core/File/GetMemberPropertiesTest.inc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,18 @@ $anon = class {
261261
]
262262
private mixed $baz;
263263
};
264+
265+
enum Suit
266+
{
267+
/* testEnumProperty */
268+
protected $anonymous;
269+
}
270+
271+
enum Direction implements ArrayAccess
272+
{
273+
case Up;
274+
case Down;
275+
276+
/* testEnumMethodParamNotProperty */
277+
public function offsetGet($val) { ... }
278+
}

tests/Core/File/GetMemberPropertiesTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,10 @@ public function dataGetMemberProperties()
662662
'nullable_type' => false,
663663
],
664664
],
665+
[
666+
'/* testEnumProperty */',
667+
[],
668+
],
665669
];
666670

667671
}//end dataGetMemberProperties()
@@ -703,6 +707,7 @@ public function dataNotClassProperty()
703707
['/* testGlobalVariable */'],
704708
['/* testNestedMethodParam 1 */'],
705709
['/* testNestedMethodParam 2 */'],
710+
['/* testEnumMethodParamNotProperty */'],
706711
];
707712

708713
}//end dataNotClassProperty()

0 commit comments

Comments
 (0)