Skip to content
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

PHP 8.0 | Keywords/NewKeywords: detect the new match keyword (RFC) #1597

Merged
merged 1 commit into from
Aug 4, 2023
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
6 changes: 6 additions & 0 deletions PHPCompatibility/Sniffs/Keywords/NewKeywordsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ class NewKeywordsSniff extends Sniff
'7.4' => true,
'description' => 'The "fn" keyword for arrow functions',
],
'T_MATCH' => [
'7.4' => false,
'8.0' => true,
'description' => 'The "match" keyword',
],
];

/**
Expand Down Expand Up @@ -276,6 +281,7 @@ public function process(File $phpcsFile, $stackPtr)
// them.
if (($nextToken === false
|| $tokenType === 'T_FN' // Open parenthesis is expected after "fn" keyword.
|| $tokenType === 'T_MATCH' // ... and after the "match" keyword.
|| $tokens[$nextToken]['type'] !== 'T_OPEN_PARENTHESIS')
&& ($prevToken === false
|| $tokens[$prevToken]['type'] !== 'T_CLASS'
Expand Down
60 changes: 60 additions & 0 deletions PHPCompatibility/Tests/Keywords/NewKeywordsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,66 @@ $result = array_map(
$numbers
);

/*
* PHP 8.0: match expressions.
*/
// OK: match, but not an match expression.
$a = Foo::match($param);
$a = MyClass::match;
$a = MyClass::match[$a];
$a = $obj->match($param);
$a = $obj->match->chain($param);
$a = $obj?->match;
$a = MyNS\Sub\match($param);
$a = namespace\match($param);

class Match {
const match = 'a';

public static function match($param) {}

public function bar() {
$this->match = 'a';
}
}

// PHP 8.0 match expressions.
$statement = match ($this->lexer->lookahead['type']) {
Lexer::T_SELECT => $this->SelectStatement(),
Lexer::T_UPDATE => $this->UpdateStatement(),
Lexer::T_DELETE => $this->DeleteStatement(),
default => $this->syntaxError('SELECT, UPDATE or DELETE'),
};

echo match (1) {
0 => 'Foo',
1 => 'Bar',
2 => 'Baz',
};

MATCH ($pressedKey) {
Key::RETURN_ => save(),
Key::DELETE => delete(),
};

echo Match ($x) {
1, 2 => 'Same for 1 and 2',
3, 4 => 'Same for 3 and 4',
};

$result = match ($x) {
foo() => ...,
$this->bar() => ..., // bar() isn't called if foo() matched with $x
$this->baz => ...,
// etc.
};

$x = match ($y) {
default => {
// This should work fine
(match ($z) { ... })
},
};

__halt_compiler();

Expand Down
73 changes: 72 additions & 1 deletion PHPCompatibility/Tests/Keywords/NewKeywordsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,77 @@ public static function dataArrowFunctionNoFalsePositives()
return $data;
}

/**
* Test match expressions.
*
* @dataProvider dataMatchExpressions
*
* @param int $line The line number.
*
* @return void
*/
public function testMatchExpressions($line)
{
$file = $this->sniffFile(__FILE__, '7.4');
$this->assertError($file, $line, 'The "match" keyword is not present in PHP version 7.4 or earlier');

$file = $this->sniffFile(__FILE__, '8.0');
$this->assertNoViolation($file, $line);
}

/**
* Data provider.
*
* @see testMatchExpressions()
*
* @return array
*/
public static function dataMatchExpressions()
{
return [
[189],
[196],
[202],
[207],
[212],
[219],
[222],
];
}

/**
* Test against false positives for match expressions.
*
* @dataProvider dataMatchExpressionsNoFalsePositives
*
* @param int $line The line number.
*
* @return void
*/
public function testMatchExpressionsNoFalsePositives($line)
{
$file = $this->sniffFile(__FILE__, '8.0');
$this->assertNoViolation($file, $line);
}

/**
* Data provider.
*
* @see testMatchExpressionsNoFalsePositives()
*
* @return array
*/
public static function dataMatchExpressionsNoFalsePositives()
{
$data = [];

for ($i = 168; $i <= 187; $i++) {
$data[] = [$i];
}

return $data;
}

/**
* testHaltCompiler
*
Expand All @@ -486,7 +557,7 @@ public function testHaltCompiler()
* not be reported.
*/
$file = $this->sniffFile(__FILE__, '5.2');
$this->assertNoViolation($file, 170);
$this->assertNoViolation($file, 229);
}


Expand Down