Skip to content

Commit 87fa6a9

Browse files
committed
refactor(rules): Rename ForbiddenSideEffectsFunctionLikeRule to ForbiddenSideEffectsRule
- Updated references in configuration files to reflect the new rule name. - Improved clarity and consistency in naming conventions for better maintainability.
1 parent 4fa8bc9 commit 87fa6a9

23 files changed

+79
-72
lines changed

baselines/shipmonk.checkedExceptionInCallable.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ parameters:
55
-
66
message: '#^Throwing checked exception PHPStan\\ShouldNotHappenException in arrow function\!$#'
77
count: 1
8-
path: ../src/Rule/ForbiddenSideEffectsFunctionLikeRule.php
8+
path: ../src/Rule/File/ForbiddenSideEffectsRule.php
99

1010
-
1111
message: '#^Throwing checked exception ErrorException in closure\!$#'

baselines/symplify.forbiddenFuncCall.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ parameters:
55
-
66
message: '#^Function "property_exists\(\)" cannot be used/left in the code$#'
77
count: 1
8-
path: ../src/Rule/ForbiddenSideEffectsFunctionLikeRule.php
8+
path: ../src/Rule/File/ForbiddenSideEffectsRule.php
99

1010
-
1111
message: '#^Function "property_exists\(\)" cannot be used/left in the code\: use ReflectionProvider\-\>has\*\(\) instead$#'
1212
count: 1
13-
path: ../src/Rule/ForbiddenSideEffectsFunctionLikeRule.php
13+
path: ../src/Rule/File/ForbiddenSideEffectsRule.php

composer-dependency-analyser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
)
4444
->ignoreErrorsOnPackageAndPath(
4545
'staabm/side-effects-detector',
46-
__DIR__.'/src/Rule/ForbiddenSideEffectsFunctionLikeRule.php',
46+
__DIR__.'/src/Rule/File/ForbiddenSideEffectsRule.php',
4747
[ErrorType::DEV_DEPENDENCY_IN_PROD]
4848
)
4949
->ignoreErrorsOnPackages(

config/extension.neon

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
# https://github.com/symplify/phpstan-rules/tree/main/config/
33

44
conditionalTags:
5-
Guanguans\PHPStanRules\Rule\ExceptionMustImplementNativeThrowableRule:
5+
Guanguans\PHPStanRules\Rule\New_\ExceptionMustImplementNativeThrowableRule:
66
phpstan.rules.rule: %guanguans.exceptionMustImplementNativeThrowable.enabled%
7-
Guanguans\PHPStanRules\Rule\ForbiddenSideEffectsFunctionLikeRule:
8-
phpstan.rules.rule: %guanguans.forbiddenSideEffectsFunctionLike.enabled%
7+
Guanguans\PHPStanRules\Rule\File\ForbiddenSideEffectsRule:
8+
phpstan.rules.rule: %guanguans.forbiddenSideEffects.enabled%
99

1010
parameters:
1111
guanguans:
1212
allRules: true
1313
exceptionMustImplementNativeThrowable:
1414
nativeThrowable: \Throwable
1515
enabled: %guanguans.allRules%
16-
forbiddenSideEffectsFunctionLike:
16+
forbiddenSideEffects:
1717
enabled: false
1818

1919
parametersSchema:
@@ -23,7 +23,7 @@ parametersSchema:
2323
nativeThrowable: string()
2424
enabled: bool(),
2525
])
26-
forbiddenSideEffectsFunctionLike: structure([
26+
forbiddenSideEffects: structure([
2727
enabled: bool(),
2828
])
2929
])
@@ -33,8 +33,8 @@ services:
3333
class: staabm\SideEffectsDetector\SideEffectsDetector
3434

3535
-
36-
class: Guanguans\PHPStanRules\Rule\ExceptionMustImplementNativeThrowableRule
36+
class: Guanguans\PHPStanRules\Rule\New_\ExceptionMustImplementNativeThrowableRule
3737
arguments:
3838
nativeThrowable: %guanguans.exceptionMustImplementNativeThrowable.nativeThrowable%
3939
-
40-
class: Guanguans\PHPStanRules\Rule\ForbiddenSideEffectsFunctionLikeRule
40+
class: Guanguans\PHPStanRules\Rule\File\ForbiddenSideEffectsRule

phpstan.neon.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ parameters:
6767
exceptionMustImplementNativeThrowable:
6868
nativeThrowable: Guanguans\PHPStanRules\Contract\ThrowableContract
6969
enabled: false
70+
forbiddenSideEffects:
71+
enabled: true
7072
cognitive_complexity:
7173
class: 42
7274
function: 8

src/Rule/ForbiddenSideEffectsFunctionLikeRule.php renamed to src/Rule/File/ForbiddenSideEffectsRule.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
* @see https://github.com/guanguans/phpstan-rules
1212
*/
1313

14-
namespace Guanguans\PHPStanRules\Rule;
14+
namespace Guanguans\PHPStanRules\Rule\File;
1515

16+
use Guanguans\PHPStanRules\Rule\AbstractRule;
1617
use Illuminate\Support\Str;
1718
use PhpParser\Node;
1819
use PhpParser\Node\Stmt;
@@ -23,12 +24,12 @@
2324
use staabm\SideEffectsDetector\SideEffectsDetector;
2425

2526
/**
26-
* @see \Guanguans\PHPStanRulesTests\Rule\ForbiddenSideEffectsFunctionLikeRule\ForbiddenSideEffectsFunctionLikeRuleTest
27+
* @see \Guanguans\PHPStanRulesTests\Rule\File\ForbiddenSideEffectsRule\ForbiddenSideEffectsRuleTest
2728
* @see https://github.com/staabm/side-effects-detector
2829
*
2930
* @extends AbstractRule<FileNode>
3031
*/
31-
final class ForbiddenSideEffectsFunctionLikeRule extends AbstractRule
32+
final class ForbiddenSideEffectsRule extends AbstractRule
3233
{
3334
private const IGNORED_SIDE_EFFECTS = [
3435
'maybe_has_side_effects',
@@ -108,7 +109,7 @@ private function parseNodeCode(Node $node, string $contents): string
108109
private function errorMessage(array $sideEffects): string
109110
{
110111
return \sprintf(
111-
'The function like contains side effects: [%s].',
112+
'The statement contains side effects: [%s].',
112113
implode(', ', $sideEffects)
113114
);
114115
}

src/Rule/ExceptionMustImplementNativeThrowableRule.php renamed to src/Rule/New_/ExceptionMustImplementNativeThrowableRule.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111
* @see https://github.com/guanguans/phpstan-rules
1212
*/
1313

14-
namespace Guanguans\PHPStanRules\Rule;
14+
namespace Guanguans\PHPStanRules\Rule\New_;
1515

16+
use Guanguans\PHPStanRules\Rule\AbstractRule;
1617
use PhpParser\Node;
1718
use PhpParser\Node\Expr\New_;
1819
use PhpParser\Node\Name;
1920
use PHPStan\Analyser\Scope;
2021
use PHPStan\Rules\RuleErrorBuilder;
2122

2223
/**
23-
* @see \Guanguans\PHPStanRulesTests\Rule\ExceptionMustImplementNativeThrowableRule\ExceptionMustImplementNativeThrowableRuleTest
24+
* @see \Guanguans\PHPStanRulesTests\Rule\New_\ExceptionMustImplementNativeThrowableRule\ExceptionMustImplementNativeThrowableRuleTest
2425
* @see \Guanguans\RectorRules\Rector\New_\NewExceptionToNewAnonymousExtendsExceptionImplementsRector
2526
* @see https://github.com/symfony/ai/blob/main/.phpstan/ForbidNativeExceptionRule.php
2627
* @see https://github.com/thecodingmachine/phpstan-strict-rules/tree/master/src/Rules/Exceptions/

tests/Pest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
use Faker\Factory;
2626
use Faker\Generator;
27-
use Guanguans\PHPStanRulesTests\Rector\AbstractRectorTestCase;
27+
use Guanguans\PHPStanRulesTests\Rule\AbstractRuleTestCase;
2828
use Guanguans\PHPStanRulesTests\TestCase;
2929
use Pest\Expectation;
3030

@@ -42,13 +42,13 @@
4242
// __DIR__.'/Unit/'
4343
);
4444

45-
// uses(AbstractRectorTestCase::class)
46-
// // ->compact()
47-
// ->beforeAll(function (): void {})
48-
// ->beforeEach(function (): void {})
49-
// ->afterEach(function (): void {})
50-
// ->afterAll(function (): void {})
51-
// ->in(__DIR__.'/Feature/');
45+
uses(AbstractRuleTestCase::class)
46+
// ->compact()
47+
->beforeAll(function (): void {})
48+
->beforeEach(function (): void {})
49+
->afterEach(function (): void {})
50+
->afterAll(function (): void {})
51+
->in(__DIR__.'/Feature/');
5252

5353
/*
5454
|--------------------------------------------------------------------------

tests/Rule/AbstractRuleTestCase.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323
use Guanguans\PHPStanRules\Rule\AbstractRule;
2424
use Illuminate\Support\Str;
2525
use PHPStan\Testing\RuleTestCase;
26-
use Webmozart\Assert\Assert;
2726

2827
abstract class AbstractRuleTestCase extends RuleTestCase
2928
{
29+
private const ERROR_MESSAGE_METHOD_NAME = 'errorMessage';
30+
3031
/**
3132
* @dataProvider provideRuleCases()
3233
*
33-
* @param array<int, list<int|string>> $expectedErrorMessagesWithLines
34+
* @param list<array{0: string, 1: int, 2?: null|string}> $expectedErrorMessages
3435
*
3536
* @noinspection PhpUndefinedNamespaceInspection
3637
* @noinspection PhpLanguageLevelInspection
@@ -39,21 +40,25 @@ abstract class AbstractRuleTestCase extends RuleTestCase
3940
* @noinspection PhpUnitTestsInspection
4041
*/
4142
#[\PHPUnit\Framework\Attributes\DataProvider('provideRuleCases')]
42-
final public function testRule(string $filePath, array $expectedErrorMessagesWithLines): void
43+
final public function testRule(string $filePath, array $expectedErrorMessages): void
4344
{
44-
Assert::allInteger(array_keys($expectedErrorMessagesWithLines));
45-
$this->analyse([$filePath], $expectedErrorMessagesWithLines);
45+
$this->analyse([$filePath], $expectedErrorMessages);
4646
}
4747

4848
final public function testRuleWithoutErrorMessage(): void
4949
{
5050
$this->analyse(glob(static::directory().'/Fixtures/Skip*.php'), []);
5151
}
5252

53-
final public function testRuleCommon(): void
53+
final public function testRuleBasicInformation(): void
5454
{
55-
// self::assertInstanceOf(static::ruleClass(), $this->getRule());
56-
self::assertTrue(method_exists($this->getRule(), 'errorMessage'));
55+
self::assertTrue(is_subclass_of(static::ruleClass(), AbstractRule::class));
56+
self::assertTrue(method_exists($this->getRule(), self::ERROR_MESSAGE_METHOD_NAME));
57+
self::assertFileExists(\sprintf(
58+
'%s/Fixtures/%s.php',
59+
static::directory(),
60+
Str::beforeLast(static::ruleReflectionClass()->getShortName(), 'Rule')
61+
));
5762
}
5863

5964
/**
@@ -79,15 +84,15 @@ protected static function rawGetRule(): AbstractRule
7984
return static::getContainer()->getByType(static::ruleClass());
8085
}
8186

82-
protected static function invokeErrorMessage(...$args)
87+
protected static function invokeRuleErrorMessageMethod(...$args)
8388
{
84-
return static::invoke('errorMessage', ...$args);
89+
return static::invokeRuleMethod(self::ERROR_MESSAGE_METHOD_NAME, ...$args);
8590
}
8691

87-
protected static function invoke(string $method, ...$args)
92+
protected static function invokeRuleMethod(string $method, ...$args)
8893
{
8994
$reflectionMethod = static::ruleReflectionClass()->getMethod($method);
90-
$reflectionMethod->setAccessible(true);
95+
\PHP_VERSION_ID < 80100 and $reflectionMethod->setAccessible(true);
9196

9297
return $reflectionMethod->invoke(static::rawGetRule(), ...$args);
9398
}

tests/Rule/Concern/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)