Skip to content

PHP 8.4 | Add tokenization of asymmetric visibility #871

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
12 changes: 12 additions & 0 deletions src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,15 @@ class SkipOverPHP84FinalProperties {
final MyType|FALSE $propA;
private static final NULL|MyClass $propB;
}

// PHP 8.4 asymmetric visibility
class WithAsym {

private(set) NULL|TRUE $asym1 = TRUE;

public private(set) ?bool $asym2 = FALSE;

protected(set) FALSE|string|null $asym3 = NULL;

public protected(set) Type|NULL|bool $asym4 = TRUE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,15 @@ class SkipOverPHP84FinalProperties {
final MyType|FALSE $propA;
private static final NULL|MyClass $propB;
}

// PHP 8.4 asymmetric visibility
class WithAsym {

private(set) NULL|TRUE $asym1 = true;

public private(set) ?bool $asym2 = false;

protected(set) FALSE|string|null $asym3 = null;

public protected(set) Type|NULL|bool $asym4 = true;
}
4 changes: 4 additions & 0 deletions src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public function getErrorList($testFile='')
129 => 1,
149 => 1,
153 => 1,
167 => 1,
169 => 1,
171 => 1,
173 => 1,
];

case 'LowerCaseConstantUnitTest.js':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ enum SomeEnum {
public const BAR = 'bar';
const BAZ = 'baz';
}

// Don't break on asymmetric visibility
class WithAsym {

private(set) string $asym1;

public private(set) string $asym2;

protected(set) string $asym3;

public protected(set) string $asym4;
}
14 changes: 14 additions & 0 deletions src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,17 @@ class FinalProperties {
public FINAL ?int $wrongOrder1;
static protected final ?string $wrongOrder2;
}

class AsymmetricVisibility {
private(set) int $foo,
$bar,
$var = 5;

public private(set) readonly ?string $spaces;

protected(set) array $unfixed;

protected(set) public int $wrongOrder1;

private(set) protected ?string $wrongOrder2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,17 @@ class FinalProperties {
FINAL public ?int $wrongOrder1;
final protected static ?string $wrongOrder2;
}

class AsymmetricVisibility {
private(set) int $foo,
$bar,
$var = 5;

public private(set) readonly ?string $spaces;

protected(set) array $unfixed;

protected(set) public int $wrongOrder1;

private(set) protected ?string $wrongOrder2;
}
65 changes: 35 additions & 30 deletions src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,41 @@ final class PropertyDeclarationUnitTest extends AbstractSniffUnitTest
public function getErrorList()
{
return [
7 => 1,
9 => 2,
10 => 1,
11 => 1,
17 => 1,
18 => 1,
23 => 1,
38 => 1,
41 => 1,
42 => 1,
50 => 2,
51 => 1,
55 => 1,
56 => 1,
61 => 1,
62 => 1,
68 => 1,
69 => 1,
71 => 1,
72 => 1,
76 => 1,
80 => 1,
82 => 1,
84 => 1,
86 => 1,
90 => 1,
94 => 1,
95 => 1,
96 => 1,
97 => 2,
7 => 1,
9 => 2,
10 => 1,
11 => 1,
17 => 1,
18 => 1,
23 => 1,
38 => 1,
41 => 1,
42 => 1,
50 => 2,
51 => 1,
55 => 1,
56 => 1,
61 => 1,
62 => 1,
68 => 1,
69 => 1,
71 => 1,
72 => 1,
76 => 1,
80 => 1,
82 => 1,
84 => 1,
86 => 1,
90 => 1,
94 => 1,
95 => 1,
96 => 1,
97 => 2,
101 => 2,
105 => 1,
107 => 1,
109 => 1,
111 => 1,
];

}//end getErrorList()
Expand Down
47 changes: 47 additions & 0 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,11 @@ class PHP extends Tokenizer
T_PLUS_EQUAL => 2,
T_PRINT => 5,
T_PRIVATE => 7,
T_PRIVATE_SET => 12,
T_PUBLIC => 6,
T_PUBLIC_SET => 11,
T_PROTECTED => 9,
T_PROTECTED_SET => 14,
T_READONLY => 8,
T_REQUIRE => 7,
T_REQUIRE_ONCE => 12,
Expand Down Expand Up @@ -1265,6 +1268,49 @@ protected function tokenize($string)
}
}//end if

/*
Asymmetric visibility for PHP < 8.4
*/

if ($tokenIsArray === true
&& in_array($token[0], [T_PUBLIC, T_PROTECTED, T_PRIVATE], true) === true
&& ($stackPtr + 3) < $numTokens
&& $tokens[($stackPtr + 1)] === '('
&& is_array($tokens[($stackPtr + 2)]) === true
&& $tokens[($stackPtr + 2)][0] === T_STRING
&& strtolower($tokens[($stackPtr + 2)][1]) === 'set'
&& $tokens[($stackPtr + 3)] === ')'
) {
$newToken = [];
if ($token[0] === T_PUBLIC) {
$oldCode = 'T_PUBLIC';
$newToken['code'] = T_PUBLIC_SET;
$newToken['type'] = 'T_PUBLIC_SET';
} else if ($token[0] === T_PROTECTED) {
$oldCode = 'T_PROTECTED';
$newToken['code'] = T_PROTECTED_SET;
$newToken['type'] = 'T_PROTECTED_SET';
} else {
$oldCode = 'T_PRIVATE';
$newToken['code'] = T_PRIVATE_SET;
$newToken['type'] = 'T_PRIVATE_SET';
}

$newToken['content'] = $token[1].'('.$tokens[($stackPtr + 2)][1].')';
$finalTokens[$newStackPtr] = $newToken;
$newStackPtr++;

if (PHP_CODESNIFFER_VERBOSITY > 1) {
$newCode = $newToken['type'];
echo "\t\t* tokens from $stackPtr changed from $oldCode to $newCode".PHP_EOL;
}

// We processed an extra 3 tokens, for `(`, `set`, and `)`.
$stackPtr += 3;

continue;
}//end if

/*
As of PHP 8.0 fully qualified, partially qualified and namespace relative
identifier names are tokenized differently.
Expand Down Expand Up @@ -2189,6 +2235,7 @@ protected function tokenize($string)
if ($tokenType === T_FUNCTION
|| $tokenType === T_FN
|| isset(Tokens::$methodPrefixes[$tokenType]) === true
|| isset(Tokens::$scopeModifiers[$tokenType]) === true
|| $tokenType === T_VAR
|| $tokenType === T_READONLY
) {
Expand Down
Loading