Skip to content

Commit eaa34e0

Browse files
committed
refactor: apply rector
1 parent 30334a2 commit eaa34e0

File tree

7 files changed

+8
-162
lines changed

7 files changed

+8
-162
lines changed

composer-bump

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ final class ComposerBump
307307
$process = Process::fromShellCommandline(implode(' ', array_merge([$command], $options)), $cwd, $env, $input, $timeout);
308308
$this->symfonyStyle->warning("The command [{$process->getCommandLine()}] is running in the working directory [$cwd] .");
309309

310-
return $process->mustRun(function (string $type, string $buffer): void {
310+
return $process->mustRun(function (string $_, string $buffer): void {
311311
$this->symfonyStyle->isVerbose() and $this->symfonyStyle->write($buffer);
312312
});
313313
}

monorepo-builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
]))
7979
->setEnv(['COMPOSER_MEMORY_LIMIT' => -1])
8080
->setTimeout(600)
81-
->mustRun(static function (string $type, string $buffer): void {
81+
->mustRun(static function (string $_, string $buffer): void {
8282
$symfonyStyle ??= new SymfonyStyle(new ArgvInput, new ConsoleOutput);
8383
$symfonyStyle->write($buffer);
8484
});

phpstan.neon.dist

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,12 @@ parameters:
147147
- identifier: symplify.forbiddenNode
148148
# - message: '#^Cognitive complexity for "Guanguans\\PHPStanRules\\Rule\\.*\\.*Rule\:\:processNode\(\)" is 1[0-0], keep it under 8$#'
149149

150-
-
151-
identifier: rector.noClassReflectionStaticReflection
152-
path: src/Support/helpers.php
153150
-
154151
identifier: rector.noInstanceOfStaticReflection
155152
path: src/Support/helpers.php
156153
-
157154
identifier: symplify.forbiddenFuncCall
158155
path: src/Support/helpers.php
159-
-
160-
identifier: shipmonk.unusedParameter
161-
path: src/Support/helpers.php
162156

163157
- identifier: typeCoverage.paramTypeCoverage
164158
# - identifier: typeCoverage.returnTypeCoverage

rector.php

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
use Ergebnis\Rector\Rules\Arrays\SortAssociativeArrayByKeyRector;
2020
use Guanguans\RectorRules\Rector\Array_\SortListItemOfSameScalarTypeRector;
2121
use Guanguans\RectorRules\Rector\File\AddNoinspectionDocblockToFileFirstStmtRector;
22-
use Guanguans\RectorRules\Rector\Name\RenameToPsrNameRector;
22+
use Guanguans\RectorRules\Rector\Name\RenameToConventionalCaseNameRector;
2323
use Guanguans\RectorRules\Rector\New_\NewExceptionToNewAnonymousExtendsExceptionImplementsRector;
24-
use Illuminate\Support\Str;
2524
use PhpParser\NodeVisitor\ParentConnectingVisitor;
2625
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
2726
use Rector\CodeQuality\Rector\LogicalAnd\LogicalToBooleanRector;
@@ -44,18 +43,10 @@
4443
use Rector\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector;
4544
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
4645
use Rector\PHPUnit\Set\PHPUnitSetList;
47-
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
4846
use Rector\Set\ValueObject\DowngradeLevelSetList;
4947
use Rector\Set\ValueObject\SetList;
50-
use Rector\Transform\Rector\FuncCall\FuncCallToStaticCallRector;
51-
use Rector\Transform\Rector\StaticCall\StaticCallToFuncCallRector;
52-
use Rector\Transform\ValueObject\FuncCallToStaticCall;
53-
use Rector\Transform\ValueObject\StaticCallToFuncCall;
48+
use Rector\Transform\Rector\Scalar\ScalarValueToConstFetchRector;
5449
use Rector\ValueObject\PhpVersion;
55-
use Rector\ValueObject\Visibility;
56-
use Rector\Visibility\Rector\ClassMethod\ChangeMethodVisibilityRector;
57-
use Rector\Visibility\ValueObject\ChangeMethodVisibility;
58-
use function Guanguans\PHPStanRules\Support\classes;
5950

6051
return RectorConfig::configure()
6152
->withPaths([
@@ -138,7 +129,7 @@
138129
])
139130
// ->withConfiguredRule(NewExceptionToNewAnonymousExtendsExceptionImplementsRector::class, [ThrowableContract::class])
140131
->registerDecoratingNodeVisitor(ParentConnectingVisitor::class)
141-
->withConfiguredRule(RenameToPsrNameRector::class, [
132+
->withConfiguredRule(RenameToConventionalCaseNameRector::class, [
142133
'MIT',
143134
])
144135
// ->withConfiguredRule(SortListItemOfSameScalarTypeRector::class, [
@@ -152,42 +143,15 @@
152143
'phpstan-ignore-next-line',
153144
'psalm-suppress',
154145
])
155-
->withConfiguredRule(StaticCallToFuncCallRector::class, [
156-
// new StaticCallToFuncCall(Str::class, 'of', 'str'),
157-
])
158-
->withConfiguredRule(FuncCallToStaticCallRector::class, [
159-
new FuncCallToStaticCall('str', Str::class, 'of'),
160-
])
161-
->withConfiguredRule(
162-
ChangeMethodVisibilityRector::class,
163-
classes(static fn (string $class, string $file): bool => str_starts_with($class, 'Guanguans\PHPStanRules'))
164-
->filter(static fn (ReflectionClass $reflectionClass): bool => $reflectionClass->isTrait())
165-
->map(
166-
static fn (ReflectionClass $reflectionClass): array => collect($reflectionClass->getMethods(ReflectionMethod::IS_PRIVATE))
167-
->reject(static fn (ReflectionMethod $reflectionMethod): bool => $reflectionMethod->isFinal() || $reflectionMethod->isInternal())
168-
->map(static fn (ReflectionMethod $reflectionMethod): ChangeMethodVisibility => new ChangeMethodVisibility(
169-
$reflectionClass->getName(),
170-
$reflectionMethod->getName(),
171-
Visibility::PROTECTED
172-
))
173-
->all()
174-
)
175-
->flatten()
176-
// ->dd()
177-
->all(),
178-
)
179-
->withConfiguredRule(RenameFunctionRector::class, [
180-
'Pest\Faker\fake' => 'fake',
181-
'Pest\Faker\faker' => 'fake',
182-
'test' => 'it',
183-
])
184146
->withSkip([
185147
DowngradeArrayIsListRector::class,
186148
DowngradeStrContainsRector::class,
187149
DowngradeStrEndsWithRector::class,
188150
DowngradeStrStartsWithRector::class,
189151
])
190152
->withSkip([
153+
ScalarValueToConstFetchRector::class,
154+
191155
ChangeOrIfContinueToMultiContinueRector::class,
192156
EncapsedStringsToSprintfRector::class,
193157
ExplicitBoolCompareRector::class,

src/Support/ComposerScripts.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Symfony\Component\Console\Style\SymfonyStyle;
2929
use Symfony\Component\Finder\Finder;
3030
use Symfony\Component\Finder\SplFileInfo;
31+
use function Guanguans\RectorRules\Support\classes;
3132

3233
/**
3334
* @internal

src/Support/helpers.php

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -13,109 +13,10 @@
1313

1414
namespace Guanguans\PHPStanRules\Support;
1515

16-
use Composer\Autoload\ClassLoader;
17-
use Illuminate\Support\Collection;
1816
use PhpParser\Node;
1917
use PhpParser\NodeTraverser;
2018
use PhpParser\NodeVisitor\CloningVisitor;
2119

22-
if (!\function_exists('Guanguans\PHPStanRules\Support\classes')) {
23-
/**
24-
* @see https://github.com/illuminate/collections
25-
* @see https://github.com/alekitto/class-finder
26-
* @see https://github.com/ergebnis/classy
27-
* @see https://gitlab.com/hpierce1102/ClassFinder
28-
* @see https://packagist.org/packages/haydenpierce/class-finder
29-
* @see \get_declared_classes()
30-
* @see \get_declared_interfaces()
31-
* @see \get_declared_traits()
32-
* @see \Composer\Util\ErrorHandler
33-
* @see \Composer\Util\Silencer::call()
34-
* @see \DG\BypassFinals::enable()
35-
* @see \Illuminate\Foundation\Bootstrap\HandleExceptions::bootstrap()
36-
* @see \Monolog\ErrorHandler
37-
* @see \PhpCsFixer\ExecutorWithoutErrorHandler
38-
* @see \Phrity\Util\ErrorHandler
39-
*
40-
* @template TObject of object
41-
*
42-
* @internal
43-
*
44-
* @param null|(callable(class-string<TObject>, string): bool) $filter
45-
*
46-
* @throws \ErrorException
47-
* @throws \ReflectionException
48-
*
49-
* @return \Illuminate\Support\Collection<class-string<TObject>, \ReflectionClass<TObject>>
50-
*
51-
* @noinspection PhpUndefinedNamespaceInspection
52-
*/
53-
function classes(?callable $filter = null): Collection
54-
{
55-
$func = __FUNCTION__;
56-
$errorMessenger = static fn (
57-
string $file,
58-
string $class
59-
): string => "Failed to reflect the class [$class] in the file [$file]. "
60-
."You may need to filter out the class or file using the callback parameter of the function [$func()].";
61-
62-
/** @var null|array{file: string, class: class-string<TObject>, line: int} $context */
63-
static $context = null;
64-
static $registered = false;
65-
66-
if (!$registered) {
67-
register_shutdown_function(
68-
static function () use (&$context, $errorMessenger): void {
69-
// @codeCoverageIgnoreStart
70-
if (
71-
null === $context
72-
|| null === ($error = error_get_last())
73-
|| !\in_array($error['type'], [\E_COMPILE_ERROR, \E_CORE_ERROR, \E_ERROR, \E_PARSE], true)
74-
) {
75-
return;
76-
}
77-
78-
// trigger_error($errorMessenger($context['file'], $context['class']), \E_USER_ERROR);
79-
throw new \ErrorException(
80-
$errorMessenger($context['file'], $context['class']),
81-
0,
82-
$error['type'],
83-
__FILE__,
84-
$context['line'],
85-
new \ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line'])
86-
);
87-
// @codeCoverageIgnoreEnd
88-
}
89-
);
90-
$registered = true;
91-
}
92-
93-
/** @var null|\Illuminate\Support\Collection<string, class-string> $classes */
94-
static $classes;
95-
$classes ??= collect(spl_autoload_functions())->flatMap(
96-
static fn (callable $loader): array => \is_array($loader) && $loader[0] instanceof ClassLoader
97-
? $loader[0]->getClassMap()
98-
: []
99-
);
100-
$filter ??= static fn (string $class, string $file): bool => true;
101-
102-
return $classes
103-
->filter(static fn (string $file, string $class): bool => $filter($class, $file))
104-
->mapWithKeys(static function (string $file, string $class) use (&$context, $errorMessenger): array {
105-
try {
106-
$context = ['file' => $file, 'class' => $class, 'line' => __LINE__ + 2];
107-
108-
return [$class => new \ReflectionClass($class)];
109-
} catch (\Throwable $throwable) {
110-
// return [$class => $throwable];
111-
throw new \ReflectionException($errorMessenger($file, $class), 0, $throwable);
112-
} finally {
113-
$context = null;
114-
}
115-
});
116-
}
117-
}
118-
11920
if (!\function_exists('Guanguans\PHPStanRules\Support\clone_node')) {
12021
/**
12122
* @see \DeepCopy\deep_copy()

tests/Support/HelpersTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
* @see https://github.com/guanguans/phpstan-rules
1919
*/
2020

21-
use Illuminate\Support\Collection;
22-
use Illuminate\Support\Str;
2321
use Rector\PhpParser\Parser\SimplePhpParser;
24-
use function Guanguans\PHPStanRules\Support\classes;
2522
use function Guanguans\PHPStanRules\Support\clone_node;
2623
use function Guanguans\PHPStanRules\Support\is_class_of_all;
2724
use function Guanguans\PHPStanRules\Support\is_class_of_any;
@@ -30,17 +27,6 @@
3027
use function Guanguans\PHPStanRules\Support\is_subclass_of_all;
3128
use function Guanguans\PHPStanRules\Support\is_subclass_of_any;
3229

33-
it('will throw `ReflectionException` when call classes function', function (): void {
34-
classes(fn (string $class): bool => Str::of($class)->startsWith('Illuminate\Support'));
35-
})
36-
->group(__DIR__, __FILE__)
37-
->throws(ReflectionException::class);
38-
39-
it('can get classes', function (): void {
40-
expect(classes(fn (string $class): bool => Str::of($class)->startsWith(Str::class)))
41-
->toBeInstanceOf(Collection::class);
42-
})->group(__DIR__, __FILE__);
43-
4430
it('can deep clone node', function (): void {
4531
$node = (new SimplePhpParser)->parseFile(__FILE__)[0];
4632

0 commit comments

Comments
 (0)