Skip to content

Commit

Permalink
implement Final class constants (see #328)
Browse files Browse the repository at this point in the history
  • Loading branch information
llaville committed Jan 14, 2022
1 parent 1889d6c commit 4108ad5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG-6.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ using the [Keep a CHANGELOG](http://keepachangelog.com) principles.

- [#326](https://github.com/llaville/php-compatinfo/issues/326) : update sniffs to detect PHP (8.1) Intersection types
- [#327](https://github.com/llaville/php-compatinfo/issues/327) : update sniffs to detect PHP (8.1) return never
- [#328](https://github.com/llaville/php-compatinfo/issues/328) : update sniffs to detect PHP (8.1) Final class constants

### Removed

Expand Down
27 changes: 26 additions & 1 deletion src/Application/Sniffs/Constants/ClassConstantSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,37 @@

use PhpParser\Node;

use Generator;

/**
* Class constants
*
* @author Laurent Laville
* @since Release 5.4.0
*
* @link https://www.php.net/manual/en/language.oop5.constants.php
* @link https://www.php.net/releases/8.1/en.php#final_class_constants
* @link https://wiki.php.net/rfc/final_class_const
* @link https://php.watch/versions/8.1/final-class-const
* @see tests/Sniffs/ClassConstantSniffTest
*/
final class ClassConstantSniff extends SniffAbstract
{
// Rules identifiers for SARIF report
private const CA81 = 'CA8107';

/**
* {@inheritDoc}
*/
public function getRules(): Generator
{
yield self::CA81 => [
'name' => $this->getShortClass(),
'fullDescription' => "Final class constants are available since PHP 8.1.0",
'helpUri' => '%baseHelpUri%/01_Components/03_Sniffs/Features/#php-81',
];
}

/**
* {@inheritDoc}
*/
Expand All @@ -31,7 +51,12 @@ public function enterNode(Node $node)
return null;
}

$this->updateNodeElementVersion($node, $this->attributeKeyStore, ['php.min' => '4.0.0']);
if ($node->isFinal()) {
$this->updateNodeElementVersion($node, $this->attributeKeyStore, ['php.min' => '8.1.0beta1']);
$this->updateNodeElementRule($node, $this->attributeKeyStore, self::CA81);
} else {
$this->updateNodeElementVersion($node, $this->attributeKeyStore, ['php.min' => '4.0.0']);
}
return null;
}
}
29 changes: 29 additions & 0 deletions tests/Sniffs/ClassConstantSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
namespace Bartlett\CompatInfo\Tests\Sniffs;

use Exception;

/**
* Class constants
*
Expand Down Expand Up @@ -34,6 +36,7 @@ public static function setUpBeforeClass(): void
* Constant expressions with scalar expression not detected
* @group regression
* @return void
* @throws Exception
*/
public function testRegressionGH215()
{
Expand All @@ -51,4 +54,30 @@ public function testRegressionGH215()
$constants['C\THREE']['php.max']
);
}

/**
* Feature test for final class constants
*
* @link https://github.com/llaville/php-compatinfo/issues/328
* Final class constants are detected as PHP 8.1
* @group features
* @return void
* @throws Exception
*/
public function testFinalClassConstants()
{
$dataSource = 'final_const.php';
$metrics = $this->executeAnalysis($dataSource);
$constants = $metrics[self::$analyserId]['constants'];

$this->assertEquals(
'8.1.0beta1',
$constants['Foo\XX']['php.min']
);

$this->assertEquals(
'',
$constants['Foo\XX']['php.max']
);
}
}
5 changes: 5 additions & 0 deletions tests/fixtures/sniffs/constants/final_const.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
class Foo
{
final public const XX = "foo";
}

0 comments on commit 4108ad5

Please sign in to comment.