Skip to content

Commit

Permalink
add Override attribute support (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
llaville committed Dec 31, 2023
1 parent a7b1526 commit 9ece759
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/components/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
They are grouped by categories to solve PHP features (from 4.0 to 8.2)

- Arrays (3)
- Attributes (3)
- Attributes (4)
- Classes (11)
- Constants (5)
- ControlStructures (4)
Expand Down
2 changes: 2 additions & 0 deletions docs/components/sniffs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,10 @@ Here is the list of features supported and their corresponding sniffs :

| Sniff category | Sniff class name | PHP Feature |
|----------------|-------------------------|---------------------------------------------|
| Attributes | OverrideAttributeSniff | [Override attribute][OverrideAttribute] |
| Constants | TypedClassConstantSniff | [Typed Class Constants][TypedClassConstant] |

[OverrideAttribute]: https://www.php.net/releases/8.3/en.php#override_attribute
[TypedClassConstant]: https://www.php.net/releases/8.3/en.php#typed_class_constants

## Special cases
Expand Down
69 changes: 69 additions & 0 deletions src/Application/Sniffs/Attributes/OverrideAttributeSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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\Attributes;

use Bartlett\CompatInfo\Application\Sniffs\SniffAbstract;

use PhpParser\Node;

use Generator;

/**
* Override attribute since PHP 8.3.0 alpha3
*
* @author Laurent Laville
* @since Release 7.1.0
*
* @link https://wiki.php.net/rfc/marking_overriden_methods
* @link https://stitcher.io/blog/new-in-php-83##%5Boverride%5D-attribute-rfc
* @see tests/Sniffs/OverrideAttributeSniffTest
*/
final class OverrideAttributeSniff extends SniffAbstract
{
// Rules identifiers for SARIF report
private const CA83 = 'CA8302';

/**
* @inheritDoc
*/
public function getRules(): Generator
{
yield self::CA83 => [
'name' => $this->getShortClass(),
'fullDescription' => "Override attribute 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\AttributeGroup) {
return null;
}
$found = false;
foreach ($node->attrs as $attr) {
if ($attr->name->toString() == 'Override') {
$found = true;
break;
}
}
if (!$found) {
return null;
}

if (!$parent = $node->getAttribute($this->attributeParentKeyStore)) {
return null;
}
$this->updateNodeElementVersion($parent, $this->attributeKeyStore, ['php.all' => '8.3.0alpha3']);
$this->updateNodeElementRule($parent, $this->attributeKeyStore, self::CA83);
return null;
}
}
56 changes: 56 additions & 0 deletions tests/Sniffs/OverrideAttributeSniffTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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;

/**
* Override attribute since PHP 8.3.0 alpha3
*
* @author Laurent Laville
* @since Release 7.1.0
*
* @link https://wiki.php.net/rfc/marking_overriden_methods
* @link https://stitcher.io/blog/new-in-php-83##%5Boverride%5D-attribute-rfc
*/
final class OverrideAttributeSniffTest extends SniffTestCase
{
/**
* @inheritDoc
*/
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

self::$fixtures .= 'attributes' . DIRECTORY_SEPARATOR;
}

/**
* Feature test for Override attribute
*
* @link https://github.com/llaville/php-compatinfo/issues/366
* @group feature
* @return void
* @throws Exception
*/
public function testAttributes()
{
$dataSource = 'override.php';
$metrics = $this->executeAnalysis($dataSource);
$traits = $metrics[self::$analyserId]['traits'];

$this->assertEquals(
'8.3.0alpha3', // due to native override attribute
$traits['T']['php.all']
);
$this->assertEquals(
'7.1.0', // because attribute act as a comment
$traits['T']['php.min']
);
}
}
13 changes: 13 additions & 0 deletions tests/fixtures/sniffs/attributes/override.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
trait T {
#[\Override]
public function i(): void {}
}

interface I {
public function i(): void;
}

class Foo implements I {
use T;
}

0 comments on commit 9ece759

Please sign in to comment.