Skip to content

Commit 6660d82

Browse files
committed
refactor(rules): Rename AbstractMixedTypeRule to AbstractMixedNodeTypeRule
- Updated class name for clarity and consistency. - Adjusted references in ExceptionMustImplementNativeThrowableRule and its test. - Ensured proper extension in documentation comments.
1 parent 8372569 commit 6660d82

File tree

11 files changed

+59
-16
lines changed

11 files changed

+59
-16
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ includes:
3636

3737
## Usage
3838

39-
Parameter configuration refer to the parameter section the configuration file [[config/rules.neon](config/rules.neon)].
39+
Parameter configuration refer to the parameter section the configuration file [[`config/rules.neon`](config/rules.neon)].
4040

41-
You can also refer to the configuration file [tests/Rule/.../.../config/configured_rule.neon] in the tests directory.
41+
You can also refer to the configuration file [`tests/Rule/.../.../config/configured_rule.neon`] in the tests directory.
4242

4343
## Composer scripts
4444

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
* @extends AbstractRule<Node>
2525
*/
26-
abstract class AbstractMixedTypeRule extends AbstractRule
26+
abstract class AbstractMixedNodeTypeRule extends AbstractRule
2727
{
2828
/**
2929
* @return class-string<TNodeType>

src/Rule/AbstractRule.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
1414
namespace Guanguans\PHPStanRules\Rule;
1515

1616
use Illuminate\Support\Str;
17+
use Illuminate\Support\Stringable;
1718
use PhpParser\Node;
1819
use PHPStan\Rules\Rule;
1920

2021
/**
22+
* @see https://github.com/phpstan/phpstan-strict-rules
23+
* @see https://github.com/symplify/phpstan-rules
24+
* @see https://github.com/ergebnis/phpstan-rules
25+
*
2126
* @template TNodeType of Node
2227
*
2328
* @implements Rule<TNodeType>
@@ -30,7 +35,7 @@ final protected function identifier(): string
3035
->afterLast('\\')
3136
->beforeLast('Rule')
3237
// ->lcfirst()
33-
->camel()
38+
->pipe(static fn (Stringable $stringable): string => lcfirst((string) $stringable))
3439
->prepend('guanguans', '.');
3540
}
3641
}

src/Rule/Class_/ExceptionMustImplementNativeThrowableRule.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Guanguans\PHPStanRules\Rule\Class_;
1515

16-
use Guanguans\PHPStanRules\Rule\AbstractMixedTypeRule;
16+
use Guanguans\PHPStanRules\Rule\AbstractMixedNodeTypeRule;
1717
use PhpParser\Node;
1818
use PhpParser\Node\Expr\New_;
1919
use PhpParser\Node\Identifier;
@@ -29,9 +29,9 @@
2929
* @see https://github.com/symfony/ai/blob/main/.phpstan/ForbidNativeExceptionRule.php
3030
* @see https://github.com/thecodingmachine/phpstan-strict-rules/tree/master/src/Rules/Exceptions/
3131
*
32-
* @extends AbstractMixedTypeRule<Class_|New_>
32+
* @extends AbstractMixedNodeTypeRule<Class_|New_>
3333
*/
34-
final class ExceptionMustImplementNativeThrowableRule extends AbstractMixedTypeRule
34+
final class ExceptionMustImplementNativeThrowableRule extends AbstractMixedNodeTypeRule
3535
{
3636
private string $nativeThrowable;
3737

src/Support/ComposerScripts.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ final class ComposerScripts
4545
private function __construct() {}
4646

4747
/**
48-
* @see vendor/phpstan/phpstan/phpstan.phar/preload.php
49-
*
50-
* @throws \ErrorException
48+
* @throws \ErrorException*@see vendor/phpstan/phpstan/phpstan.phar/preload.php
49+
* @throws \ReflectionException
5150
*
5251
* @return int<0>|never-returns<1>
5352
*

tests/Rule/AbstractRuleTestCase.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ final public function testRuleWithoutErrorMessage(): void
5050
$this->analyse(glob(static::directory().'/Fixtures/Skip*.php'), []);
5151
}
5252

53+
final public function testRuleConstructor(): void
54+
{
55+
self::assertInstanceOf(
56+
static::ruleClass(),
57+
static::ruleReflectionClass()->newInstanceArgs(static::ruleParameters())
58+
);
59+
}
60+
5361
final public function testRuleBasicInformation(): void
5462
{
5563
self::assertTrue(is_subclass_of(static::ruleClass(), AbstractRule::class));
@@ -84,6 +92,37 @@ protected static function rawGetRule(): AbstractRule
8492
return static::getContainer()->getByType(static::ruleClass());
8593
}
8694

95+
/**
96+
* @throws \PHPStan\DependencyInjection\MissingServiceException
97+
* @throws \PHPStan\DependencyInjection\ParameterNotFoundException
98+
* @throws \ReflectionException
99+
*
100+
* @return array<string, mixed>
101+
*/
102+
protected static function ruleParameters(): array
103+
{
104+
[$namespace, $name] = explode('.', static::invokeRuleMethod('identifier'), 2);
105+
$rawParameters = static::getContainer()->getParameter($namespace)[$name];
106+
107+
return array_reduce(
108+
static::ruleReflectionClass()->getConstructor()->getParameters(),
109+
function (array $parameters, \ReflectionParameter $reflectionParameter) use ($rawParameters): array {
110+
$parameterName = $reflectionParameter->getName();
111+
112+
if (class_exists($typeName = $reflectionParameter->getType()->getName())) {
113+
$parameters[$parameterName] = static::getContainer()->getByType($typeName);
114+
} elseif (isset($rawParameters[$parameterName])) {
115+
$parameters[$parameterName] = $rawParameters[$parameterName];
116+
} elseif ($reflectionParameter->isDefaultValueAvailable()) {
117+
$parameters[$parameterName] = $reflectionParameter->getDefaultValue();
118+
}
119+
120+
return $parameters;
121+
},
122+
[]
123+
);
124+
}
125+
87126
protected static function invokeRuleErrorMessageMethod(...$args)
88127
{
89128
return static::invokeRuleMethod(self::ERROR_MESSAGE_METHOD_NAME, ...$args);

tests/Rule/Class_/ExceptionMustImplementNativeThrowableRule/ExceptionMustImplementNativeThrowableRuleTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
use Guanguans\PHPStanRulesTests\Rule\Class_\ExceptionMustImplementNativeThrowableRule\Fixtures\NonImplementedNativeThrowableException;
2525

2626
/**
27-
* @covers \Guanguans\PHPStanRules\Rule\AbstractMixedTypeRule
27+
* @covers \Guanguans\PHPStanRules\Rule\AbstractMixedNodeTypeRule
2828
* @covers \Guanguans\PHPStanRules\Rule\AbstractRule
2929
* @covers \Guanguans\PHPStanRules\Rule\Class_\ExceptionMustImplementNativeThrowableRule
3030
*/
@@ -36,10 +36,10 @@ final class ExceptionMustImplementNativeThrowableRuleTest extends AbstractRuleTe
3636
public static function provideRuleCases(): iterable
3737
{
3838
yield [__DIR__.'/Fixtures/ExceptionMustImplementNativeThrowable.php', [
39-
[self::invokeRuleErrorMessageMethod(\RuntimeException::class), 21],
39+
[self::invokeRuleErrorMessageMethod(\RuntimeException::class), 20],
40+
[self::invokeRuleErrorMessageMethod(NonImplementedNativeThrowableException::class), 21],
4041
[self::invokeRuleErrorMessageMethod(NonImplementedNativeThrowableException::class), 22],
4142
[self::invokeRuleErrorMessageMethod(NonImplementedNativeThrowableException::class), 23],
42-
[self::invokeRuleErrorMessageMethod(NonImplementedNativeThrowableException::class), 24],
4343
]];
4444

4545
yield [__DIR__.'/Fixtures/NonImplementedNativeThrowableException.php', [

tests/Rule/Class_/ExceptionMustImplementNativeThrowableRule/Fixtures/ExceptionMustImplementNativeThrowable.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
/** @noinspection ALL */
43
declare(strict_types=1);
54

65
/**

tests/Rule/Class_/ExceptionMustImplementNativeThrowableRule/Fixtures/SkipNonThrowableClass.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
/** @noinspection ALL */
43
declare(strict_types=1);
54

65
/**

tests/Rule/File/ForbiddenSideEffectsRule/Fixtures/SkipNonSideEffects.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
/** @noinspection ALL */
43
declare(strict_types=1);
54

65
/**

0 commit comments

Comments
 (0)