Skip to content

Commit

Permalink
fix ReturnTypeDeclarationSniff following code reorganization applied …
Browse files Browse the repository at this point in the history
…to solve issue GH-186
  • Loading branch information
llaville committed Jul 18, 2020
1 parent a4650f2 commit a1e75c4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/Bartlett/CompatInfo/Analyser/CompatibilityAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

use Bartlett\CompatInfo\Sniffs\Classes\AnonymousClassSniff;
use Bartlett\CompatInfo\Sniffs\ControlStructures\DeclareSniff;
use Bartlett\CompatInfo\Sniffs\FunctionDeclarations\ReturnTypeDeclarationSniff;
use Bartlett\CompatInfo\Sniffs\Keywords\ReservedSniff;
use Bartlett\CompatInfo\Sniffs\PHP\ReturnTypeDeclarationSniff;
use Bartlett\CompatInfo\Util\Database;
use Bartlett\CompatInfo\Collection\ReferenceCollection;
use Bartlett\CompatInfo\PhpParser\ConditionalCodeNodeProcessor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php declare(strict_types=1);

namespace Bartlett\CompatInfo\Sniffs\PHP;
namespace Bartlett\CompatInfo\Sniffs\FunctionDeclarations;

use Bartlett\Reflect\Sniffer\SniffAbstract;
use Bartlett\CompatInfo\Sniffs\SniffAbstract;

use PhpParser\Node;

Expand All @@ -12,26 +12,28 @@
* @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
final class ReturnTypeDeclarationSniff extends SniffAbstract
{
/** @var array */
private $returnTypeDeclaration;

/**
* {@inheritDoc}
*/
public function enterSniff(): void
{
parent::enterSniff();
$this->returnTypeDeclaration = [];
}

/**
* {@inheritDoc}
*/
public function leaveSniff(): void
{
parent::leaveSniff();

if (!empty($this->returnTypeDeclaration)) {
// inform analyser that some sniffs were found
$this->visitor->setMetrics(
[ReturnTypeDeclarationSniff::class => $this->returnTypeDeclaration]
);

$versions = $this->returnTypeDeclaration['versions'];
$versions['matches'] = count($this->returnTypeDeclaration['spots']);
$this->visitor->updateElementVersion('features', 'returnTypeDeclaration', $versions);
Expand All @@ -42,36 +44,37 @@ public function leaveSniff(): void
/**
* @return void
*/
public function enterNode(Node $node): void
public function enterNode(Node $node)
{
parent::enterNode($node);

if ($this->hasReturnType($node)) {
if (!$this->hasReturnType($node)) {
return null;
}

if (empty($this->returnTypeDeclaration)) {
$version = '7.0.0alpha1';
if (empty($this->returnTypeDeclaration)) {
$version = '7.0.0alpha1';

$versions = [
'php.min' => $version,
'php.max' => '',
];
$versions = [
'php.min' => $version,
];

$this->returnTypeDeclaration = array_merge(
[
'severity' => $this->getCurrentSeverity($version),
'spots' => [],
],
['versions' => $versions]
);
$this->visitor->updateElementVersion('features', 'returnTypeDeclaration', $versions);
$this->visitor->updateContextVersion($versions);
}
$this->returnTypeDeclaration = array_merge(
[
'spots' => [],
],
['versions' => $versions]
);
$this->visitor->updateElementVersion('features', 'returnTypeDeclaration', $versions);
$this->visitor->updateContextVersion($versions);

$this->returnTypeDeclaration['spots'][] = $this->getCurrentSpot($node);
// inform analyser (in real-time; do not wait leaveSniff) that some sniffs were found
$this->visitor->setMetrics(
[get_class($this) => $this->returnTypeDeclaration]
);
}
$this->returnTypeDeclaration['spots'][] = $this->getCurrentSpot($node);
}

protected function hasReturnType(Node $node): bool
private function hasReturnType(Node $node): bool
{
if ($node instanceof Node\Stmt\ClassMethod
|| $node instanceof Node\Stmt\Function_
Expand Down
File renamed without changes.

0 comments on commit a1e75c4

Please sign in to comment.