-
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.
add dynamic class constant fetch support (#366)
- Loading branch information
Showing
5 changed files
with
141 additions
and
7 deletions.
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
61 changes: 61 additions & 0 deletions
61
src/Application/Sniffs/Constants/DynamicClassConstantFetchSniff.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,61 @@ | ||
<?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\Constants; | ||
|
||
use Bartlett\CompatInfo\Application\Sniffs\SniffAbstract; | ||
|
||
use PhpParser\Node; | ||
|
||
use Generator; | ||
|
||
/** | ||
* Dynamic class constant fetch syntax is available since PHP 8.3.0 alpha1 | ||
* | ||
* @author Laurent Laville | ||
* @since Class available since Release 7.1.0 | ||
* | ||
* @link https://wiki.php.net/rfc/dynamic_class_constant_fetch | ||
* @link https://stitcher.io/blog/new-in-php-83#dynamic-class-constant-fetch-rfc | ||
* @see tests/Sniffs/DynamicClassConstantFetchSniffTest | ||
*/ | ||
final class DynamicClassConstantFetchSniff extends SniffAbstract | ||
{ | ||
// Rules identifiers for SARIF report | ||
private const CA83 = 'CA8304'; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getRules(): Generator | ||
{ | ||
yield self::CA83 => [ | ||
'name' => $this->getShortClass(), | ||
'fullDescription' => "Dynamic class constant fetch syntax is available since PHP 8.3.0", | ||
'helpUri' => '%baseHelpUri%/01_Components/03_Sniffs/Features/#php-83', | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function enterNode(Node $node) | ||
{ | ||
if (!$node instanceof Node\Expr\ClassConstFetch) { | ||
return null; | ||
} | ||
|
||
if ($node->name instanceof Node\Expr\Variable) { | ||
if (!$parent = $node->getAttribute($this->attributeParentKeyStore)) { | ||
return null; | ||
} | ||
$this->updateNodeElementVersion($parent, $this->attributeKeyStore, ['php.min' => '8.3.0alpha1']); | ||
$this->updateNodeElementRule($parent, $this->attributeKeyStore, self::CA83); | ||
} | ||
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,57 @@ | ||
<?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; | ||
|
||
/** | ||
* Unit test for dynamic class constant fetch syntax | ||
* | ||
* @author Laurent Laville | ||
* @since Class available since Release 7.1.0 | ||
* | ||
* @link https://www.php.net/releases/8.3/en.php#dynamic_class_constant_fetch | ||
* @link https://wiki.php.net/rfc/dynamic_class_constant_fetch | ||
* @link https://stitcher.io/blog/new-in-php-83#dynamic-class-constant-fetch-rfc | ||
*/ | ||
final class DynamicClassConstantFetchSniffTest extends SniffTestCase | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
parent::setUpBeforeClass(); | ||
|
||
self::$fixtures .= 'constants' . DIRECTORY_SEPARATOR; | ||
} | ||
|
||
/** | ||
* Feature test for dynamic class constant fetch syntax | ||
* | ||
* @group features | ||
* @return void | ||
* @throws Exception | ||
*/ | ||
public function testDynamicClassConstantFetch() | ||
{ | ||
$dataSource = 'dynamic_class_const_fetch.php'; | ||
$metrics = $this->executeAnalysis($dataSource); | ||
$versions = $metrics[self::$analyserId]['versions']; | ||
|
||
$this->assertEquals( | ||
'8.3.0alpha1', | ||
$versions['php.min'] | ||
); | ||
|
||
$this->assertEquals( | ||
'', | ||
$versions['php.max'] | ||
); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
tests/fixtures/sniffs/constants/dynamic_class_const_fetch.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,14 @@ | ||
<?php | ||
class Foo | ||
{ | ||
const BAR = 'bar'; | ||
} | ||
|
||
$name = 'BAR'; | ||
|
||
// Instead of this: | ||
constant(Foo::class . '::' . $name); | ||
|
||
// Since PHP 8.3, you can now do this: | ||
Foo::{$name}; | ||
|