Skip to content

Commit

Permalink
upgrade to PHPUnit 10, apply Rector, build only 1 version to avoid up…
Browse files Browse the repository at this point in the history
…grading issues
  • Loading branch information
TomasVotruba committed Sep 8, 2023
1 parent c5be1ca commit 6db1753
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 32 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/downgraded_release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ jobs:
-
name: "Tag Downgraded Code"
run: |
git commit -a -m "release PHP 7.2 downgraded ${GITHUB_REF#refs/tags/}"
git tag "${GITHUB_REF#refs/tags/}.72"
git push origin "${GITHUB_REF#refs/tags/}.72"
# separate a "git add" to add untracked (new) files too
git add --all
git commit -m "release PHP 7.2 downgraded"
# force push tag, so there is only 1 version
git tag "${GITHUB_REF#refs/tags/}" --force
git push origin "${GITHUB_REF#refs/tags/}" --force
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/vendor
composer.lock

.phpunit.result.cache
.phpunit.cache
4 changes: 2 additions & 2 deletions build/composer-php-72.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "PHPStan rules to measure cognitive complexity of your classes and methods",
"license": "MIT",
"require": {
"php": "^7.2 || 8.0.*",
"phpstan/phpstan": "^1.9.3",
"php": "^7.2 || ^8.0",
"phpstan/phpstan": "^1.10",
"nette/utils": "^3.2"
},
"autoload": {
Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
"license": "MIT",
"require": {
"php": "^8.1",
"phpstan/phpstan": "^1.9.3",
"nette/utils": "^3.2|^4.0"
"phpstan/phpstan": "^1.10",
"nette/utils": "^3.2"
},
"require-dev": {
"phpstan/extension-installer": "^1.2",
"phpunit/phpunit": "^9.5",
"symplify/easy-coding-standard": "^11.2",
"rector/rector": "^0.15.21",
"phpstan/extension-installer": "^1.3",
"phpunit/phpunit": "^10.3",
"symplify/easy-coding-standard": "^12.0",
"rector/rector": "^0.18",
"tracy/tracy": "^2.9",
"php-parallel-lint/php-parallel-lint": "^1.3",
"tomasvotruba/type-coverage": "^0.0.11",
"tomasvotruba/unused-public": "^0.1"
"tomasvotruba/type-coverage": "^0.2",
"tomasvotruba/unused-public": "^0.3"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
cacheDirectory=".phpunit.cache"
>
<testsuite name="all">
<directory>tests</directory>
Expand Down
7 changes: 1 addition & 6 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\CodingStyle\Rector\ClassConst\VarConstantCommentRector;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
Expand All @@ -17,6 +15,7 @@
$rectorConfig->importNames();

$rectorConfig->sets([
\Rector\PHPUnit\Set\PHPUnitSetList::PHPUNIT_100,
LevelSetList::UP_TO_PHP_81,
SetList::TYPE_DECLARATION,
SetList::PRIVATIZATION,
Expand All @@ -29,9 +28,5 @@
$rectorConfig->skip([
'*/Fixture/*',
'*/Source/*',

VarConstantCommentRector::class => [
__DIR__ . '/src/PublicClassMethodMatcher.php',
],
]);
};
6 changes: 4 additions & 2 deletions src/NodeAnalyzer/ComplexityAffectingNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace TomasVotruba\CognitiveComplexity\NodeAnalyzer;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Catch_;
Expand Down Expand Up @@ -70,11 +72,11 @@ public function isBreakingNode(Node $node): bool
if ($this->isInstanceOf($node, self::BREAKING_NODE_TYPES)) {
// skip empty breaks
/** @var Goto_|Break_|Continue_ $node */
if ($node instanceof Goto_ && $node->name !== null) {
if ($node instanceof Goto_ && $node->name instanceof Identifier) {
return true;
}

if (($node instanceof Break_ || $node instanceof Continue_) && $node->num !== null) {
if (($node instanceof Break_ || $node instanceof Continue_) && $node->num instanceof Expr) {
return true;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Rules/FunctionLikeCognitiveComplexityRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Rules\Rule;
use TomasVotruba\CognitiveComplexity\AstCognitiveComplexityAnalyzer;
use TomasVotruba\CognitiveComplexity\Configuration;
Expand Down Expand Up @@ -86,7 +87,7 @@ private function resolveFunctionName(FunctionLike $functionLike, Scope $scope):
$name = '';

$classReflection = $scope->getClassReflection();
if ($classReflection !== null) {
if ($classReflection instanceof ClassReflection) {
$name = $classReflection->getName() . '::';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\NodeFinder;
use PhpParser\ParserFactory;
use PHPStan\DependencyInjection\ContainerFactory;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use TomasVotruba\CognitiveComplexity\AstCognitiveComplexityAnalyzer;
use TomasVotruba\CognitiveComplexity\Exception\ShouldNotHappenException;
Expand All @@ -30,9 +31,7 @@ protected function setUp(): void
$this->astCognitiveComplexityAnalyzer = $container->getByType(AstCognitiveComplexityAnalyzer::class);
}

/**
* @dataProvider provideTokensAndExpectedCognitiveComplexity()
*/
#[DataProvider('provideTokensAndExpectedCognitiveComplexity')]
public function test(string $filePath, int $expectedCognitiveComlexity): void
{
$fileContents = FileSystem::read($filePath);
Expand All @@ -43,7 +42,7 @@ public function test(string $filePath, int $expectedCognitiveComlexity): void
$this->assertSame($expectedCognitiveComlexity, $cognitiveComplexity);
}

public function provideTokensAndExpectedCognitiveComplexity(): Iterator
public static function provideTokensAndExpectedCognitiveComplexity(): Iterator
{
yield [__DIR__ . '/Fixture/function_9.php.inc', 9];
yield [__DIR__ . '/Fixture/function_6.php.inc', 6];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@
use Iterator;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use TomasVotruba\CognitiveComplexity\Rules\ClassLikeCognitiveComplexityRule;

final class ClassLikeCognitiveComplexityRuleTest extends RuleTestCase
{
/**
* @dataProvider provideDataForTest()
* @param mixed[] $expectedErrorMessagesWithLines
*/
#[DataProvider('provideDataForTest')]
public function test(string $filePath, array $expectedErrorMessagesWithLines): void
{
$this->analyse([$filePath], $expectedErrorMessagesWithLines);
}

public function provideDataForTest(): Iterator
public static function provideDataForTest(): Iterator
{
$errorMessage = sprintf(ClassLikeCognitiveComplexityRule::ERROR_MESSAGE, 54, 50);
yield [__DIR__ . '/Fixture/ClassWithManyComplexMethods.php', [[$errorMessage, 7]]];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@
use Iterator;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use TomasVotruba\CognitiveComplexity\Rules\FunctionLikeCognitiveComplexityRule;
use TomasVotruba\CognitiveComplexity\Tests\Rules\FunctionLikeCognitiveComplexityRule\Fixture\ClassMethodOverComplicated;
use TomasVotruba\CognitiveComplexity\Tests\Rules\FunctionLikeCognitiveComplexityRule\Fixture\VideoRepository;

final class FunctionLikeCognitiveComplexityRuleTest extends RuleTestCase
{
/**
* @dataProvider provideDataForTest()
* @param mixed[] $expectedErrorMessagesWithLines
*/
#[DataProvider('provideDataForTest')]
public function test(string $filePath, array $expectedErrorMessagesWithLines): void
{
$this->analyse([$filePath], $expectedErrorMessagesWithLines);
}

public function provideDataForTest(): Iterator
public static function provideDataForTest(): Iterator
{
$errorMessage = sprintf(FunctionLikeCognitiveComplexityRule::ERROR_MESSAGE, 'someFunction()', 9, 8);
yield [__DIR__ . '/Fixture/function.php.inc', [[$errorMessage, 3]]];
Expand Down

0 comments on commit 6db1753

Please sign in to comment.