-
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 Override attribute support (#366)
- Loading branch information
Showing
5 changed files
with
141 additions
and
1 deletion.
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
69 changes: 69 additions & 0 deletions
69
src/Application/Sniffs/Attributes/OverrideAttributeSniff.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,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; | ||
} | ||
} |
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,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'] | ||
); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
trait T { | ||
#[\Override] | ||
public function i(): void {} | ||
} | ||
|
||
interface I { | ||
public function i(): void; | ||
} | ||
|
||
class Foo implements I { | ||
use T; | ||
} |