Skip to content
Snippets Groups Projects
Commit b30cfacc authored by David Prévot's avatar David Prévot
Browse files

New upstream version 3.2.0

parents f6a13f0b 68ff824b
Branches
Tags upstream/3.2.0
No related merge requests found
......@@ -7,7 +7,7 @@ on:
name: "CI"
env:
COMPOSER_ROOT_VERSION: "3.1-dev"
COMPOSER_ROOT_VERSION: "3.2-dev"
permissions:
contents: read
......
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="php-cs-fixer" version="^3.0" installed="3.27.0" location="./tools/php-cs-fixer" copy="true"/>
<phar name="psalm" version="^5.0" installed="5.15.0" location="./tools/psalm" copy="true"/>
<phar name="php-cs-fixer" version="^3.0" installed="3.41.1" location="./tools/php-cs-fixer" copy="true"/>
<phar name="psalm" version="^5.0" installed="5.18.0" location="./tools/psalm" copy="true"/>
<phar name="infection" version="^0.24" installed="0.24.0" location="./tools/infection" copy="true"/>
<phar name="composer" version="^2.0.3" installed="2.6.3" location="./tools/composer" copy="true"/>
<phar name="composer" version="^2.0.3" installed="2.6.6" location="./tools/composer" copy="true"/>
</phive>
......@@ -2,6 +2,16 @@
All notable changes are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.
## [3.2.0] - 2023-12-21
### Added
* `ComplexityCollection::sortByDescendingCyclomaticComplexity()`
### Changed
* This component is now compatible with `nikic/php-parser` 5.0
## [3.1.0] - 2023-09-28
### Added
......@@ -48,6 +58,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
* Initial release
[3.2.0]: https://github.com/sebastianbergmann/complexity/compare/3.1.0...3.2.0
[3.1.0]: https://github.com/sebastianbergmann/complexity/compare/3.0.1...3.1.0
[3.0.1]: https://github.com/sebastianbergmann/complexity/compare/3.0.0...3.0.1
[3.0.0]: https://github.com/sebastianbergmann/complexity/compare/2.0.2...3.0.0
......
......@@ -18,7 +18,7 @@
"prefer-stable": true,
"require": {
"php": ">=8.1",
"nikic/php-parser": "^4.10"
"nikic/php-parser": "^4.18 || ^5.0"
},
"require-dev": {
"phpunit/phpunit": "^10.0"
......@@ -37,7 +37,7 @@
},
"extra": {
"branch-alias": {
"dev-main": "3.1-dev"
"dev-main": "3.2-dev"
}
}
}
......@@ -12,12 +12,10 @@ namespace SebastianBergmann\Complexity;
use function assert;
use function file_get_contents;
use PhpParser\Error;
use PhpParser\Lexer;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\NodeVisitor\ParentConnectingVisitor;
use PhpParser\Parser;
use PhpParser\ParserFactory;
final class Calculator
......@@ -36,7 +34,7 @@ final class Calculator
public function calculateForSourceString(string $source): ComplexityCollection
{
try {
$nodes = $this->parser()->parse($source);
$nodes = (new ParserFactory)->createForHostVersion()->parse($source);
assert($nodes !== null);
......@@ -82,9 +80,4 @@ final class Calculator
return $complexityCalculatingVisitor->result();
}
private function parser(): Parser
{
return (new ParserFactory)->create(ParserFactory::PREFER_PHP7, new Lexer);
}
}
......@@ -11,8 +11,10 @@ namespace SebastianBergmann\Complexity;
use function array_filter;
use function array_merge;
use function array_reverse;
use function array_values;
use function count;
use function usort;
use Countable;
use IteratorAggregate;
......@@ -112,4 +114,19 @@ final class ComplexityCollection implements Countable, IteratorAggregate
),
);
}
public function sortByDescendingCyclomaticComplexity(): self
{
$items = $this->items;
usort(
$items,
static function (Complexity $a, Complexity $b): int
{
return $a->cyclomaticComplexity() <=> $b->cyclomaticComplexity();
},
);
return new self(array_reverse($items));
}
}
......@@ -11,8 +11,6 @@ namespace SebastianBergmann\Complexity;
use function assert;
use function file_get_contents;
use PhpParser\Lexer;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Medium;
......@@ -62,7 +60,7 @@ final class CalculatorTest extends TestCase
public function testCalculatesCyclomaticComplexityInAbstractSyntaxTree(): void
{
$nodes = $this->parser()->parse(file_get_contents(__DIR__ . '/../_fixture/ExampleClass.php'));
$nodes = (new ParserFactory)->createForHostVersion()->parse(file_get_contents(__DIR__ . '/../_fixture/ExampleClass.php'));
assert($nodes !== null);
......@@ -71,9 +69,4 @@ final class CalculatorTest extends TestCase
$this->assertSame('SebastianBergmann\Complexity\TestFixture\ExampleClass::method', $result[0]->name());
$this->assertSame(14, $result[0]->cyclomaticComplexity());
}
private function parser(): Parser
{
return (new ParserFactory)->create(ParserFactory::PREFER_PHP7, new Lexer);
}
}
......@@ -10,13 +10,11 @@
namespace SebastianBergmann\Complexity;
use function file_get_contents;
use PhpParser\Lexer;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\NodeVisitor\ParentConnectingVisitor;
use PhpParser\NodeVisitorAbstract;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
......@@ -43,7 +41,7 @@ final class ComplexityCalculatingVisitorTest extends TestCase
#[DataProvider('shortCircuitTraversalProvider')]
public function testCalculatesComplexityForAbstractSyntaxTreeOfClass(bool $shortCircuitTraversal): void
{
$nodes = $this->parser()->parse(
$nodes = (new ParserFactory)->createForHostVersion()->parse(
file_get_contents(__DIR__ . '/../_fixture/ExampleClass.php'),
);
......@@ -86,7 +84,7 @@ final class ComplexityCalculatingVisitorTest extends TestCase
#[DataProvider('shortCircuitTraversalProvider')]
public function testCalculatesComplexityForAbstractSyntaxTreeOfAnonymousClass(bool $shortCircuitTraversal): void
{
$nodes = $this->parser()->parse(
$nodes = (new ParserFactory)->createForHostVersion()->parse(
file_get_contents(__DIR__ . '/../_fixture/anonymous_class.php'),
);
......@@ -129,7 +127,7 @@ final class ComplexityCalculatingVisitorTest extends TestCase
#[DataProvider('shortCircuitTraversalProvider')]
public function testCalculatesComplexityForAbstractSyntaxTreeOfInterface(bool $shortCircuitTraversal): void
{
$nodes = $this->parser()->parse(
$nodes = (new ParserFactory)->createForHostVersion()->parse(
file_get_contents(__DIR__ . '/../_fixture/ExampleInterface.php'),
);
......@@ -163,9 +161,4 @@ final class ComplexityCalculatingVisitorTest extends TestCase
$this->assertSame(0, $complexityCalculatingVisitor->result()->cyclomaticComplexity());
$this->assertSame(11, $shortCircuitVisitor->numberOfNodesVisited());
}
private function parser(): Parser
{
return (new ParserFactory)->create(ParserFactory::PREFER_PHP7, new Lexer);
}
}
......@@ -97,4 +97,12 @@ final class ComplexityCollectionTest extends TestCase
$this->assertTrue($collection->isFunction()->asArray()[0]->isFunction());
$this->assertTrue($collection->isMethod()->asArray()[0]->isMethod());
}
public function testCanBeSorted(): void
{
$collection = ComplexityCollection::fromList($this->array[0], $this->array[1])->sortByDescendingCyclomaticComplexity();
$this->assertSame('function', $collection->asArray()[0]->name());
$this->assertSame('Class::method', $collection->asArray()[1]->name());
}
}
......@@ -10,9 +10,7 @@
namespace SebastianBergmann\Complexity;
use function file_get_contents;
use PhpParser\Lexer;
use PhpParser\NodeTraverser;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
......@@ -24,7 +22,7 @@ final class CyclomaticComplexityCalculatingVisitorTest extends TestCase
{
public function testCalculatesCyclomaticComplexityForAbstractSyntaxTree(): void
{
$nodes = $this->parser()->parse(
$nodes = (new ParserFactory)->createForHostVersion()->parse(
file_get_contents(__DIR__ . '/../_fixture/example_function.php'),
);
......@@ -39,9 +37,4 @@ final class CyclomaticComplexityCalculatingVisitorTest extends TestCase
$this->assertSame(14, $visitor->cyclomaticComplexity());
}
private function parser(): Parser
{
return (new ParserFactory)->create(ParserFactory::PREFER_PHP7, new Lexer);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment