-
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.
#363 : add support to deprecated ${} string interpolation
- Loading branch information
Showing
5 changed files
with
141 additions
and
9 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
74 changes: 74 additions & 0 deletions
74
src/Application/Sniffs/TextProcessing/DeprecateDollarBraceStringInterpolationSniff.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,74 @@ | ||
<?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\TextProcessing; | ||
|
||
use Bartlett\CompatInfo\Application\Sniffs\SniffAbstract; | ||
use Generator; | ||
use PhpParser\Node; | ||
|
||
/** | ||
* Deprecate Dollar Brace String Interpolation since PHP 8.2.0 alpha1 | ||
* | ||
* @author Laurent Laville | ||
* @since Release 7.0.1 | ||
* | ||
* @link https://wiki.php.net/rfc/deprecate_dollar_brace_string_interpolation | ||
* @see tests/Sniffs/DeprecateDollarBraceStringInterpolationSniffTest | ||
*/ | ||
final class DeprecateDollarBraceStringInterpolationSniff extends SniffAbstract | ||
{ | ||
// Rules identifiers for SARIF report | ||
private const CA82 = 'CA8206'; | ||
/** @var array<int, mixed> */ | ||
private array $tokens; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getRules(): Generator | ||
{ | ||
yield self::CA82 => [ | ||
'name' => $this->getShortClass(), | ||
'fullDescription' => "Deprecated \${} interpolation since PHP 8.2.0", | ||
'helpUri' => '%baseHelpUri%/01_Components/03_Sniffs/Features/#php-82', | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function enterSniff(): void | ||
{ | ||
parent::enterSniff(); | ||
$this->tokens = $this->visitor->getTokens(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function leaveNode(Node $node) | ||
{ | ||
if (!$this->isStringInterpolationSyntax($node)) { | ||
return null; | ||
} | ||
|
||
if (!$parent = $node->getAttribute($this->attributeParentKeyStore)) { | ||
return null; | ||
} | ||
|
||
$this->updateNodeElementVersion($parent, $this->attributeKeyStore, ['php.min' => '8.2.0alpha1']); | ||
$this->updateNodeElementRule($parent, $this->attributeKeyStore, self::CA82); | ||
return null; | ||
} | ||
|
||
private function isStringInterpolationSyntax(Node $node): bool | ||
{ | ||
$i = $node->getAttribute('startTokenPos'); | ||
return ($node instanceof Node\Scalar\Encapsed && $this->tokens[$i+1][1] == '${'); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
tests/Sniffs/DeprecateDollarBraceStringInterpolationSniffTest.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,51 @@ | ||
<?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; | ||
|
||
/** | ||
* Deprecate Dollar Brace String Interpolation since PHP 8.2.0 alpha1 | ||
* | ||
* @author Laurent Laville | ||
* @since Release 7.0.1 | ||
* | ||
* @link https://wiki.php.net/rfc/deprecate_dollar_brace_string_interpolation | ||
*/ | ||
final class DeprecateDollarBraceStringInterpolationSniffTest extends SniffTestCase | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
parent::setUpBeforeClass(); | ||
|
||
self::$fixtures .= 'strings' . DIRECTORY_SEPARATOR; | ||
} | ||
|
||
/** | ||
* Feature test for ${} interpolation deprecation | ||
* | ||
* @link https://github.com/llaville/php-compatinfo/issues/363 | ||
* @group feature | ||
* @return void | ||
* @throws Exception | ||
*/ | ||
public function testDeprecation() | ||
{ | ||
$dataSource = 'dollar_brace_string_interpolation.php'; | ||
$metrics = $this->executeAnalysis($dataSource); | ||
$versions = $metrics[self::$analyserId]['versions']; | ||
|
||
$this->assertEquals( | ||
'8.2.0alpha1', | ||
$versions['php.min'] | ||
); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/fixtures/sniffs/strings/dollar_brace_string_interpolation.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,6 @@ | ||
<?php | ||
$foo = ['bar' => 'bar']; | ||
|
||
var_dump("$foo[bar]"); | ||
var_dump("{$foo['bar']}"); | ||
var_dump("${foo['bar']}"); // deprecated since PHP 8.2.0 |