Skip to content

Commit

Permalink
use ShortTernaryOperatorSniff to detect elvis syntax and move gh-211
Browse files Browse the repository at this point in the history
…unit test case
  • Loading branch information
llaville committed Aug 16, 2020
1 parent 4fb56b8 commit 1cb1aea
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 85 deletions.
4 changes: 3 additions & 1 deletion src/Bartlett/CompatInfo/Analyser/CompatibilityAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Bartlett\CompatInfo\Sniffs\Keywords\ReservedSniff;
use Bartlett\CompatInfo\Sniffs\Operators\NullCoalesceOperatorSniff;
use Bartlett\CompatInfo\Sniffs\Operators\PowOperatorSniff;
use Bartlett\CompatInfo\Sniffs\Operators\ShortTernaryOperatorSniff;
use Bartlett\CompatInfo\Sniffs\UseDeclarations\UseConstFunctionSniff;
use Bartlett\CompatInfo\Util\Database;
use Bartlett\CompatInfo\Collection\ReferenceCollection;
Expand Down Expand Up @@ -89,6 +90,7 @@ public function __construct()
// Operators
new NullCoalesceOperatorSniff(),
new PowOperatorSniff(),
new ShortTernaryOperatorSniff(),
// UseDeclarations
new UseConstFunctionSniff(),
];
Expand Down Expand Up @@ -539,7 +541,7 @@ private function _leaveExprEmpty(Node\Expr\Empty_ $node): void
$this->updateContextVersion($versions);
}
}

private function _leaveExprClassConstFetch(Node\Expr\ClassConstFetch $node): void
{
if (strcasecmp('class', (string) $node->name) !== 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function normalize($node, $format = null, array $context = [])
} elseif ($node instanceof Node\Expr\BinaryOp\Coalesce
|| $node instanceof Node\Expr\BinaryOp\Pow
|| $node instanceof Node\Expr\AssignOp\Pow
|| $node instanceof Node\Expr\Ternary
) {
$name = '';
} elseif (property_exists($node, 'namespacedName')) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);

namespace Bartlett\CompatInfo\Sniffs\Operators;

use Bartlett\CompatInfo\Sniffs\SniffAbstract;

use PhpParser\Node;

/**
* Use of Elvis syntax (middle portion of ternary operator missing) since PHP 5.3
*
* @since https://www.php.net/manual/en/migration53.php
* @since https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
*/
class ShortTernaryOperatorSniff extends SniffAbstract
{
/**
* {@inheritDoc}
*/
public function leaveNode(Node $node)
{
if (!$node instanceof Node\Expr\Ternary) {
return null;
}

if (null === $node->if) {
$this->updateNodeElementVersion($node, $this->attributeKeyStore, ['php.min' => '5.3.0']);
}
}
}

This file was deleted.

25 changes: 0 additions & 25 deletions tests/PhpFeaturesIssueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ final class PhpFeaturesIssueTest extends \PHPUnit\Framework\TestCase
private const GH168 = 'gh168.php';
private const GH200 = 'gh200.php';
private const GH207 = 'gh207.php';
private const GH211 = 'gh211.php';
private const GH213 = 'gh213.php';
private const GH215 = 'gh215.php';
private const GH218 = 'gh218.php';
Expand Down Expand Up @@ -232,31 +231,7 @@ public function testFeatureGH207()
);
}

/**
* Regression test for feature GH#211
*
* @link https://github.com/llaville/php-compat-info/issues/211
* PHP 5.6 **-Operator not recognized
* @link http://php.net/manual/en/migration56.new-features.php#migration56.new-features.exponentiation
* @group features
* @return void
*/
public function testFeatureGH211()
{
$dataSource = self::$fixtures . self::GH211;
$analysers = ['compatibility'];
$metrics = self::$api->run($dataSource, $analysers);
$versions = $metrics[self::$analyserId]['versions'];

$this->assertEquals(
[
'php.min' => '5.6.0',
'php.max' => '',
'php.all' => '5.6.0',
],
$versions
);
}

/**
* Regression test for feature GH#213
Expand Down
28 changes: 28 additions & 0 deletions tests/Sniffs/PowOperatorSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/**
* @link https://github.com/llaville/php-compat-info/issues/142
* @link https://github.com/llaville/php-compat-info/issues/211
*/
class PowOperatorSniffTest extends SniffTestCase
{
Expand Down Expand Up @@ -52,4 +53,31 @@ public function testRegressionGH142()
$functions['bar']['php.max']
);
}

/**
* Regression test for Exponentiation feature detection GH#211
*
* @link https://github.com/llaville/php-compat-info/issues/211
* PHP 5.6 **-Operator not recognized
* @link https://www.php.net/manual/en/migration56.new-features.php#migration56.new-features.exponentiation
* @group features
* @group regression
* @return void
*/
public function testFeatureGH211()
{
$dataSource = self::$fixtures . 'gh211.php';
$analysers = ['compatibility'];
$metrics = self::$api->run($dataSource, $analysers);
$versions = $metrics[self::$analyserId]['versions'];

$this->assertEquals(
'5.6.0',
$versions['php.min']
);
$this->assertEquals(
'',
$versions['php.max']
);
}
}
40 changes: 40 additions & 0 deletions tests/Sniffs/ShortTernaryOperatorSniffTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);

namespace Bartlett\Tests\CompatInfo\Sniffs;

class ShortTernaryOperatorSniffTest extends SniffTestCase
{
/**
* {@inheritDoc}
*/
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

self::$fixtures .= 'operators' . DIRECTORY_SEPARATOR;
}

/**
* Regression test for short ternary operator (Elvis syntax)
*
* @group features
* @return void
*/
public function testElvisSyntax()
{
$dataSource = self::$fixtures . 'elvis_syntax.php';
$analysers = ['compatibility'];
$metrics = self::$api->run($dataSource, $analysers);
$versions = $metrics[self::$analyserId]['versions'];

$this->assertEquals(
'5.3.0',
$versions['php.min']
);
$this->assertEquals(
'',
$versions['php.max']
);
}

}
3 changes: 3 additions & 0 deletions tests/fixtures/sniffs/operators/elvis_syntax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
echo 0 ?: 1 ?: 2 ?: 3; // output: 1
echo PHP_EOL;
File renamed without changes.

0 comments on commit 1cb1aea

Please sign in to comment.