Skip to content

Commit

Permalink
fix issue GH-233
Browse files Browse the repository at this point in the history
  • Loading branch information
llaville committed Jul 10, 2020
1 parent 49c6670 commit bac7a02
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/Bartlett/CompatInfo/Analyser/CompatibilityAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,6 +77,7 @@ public function __construct()
$this->sniffs = [
new DeclareSniff(),
new AnonymousClassSniff(),
new ReturnTypeDeclarationSniff(),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -18,18 +19,17 @@ class ReturnTypeDeclarationSniff extends SniffAbstract
public function enterSniff(): void
{
parent::enterSniff();

$this->returnTypeDeclaration = array();
$this->returnTypeDeclaration = [];
}

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]
);
}
}
Expand All @@ -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);
Expand Down
75 changes: 75 additions & 0 deletions tests/ReturnTypeDeclarationSniffTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Unit tests for PHP_CompatInfo package, return type declaration sniff
*
* PHP version 7
*
* @category PHP
* @package PHP_CompatInfo
* @subpackage Tests
* @author Laurent Laville <[email protected]>
* @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
);
}
}
7 changes: 6 additions & 1 deletion tests/fixtures/sniffs/return_types.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit bac7a02

Please sign in to comment.