-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix AnonymousClassSniff following code reorganization applied to solv…
…e issue GH-186
- Loading branch information
Showing
5 changed files
with
89 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/Bartlett/CompatInfo/Sniffs/Classes/AnonymousClassSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
81
src/Bartlett/CompatInfo/Sniffs/PHP/AnonymousClassSniff.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.