-
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.
- Loading branch information
Showing
6 changed files
with
202 additions
and
1 deletion.
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
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,67 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Bartlett\CompatInfo\Sniffs\PHP; | ||
|
||
use Bartlett\Reflect\Sniffer\SniffAbstract; | ||
|
||
use PhpParser\Node; | ||
|
||
/** | ||
* Class DeclareSniff to detect declare control structures | ||
* @link https://www.php.net/manual/en/control-structures.declare.php | ||
* @since Class available since Release 5.4.0 | ||
*/ | ||
class DeclareSniff extends SniffAbstract | ||
{ | ||
private $directives; | ||
|
||
public function enterSniff(): void | ||
{ | ||
parent::enterSniff(); | ||
$this->directives = []; | ||
} | ||
|
||
public function leaveSniff(): void | ||
{ | ||
parent::leaveSniff(); | ||
|
||
if (!empty($this->directives)) { | ||
// inform analyser that few sniffs were found | ||
$this->visitor->setMetrics( | ||
array(DeclareSniff::class => $this->directives) | ||
); | ||
} | ||
} | ||
|
||
public function enterNode(Node $node) | ||
{ | ||
parent::enterNode($node); | ||
|
||
if (!$node instanceof Node\Stmt\Declare_) { | ||
return; | ||
} | ||
|
||
foreach ($node->declares as $declare) { | ||
$id = (string) $declare->key; | ||
|
||
if (strcasecmp($id, 'strict_types') === 0) { | ||
$version = '7.0.0'; | ||
} elseif (strcasecmp($id, 'ticks') === 0) { | ||
$version = '7.0.0'; | ||
} elseif (strcasecmp($id, 'encoding') === 0) { | ||
$version = '5.3.0'; | ||
} else { | ||
$version = '4.0.0'; | ||
} | ||
|
||
if (!isset($this->directives[$id])) { | ||
$this->directives[$id] = array( | ||
'severity' => $this->getCurrentSeverity($version), | ||
'version' => $version, | ||
'spots' => [] | ||
); | ||
} | ||
$this->directives[$id]['spots'][] = $this->getCurrentSpot($node); | ||
} | ||
} | ||
} |
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,115 @@ | ||
<?php | ||
/** | ||
* Unit tests for PHP_CompatInfo package, declare sniff | ||
* | ||
* PHP version 7 | ||
* | ||
* @category PHP | ||
* @package PHP_CompatInfo | ||
* @subpackage Tests | ||
* @author Laurent Laville <[email protected]> | ||
* @license https://opensource.org/licenses/BSD-3-Clause The 3-Clause BSD License | ||
* @since Class available since Release 5.4.0 | ||
*/ | ||
|
||
namespace Bartlett\Tests\CompatInfo; | ||
|
||
use Bartlett\CompatInfo\Analyser\CompatibilityAnalyser; | ||
use Bartlett\Reflect\Client; | ||
|
||
/** | ||
* @link https://www.php.net/manual/en/control-structures.declare.php | ||
*/ | ||
class DeclareSniffTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
protected static $fixtures; | ||
protected static $analyserId; | ||
protected static $api; | ||
|
||
/** | ||
* Sets up the shared fixture. | ||
* | ||
* @return void | ||
*/ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
self::$fixtures = __DIR__ . DIRECTORY_SEPARATOR | ||
. 'fixtures' . DIRECTORY_SEPARATOR | ||
. 'sniffs' . DIRECTORY_SEPARATOR | ||
; | ||
|
||
self::$analyserId = CompatibilityAnalyser::class; | ||
|
||
$client = new Client(); | ||
|
||
// request for a Bartlett\Reflect\Api\Analyser | ||
self::$api = $client->api('analyser'); | ||
} | ||
|
||
/** | ||
* Regression test for declare control structure detection GH#268 | ||
* | ||
* @link https://github.com/llaville/php-compat-info/issues/268 | ||
* CompatInfo 5.3.0 is detected as PHP 5.6 | ||
* @group features, regression | ||
* @return void | ||
*/ | ||
public function testStructureTypesDirective() | ||
{ | ||
$dataSource = self::$fixtures . 'declare_strict_types.php'; | ||
$analysers = ['compatibility']; | ||
$metrics = self::$api->run($dataSource, $analysers); | ||
$versions = $metrics[self::$analyserId]['versions']; | ||
|
||
$this->assertEquals( | ||
[ | ||
'php.min' => '7.0.0', | ||
'php.max' => '', | ||
'php.all' => '7.0.0', | ||
], | ||
$versions | ||
); | ||
} | ||
|
||
/** | ||
* @group features | ||
* @return void | ||
*/ | ||
public function testTicksDirective() | ||
{ | ||
$dataSource = self::$fixtures . 'declare_ticks.php'; | ||
$analysers = ['compatibility']; | ||
$metrics = self::$api->run($dataSource, $analysers); | ||
$versions = $metrics[self::$analyserId]['versions']; | ||
|
||
$this->assertEquals( | ||
[ | ||
'php.min' => '7.0.0', | ||
'php.max' => '', | ||
'php.all' => '7.0.0', | ||
], | ||
$versions | ||
); | ||
} | ||
|
||
/** | ||
* @group features | ||
* @return void | ||
*/ | ||
public function testEncodingDirective() | ||
{ | ||
$dataSource = self::$fixtures . 'declare_encoding.php'; | ||
$analysers = ['compatibility']; | ||
$metrics = self::$api->run($dataSource, $analysers); | ||
$versions = $metrics[self::$analyserId]['versions']; | ||
|
||
$this->assertEquals( | ||
[ | ||
'php.min' => '5.3.0', | ||
'php.max' => '', | ||
'php.all' => '5.3.0', | ||
], | ||
$versions | ||
); | ||
} | ||
} |
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,2 @@ | ||
<?php | ||
declare(encoding="utf8"); |
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 @@ | ||
<?php declare(strict_types=1); |
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,2 @@ | ||
<?php | ||
declare(ticks=1); |