-
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 static variable initializers support (#366)
- Loading branch information
Showing
5 changed files
with
125 additions
and
5 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
59 changes: 59 additions & 0 deletions
59
src/Application/Sniffs/Expressions/StaticVarInitializerSniff.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,59 @@ | ||
<?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\Expressions; | ||
|
||
use Bartlett\CompatInfo\Application\Sniffs\SniffAbstract; | ||
|
||
use Generator; | ||
use PhpParser\Node; | ||
|
||
/** | ||
* Static variable initializers available since PHP 8.3.0 alpha1 | ||
* | ||
* @author Laurent Laville | ||
* @since Class available since Release 7.1.0 | ||
* | ||
* @link https://wiki.php.net/rfc/arbitrary_static_variable_initializers | ||
* @see tests/Sniffs/StaticVarInitializerSniffTest | ||
*/ | ||
final class StaticVarInitializerSniff extends SniffAbstract | ||
{ | ||
// Rules identifiers for SARIF report | ||
private const CA83 = 'CA8303'; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getRules(): Generator | ||
{ | ||
yield self::CA83 => [ | ||
'name' => $this->getShortClass(), | ||
'fullDescription' => "Static variable initializers 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\Stmt\StaticVar) { | ||
return null; | ||
} | ||
|
||
if ($node->default instanceof Node\Expr\CallLike && !$node->default instanceof Node\Expr\New_) { | ||
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,50 @@ | ||
<?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 case for Static variable initializers | ||
* | ||
* @author Laurent Laville | ||
* @since Class available since Release 7.1.0 | ||
* | ||
* @link https://wiki.php.net/rfc/arbitrary_static_variable_initializers | ||
*/ | ||
final class StaticVarInitializerSniffTest extends SniffTestCase | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
parent::setUpBeforeClass(); | ||
|
||
self::$fixtures .= 'expressions' . DIRECTORY_SEPARATOR; | ||
} | ||
|
||
/** | ||
* Feature test to detect static variable initializers | ||
* | ||
* @group features | ||
* @return void | ||
* @throws Exception | ||
*/ | ||
public function testStaticVariableInitializer() | ||
{ | ||
$dataSource = 'static_var_initializer.php'; | ||
$metrics = $this->executeAnalysis($dataSource); | ||
$functions = $metrics[self::$analyserId]['functions']; | ||
|
||
$this->assertEquals( | ||
'8.3.0alpha1', | ||
$functions['foo']['php.min'] | ||
); | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
function bar() { | ||
return 1; | ||
} | ||
|
||
function foo() { | ||
static $i = bar(); | ||
echo $i++, "\n"; | ||
} |