Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Add rule error identifiers #876

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/integrate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
- name: "Collect code coverage with Xdebug and phpunit/phpunit"
env:
XDEBUG_MODE: "coverage"
run: "vendor/bin/phpunit --colors=always --configuration=test/Integration/phpunit.xml --coverage-clover=.build/phpunit/logs/clover.xml"
run: "vendor/bin/phpunit --colors=always --configuration=test/phpunit.xml --coverage-clover=.build/phpunit/logs/clover.xml"

- name: "Send code coverage report to codecov.io"
uses: "codecov/codecov-action@v5.0.7"
Expand Down Expand Up @@ -358,5 +358,8 @@ jobs:
with:
dependencies: "${{ matrix.dependencies }}"

- name: "Run unit tests with phpunit/phpunit"
run: "vendor/bin/phpunit --colors=always --configuration=test/phpunit.xml --testsuite=unit"

- name: "Run integration tests with phpunit/phpunit"
run: "vendor/bin/phpunit --colors=always --configuration=test/Integration/phpunit.xml"
run: "vendor/bin/phpunit --colors=always --configuration=test/phpunit.xml --testsuite=integration"
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

For a full diff see [`2.4.0...main`][2.4.0...main].

### Added

- Added rule error identifiers ([#875]), by [@localheinz]

## [`2.4.0`][2.4.0]

For a full diff see [`2.3.0...2.4.0`][2.3.0...2.4.0].
Expand Down Expand Up @@ -523,6 +527,7 @@ For a full diff see [`362c7ea...0.1.0`][362c7ea...0.1.0].
[#735]: /~https://github.com/ergebnis/phpstan-rules/pull/735
[#862]: /~https://github.com/ergebnis/phpstan-rules/pull/862
[#872]: /~https://github.com/ergebnis/phpstan-rules/pull/872
[#875]: /~https://github.com/ergebnis/phpstan-rules/pull/875

[@enumag]: /~https://github.com/enumag
[@ergebnis]: /~https://github.com/ergebnis
Expand Down
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
it: coding-standards security-analysis static-code-analysis tests ## Runs the coding-standards, security-analysis, static-code-analysis, and tests targets

.PHONY: code-coverage
code-coverage: vendor ## Collects coverage from running integration tests with phpunit/phpunit
vendor/bin/phpunit --configuration=test/Integration/phpunit.xml --coverage-text
code-coverage: vendor ## Collects coverage from running unit and integration tests with phpunit/phpunit
vendor/bin/phpunit --configuration=test/phpunit.xml --coverage-text

.PHONY: coding-standards
coding-standards: vendor ## Lints YAML files with yamllint, normalizes composer.json with ergebnis/composer-normalize, and fixes code style issues with friendsofphp/php-cs-fixer
Expand Down Expand Up @@ -39,8 +39,9 @@ static-code-analysis-baseline: vendor ## Generates a baseline for static code an
vendor/bin/phpstan --allow-empty-baseline --configuration=phpstan.neon --generate-baseline=phpstan-baseline.neon --memory-limit=-1

.PHONY: tests
tests: vendor ## Runs integration tests with phpunit/phpunit
vendor/bin/phpunit --configuration=test/Integration/phpunit.xml
tests: vendor ## Runs unit and integration tests with phpunit/phpunit
vendor/bin/phpunit --configuration=test/phpunit.xml --testsuite=unit
vendor/bin/phpunit --configuration=test/phpunit.xml --testsuite=integration

vendor: composer.json composer.lock
composer validate --strict
Expand Down
3 changes: 2 additions & 1 deletion src/Classes/FinalRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPStan\Rules\Classes;

use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Comment;
use PhpParser\Node;
use PHPStan\Analyser;
Expand Down Expand Up @@ -107,7 +108,7 @@ public function processNode(
));

return [
$ruleErrorBuilder->build(),
$ruleErrorBuilder->identifier(ErrorIdentifier::final()->toString())->build(),
];
}

Expand Down
5 changes: 3 additions & 2 deletions src/Classes/NoExtendsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPStan\Rules\Classes;

use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
use PHPStan\Rules;
Expand Down Expand Up @@ -83,7 +84,7 @@ public function processNode(
));

return [
$ruleErrorBuilder->build(),
$ruleErrorBuilder->identifier(ErrorIdentifier::noExtends()->toString())->build(),
];
}

Expand All @@ -94,7 +95,7 @@ public function processNode(
));

return [
$ruleErrorBuilder->build(),
$ruleErrorBuilder->identifier(ErrorIdentifier::noExtends()->toString())->build(),
];
}
}
3 changes: 2 additions & 1 deletion src/Classes/PHPUnit/Framework/TestCaseWithSuffixRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPStan\Rules\Classes\PHPUnit\Framework;

use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
use PHPStan\Reflection;
Expand Down Expand Up @@ -96,7 +97,7 @@ public function processNode(
));

return [
$ruleErrorBuilder->build(),
$ruleErrorBuilder->identifier(ErrorIdentifier::testCaseWithSuffix()->toString())->build(),
];
}
}
3 changes: 2 additions & 1 deletion src/Closures/NoNullableReturnTypeDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPStan\Rules\Closures;

use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
use PHPStan\Rules;
Expand Down Expand Up @@ -47,7 +48,7 @@ public function processNode(
$ruleErrorBuilder = Rules\RuleErrorBuilder::message('Closure has a nullable return type declaration.');

return [
$ruleErrorBuilder->build(),
$ruleErrorBuilder->identifier(ErrorIdentifier::noNullableReturnTypeDeclaration()->toString())->build(),
];
}

Expand Down
3 changes: 2 additions & 1 deletion src/Closures/NoParameterWithNullDefaultValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPStan\Rules\Closures;

use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
use PHPStan\Rules;
Expand Down Expand Up @@ -68,7 +69,7 @@ public function processNode(
$parameterName,
));

return $ruleErrorBuilder->build();
return $ruleErrorBuilder->identifier(ErrorIdentifier::noParameterWithNullDefaultValue()->toString())->build();
}, $params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPStan\Rules\Closures;

use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
use PHPStan\Rules;
Expand Down Expand Up @@ -64,7 +65,7 @@ public function processNode(
$parameterName,
));

return $ruleErrorBuilder->build();
return $ruleErrorBuilder->identifier(ErrorIdentifier::noParameterWithNullableTypeDeclaration()->toString())->build();
}, $params);
}

Expand Down
112 changes: 112 additions & 0 deletions src/ErrorIdentifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2018-2024 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see /~https://github.com/ergebnis/phpstan-rules
*/

namespace Ergebnis\PHPStan\Rules;

/**
* @internale
*/
final class ErrorIdentifier
{
private function __construct(private readonly string $value)
{
}

public static function declareStrictTypes(): self
{
return new self('declareStrictTypes');
}

public static function final(): self
{
return new self('final');
}

public static function finalInAbstractClass(): self
{
return new self('finalInAbstractClass');
}

public static function noCompact(): self
{
return new self('noCompact');
}

public static function noConstructorParameterWithDefaultValue(): self
{
return new self('noConstructorParameterWithDefaultValue');
}

public static function noErrorSuppression(): self
{
return new self('noErrorSuppression');
}

public static function noEval(): self
{
return new self('noEval');
}

public static function noExtends(): self
{
return new self('noExtends');
}

public static function noIsset(): self
{
return new self('noIsset');
}

public static function noParameterWithContainerTypeDeclaration(): self
{
return new self('noParameterWithContainerTypeDeclaration');
}

public static function noParameterWithNullDefaultValue(): self
{
return new self('noParameterWithNullDefaultValue');
}

public static function noParameterWithNullableTypeDeclaration(): self
{
return new self('noParameterWithNullableTypeDeclaration');
}

public static function noNullableReturnTypeDeclaration(): self
{
return new self('noNullableReturnTypeDeclaration');
}

public static function noSwitch(): self
{
return new self('noSwitch');
}

public static function privateInFinalClass(): self
{
return new self('privateInFinalClass');
}

public static function testCaseWithSuffix(): self
{
return new self('testCaseWithSuffix');
}

public function toString(): string
{
return \sprintf(
'ergebnis.%s',
$this->value,
);
}
}
3 changes: 2 additions & 1 deletion src/Expressions/NoCompactRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPStan\Rules\Expressions;

use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
use PHPStan\Rules;
Expand Down Expand Up @@ -51,7 +52,7 @@ public function processNode(
$ruleErrorBuilder = Rules\RuleErrorBuilder::message('Function compact() should not be used.');

return [
$ruleErrorBuilder->build(),
$ruleErrorBuilder->identifier(ErrorIdentifier::noCompact()->toString())->build(),
];
}
}
3 changes: 2 additions & 1 deletion src/Expressions/NoErrorSuppressionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPStan\Rules\Expressions;

use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
use PHPStan\Rules;
Expand All @@ -34,7 +35,7 @@ public function processNode(
$ruleErrorBuilder = Rules\RuleErrorBuilder::message('Error suppression via "@" should not be used.');

return [
$ruleErrorBuilder->build(),
$ruleErrorBuilder->identifier(ErrorIdentifier::noErrorSuppression()->toString())->build(),
];
}
}
3 changes: 2 additions & 1 deletion src/Expressions/NoEvalRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPStan\Rules\Expressions;

use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
use PHPStan\Rules;
Expand All @@ -34,7 +35,7 @@ public function processNode(
$ruleErrorBuilder = Rules\RuleErrorBuilder::message('Language construct eval() should not be used.');

return [
$ruleErrorBuilder->build(),
$ruleErrorBuilder->identifier(ErrorIdentifier::noEval()->toString())->build(),
];
}
}
3 changes: 2 additions & 1 deletion src/Expressions/NoIssetRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPStan\Rules\Expressions;

use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
use PHPStan\Rules;
Expand All @@ -34,7 +35,7 @@ public function processNode(
$ruleErrorBuilder = Rules\RuleErrorBuilder::message('Language construct isset() should not be used.');

return [
$ruleErrorBuilder->build(),
$ruleErrorBuilder->identifier(ErrorIdentifier::noIsset()->toString())->build(),
];
}
}
3 changes: 2 additions & 1 deletion src/Files/DeclareStrictTypesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPStan\Rules\Files;

use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
use PHPStan\Node\FileNode;
Expand Down Expand Up @@ -72,7 +73,7 @@ public function processNode(
$ruleErrorBuilder = Rules\RuleErrorBuilder::message('File is missing a "declare(strict_types=1)" declaration.');

return [
$ruleErrorBuilder->build(),
$ruleErrorBuilder->identifier(ErrorIdentifier::declareStrictTypes()->toString())->build(),
];
}
}
3 changes: 2 additions & 1 deletion src/Functions/NoNullableReturnTypeDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPStan\Rules\Functions;

use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
use PHPStan\Rules;
Expand Down Expand Up @@ -54,7 +55,7 @@ public function processNode(
));

return [
$ruleErrorBuilder->build(),
$ruleErrorBuilder->identifier(ErrorIdentifier::noNullableReturnTypeDeclaration()->toString())->build(),
];
}

Expand Down
Loading