-
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 DeclareSniff following code reorganization applied to solve issue G…
- Loading branch information
Showing
7 changed files
with
101 additions
and
86 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
97 changes: 97 additions & 0 deletions
97
src/Bartlett/CompatInfo/Sniffs/ControlStructures/DeclareSniff.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,97 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Bartlett\CompatInfo\Sniffs\ControlStructures; | ||
|
||
use Bartlett\CompatInfo\Sniffs\KeywordBag; | ||
|
||
use Bartlett\CompatInfo\Sniffs\SniffAbstract; | ||
|
||
use PhpParser\Node; | ||
|
||
/** | ||
* Class DeclareSniff to detect declare control structures | ||
* | ||
* @link https://www.php.net/manual/en/control-structures.declare.php | ||
* @link https://wiki.php.net/rfc/scalar_type_hints_v5#strict_types_declare_directive | ||
* @since Class available since Release 5.4.0 | ||
*/ | ||
final class DeclareSniff extends SniffAbstract | ||
{ | ||
/** @var KeywordBag */ | ||
private $directives; | ||
|
||
/** @var array */ | ||
private $directivesDeclared; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function enterSniff(): void | ||
{ | ||
parent::enterSniff(); | ||
|
||
$this->directives = new KeywordBag( | ||
[ | ||
'ticks' => '4.0', | ||
'encoding' => '5.3', | ||
'strict_types' => '7.0', | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Process this sniff only on this scope. | ||
* | ||
* @param Node $node | ||
* @return int|Node|void|null | ||
*/ | ||
public function enterNode(Node $node) | ||
{ | ||
if ($node instanceof Node\Stmt\Declare_) { | ||
$this->contextCallback = [$this, 'enter' . str_replace('_', '', $node->getType())]; | ||
} else { | ||
$this->contextCallback = null; | ||
} | ||
|
||
if (!empty($this->contextCallback) && is_callable($this->contextCallback)) { | ||
call_user_func($this->contextCallback, $node); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param Node\Stmt\Declare_ $node | ||
* | ||
* @return void | ||
* | ||
* @see enterNode | ||
*/ | ||
private function enterStmtDeclare(Node\Stmt\Declare_ $node): void | ||
{ | ||
foreach ($node->declares as $declare) { | ||
$key = (string) $declare->key; | ||
|
||
if (!isset($this->directivesDeclared[$key])) { | ||
$versions = [ | ||
'php.min' => $this->directives->get($key), | ||
]; | ||
|
||
$this->directivesDeclared[$key] = array_merge( | ||
[ | ||
'spots' => [], | ||
], | ||
['versions' => $versions] | ||
); | ||
$this->visitor->updateElementVersion('directives', $key, $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->directivesDeclared] | ||
); | ||
} | ||
$this->directivesDeclared[$key]['spots'][] = $this->getCurrentSpot($node); | ||
} | ||
} | ||
} |
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.
File renamed without changes.
File renamed without changes.