-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement first class callable syntax (see #324)
- Loading branch information
Showing
8 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/Application/Sniffs/FunctionDeclarations/FirstClassCallableSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* This file is part of the PHP_CompatInfo package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace Bartlett\CompatInfo\Application\Sniffs\FunctionDeclarations; | ||
|
||
use Bartlett\CompatInfo\Application\Sniffs\SniffAbstract; | ||
|
||
use PhpParser\Node; | ||
|
||
use Generator; | ||
|
||
/** | ||
* First class callable syntax (since PHP 8.1) | ||
* | ||
* @author Laurent Laville | ||
* @since Release 6.2.0 | ||
* | ||
* @link https://www.php.net/manual/en/functions.first_class_callable_syntax.php | ||
* @link https://wiki.php.net/rfc/first_class_callable_syntax | ||
* @link https://php.watch/versions/8.1/first-class-callable-syntax | ||
* @see tests/Sniffs/FirstClassCallableSniffTest.php | ||
*/ | ||
final class FirstClassCallableSniff extends SniffAbstract | ||
{ | ||
// Rules identifiers for SARIF report | ||
private const CA81 = 'CA8103'; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getRules(): Generator | ||
{ | ||
yield self::CA81 => [ | ||
'name' => $this->getShortClass(), | ||
'fullDescription' => "First class callable syntax is available since PHP 8.1.0", | ||
'helpUri' => '%baseHelpUri%/01_Components/03_Sniffs/Features/#php-81', | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function enterNode(Node $node) | ||
{ | ||
if (!$node instanceof Node\Expr\CallLike) { | ||
return null; | ||
} | ||
if (!$node->isFirstClassCallable()) { | ||
return null; | ||
} | ||
|
||
$this->updateNodeElementVersion($node, $this->attributeKeyStore, ['php.min' => '8.1.0beta1']); | ||
$this->updateNodeElementRule($node, $this->attributeKeyStore, self::CA81); | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* This file is part of the PHP_CompatInfo package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace Bartlett\CompatInfo\Tests\Sniffs; | ||
|
||
use Exception; | ||
|
||
/** | ||
* First class callable syntax (since PHP 8.1) | ||
* | ||
* @author Laurent Laville | ||
* @since Release 6.2.0 | ||
* | ||
* @link https://www.php.net/manual/en/functions.first_class_callable_syntax.php | ||
* @link https://wiki.php.net/rfc/first_class_callable_syntax | ||
* @link https://php.watch/versions/8.1/first-class-callable-syntax | ||
* @see tests/Sniffs/FirstClassCallableSniffTest.php | ||
*/ | ||
final class FirstClassCallableSniffTest extends SniffTestCase | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
parent::setUpBeforeClass(); | ||
|
||
self::$fixtures .= 'functions' . DIRECTORY_SEPARATOR; | ||
} | ||
|
||
/** | ||
* Regression test for issue #324 | ||
* | ||
* @link https://github.com/llaville/php-compatinfo/issues/324 | ||
* First class callable syntax is detected as PHP 8.1 | ||
* @group regression | ||
* @return void | ||
* @throws Exception | ||
*/ | ||
public function testFuncCall() | ||
{ | ||
$dataSource = 'callable_expr_function.php'; | ||
$metrics = $this->executeAnalysis($dataSource); | ||
$versions = $metrics[self::$analyserId]['versions']; | ||
|
||
$this->assertEquals( | ||
'8.1.0beta1', | ||
$versions['php.min'] | ||
); | ||
|
||
$this->assertEquals( | ||
'', | ||
$versions['php.max'] | ||
); | ||
} | ||
|
||
/** | ||
* Regression test for issue #324 | ||
* | ||
* @link https://github.com/llaville/php-compatinfo/issues/324 | ||
* First class callable syntax is detected as PHP 8.1 | ||
* @group regression | ||
* @return void | ||
* @throws Exception | ||
*/ | ||
public function testMethodCall() | ||
{ | ||
$dataSource = 'callable_expr_method.php'; | ||
$metrics = $this->executeAnalysis($dataSource); | ||
$versions = $metrics[self::$analyserId]['versions']; | ||
|
||
$this->assertEquals( | ||
'8.1.0beta1', | ||
$versions['php.min'] | ||
); | ||
|
||
$this->assertEquals( | ||
'', | ||
$versions['php.max'] | ||
); | ||
} | ||
|
||
/** | ||
* Regression test for issue #324 | ||
* | ||
* @link https://github.com/llaville/php-compatinfo/issues/324 | ||
* First class callable syntax is detected as PHP 8.1 | ||
* @group regression | ||
* @return void | ||
* @throws Exception | ||
*/ | ||
public function testStaticCall() | ||
{ | ||
$dataSource = 'callable_expr_static.php'; | ||
$metrics = $this->executeAnalysis($dataSource); | ||
$versions = $metrics[self::$analyserId]['versions']; | ||
|
||
$this->assertEquals( | ||
'8.1.0beta1', | ||
$versions['php.min'] | ||
); | ||
|
||
$this->assertEquals( | ||
'', | ||
$versions['php.max'] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
$callable = strtoupper(...); | ||
echo $callable('foo'); // FOO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
class Clock { | ||
public function getClockCallable(): callable { | ||
return $this->getTime(...); | ||
} | ||
|
||
private function getTime(): int { | ||
return time(); | ||
} | ||
} | ||
|
||
$clock = new Clock(); | ||
$clock_callback = $clock->getClockCallable(); | ||
echo $clock_callback(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
class Item | ||
{ | ||
public static function doSomething() | ||
{ | ||
echo __METHOD__ . PHP_EOL; | ||
} | ||
} | ||
|
||
Item::doSomething(...); |