Skip to content

Commit

Permalink
#363 : add support to deprecated ${} string interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
llaville committed Nov 14, 2023
1 parent c3d9b58 commit 82301a5
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 9 deletions.
17 changes: 9 additions & 8 deletions docs/01_Components/03_Sniffs/Features.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,15 @@ Here is the list of features supported and their corresponding sniffs :

## [PHP 8.2](https://www.php.net/manual/en/migration82.php)

| Sniff category | Sniff class name | PHP Feature |
|----------------------|----------------------------------|---------------------------------------------------------------------------------------------------------------------------------------|
| Attributes | AllowDynamicPropertiesSniff | [AllowDynamicProperties attribute](https://www.php.net/manual/en/class.allow-dynamic-properties.php) |
| Attributes | SensitiveParameterAttributeSniff | [SensitiveParameter attribute](https://www.php.net/manual/en/class.sensitive-parameter.php) |
| Classes | ReadonlyClassSniff | [Readonly Classes](https://www.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.class.readonly) |
| Constants | ConstantsInTraitsSniff | [Constants in Traits](https://www.php.net/manual/en/migration82.new-features.php#migration82.new-features.core.constant-in-traits) |
| FunctionDeclarations | ParamTypeDeclarationSniff | [Disjunctive Normal Form Types](https://www.php.net/manual/en/migration82.new-features.php#migration82.new-features.core.type-system) |
| FunctionDeclarations | ReturnTypeDeclarationSniff | [Allow null, false, and true as stand-alone types](https://wiki.php.net/rfc/true-type) |
| Sniff category | Sniff class name | PHP Feature |
|----------------------|----------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------|
| Attributes | AllowDynamicPropertiesSniff | [AllowDynamicProperties attribute](https://www.php.net/manual/en/class.allow-dynamic-properties.php) |
| Attributes | SensitiveParameterAttributeSniff | [SensitiveParameter attribute](https://www.php.net/manual/en/class.sensitive-parameter.php) |
| Classes | ReadonlyClassSniff | [Readonly Classes](https://www.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.class.readonly) |
| Constants | ConstantsInTraitsSniff | [Constants in Traits](https://www.php.net/manual/en/migration82.new-features.php#migration82.new-features.core.constant-in-traits) |
| FunctionDeclarations | ParamTypeDeclarationSniff | [Disjunctive Normal Form Types](https://www.php.net/manual/en/migration82.new-features.php#migration82.new-features.core.type-system) |
| FunctionDeclarations | ReturnTypeDeclarationSniff | [Allow null, false, and true as stand-alone types](https://wiki.php.net/rfc/true-type) |
| TextProcessing | DeprecateDollarBraceStringInterpolationSniff | [Deprecated \${} string interpolation](https://wiki.php.net/rfc/deprecate_dollar_brace_string_interpolation) |

## Special cases

Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ They are grouped by categories to solve PHP features (from 4.0 to 8.2)
- Keywords (1)
- Numbers (2)
- Operators (5)
- TextProcessing (1)
- TextProcessing (2)
- UseDeclarations (2)

### [Extensions](01_Components/04_Extensions/Hooks.md)
Expand Down
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 tests/Sniffs/DeprecateDollarBraceStringInterpolationSniffTest.php
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']
);
}
}
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

0 comments on commit 82301a5

Please sign in to comment.