Skip to content

Commit

Permalink
fix AnonymousClassSniff following code reorganization applied to solv…
Browse files Browse the repository at this point in the history
…e issue GH-186
  • Loading branch information
llaville committed Jul 18, 2020
1 parent e75f691 commit a4650f2
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 82 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 @@ -11,9 +11,9 @@

namespace Bartlett\CompatInfo\Analyser;

use Bartlett\CompatInfo\Sniffs\Classes\AnonymousClassSniff;
use Bartlett\CompatInfo\Sniffs\ControlStructures\DeclareSniff;
use Bartlett\CompatInfo\Sniffs\Keywords\ReservedSniff;
use Bartlett\CompatInfo\Sniffs\PHP\AnonymousClassSniff;
use Bartlett\CompatInfo\Sniffs\PHP\ReturnTypeDeclarationSniff;
use Bartlett\CompatInfo\Util\Database;
use Bartlett\CompatInfo\Collection\ReferenceCollection;
Expand Down
87 changes: 87 additions & 0 deletions src/Bartlett/CompatInfo/Sniffs/Classes/AnonymousClassSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php declare(strict_types=1);

namespace Bartlett\CompatInfo\Sniffs\Classes;

use Bartlett\CompatInfo\Sniffs\SniffAbstract;

use PhpParser\Node;

/**
* Anonymous classes since PHP 7.0.0 alpha1
*
* @link https://wiki.php.net/rfc/anonymous_classes
* @link https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.anonymous-classes
*/
final class AnonymousClassSniff extends SniffAbstract
{
private $anonymousClass;

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

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

if (!empty($this->anonymousClass)) {
$versions = $this->anonymousClass['versions'];
$versions['matches'] = count($this->anonymousClass['spots']);
$this->visitor->updateElementVersion('classes', 'class', $versions);
$this->visitor->updateGlobalVersion($versions['php.min'], '', $versions['php.min']);
}
}

/**
* Process this sniff only on this scope.
*
* @param Node $node
* @return int|Node|void|null
*/
public function enterNode(Node $node)
{
if (!$this->isAnonymousClass($node)) {
return null;
}

$name = 'class';

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

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

$this->anonymousClass = array_merge(
[
'spots' => [],
],
['versions' => $versions]
);
$this->visitor->updateElementVersion('classes', $name, $versions);
$this->visitor->updateContextVersion($versions);

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

private function isAnonymousClass(Node $node): bool
{
return ($node instanceof Node\Expr\New_
&& $node->class instanceof Node\Stmt\Class_
);
}
}
81 changes: 0 additions & 81 deletions src/Bartlett/CompatInfo/Sniffs/PHP/AnonymousClassSniff.php

This file was deleted.

1 change: 1 addition & 0 deletions tests/AnonymousClassSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static function setUpBeforeClass(): void
self::$fixtures = __DIR__ . DIRECTORY_SEPARATOR
. 'fixtures' . DIRECTORY_SEPARATOR
. 'sniffs' . DIRECTORY_SEPARATOR
. 'classes' . DIRECTORY_SEPARATOR
;

self::$analyserId = CompatibilityAnalyser::class;
Expand Down
File renamed without changes.

0 comments on commit a4650f2

Please sign in to comment.