Skip to content

Commit

Permalink
fix DeclareSniff following code reorganization applied to solve issue G…
Browse files Browse the repository at this point in the history
  • Loading branch information
llaville committed Jul 18, 2020
1 parent ea73059 commit e75f691
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 86 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\ControlStructures\DeclareSniff;
use Bartlett\CompatInfo\Sniffs\Keywords\ReservedSniff;
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;
Expand Down
97 changes: 97 additions & 0 deletions src/Bartlett/CompatInfo/Sniffs/ControlStructures/DeclareSniff.php
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);
}
}
}
83 changes: 0 additions & 83 deletions src/Bartlett/CompatInfo/Sniffs/PHP/DeclareSniff.php

This file was deleted.

5 changes: 3 additions & 2 deletions tests/DeclareSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static function setUpBeforeClass(): void
self::$fixtures = __DIR__ . DIRECTORY_SEPARATOR
. 'fixtures' . DIRECTORY_SEPARATOR
. 'sniffs' . DIRECTORY_SEPARATOR
. 'structures' . DIRECTORY_SEPARATOR
;

self::$analyserId = CompatibilityAnalyser::class;
Expand Down Expand Up @@ -85,9 +86,9 @@ public function testTicksDirective()

$this->assertEquals(
[
'php.min' => '7.0.0',
'php.min' => '4.0.0',
'php.max' => '',
'php.all' => '7.0.0',
'php.all' => '4.0.0',
],
$versions
);
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit e75f691

Please sign in to comment.