Skip to content

Commit ffd5139

Browse files
authored
Merge pull request #371 from Nielsvanpach/chore/rector
Add Rector
2 parents 5409fbc + 6fb166c commit ffd5139

27 files changed

Lines changed: 122 additions & 117 deletions

.github/workflows/psalm.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ name: Psalm
22

33
on:
44
push:
5-
branches: [master]
65
paths:
76
- '**.php'
87
- 'psalm*'

.github/workflows/test-laravel.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ name: Test laravel projects
22

33
on:
44
push:
5-
branches: [master]
65
paths:
76
- '**.php'
87
- '**.stubphp'

.github/workflows/test.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ name: Tests
33
on:
44
workflow_dispatch:
55
push:
6-
branches: [master]
76
paths:
87
- '**.php'
98
- '**.stubphp'

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"phpunit/phpunit": "^10.5 || ^11.0",
3535
"phpyh/psalm-tester": "^0.1.0",
3636
"ramsey/collection": "^1.3",
37+
"rector/rector": "^1.0",
3738
"slevomat/coding-standard": "^8.8",
3839
"squizlabs/php_codesniffer": "*",
3940
"symfony/http-foundation": "^6.0 || ^7.0"
@@ -74,6 +75,7 @@
7475
"@test:type"
7576
],
7677
"test:type": "phpunit --testsuite=type",
77-
"test:unit": "phpunit --testsuite=unit"
78+
"test:unit": "phpunit --testsuite=unit",
79+
"rector": "./vendor/bin/rector --dry-run"
7880
}
7981
}

psalm-baseline.xml

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="5.23.1@8471a896ccea3526b26d082f4461eeea467f10a4">
2+
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
3+
<file src="src/Handlers/Application/ContainerHandler.php">
4+
<MixedArgument>
5+
<code>$abstract</code>
6+
</MixedArgument>
7+
</file>
38
<file src="src/Handlers/Auth/AuthConfigAnalyzer.php">
4-
<LessSpecificReturnStatement>
5-
<code><![CDATA['\Illuminate\Auth\GenericUser']]></code>
6-
</LessSpecificReturnStatement>
79
<MixedArgument>
810
<code><![CDATA[$this->config->get('auth.guards')]]></code>
911
</MixedArgument>
1012
<MixedInferredReturnType>
11-
<code><![CDATA[?string]]></code>
13+
<code>?string</code>
1214
<code><![CDATA[class-string<\Illuminate\Contracts\Auth\Authenticatable>|null]]></code>
1315
</MixedInferredReturnType>
1416
<MixedReturnStatement>
15-
<code><![CDATA[$this->config->get("auth.providers.$provider.model", null)]]></code>
17+
<code><![CDATA[$this->config->get("auth.providers.{$provider}.model", null)]]></code>
1618
<code><![CDATA[$this->config->get('auth.defaults.guard')]]></code>
1719
</MixedReturnStatement>
1820
</file>
1921
<file src="src/Handlers/Auth/AuthHandler.php">
2022
<TypeDoesNotContainType>
21-
<code><![CDATA[null]]></code>
23+
<code>null</code>
2224
</TypeDoesNotContainType>
2325
</file>
2426
<file src="src/Handlers/Auth/GuardHandler.php">
@@ -33,9 +35,9 @@
3335
<code><![CDATA[$event->getFqClasslikeName()]]></code>
3436
</ArgumentTypeCoercion>
3537
<LessSpecificImplementedReturnType>
36-
<code><![CDATA[array]]></code>
37-
<code><![CDATA[array]]></code>
38-
<code><![CDATA[array]]></code>
38+
<code>array</code>
39+
<code>array</code>
40+
<code>array</code>
3941
</LessSpecificImplementedReturnType>
4042
</file>
4143
<file src="src/Handlers/Eloquent/Schema/SchemaAggregator.php">
@@ -45,17 +47,17 @@
4547
</file>
4648
<file src="src/Handlers/Helpers/PathHandler.php">
4749
<InternalMethod>
48-
<code><![CDATA[new TLiteralString($result)]]></code>
50+
<code>new TLiteralString($result)</code>
4951
</InternalMethod>
5052
</file>
5153
<file src="src/Providers/ApplicationProvider.php">
52-
<InternalMethod>
53-
<code><![CDATA[createApplication]]></code>
54-
</InternalMethod>
54+
<InternalMethod>
55+
<code><![CDATA[createApplication]]></code>
56+
</InternalMethod>
5557
</file>
5658
<file src="src/Util/ContainerResolver.php">
5759
<InternalMethod>
58-
<code><![CDATA[new TLiteralString($concrete)]]></code>
60+
<code>new TLiteralString($concrete)</code>
5961
</InternalMethod>
6062
</file>
6163
</files>

rector.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector;
4+
use Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector;
5+
use Rector\CodingStyle\Rector\Closure\StaticClosureRector;
6+
use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
7+
use Rector\CodingStyle\Rector\If_\NullableCompareToNullRector;
8+
use Rector\Config\RectorConfig;
9+
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
10+
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
11+
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
12+
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;
13+
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
14+
use Rector\Strict\Rector\BooleanNot\BooleanInBooleanNotRuleFixerRector;
15+
use Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector;
16+
17+
return RectorConfig::configure()
18+
->withPaths(['src', 'tests'])
19+
->withPhpSets(php81: true)
20+
->withPreparedSets(deadCode: true, codingStyle: true, typeDeclarations: true)
21+
->withSkip([
22+
ReadOnlyPropertyRector::class,
23+
ClosureToArrowFunctionRector::class,
24+
FirstClassCallableRector::class,
25+
NullToStrictStringFuncCallArgRector::class,
26+
BooleanInIfConditionRuleFixerRector::class,
27+
BooleanInBooleanNotRuleFixerRector::class,
28+
RemoveUnusedPrivateMethodRector::class,
29+
SimplifyUselessVariableRector::class,
30+
NullableCompareToNullRector::class,
31+
EncapsedStringsToSprintfRector::class,
32+
StaticClosureRector::class,
33+
SplitDoubleAssignRector::class,
34+
]);

src/Fakes/FakeFilesystem.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,25 @@
66

77
final class FakeFilesystem extends Filesystem
88
{
9-
/** @var ?string */
10-
private $destination = '';
9+
private ?string $destination = '';
1110

1211
/**
1312
* Write the contents of a file.
1413
*
1514
* @param string $path
1615
* @param string $contents
1716
* @param bool $lock
18-
* @return bool|int
1917
*/
20-
public function put($path, $contents, $lock = false)
18+
public function put($path, $contents, $lock = false): bool|int
2119
{
22-
$destination = $this->destination !== null ? $this->destination : $path;
20+
$destination = $this->destination ?? $path;
2321

2422
$this->destination = null;
2523

2624
return parent::put($destination, $contents, $lock);
2725
}
2826

29-
/**
30-
* @return void
31-
*/
32-
public function setDestination(string $destination)
27+
public function setDestination(string $destination): void
3328
{
3429
$this->destination = $destination;
3530
}

src/Fakes/FakeModelsCommand.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,11 @@
1919
class FakeModelsCommand extends ModelsCommand
2020
{
2121
/** @var list<class-string<\Illuminate\Database\Eloquent\Model>> */
22-
private $model_classes = [];
22+
private array $model_classes = [];
2323

24-
/** @var SchemaAggregator */
25-
private $schema;
26-
27-
public function __construct(Filesystem $files, SchemaAggregator $schema)
24+
public function __construct(Filesystem $files, private SchemaAggregator $schema)
2825
{
2926
parent::__construct($files);
30-
$this->schema = $schema;
3127
}
3228

3329
/** @return list<class-string<\Illuminate\Database\Eloquent\Model>> */
@@ -66,15 +62,15 @@ public function getPropertiesFromTable($model): void
6662
return;
6763
}
6864

69-
$this->model_classes[] = get_class($model);
65+
$this->model_classes[] = $model::class;
7066

7167
$columns = $this->schema->tables[$table_name]->columns;
7268

7369
foreach ($columns as $column) {
7470
$column_name = $column->name;
7571

7672
if (in_array($column_name, $model->getDates(), true)) {
77-
$get_type = $set_type = '\Illuminate\Support\Carbon';
73+
$get_type = $set_type = \Illuminate\Support\Carbon::class;
7874
} else {
7975
switch ($column->type) {
8076
case SchemaColumn::TYPE_STRING:
@@ -101,7 +97,7 @@ public function getPropertiesFromTable($model): void
10197
if (!$column->options) {
10298
$get_type = $set_type = 'string';
10399
} else {
104-
$get_type = $set_type = '\'' . implode('\'|\'', $column->options) . '\'';
100+
$get_type = $set_type = "'" . implode("'|'", $column->options) . "'";
105101
}
106102

107103
break;
@@ -128,7 +124,7 @@ public function getPropertiesFromTable($model): void
128124
$this->setMethod(
129125
Str::camel("where_" . $column_name),
130126
'\Illuminate\Database\Eloquent\Builder<static>', // @todo support custom EloquentBuilders
131-
array('$value')
127+
['$value']
132128
);
133129
}
134130
}

src/Handlers/Application/ContainerHandler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static function getMethodReturnType(MethodReturnTypeProviderEvent $event)
6666
* @see https://github.com/psalm/psalm-plugin-symfony/issues/25
6767
* psalm needs to know about any classes that could be returned before analysis begins. This is a naive first approach
6868
*/
69-
public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event)
69+
public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event): void
7070
{
7171
if (!in_array($event->getStorage()->name, ApplicationInterfaceProvider::getApplicationInterfaceClassLikes())) {
7272
return;
@@ -91,13 +91,13 @@ public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event)
9191
if ($reflectionClass->isAnonymous()) {
9292
continue;
9393
}
94-
} catch (Throwable $e) {
94+
} catch (Throwable) {
9595
// cannot just catch binding exception as the following error is emitted within laravel:
9696
// Class 'Symfony\Component\Cache\Adapter\Psr16Adapter' not found
9797
continue;
9898
}
9999

100-
$className = get_class($concrete);
100+
$className = $concrete::class;
101101
$filePath = $event->getStatementsSource()->getFilePath();
102102
$fileStorage = $event->getCodebase()->file_storage_provider->get($filePath);
103103
$fileStorage->referenced_classlikes[strtolower($className)] = $className;

src/Handlers/Auth/AuthConfigAnalyzer.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@ final class AuthConfigAnalyzer
1414
{
1515
private static ?AuthConfigAnalyzer $instance = null;
1616

17-
private ConfigRepository $config;
18-
19-
private function __construct(ConfigRepository $config)
17+
private function __construct(private ConfigRepository $config)
2018
{
21-
$this->config = $config;
2219
}
2320

2421
public static function instance(): self
@@ -43,17 +40,17 @@ public function getAuthenticatableFQCN(?string $guard = null): ?string
4340
}
4441
}
4542

46-
$provider = $this->config->get("auth.guards.$guard.provider");
43+
$provider = $this->config->get("auth.guards.{$guard}.provider");
4744

4845
if (! is_string($provider)) {
4946
return null;
5047
}
5148

52-
if ($this->config->get("auth.providers.$provider.driver") === 'database') {
53-
return '\Illuminate\Auth\GenericUser';
49+
if ($this->config->get("auth.providers.{$provider}.driver") === 'database') {
50+
return \Illuminate\Auth\GenericUser::class;
5451
}
5552

56-
return $this->config->get("auth.providers.$provider.model", null);
53+
return $this->config->get("auth.providers.{$provider}.model", null);
5754
}
5855

5956
public function getDefaultGuard(): ?string

0 commit comments

Comments
 (0)