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

Merged
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
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 @@ -50,8 +50,14 @@ public function process(File $phpcsFile, $stackPtr)
$ignore = Tokens::$emptyTokens;
$ignore[] = T_FINAL;

$validVisibility = [
T_PRIVATE => T_PRIVATE,
T_PUBLIC => T_PUBLIC,
T_PROTECTED => T_PROTECTED,
];

$prev = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
if (isset(Tokens::$scopeModifiers[$tokens[$prev]['code']]) === true) {
if (isset($validVisibility[$tokens[$prev]['code']]) === true) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Intentional parse error - unsupported asym visibility used on class constant.
// Testing that the sniff will flags this as the constant doesn't have a valid visibility.
class Foo {
public(set) const BAR = 'bar';
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,28 @@ public function getErrorList()
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
public function getWarningList()
public function getWarningList($testFile='')
{
return [
4 => 1,
12 => 1,
21 => 1,
];
switch ($testFile) {
case 'ConstantVisibilityUnitTest.1.inc':
return [
4 => 1,
12 => 1,
21 => 1,
];

case 'ConstantVisibilityUnitTest.2.inc':
return [
6 => 1,
];

default:
return [];
}

}//end getWarningList()

Expand Down
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
22 changes: 19 additions & 3 deletions src/Util/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@
define('T_ENUM', 'PHPCS_T_ENUM');
}

// Some PHP 8.4 tokens, replicated for lower versions.
if (defined('T_PUBLIC_SET') === false) {
define('T_PUBLIC_SET', 'PHPCS_T_PUBLIC_SET');
}

if (defined('T_PROTECTED_SET') === false) {
define('T_PROTECTED_SET', 'PHPCS_T_PROTECTED_SET');
}

if (defined('T_PRIVATE_SET') === false) {
define('T_PRIVATE_SET', 'PHPCS_T_PRIVATE_SET');
}

// Tokens used for parsing doc blocks.
define('T_DOC_COMMENT_STAR', 'PHPCS_T_DOC_COMMENT_STAR');
define('T_DOC_COMMENT_WHITESPACE', 'PHPCS_T_DOC_COMMENT_WHITESPACE');
Expand Down Expand Up @@ -463,9 +476,12 @@ final class Tokens
* @var array<int|string, int|string>
*/
public static $scopeModifiers = [
T_PRIVATE => T_PRIVATE,
T_PUBLIC => T_PUBLIC,
T_PROTECTED => T_PROTECTED,
T_PRIVATE => T_PRIVATE,
T_PUBLIC => T_PUBLIC,
T_PROTECTED => T_PROTECTED,
T_PUBLIC_SET => T_PUBLIC_SET,
T_PROTECTED_SET => T_PROTECTED_SET,
T_PRIVATE_SET => T_PRIVATE_SET,
];

/**
Expand Down
60 changes: 60 additions & 0 deletions tests/Core/Tokenizers/PHP/BackfillAsymmetricVisibilityTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

class PropertyDemo {
/* testPublicSetProperty */ public(set) mixed $pub1;
/* testPublicSetPropertyUC */ PUBLIC(SET) mixed $pub2;
public /* testPublicPublicSetProperty */ public(set) mixed $pub3;
public /* testPublicPublicSetPropertyUC */ PUBLIC(SET) mixed $pub4;

/* testProtectedSetProperty */ protected(set) mixed $prot1;
/* testProtectedSetPropertyUC */ PROTECTED(SET) mixed $prot2;
public /* testPublicProtectedSetProperty */ protected(set) mixed $prot3;
public /* testPublicProtectedSetPropertyUC */ PROTECTED(SET) mixed $prot4;

/* testPrivateSetProperty */ private(set) mixed $priv1;
/* testPrivateSetPropertyUC */ PRIVATE(SET) mixed $priv2;
public /* testPublicPrivateSetProperty */ private(set) mixed $priv3;
public /* testPublicPrivateSetPropertyUC */ PRIVATE(SET) mixed $priv4;

/* testInvalidUnsetProperty */ public(unset) mixed $invalid1;
/* testInvalidSpaceProperty */ public (set) mixed $invalid2;
/* testInvalidCommentProperty */ protected/* foo */(set) mixed $invalid3;
/* testInvalidGetProperty */ private(get) mixed $invalid4;
/* testInvalidNoParenProperty */ private set mixed $invalid5;
}

class ConstructorPromotionDemo {
public function __construct(
/* testPublicSetCPP */ public(set) mixed $pub1,
/* testPublicSetCPPUC */ PUBLIC(SET) mixed $pub2,
public /* testPublicPublicSetCPP */ public(set) mixed $pub3,
public /* testPublicPublicSetCPPUC */ PUBLIC(SET) mixed $pub4,

/* testProtectedSetCPP */ protected(set) mixed $prot1,
/* testProtectedSetCPPUC */ PROTECTED(SET) mixed $prot2,
public /* testPublicProtectedSetCPP */ protected(set) mixed $prot3,
public /* testPublicProtectedSetCPPUC */ PROTECTED(SET) mixed $prot4,

/* testPrivateSetCPP */ private(set) mixed $priv1,
/* testPrivateSetCPPUC */ PRIVATE(SET) mixed $priv2,
public /* testPublicPrivateSetCPP */ private(set) mixed $priv3,
public /* testPublicPrivateSetCPPUC */ PRIVATE(SET) mixed $priv4,

/* testInvalidUnsetCPP */ public(unset) mixed $invalid1,
/* testInvalidSpaceCPP */ public (set) mixed $invalid2,
/* testInvalidCommentCPP */ protected/* foo */(set) mixed $invalid3,
/* testInvalidGetCPP */ private(get) mixed $invalid4,
/* testInvalidNoParenCPP */ private set mixed $invalid5,
) {}
}

class NonVisibilityCases {
function /* testProtectedFunctionName */ protected() {}
function /* testPublicFunctionName */ public(
/* testSetParameterType */ Set $setter
) {}
}

// Intentional parse error. This must be the last test in the file.
class LiveCodingDemo {
/* testLiveCoding */ private(set
Loading