diff --git a/CHANGELOG.md b/CHANGELOG.md index 808062f9..19cbbba8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ using the [Keep a CHANGELOG](http://keepachangelog.com) principles. - [Application version strategy](https://github.com/llaville/php-compat-info/issues/267) : `composer/package-versions-deprecated` is used to handle version in Composer/Git strategy - uses `AnonymousClassSniff` to enhance PHP 7 detection - (see also [GH-269](https://github.com/llaville/php-compat-info/issues/269)) +- uses `ReturnTypeDeclarationSniff` to enhance PHP 7 detection - (see also [GH-233](https://github.com/llaville/php-compat-info/issues/233). Thanks to @CybotTM) ### Removed diff --git a/src/Bartlett/CompatInfo/Analyser/CompatibilityAnalyser.php b/src/Bartlett/CompatInfo/Analyser/CompatibilityAnalyser.php index 57a03806..5d879c98 100644 --- a/src/Bartlett/CompatInfo/Analyser/CompatibilityAnalyser.php +++ b/src/Bartlett/CompatInfo/Analyser/CompatibilityAnalyser.php @@ -13,6 +13,7 @@ use Bartlett\CompatInfo\Sniffs\PHP\AnonymousClassSniff; use Bartlett\CompatInfo\Sniffs\PHP\DeclareSniff; +use Bartlett\CompatInfo\Sniffs\PHP\ReturnTypeDeclarationSniff; use Bartlett\CompatInfo\Util\Database; use Bartlett\CompatInfo\Collection\ReferenceCollection; use Bartlett\CompatInfo\PhpParser\ConditionalCodeNodeProcessor; @@ -76,6 +77,7 @@ public function __construct() $this->sniffs = [ new DeclareSniff(), new AnonymousClassSniff(), + new ReturnTypeDeclarationSniff(), ]; } diff --git a/src/Bartlett/CompatInfo/Sniffs/PHP/ReturnTypeDeclarationSniff.php b/src/Bartlett/CompatInfo/Sniffs/PHP/ReturnTypeDeclarationSniff.php index 20fdbf53..686caadc 100644 --- a/src/Bartlett/CompatInfo/Sniffs/PHP/ReturnTypeDeclarationSniff.php +++ b/src/Bartlett/CompatInfo/Sniffs/PHP/ReturnTypeDeclarationSniff.php @@ -10,6 +10,7 @@ * Return Type Declarations since PHP 7.0.0 alpha1 * * @link https://wiki.php.net/rfc/return_types + * @link https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.return-type-declarations */ class ReturnTypeDeclarationSniff extends SniffAbstract { @@ -18,8 +19,7 @@ class ReturnTypeDeclarationSniff extends SniffAbstract public function enterSniff(): void { parent::enterSniff(); - - $this->returnTypeDeclaration = array(); + $this->returnTypeDeclaration = []; } public function leaveSniff(): void @@ -27,9 +27,9 @@ public function leaveSniff(): void parent::leaveSniff(); if (!empty($this->returnTypeDeclaration)) { - // inform analyser that few sniffs were found + // inform analyser that some sniffs were found $this->visitor->setMetrics( - array(Metrics::RETURN_TYPE_DECLARATION => $this->returnTypeDeclaration) + [ReturnTypeDeclarationSniff::class => $this->returnTypeDeclaration] ); } } @@ -47,11 +47,11 @@ public function enterNode(Node $node): void if (empty($this->returnTypeDeclaration)) { $version = '7.0.0alpha1'; - $this->returnTypeDeclaration[$name] = array( + $this->returnTypeDeclaration[$name] = [ 'severity' => $this->getCurrentSeverity($version), 'version' => $version, - 'spots' => array() - ); + 'spots' => [], + ]; } $this->returnTypeDeclaration[$name]['spots'][] = $this->getCurrentSpot($node); diff --git a/tests/ReturnTypeDeclarationSniffTest.php b/tests/ReturnTypeDeclarationSniffTest.php new file mode 100644 index 00000000..b8144e99 --- /dev/null +++ b/tests/ReturnTypeDeclarationSniffTest.php @@ -0,0 +1,75 @@ + + * @license https://opensource.org/licenses/BSD-3-Clause The 3-Clause BSD License + * @since Class available since Release 5.4.0 + */ + +namespace Bartlett\Tests\CompatInfo; + +use Bartlett\CompatInfo\Analyser\CompatibilityAnalyser; +use Bartlett\Reflect\Client; + +/** + * @link https://wiki.php.net/rfc/return_types + * @link https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.return-type-declarations + */ +class ReturnTypeDeclarationSniffTest extends \PHPUnit\Framework\TestCase +{ + protected static $fixtures; + protected static $analyserId; + protected static $api; + + /** + * Sets up the shared fixture. + * + * @return void + */ + public static function setUpBeforeClass(): void + { + self::$fixtures = __DIR__ . DIRECTORY_SEPARATOR + . 'fixtures' . DIRECTORY_SEPARATOR + . 'sniffs' . DIRECTORY_SEPARATOR + ; + + self::$analyserId = CompatibilityAnalyser::class; + + $client = new Client(); + + // request for a Bartlett\Reflect\Api\Analyser + self::$api = $client->api('analyser'); + } + + /** + * Regression test for return type hint declaration detection GH#233 + * + * @link https://github.com/llaville/php-compat-info/issues/233 + * PHP 7 requirement not detected for return type hint + * @group features + * @group regression + * @return void + */ + public function testReturnTypeHint() + { + $dataSource = self::$fixtures . 'return_types.php'; + $analysers = ['compatibility']; + $metrics = self::$api->run($dataSource, $analysers); + $versions = $metrics[self::$analyserId]['versions']; + + $this->assertEquals( + [ + 'php.min' => '7.0.0alpha1', + 'php.max' => '', + 'php.all' => '7.0.0alpha1', + ], + $versions + ); + } +} diff --git a/tests/fixtures/sniffs/return_types.php b/tests/fixtures/sniffs/return_types.php index 2bd07438..f991194a 100644 --- a/tests/fixtures/sniffs/return_types.php +++ b/tests/fixtures/sniffs/return_types.php @@ -9,10 +9,15 @@ static function make(): B { // must exactly match parent; this will error } } -function foo(): DateTime { +function foo(): DateTime { return null; // invalid } $foo = function (): array { return ["foo", "bar"]; }; + +function fooBar(int $i) : string +{ + return (string) $i; +}