Skip to content

Commit

Permalink
Merge pull request #371 from Nielsvanpach/chore/rector
Browse files Browse the repository at this point in the history
Add Rector
  • Loading branch information
alies-dev authored Mar 19, 2024
2 parents 5409fbc + 6fb166c commit ffd5139
Show file tree
Hide file tree
Showing 27 changed files with 122 additions and 117 deletions.
1 change: 0 additions & 1 deletion .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: Psalm

on:
push:
branches: [master]
paths:
- '**.php'
- 'psalm*'
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/test-laravel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: Test laravel projects

on:
push:
branches: [master]
paths:
- '**.php'
- '**.stubphp'
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: Tests
on:
workflow_dispatch:
push:
branches: [master]
paths:
- '**.php'
- '**.stubphp'
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"phpunit/phpunit": "^10.5 || ^11.0",
"phpyh/psalm-tester": "^0.1.0",
"ramsey/collection": "^1.3",
"rector/rector": "^1.0",
"slevomat/coding-standard": "^8.8",
"squizlabs/php_codesniffer": "*",
"symfony/http-foundation": "^6.0 || ^7.0"
Expand Down Expand Up @@ -74,6 +75,7 @@
"@test:type"
],
"test:type": "phpunit --testsuite=type",
"test:unit": "phpunit --testsuite=unit"
"test:unit": "phpunit --testsuite=unit",
"rector": "./vendor/bin/rector --dry-run"
}
}
32 changes: 17 additions & 15 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.23.1@8471a896ccea3526b26d082f4461eeea467f10a4">
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<file src="src/Handlers/Application/ContainerHandler.php">
<MixedArgument>
<code>$abstract</code>
</MixedArgument>
</file>
<file src="src/Handlers/Auth/AuthConfigAnalyzer.php">
<LessSpecificReturnStatement>
<code><![CDATA['\Illuminate\Auth\GenericUser']]></code>
</LessSpecificReturnStatement>
<MixedArgument>
<code><![CDATA[$this->config->get('auth.guards')]]></code>
</MixedArgument>
<MixedInferredReturnType>
<code><![CDATA[?string]]></code>
<code>?string</code>
<code><![CDATA[class-string<\Illuminate\Contracts\Auth\Authenticatable>|null]]></code>
</MixedInferredReturnType>
<MixedReturnStatement>
<code><![CDATA[$this->config->get("auth.providers.$provider.model", null)]]></code>
<code><![CDATA[$this->config->get("auth.providers.{$provider}.model", null)]]></code>
<code><![CDATA[$this->config->get('auth.defaults.guard')]]></code>
</MixedReturnStatement>
</file>
<file src="src/Handlers/Auth/AuthHandler.php">
<TypeDoesNotContainType>
<code><![CDATA[null]]></code>
<code>null</code>
</TypeDoesNotContainType>
</file>
<file src="src/Handlers/Auth/GuardHandler.php">
Expand All @@ -33,9 +35,9 @@
<code><![CDATA[$event->getFqClasslikeName()]]></code>
</ArgumentTypeCoercion>
<LessSpecificImplementedReturnType>
<code><![CDATA[array]]></code>
<code><![CDATA[array]]></code>
<code><![CDATA[array]]></code>
<code>array</code>
<code>array</code>
<code>array</code>
</LessSpecificImplementedReturnType>
</file>
<file src="src/Handlers/Eloquent/Schema/SchemaAggregator.php">
Expand All @@ -45,17 +47,17 @@
</file>
<file src="src/Handlers/Helpers/PathHandler.php">
<InternalMethod>
<code><![CDATA[new TLiteralString($result)]]></code>
<code>new TLiteralString($result)</code>
</InternalMethod>
</file>
<file src="src/Providers/ApplicationProvider.php">
<InternalMethod>
<code><![CDATA[createApplication]]></code>
</InternalMethod>
<InternalMethod>
<code><![CDATA[createApplication]]></code>
</InternalMethod>
</file>
<file src="src/Util/ContainerResolver.php">
<InternalMethod>
<code><![CDATA[new TLiteralString($concrete)]]></code>
<code>new TLiteralString($concrete)</code>
</InternalMethod>
</file>
</files>
34 changes: 34 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector;
use Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector;
use Rector\CodingStyle\Rector\Closure\StaticClosureRector;
use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
use Rector\CodingStyle\Rector\If_\NullableCompareToNullRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
use Rector\Strict\Rector\BooleanNot\BooleanInBooleanNotRuleFixerRector;
use Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector;

return RectorConfig::configure()
->withPaths(['src', 'tests'])
->withPhpSets(php81: true)
->withPreparedSets(deadCode: true, codingStyle: true, typeDeclarations: true)
->withSkip([
ReadOnlyPropertyRector::class,
ClosureToArrowFunctionRector::class,
FirstClassCallableRector::class,
NullToStrictStringFuncCallArgRector::class,
BooleanInIfConditionRuleFixerRector::class,
BooleanInBooleanNotRuleFixerRector::class,
RemoveUnusedPrivateMethodRector::class,
SimplifyUselessVariableRector::class,
NullableCompareToNullRector::class,
EncapsedStringsToSprintfRector::class,
StaticClosureRector::class,
SplitDoubleAssignRector::class,
]);
13 changes: 4 additions & 9 deletions src/Fakes/FakeFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,25 @@

final class FakeFilesystem extends Filesystem
{
/** @var ?string */
private $destination = '';
private ?string $destination = '';

/**
* Write the contents of a file.
*
* @param string $path
* @param string $contents
* @param bool $lock
* @return bool|int
*/
public function put($path, $contents, $lock = false)
public function put($path, $contents, $lock = false): bool|int
{
$destination = $this->destination !== null ? $this->destination : $path;
$destination = $this->destination ?? $path;

$this->destination = null;

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

/**
* @return void
*/
public function setDestination(string $destination)
public function setDestination(string $destination): void
{
$this->destination = $destination;
}
Expand Down
16 changes: 6 additions & 10 deletions src/Fakes/FakeModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@
class FakeModelsCommand extends ModelsCommand
{
/** @var list<class-string<\Illuminate\Database\Eloquent\Model>> */
private $model_classes = [];
private array $model_classes = [];

/** @var SchemaAggregator */
private $schema;

public function __construct(Filesystem $files, SchemaAggregator $schema)
public function __construct(Filesystem $files, private SchemaAggregator $schema)
{
parent::__construct($files);
$this->schema = $schema;
}

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

$this->model_classes[] = get_class($model);
$this->model_classes[] = $model::class;

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

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

if (in_array($column_name, $model->getDates(), true)) {
$get_type = $set_type = '\Illuminate\Support\Carbon';
$get_type = $set_type = \Illuminate\Support\Carbon::class;
} else {
switch ($column->type) {
case SchemaColumn::TYPE_STRING:
Expand All @@ -101,7 +97,7 @@ public function getPropertiesFromTable($model): void
if (!$column->options) {
$get_type = $set_type = 'string';
} else {
$get_type = $set_type = '\'' . implode('\'|\'', $column->options) . '\'';
$get_type = $set_type = "'" . implode("'|'", $column->options) . "'";
}

break;
Expand All @@ -128,7 +124,7 @@ public function getPropertiesFromTable($model): void
$this->setMethod(
Str::camel("where_" . $column_name),
'\Illuminate\Database\Eloquent\Builder<static>', // @todo support custom EloquentBuilders
array('$value')
['$value']
);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Handlers/Application/ContainerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function getMethodReturnType(MethodReturnTypeProviderEvent $event)
* @see https://github.com/psalm/psalm-plugin-symfony/issues/25
* psalm needs to know about any classes that could be returned before analysis begins. This is a naive first approach
*/
public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event)
public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event): void
{
if (!in_array($event->getStorage()->name, ApplicationInterfaceProvider::getApplicationInterfaceClassLikes())) {
return;
Expand All @@ -91,13 +91,13 @@ public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event)
if ($reflectionClass->isAnonymous()) {
continue;
}
} catch (Throwable $e) {
} catch (Throwable) {
// cannot just catch binding exception as the following error is emitted within laravel:
// Class 'Symfony\Component\Cache\Adapter\Psr16Adapter' not found
continue;
}

$className = get_class($concrete);
$className = $concrete::class;
$filePath = $event->getStatementsSource()->getFilePath();
$fileStorage = $event->getCodebase()->file_storage_provider->get($filePath);
$fileStorage->referenced_classlikes[strtolower($className)] = $className;
Expand Down
13 changes: 5 additions & 8 deletions src/Handlers/Auth/AuthConfigAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ final class AuthConfigAnalyzer
{
private static ?AuthConfigAnalyzer $instance = null;

private ConfigRepository $config;

private function __construct(ConfigRepository $config)
private function __construct(private ConfigRepository $config)
{
$this->config = $config;
}

public static function instance(): self
Expand All @@ -43,17 +40,17 @@ public function getAuthenticatableFQCN(?string $guard = null): ?string
}
}

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

if (! is_string($provider)) {
return null;
}

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

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

public function getDefaultGuard(): ?string
Expand Down
1 change: 1 addition & 0 deletions src/Handlers/Auth/GuardHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ private static function findGuardNameInCallChain(MethodCall $methodCall): ?strin

$previous_call = null; // exit from while loop
}

unset($previous_call);

if (! $call_contains_guard_name instanceof CallLike) {
Expand Down
1 change: 1 addition & 0 deletions src/Handlers/Eloquent/ModelMethodHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static function getMethodReturnType(MethodReturnTypeProviderEvent $event)
if ($called_method_name_lowercase === null) {
return null;
}

$methodId = new MethodIdentifier($called_fq_classlike_name, $called_method_name_lowercase);

$fake_method_call = new MethodCall(
Expand Down
4 changes: 2 additions & 2 deletions src/Handlers/Eloquent/ModelPropertyAccessorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static function getPropertyType(PropertyTypeProviderEvent $event): ?Type\

if (self::accessorExists($codebase, $fq_classlike_name, $property_name)) {
$attributeGetterName = 'get' . str_replace('_', '', $property_name) . 'Attribute';
return $codebase->getMethodReturnType("$fq_classlike_name::$attributeGetterName", $fq_classlike_name)
return $codebase->getMethodReturnType("{$fq_classlike_name}::{$attributeGetterName}", $fq_classlike_name)
?: Type::getMixed();
}

Expand All @@ -94,7 +94,7 @@ private static function hasNativeProperty(string $fqcn, string $property_name):
{
try {
new \ReflectionProperty($fqcn, $property_name);
} catch (\ReflectionException $exception) {
} catch (\ReflectionException) {
return false;
}

Expand Down
7 changes: 0 additions & 7 deletions src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,6 @@ public static function getPropertyType(PropertyTypeProviderEvent $event): ?Union
return null;
}

/**
* @param Codebase $codebase
* @param string $fq_classlike_name
* @param string $property_name
*
* @return bool
*/
private static function relationExists(Codebase $codebase, string $fq_classlike_name, string $property_name): bool
{
// @todo: ensure this is a relation method
Expand Down
2 changes: 2 additions & 0 deletions src/Handlers/Eloquent/Schema/SchemaAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ private function processColumnUpdates(string $table_name, string $call_arg_name,
if ($second_arg instanceof PhpParser\Node\Scalar\String_) {
$table->renameColumn($column_name, $second_arg->value);
}

break;

case 'set':
Expand All @@ -479,6 +480,7 @@ private function processColumnUpdates(string $table_name, string $call_arg_name,
// @todo extract nullable value from 3rd arg
$table->renameColumn($_column_type, $column_name);
}

break;
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/Handlers/Eloquent/Schema/SchemaColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
class SchemaColumn
{
public const TYPE_STRING = 'string';

public const TYPE_INT = 'int';

public const TYPE_FLOAT = 'float';

public const TYPE_BOOL = 'bool';

public const TYPE_ENUM = 'enum';

public const TYPE_MIXED = 'mixed';

/** @var string */
Expand All @@ -35,6 +40,6 @@ public function __construct(
$this->name = $name;
$this->type = $type;
$this->nullable = $nullable;
$this->options = $options !== null ? $options : [];
$this->options = $options ?? [];
}
}
Loading

0 comments on commit ffd5139

Please sign in to comment.