Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 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
Loading
Loading