Skip to content

Commit

Permalink
add UseTraitSniff to solve issue gh-227
Browse files Browse the repository at this point in the history
  • Loading branch information
llaville committed Aug 16, 2020
1 parent 25db2cc commit 36f2ebe
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/Bartlett/CompatInfo/Analyser/CompatibilityAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Bartlett\CompatInfo\Sniffs\Operators\PowOperatorSniff;
use Bartlett\CompatInfo\Sniffs\Operators\ShortTernaryOperatorSniff;
use Bartlett\CompatInfo\Sniffs\UseDeclarations\UseConstFunctionSniff;
use Bartlett\CompatInfo\Sniffs\UseDeclarations\UseTraitSniff;
use Bartlett\CompatInfo\Util\Database;
use Bartlett\CompatInfo\Collection\ReferenceCollection;
use Bartlett\CompatInfo\PhpParser\ConditionalCodeNodeProcessor;
Expand Down Expand Up @@ -95,6 +96,7 @@ public function __construct()
new ShortTernaryOperatorSniff(),
// UseDeclarations
new UseConstFunctionSniff(),
new UseTraitSniff(),
];

parent::__construct(self::PARENT_NODE_ATTRIBUTE, self::ANALYSER_NODE_ATTRIBUTE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public function normalize($node, $format = null, array $context = [])
$keys[] = (string) $use->name;
}
$name = implode(', ', $keys);
} elseif ($node instanceof Node\Stmt\TraitUse) {
$keys = [];
foreach ($node->traits as $trait) {
$keys[] = (string) $trait;
}
$name = implode(', ', $keys);
} elseif ($node instanceof Node\Stmt\Property) {
$props = [];
foreach ($node->props as $prop) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ public function getInterfaces(): array

public function getTraits(): array
{
return $this->data['Stmt_Trait'] ?? [];
return $this->mergeElementVersion(
$this->data['Stmt_Trait'] ?? [],
['Stmt_TraitUse']
);
}

public function getMethods(): array
Expand Down
27 changes: 27 additions & 0 deletions src/Bartlett/CompatInfo/Sniffs/UseDeclarations/UseTraitSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

namespace Bartlett\CompatInfo\Sniffs\UseDeclarations;

use Bartlett\CompatInfo\Sniffs\SniffAbstract;

use PhpParser\Node;

/**
* Use trait is PHP 5.4 or greater
*
* @link https://www.php.net/manual/en/language.oop5.traits.php
*/
class UseTraitSniff extends SniffAbstract
{
/**
* {@inheritDoc}
*/
public function enterNode(Node $node)
{
if (!$node instanceof Node\Stmt\TraitUse) {
return;
}

$this->updateNodeElementVersion($node, $this->attributeKeyStore, ['php.min' => '5.4.0']);
}
}
25 changes: 0 additions & 25 deletions tests/PhpFeaturesIssueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ final class PhpFeaturesIssueTest extends \PHPUnit\Framework\TestCase
private const GH218 = 'gh218.php';
private const GH222 = 'gh222.php';
private const GH226 = 'gh226.php';
private const GH227 = 'gh227.php';
private const GH228 = 'gh228.php';
private const GH238 = 'gh238.php';
private const GH239 = 'gh239.php';
Expand Down Expand Up @@ -354,30 +353,6 @@ public function testFeatureGH226()
);
}

/**
* Regression test for feature GH#227
*
* @link https://github.com/llaville/php-compat-info/issues/227
* Does not detect Use traits
* @group features
* @return void
*/
public function testFeatureGH227()
{
$dataSource = self::$fixtures . self::GH227;
$analysers = ['compatibility'];
$metrics = self::$api->run($dataSource, $analysers);
$versions = $metrics[self::$analyserId]['versions'];
$this->assertEquals(
[
'php.min' => '5.4.0',
'php.max' => '',
'php.all' => '5.4.0',
],
$versions
);
}

/**
* Regression test for feature GH#228
*
Expand Down
45 changes: 45 additions & 0 deletions tests/Sniffs/UseTraitSniffTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

namespace Bartlett\Tests\CompatInfo\Sniffs;

/**
* @link https://github.com/llaville/php-compat-info/issues/227
*/
class UseTraitSniffTest extends SniffTestCase
{
/**
* {@inheritDoc}
*/
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

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

/**
* Regression test for use trait detection
*
* @link https://github.com/llaville/php-compat-info/issues/227
* Does not detect Use traits
* @group features
* @group regression
* @return void
*/
public function testRegressionGH227()
{
$dataSource = self::$fixtures . 'gh227.php';
$analysers = ['compatibility'];
$metrics = self::$api->run($dataSource, $analysers);
$classes = $metrics[self::$analyserId]['classes'];

$this->assertEquals(
'5.4.0',
$classes['A']['php.min']
);
$this->assertEquals(
'',
$classes['A']['php.max']
);
}
}
File renamed without changes.

0 comments on commit 36f2ebe

Please sign in to comment.