Skip to content

File::getMemberProperties(): removed parse error warning #991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 6 additions & 25 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -1791,34 +1791,15 @@ public function getMemberProperties($stackPtr)
throw new RuntimeException('$stackPtr must be of type T_VARIABLE');
}

$conditions = array_keys($this->tokens[$stackPtr]['conditions']);
$conditions = $this->tokens[$stackPtr]['conditions'];
$conditions = array_keys($conditions);
$ptr = array_pop($conditions);
if (isset($this->tokens[$ptr]) === false
|| ($this->tokens[$ptr]['code'] !== T_CLASS
&& $this->tokens[$ptr]['code'] !== T_ANON_CLASS
&& $this->tokens[$ptr]['code'] !== T_TRAIT)
|| isset(Tokens::$ooScopeTokens[$this->tokens[$ptr]['code']]) === false
|| $this->tokens[$ptr]['code'] === T_ENUM
) {
if (isset($this->tokens[$ptr]) === true
&& ($this->tokens[$ptr]['code'] === T_INTERFACE
|| $this->tokens[$ptr]['code'] === T_ENUM)
) {
// T_VARIABLEs in interfaces/enums can actually be method arguments
// but they won't be seen as being inside the method because there
// are no scope openers and closers for abstract methods. If it is in
// parentheses, we can be pretty sure it is a method argument.
if (isset($this->tokens[$stackPtr]['nested_parenthesis']) === false
|| empty($this->tokens[$stackPtr]['nested_parenthesis']) === true
) {
$error = 'Possible parse error: %ss may not include member vars';
$code = sprintf('Internal.ParseError.%sHasMemberVar', ucfirst($this->tokens[$ptr]['content']));
$data = [strtolower($this->tokens[$ptr]['content'])];
$this->addWarning($error, $stackPtr, $code, $data);
return [];
}
} else {
throw new RuntimeException('$stackPtr is not a class member var');
}
}//end if
throw new RuntimeException('$stackPtr is not a class member var');
}

// Make sure it's not a method parameter.
if (empty($this->tokens[$stackPtr]['nested_parenthesis']) === false) {
Expand Down
7 changes: 1 addition & 6 deletions src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,10 @@ public function process(File $phpcsFile, $stackPtr)
try {
$props = $phpcsFile->getMemberProperties($i);
} catch (RuntimeException $e) {
// Not an OO property.
// Parse error: property in enum. Ignore.
continue;
}

if (empty($props) === true) {
// Parse error - property in interface or enum. Ignore.
return;
}

// Strip off potential nullable indication.
$type = ltrim($props['type'], '?');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ class DNFTypes {
function DNFReturnTypes ($var): object|(Self&\Package\Other_Class)|sTRINg|false {}
}

// Intentional error, should be ignored by the sniff.
interface PropertiesNotAllowed {
public $notAllowed;
interface PHP84HookedProperty {
public String $readable { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ class DNFTypes {
function DNFReturnTypes ($var): object|(self&\Package\Other_Class)|string|false {}
}

// Intentional error, should be ignored by the sniff.
interface PropertiesNotAllowed {
public $notAllowed;
interface PHP84HookedProperty {
public string $readable { get; }
}
8 changes: 8 additions & 0 deletions src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

// Intentional parse error, should be ignored by the sniff.
// This should be the only test in this test case file.

enum PropertiesNotAllowed {
public STRING $notAllowed;
}
142 changes: 75 additions & 67 deletions src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,74 +26,83 @@ final class LowerCaseTypeUnitTest extends AbstractSniffUnitTest
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
public function getErrorList()
public function getErrorList($testFile='')
{
return [
14 => 1,
15 => 1,
16 => 1,
17 => 1,
18 => 1,
21 => 4,
22 => 3,
23 => 3,
25 => 1,
26 => 2,
27 => 2,
32 => 4,
36 => 1,
37 => 1,
38 => 1,
39 => 1,
43 => 2,
44 => 1,
46 => 1,
49 => 1,
51 => 2,
53 => 1,
55 => 2,
60 => 1,
61 => 1,
62 => 1,
63 => 1,
64 => 1,
65 => 1,
66 => 1,
67 => 1,
68 => 1,
69 => 1,
71 => 3,
72 => 2,
73 => 3,
74 => 3,
78 => 3,
82 => 2,
85 => 1,
94 => 5,
96 => 4,
105 => 1,
106 => 1,
107 => 1,
111 => 1,
112 => 1,
113 => 1,
114 => 1,
117 => 1,
118 => 1,
119 => 1,
122 => 3,
123 => 2,
124 => 3,
125 => 3,
129 => 2,
131 => 1,
134 => 1,
135 => 1,
136 => 1,
139 => 2,
];
switch ($testFile) {
case 'LowerCaseTypeUnitTest.1.inc':
return [
14 => 1,
15 => 1,
16 => 1,
17 => 1,
18 => 1,
21 => 4,
22 => 3,
23 => 3,
25 => 1,
26 => 2,
27 => 2,
32 => 4,
36 => 1,
37 => 1,
38 => 1,
39 => 1,
43 => 2,
44 => 1,
46 => 1,
49 => 1,
51 => 2,
53 => 1,
55 => 2,
60 => 1,
61 => 1,
62 => 1,
63 => 1,
64 => 1,
65 => 1,
66 => 1,
67 => 1,
68 => 1,
69 => 1,
71 => 3,
72 => 2,
73 => 3,
74 => 3,
78 => 3,
82 => 2,
85 => 1,
94 => 5,
96 => 4,
105 => 1,
106 => 1,
107 => 1,
111 => 1,
112 => 1,
113 => 1,
114 => 1,
117 => 1,
118 => 1,
119 => 1,
122 => 3,
123 => 2,
124 => 3,
125 => 3,
129 => 2,
131 => 1,
134 => 1,
135 => 1,
136 => 1,
139 => 2,
143 => 1,
];

default:
return [];
}//end switch

}//end getErrorList()

Expand All @@ -108,8 +117,7 @@ public function getErrorList()
*/
public function getWarningList()
{
// Warning from getMemberProperties() about parse error.
return [144 => 1];
return [];

}//end getWarningList()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventions;

use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;

Expand All @@ -27,13 +28,14 @@ class ValidVariableNameSniff extends AbstractVariableSniff
*/
protected function processMemberVar(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

$memberProps = $phpcsFile->getMemberProperties($stackPtr);
if (empty($memberProps) === true) {
try {
$memberProps = $phpcsFile->getMemberProperties($stackPtr);
} catch (RuntimeException $e) {
// Parse error: property in enum. Ignore.
return;
}

$tokens = $phpcsFile->getTokens();
$memberName = ltrim($tokens[$stackPtr]['content'], '$');
$scope = $memberProps['scope'];
$scopeSpecified = $memberProps['scope_specified'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,8 @@ $util->setLogger(
private $varName = 'hello';
private $_varName = 'hello';
});

interface PHP84HookedProperty {
public $thisisfine { get; }
public $_underscore { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

// Intentional parse error, should be ignored by the sniff.
// This should be the only test in this test case file.

enum PropertiesNotAllowed {
public $_notAllowed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,28 @@ final class ValidVariableNameUnitTest extends AbstractSniffUnitTest
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
public function getErrorList()
public function getErrorList($testFile='')
{
return [
12 => 1,
17 => 1,
22 => 1,
92 => 1,
93 => 1,
94 => 1,
99 => 1,
];
switch ($testFile) {
case 'ValidVariableNameUnitTest.1.inc':
return [
12 => 1,
17 => 1,
22 => 1,
92 => 1,
93 => 1,
94 => 1,
99 => 1,
105 => 1,
];

default:
return [];
}//end switch

}//end getErrorList()

Expand Down
17 changes: 7 additions & 10 deletions src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class PropertyDeclarationSniff extends AbstractVariableSniff
*/
protected function processMemberVar(File $phpcsFile, $stackPtr)
{
try {
$propertyInfo = $phpcsFile->getMemberProperties($stackPtr);
} catch (Exception $e) {
// Parse error: property in enum. Ignore.
return;
}

$tokens = $phpcsFile->getTokens();

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

try {
$propertyInfo = $phpcsFile->getMemberProperties($stackPtr);
if (empty($propertyInfo) === true) {
return;
}
} catch (Exception $e) {
// Turns out not to be a property after all.
return;
}

if ($propertyInfo['type'] !== '') {
$typeToken = $propertyInfo['type_end_token'];
$error = 'There must be 1 space after the property type declaration; %s found';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,8 @@ class FinalProperties {
public FINAL ?int $wrongOrder1;
static protected final ?string $wrongOrder2;
}

interface PHP84HookedProperty {
public $thisisfine { get; }
public $_underscore { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,8 @@ class FinalProperties {
FINAL public ?int $wrongOrder1;
final protected static ?string $wrongOrder2;
}

interface PHP84HookedProperty {
public $thisisfine { get; }
public $_underscore { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

// Intentional parse error, should be ignored by the sniff.
// This should be the only test in this test case file.

enum PropertiesNotAllowed {
public $_notAllowed;
}
Loading