From 54162c61c5150e7a43bb7c541b87e836c5c5884d Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:12:55 +0100 Subject: [PATCH 01/17] init Rector --- composer.json | 4 +++- rector.php | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 rector.php diff --git a/composer.json b/composer.json index abe35fcc..07d11a41 100644 --- a/composer.json +++ b/composer.json @@ -37,6 +37,7 @@ "laravel/framework": "^10.48 || ^11.0", "phpunit/phpunit": "^10.5 || ^11.0", "ramsey/collection": "^1.3", + "rector/rector": "^1.0", "slevomat/coding-standard": "^8.8", "squizlabs/php_codesniffer": "*", "symfony/http-foundation": "^6.0 || ^7.0" @@ -80,6 +81,7 @@ "Composer\\Config::disableProcessTimeout", "codecept run --skip-group skip" ], - "test:unit": "phpunit" + "test:unit": "phpunit", + "rector": "./vendor/bin/rector --dry-run" } } diff --git a/rector.php b/rector.php new file mode 100644 index 00000000..a8190644 --- /dev/null +++ b/rector.php @@ -0,0 +1,26 @@ +withPaths(['src', 'tests']) + ->withPhpSets(php81: true) + ->withPreparedSets(deadCode: true) + ->withSkip([ + ReadOnlyPropertyRector::class, + ClosureToArrowFunctionRector::class, + FirstClassCallableRector::class, + NullToStrictStringFuncCallArgRector::class, + BooleanInIfConditionRuleFixerRector::class, + BooleanInBooleanNotRuleFixerRector::class, + RemoveUnusedPrivateMethodRector::class, + SimplifyUselessVariableRector::class, + ]); From 500fb63135930040bd27bb68cc007e0738422a76 Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:14:02 +0100 Subject: [PATCH 02/17] run the 8.1 ruleset for Rector --- rector.php | 2 +- src/Fakes/FakeModelsCommand.php | 12 ++++-------- src/Handlers/Application/ContainerHandler.php | 4 ++-- src/Handlers/Auth/AuthConfigAnalyzer.php | 7 ++----- .../Eloquent/ModelPropertyAccessorHandler.php | 2 +- .../Eloquent/ModelRelationshipPropertyHandler.php | 3 --- src/Handlers/SuppressHandler.php | 5 ++--- src/Providers/ApplicationProvider.php | 12 ++++++------ src/Util/ContainerResolver.php | 4 ++-- tests/Acceptance/Support/Module.php | 4 ++-- .../Schema/AbstractSchemaAggregatorTestCase.php | 1 - 11 files changed, 22 insertions(+), 34 deletions(-) diff --git a/rector.php b/rector.php index a8190644..4ee30b35 100644 --- a/rector.php +++ b/rector.php @@ -13,7 +13,7 @@ return RectorConfig::configure() ->withPaths(['src', 'tests']) ->withPhpSets(php81: true) - ->withPreparedSets(deadCode: true) + //->withPreparedSets(deadCode: true) ->withSkip([ ReadOnlyPropertyRector::class, ClosureToArrowFunctionRector::class, diff --git a/src/Fakes/FakeModelsCommand.php b/src/Fakes/FakeModelsCommand.php index 391087ee..ad9f1098 100644 --- a/src/Fakes/FakeModelsCommand.php +++ b/src/Fakes/FakeModelsCommand.php @@ -21,13 +21,9 @@ class FakeModelsCommand extends ModelsCommand /** @var list> */ private $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> */ @@ -66,7 +62,7 @@ public function getPropertiesFromTable($model): void return; } - $this->model_classes[] = get_class($model); + $this->model_classes[] = $model::class; $columns = $this->schema->tables[$table_name]->columns; @@ -74,7 +70,7 @@ public function getPropertiesFromTable($model): void $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: @@ -128,7 +124,7 @@ public function getPropertiesFromTable($model): void $this->setMethod( Str::camel("where_" . $column_name), '\Illuminate\Database\Eloquent\Builder', // @todo support custom EloquentBuilders - array('$value') + ['$value'] ); } } diff --git a/src/Handlers/Application/ContainerHandler.php b/src/Handlers/Application/ContainerHandler.php index 66caf7d0..45154dde 100644 --- a/src/Handlers/Application/ContainerHandler.php +++ b/src/Handlers/Application/ContainerHandler.php @@ -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; diff --git a/src/Handlers/Auth/AuthConfigAnalyzer.php b/src/Handlers/Auth/AuthConfigAnalyzer.php index 63b43c1c..541174b5 100644 --- a/src/Handlers/Auth/AuthConfigAnalyzer.php +++ b/src/Handlers/Auth/AuthConfigAnalyzer.php @@ -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 @@ -50,7 +47,7 @@ public function getAuthenticatableFQCN(?string $guard = null): ?string } if ($this->config->get("auth.providers.$provider.driver") === 'database') { - return '\Illuminate\Auth\GenericUser'; + return \Illuminate\Auth\GenericUser::class; } return $this->config->get("auth.providers.$provider.model", null); diff --git a/src/Handlers/Eloquent/ModelPropertyAccessorHandler.php b/src/Handlers/Eloquent/ModelPropertyAccessorHandler.php index b108ce07..147b6114 100644 --- a/src/Handlers/Eloquent/ModelPropertyAccessorHandler.php +++ b/src/Handlers/Eloquent/ModelPropertyAccessorHandler.php @@ -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; } diff --git a/src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php b/src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php index f2ffed0f..b4e46d0f 100644 --- a/src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php +++ b/src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php @@ -153,9 +153,6 @@ public static function getPropertyType(PropertyTypeProviderEvent $event): ?Union } /** - * @param Codebase $codebase - * @param string $fq_classlike_name - * @param string $property_name * * @return bool */ diff --git a/src/Handlers/SuppressHandler.php b/src/Handlers/SuppressHandler.php index 99bb7aab..93c4a612 100644 --- a/src/Handlers/SuppressHandler.php +++ b/src/Handlers/SuppressHandler.php @@ -95,7 +95,7 @@ public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event) foreach (self::BY_NAMESPACE as $issue => $namespaces) { foreach ($namespaces as $namespace) { - if (0 !== strpos($class->name, "$namespace\\")) { + if (!str_starts_with($class->name, "$namespace\\")) { continue; } @@ -106,7 +106,7 @@ public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event) foreach (self::BY_NAMESPACE_METHOD as $issue => $methods_by_namespaces) { foreach ($methods_by_namespaces as $namespace => $method_names) { - if (0 !== strpos($class->name, "$namespace\\")) { + if (!str_starts_with($class->name, "$namespace\\")) { continue; } @@ -146,7 +146,6 @@ public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event) } /** - * @param string $issue * @param ClassLikeStorage|PropertyStorage|MethodStorage|null $storage */ private static function suppress(string $issue, $storage): void diff --git a/src/Providers/ApplicationProvider.php b/src/Providers/ApplicationProvider.php index 0510bbc5..cb6baf1d 100644 --- a/src/Providers/ApplicationProvider.php +++ b/src/Providers/ApplicationProvider.php @@ -70,7 +70,7 @@ public static function getApp(): LaravelApplication */ public static function getAppFullyQualifiedClassName(): string { - return get_class(self::getApp()); + return self::getApp()::class; } /** @@ -87,16 +87,16 @@ protected function resolveApplicationBootstrappers($app) // of 0 //$app->make('Illuminate\Foundation\Bootstrap\HandleExceptions')->bootstrap($app); /** @psalm-suppress MixedMethodCall */ - $app->make('Illuminate\Foundation\Bootstrap\RegisterFacades')->bootstrap($app); + $app->make(\Illuminate\Foundation\Bootstrap\RegisterFacades::class)->bootstrap($app); /** @psalm-suppress MixedMethodCall */ - $app->make('Illuminate\Foundation\Bootstrap\SetRequestForConsole')->bootstrap($app); + $app->make(\Illuminate\Foundation\Bootstrap\SetRequestForConsole::class)->bootstrap($app); /** @psalm-suppress MixedMethodCall */ - $app->make('Illuminate\Foundation\Bootstrap\RegisterProviders')->bootstrap($app); + $app->make(\Illuminate\Foundation\Bootstrap\RegisterProviders::class)->bootstrap($app); $this->getEnvironmentSetUp($app); /** @psalm-suppress MixedMethodCall */ - $app->make('Illuminate\Foundation\Bootstrap\BootProviders')->bootstrap($app); + $app->make(\Illuminate\Foundation\Bootstrap\BootProviders::class)->bootstrap($app); foreach ($this->getPackageBootstrappers($app) as $bootstrap) { /** @psalm-suppress MixedMethodCall */ @@ -104,7 +104,7 @@ protected function resolveApplicationBootstrappers($app) } /** @psalm-suppress MixedMethodCall */ - $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap(); + $app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap(); /** @var \Illuminate\Routing\Router $router */ $router = $app['router']; diff --git a/src/Util/ContainerResolver.php b/src/Util/ContainerResolver.php index 7db95ff7..093b5856 100644 --- a/src/Util/ContainerResolver.php +++ b/src/Util/ContainerResolver.php @@ -41,7 +41,7 @@ private static function resolveFromApplicationContainer(string $abstract): ?stri try { /** @var mixed $concrete */ $concrete = ApplicationProvider::getApp()->make($abstract); - } catch (\Throwable $e) { + } catch (\Throwable) { return null; } @@ -50,7 +50,7 @@ private static function resolveFromApplicationContainer(string $abstract): ?stri $concreteClass = $concrete; } elseif (is_object($concrete)) { // normally we have an object resolved - $concreteClass = get_class($concrete); + $concreteClass = $concrete::class; } else { // not sure how to handle this yet return null; diff --git a/tests/Acceptance/Support/Module.php b/tests/Acceptance/Support/Module.php index c76bf1b4..acd13b0a 100644 --- a/tests/Acceptance/Support/Module.php +++ b/tests/Acceptance/Support/Module.php @@ -250,7 +250,7 @@ private function packageSatisfiesVersionConstraint(string $package, string $vers { try { $currentVersion = $this->getShortVersion($package); - } catch (OutOfBoundsException $ex) { + } catch (OutOfBoundsException) { $this->debug(sprintf("Package %s is not installed", $package)); return false; } @@ -540,7 +540,7 @@ private function getShortVersion(string $package): string ); } - if (false === strpos($version, '@')) { + if (!str_contains($version, '@')) { throw new RuntimeException('$version must contain @'); } diff --git a/tests/Unit/Handlers/Eloquent/Schema/AbstractSchemaAggregatorTestCase.php b/tests/Unit/Handlers/Eloquent/Schema/AbstractSchemaAggregatorTestCase.php index ae4531a7..4bc326b0 100644 --- a/tests/Unit/Handlers/Eloquent/Schema/AbstractSchemaAggregatorTestCase.php +++ b/tests/Unit/Handlers/Eloquent/Schema/AbstractSchemaAggregatorTestCase.php @@ -132,7 +132,6 @@ protected function assertSchemaHasTableAndNotNullableColumnOfType(string $tableW } /** - * @param string $tableWithColumn * @return array{0: non-empty-string, 1: non-empty-string} */ private function parseTableWithColumn(string $tableWithColumn): array From b37f17f5b7bd23e6ac53bd6fa992df2ef865a328 Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:14:36 +0100 Subject: [PATCH 03/17] run the deadcode ruleset --- rector.php | 2 +- src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/rector.php b/rector.php index 4ee30b35..a8190644 100644 --- a/rector.php +++ b/rector.php @@ -13,7 +13,7 @@ return RectorConfig::configure() ->withPaths(['src', 'tests']) ->withPhpSets(php81: true) - //->withPreparedSets(deadCode: true) + ->withPreparedSets(deadCode: true) ->withSkip([ ReadOnlyPropertyRector::class, ClosureToArrowFunctionRector::class, diff --git a/src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php b/src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php index b4e46d0f..1afb8965 100644 --- a/src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php +++ b/src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php @@ -152,10 +152,7 @@ public static function getPropertyType(PropertyTypeProviderEvent $event): ?Union return null; } - /** - * - * @return bool - */ + private static function relationExists(Codebase $codebase, string $fq_classlike_name, string $property_name): bool { // @todo: ensure this is a relation method From be1bff83edf867bbc93d76025d14b1bf2158b968 Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:21:11 +0100 Subject: [PATCH 04/17] setup config rules for coding style --- rector.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/rector.php b/rector.php index a8190644..98195313 100644 --- a/rector.php +++ b/rector.php @@ -1,6 +1,9 @@ withPaths(['src', 'tests']) + ->withSkipPath('tests/Acceptance/_support/_generated') ->withPhpSets(php81: true) - ->withPreparedSets(deadCode: true) + ->withPreparedSets(deadCode: true, codingStyle: true) ->withSkip([ ReadOnlyPropertyRector::class, ClosureToArrowFunctionRector::class, @@ -23,4 +27,7 @@ BooleanInBooleanNotRuleFixerRector::class, RemoveUnusedPrivateMethodRector::class, SimplifyUselessVariableRector::class, + NullableCompareToNullRector::class, + EncapsedStringsToSprintfRector::class, + StaticClosureRector::class, ]); From f07718e0c874ede0bb917312415aa3161648489b Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:21:34 +0100 Subject: [PATCH 05/17] run coding style rule set --- src/Fakes/FakeModelsCommand.php | 18 ++++++++++------ src/Handlers/Auth/AuthConfigAnalyzer.php | 6 +++--- src/Handlers/Auth/GuardHandler.php | 1 + src/Handlers/Eloquent/ModelMethodHandler.php | 1 + .../Eloquent/ModelPropertyAccessorHandler.php | 2 +- .../ModelRelationshipPropertyHandler.php | 1 - .../Eloquent/Schema/SchemaAggregator.php | 2 ++ src/Handlers/Eloquent/Schema/SchemaColumn.php | 5 +++++ src/Handlers/SuppressHandler.php | 6 +++--- src/Plugin.php | 4 ++-- src/Providers/GeneratesStubs.php | 1 + src/Util/ContainerResolver.php | 2 +- tests/Acceptance/Support/Module.php | 21 ++++++++++++------- .../AbstractSchemaAggregatorTestCase.php | 6 +++--- .../2014_10_12_000000_create_users_table.php | 1 + 15 files changed, 50 insertions(+), 27 deletions(-) diff --git a/src/Fakes/FakeModelsCommand.php b/src/Fakes/FakeModelsCommand.php index ad9f1098..d0cf0cc0 100644 --- a/src/Fakes/FakeModelsCommand.php +++ b/src/Fakes/FakeModelsCommand.php @@ -70,13 +70,15 @@ public function getPropertiesFromTable($model): void $column_name = $column->name; if (in_array($column_name, $model->getDates(), true)) { - $get_type = $set_type = \Illuminate\Support\Carbon::class; + $get_type = \Illuminate\Support\Carbon::class; + $set_type = \Illuminate\Support\Carbon::class; } else { switch ($column->type) { case SchemaColumn::TYPE_STRING: case SchemaColumn::TYPE_INT: case SchemaColumn::TYPE_FLOAT: - $get_type = $set_type = $column->type; + $get_type = $column->type; + $set_type = $column->type; break; case SchemaColumn::TYPE_BOOL: @@ -87,7 +89,8 @@ public function getPropertiesFromTable($model): void $get_type = '0|1'; break; default: - $get_type = $set_type = 'bool'; + $get_type = 'bool'; + $set_type = 'bool'; break; } @@ -95,15 +98,18 @@ public function getPropertiesFromTable($model): void case SchemaColumn::TYPE_ENUM: if (!$column->options) { - $get_type = $set_type = 'string'; + $get_type = 'string'; + $set_type = 'string'; } else { - $get_type = $set_type = '\'' . implode('\'|\'', $column->options) . '\''; + $get_type = "'" . implode("'|'", $column->options) . "'"; + $set_type = "'" . implode("'|'", $column->options) . "'"; } break; default: - $get_type = $set_type = SchemaColumn::TYPE_MIXED; + $get_type = SchemaColumn::TYPE_MIXED; + $set_type = SchemaColumn::TYPE_MIXED; break; } } diff --git a/src/Handlers/Auth/AuthConfigAnalyzer.php b/src/Handlers/Auth/AuthConfigAnalyzer.php index 541174b5..22094de7 100644 --- a/src/Handlers/Auth/AuthConfigAnalyzer.php +++ b/src/Handlers/Auth/AuthConfigAnalyzer.php @@ -40,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') { + 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 diff --git a/src/Handlers/Auth/GuardHandler.php b/src/Handlers/Auth/GuardHandler.php index b46d4cde..4c547d35 100644 --- a/src/Handlers/Auth/GuardHandler.php +++ b/src/Handlers/Auth/GuardHandler.php @@ -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) { diff --git a/src/Handlers/Eloquent/ModelMethodHandler.php b/src/Handlers/Eloquent/ModelMethodHandler.php index 11f0fea6..4c0fc8b0 100644 --- a/src/Handlers/Eloquent/ModelMethodHandler.php +++ b/src/Handlers/Eloquent/ModelMethodHandler.php @@ -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( diff --git a/src/Handlers/Eloquent/ModelPropertyAccessorHandler.php b/src/Handlers/Eloquent/ModelPropertyAccessorHandler.php index 147b6114..b9686754 100644 --- a/src/Handlers/Eloquent/ModelPropertyAccessorHandler.php +++ b/src/Handlers/Eloquent/ModelPropertyAccessorHandler.php @@ -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(); } diff --git a/src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php b/src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php index 1afb8965..594306b4 100644 --- a/src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php +++ b/src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php @@ -152,7 +152,6 @@ public static function getPropertyType(PropertyTypeProviderEvent $event): ?Union return null; } - private static function relationExists(Codebase $codebase, string $fq_classlike_name, string $property_name): bool { // @todo: ensure this is a relation method diff --git a/src/Handlers/Eloquent/Schema/SchemaAggregator.php b/src/Handlers/Eloquent/Schema/SchemaAggregator.php index 404b5419..bd8fa246 100644 --- a/src/Handlers/Eloquent/Schema/SchemaAggregator.php +++ b/src/Handlers/Eloquent/Schema/SchemaAggregator.php @@ -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': @@ -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; } } diff --git a/src/Handlers/Eloquent/Schema/SchemaColumn.php b/src/Handlers/Eloquent/Schema/SchemaColumn.php index 9d8dc86a..ef4533ae 100644 --- a/src/Handlers/Eloquent/Schema/SchemaColumn.php +++ b/src/Handlers/Eloquent/Schema/SchemaColumn.php @@ -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 */ diff --git a/src/Handlers/SuppressHandler.php b/src/Handlers/SuppressHandler.php index 93c4a612..26449d6d 100644 --- a/src/Handlers/SuppressHandler.php +++ b/src/Handlers/SuppressHandler.php @@ -10,8 +10,8 @@ use function array_intersect; use function in_array; -use function strpos; use function strtolower; +use function str_starts_with; class SuppressHandler implements AfterClassLikeVisitInterface { @@ -95,7 +95,7 @@ public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event) foreach (self::BY_NAMESPACE as $issue => $namespaces) { foreach ($namespaces as $namespace) { - if (!str_starts_with($class->name, "$namespace\\")) { + if (!str_starts_with($class->name, "{$namespace}\\")) { continue; } @@ -106,7 +106,7 @@ public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event) foreach (self::BY_NAMESPACE_METHOD as $issue => $methods_by_namespaces) { foreach ($methods_by_namespaces as $namespace => $method_names) { - if (!str_starts_with($class->name, "$namespace\\")) { + if (!str_starts_with($class->name, "{$namespace}\\")) { continue; } diff --git a/src/Plugin.php b/src/Plugin.php index b95ef1e9..e4dc1421 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -40,8 +40,8 @@ public function __invoke(RegistrationInterface $registration, ?SimpleXMLElement try { ApplicationProvider::bootApp(); $this->generateStubFiles(); - } catch (\Throwable $t) { - fwrite(\STDERR, "Laravel plugin error: “{$t->getMessage()}”\n"); + } catch (\Throwable $throwable) { + fwrite(\STDERR, "Laravel plugin error: “{$throwable->getMessage()}”\n"); return; } diff --git a/src/Providers/GeneratesStubs.php b/src/Providers/GeneratesStubs.php index 73ee06a9..f06dd83c 100644 --- a/src/Providers/GeneratesStubs.php +++ b/src/Providers/GeneratesStubs.php @@ -5,5 +5,6 @@ interface GeneratesStubs { public static function generateStubFile(): void; + public static function getStubFileLocation(): string; } diff --git a/src/Util/ContainerResolver.php b/src/Util/ContainerResolver.php index 093b5856..0f45feb7 100644 --- a/src/Util/ContainerResolver.php +++ b/src/Util/ContainerResolver.php @@ -66,7 +66,7 @@ private static function resolveFromApplicationContainer(string $abstract): ?stri */ public static function resolvePsalmTypeFromApplicationContainerViaArgs(NodeTypeProvider $nodeTypeProvider, array $call_args): ?Union { - if (! count($call_args)) { + if ($call_args === []) { return null; } diff --git a/tests/Acceptance/Support/Module.php b/tests/Acceptance/Support/Module.php index acd13b0a..d88459ab 100644 --- a/tests/Acceptance/Support/Module.php +++ b/tests/Acceptance/Support/Module.php @@ -110,6 +110,7 @@ public function _beforeSuite($settings = []): void if (is_dir($defaultDir)) { return; } + unlink($defaultDir); } @@ -193,7 +194,7 @@ public function seeThisError(string $type, string $message): void { $this->parseErrors(); if (empty($this->errors)) { - Assert::fail("No errors! Didn't see [ $type $message ] in an empty output.\n"); + Assert::fail("No errors! Didn't see [ {$type} {$message} ] in an empty output.\n"); } foreach ($this->errors as $i => $error) { @@ -206,7 +207,7 @@ public function seeThisError(string $type, string $message): void } } - Assert::fail("Didn't see [ $type $message ] in: \n" . $this->remainingErrors()); + Assert::fail("Didn't see [ {$type} {$message} ] in: \n" . $this->remainingErrors()); } private function matches(string $expected, string $actual): bool @@ -268,7 +269,7 @@ private function packageSatisfiesVersionConstraint(string $package, string $vers $result = Semver::satisfies($currentVersion, $versionConstraint); - $this->debug("Comparing $currentVersion against $versionConstraint => " . ($result ? 'ok' : 'ko')); + $this->debug("Comparing {$currentVersion} against {$versionConstraint} => " . ($result ? 'ok' : 'ko')); return $result; } @@ -370,7 +371,7 @@ public function haveTheFollowingCode($code): void public function haveSomeFuturePsalmThatSupportsThisFeature(string $ref): void { /** @psalm-suppress InternalClass,InternalMethod */ - throw new SkippedTestSuiteError("Future functionality that Psalm has yet to support: $ref"); + throw new SkippedTestSuiteError("Future functionality that Psalm has yet to support: {$ref}"); } /** @@ -379,14 +380,14 @@ public function haveSomeFuturePsalmThatSupportsThisFeature(string $ref): void public function havePsalmOfACertainVersionRangeBecauseOf(string $operator, string $version, string $reason): void { if (!isset(self::VERSION_OPERATORS[$operator])) { - throw new TestRuntimeException("Unknown operator: $operator"); + throw new TestRuntimeException("Unknown operator: {$operator}"); } $op = self::VERSION_OPERATORS[$operator]; if (!$this->packageSatisfiesVersionConstraint('vimeo/psalm', $op . $version)) { /** @psalm-suppress InternalClass,InternalMethod */ - throw new SkippedTestSuiteError("This scenario requires Psalm $op $version because of $reason"); + throw new SkippedTestSuiteError("This scenario requires Psalm {$op} {$version} because of {$reason}"); } } @@ -401,6 +402,7 @@ public function seeTheseErrors(TableNode $list): void if (0 === $i) { continue; } + $this->seeThisError((string) $error[0], (string) $error[1]); } } @@ -429,6 +431,7 @@ public function haveTheFollowingAutoloadMap(TableNode $list): void if (0 === $i) { continue; } + assert(is_string($row[0])); assert(is_string($row[1])); $map[] = [$row[0], $row[1]]; @@ -472,7 +475,7 @@ public function haveADependencySatisfied(string $package, string $versionConstra } /** @psalm-suppress InternalClass,InternalMethod */ - throw new SkippedTestSuiteError("This scenario requires $package to match $versionConstraint"); + throw new SkippedTestSuiteError("This scenario requires {$package} to match {$versionConstraint}"); } private function convertToRegexp(string $in): string @@ -487,8 +490,10 @@ private function cli(): Cli if (!$cli instanceof Cli) { throw new ModuleRequireException($this, 'Needs Cli module'); } + $this->cli = $cli; } + return $this->cli; } @@ -499,8 +504,10 @@ private function fs(): Filesystem if (!$fs instanceof Filesystem) { throw new ModuleRequireException($this, 'Needs Filesystem module'); } + $this->fs = $fs; } + return $this->fs; } diff --git a/tests/Unit/Handlers/Eloquent/Schema/AbstractSchemaAggregatorTestCase.php b/tests/Unit/Handlers/Eloquent/Schema/AbstractSchemaAggregatorTestCase.php index 4bc326b0..ffe48325 100644 --- a/tests/Unit/Handlers/Eloquent/Schema/AbstractSchemaAggregatorTestCase.php +++ b/tests/Unit/Handlers/Eloquent/Schema/AbstractSchemaAggregatorTestCase.php @@ -44,7 +44,7 @@ final protected function instantiateSchemaAggregator(string $filepath): SchemaAg foreach ($migrationFiles as $migrationFile) { $fileContents = file_get_contents($migrationFile); if ($fileContents === false) { - $this->fail("Could not read $migrationFile file. Please make sure it exists and readable."); + $this->fail("Could not read {$migrationFile} file. Please make sure it exists and readable."); } $statements = StatementsProvider::parseStatements($fileContents, PHP_VERSION_ID, $hasErrors); @@ -119,7 +119,7 @@ protected function assertSchemaHasTableAndNullableColumnOfType(string $tableWith [$tableName, $columnName] = self::parseTableWithColumn($tableWithColumn); - self::assertTrue($schemaAggregator->tables[$tableName]->columns[$columnName]->nullable, "Column $tableWithColumn is not nullable"); + self::assertTrue($schemaAggregator->tables[$tableName]->columns[$columnName]->nullable, "Column {$tableWithColumn} is not nullable"); } protected function assertSchemaHasTableAndNotNullableColumnOfType(string $tableWithColumn, string $type, SchemaAggregator $schemaAggregator): void @@ -128,7 +128,7 @@ protected function assertSchemaHasTableAndNotNullableColumnOfType(string $tableW [$tableName, $columnName] = self::parseTableWithColumn($tableWithColumn); - self::assertFalse($schemaAggregator->tables[$tableName]->columns[$columnName]->nullable, "Column $tableWithColumn is nullable"); + self::assertFalse($schemaAggregator->tables[$tableName]->columns[$columnName]->nullable, "Column {$tableWithColumn} is nullable"); } /** diff --git a/tests/Unit/Handlers/Eloquent/Schema/migrations/default_users_table_named/2014_10_12_000000_create_users_table.php b/tests/Unit/Handlers/Eloquent/Schema/migrations/default_users_table_named/2014_10_12_000000_create_users_table.php index 16e0768e..621a24eb 100644 --- a/tests/Unit/Handlers/Eloquent/Schema/migrations/default_users_table_named/2014_10_12_000000_create_users_table.php +++ b/tests/Unit/Handlers/Eloquent/Schema/migrations/default_users_table_named/2014_10_12_000000_create_users_table.php @@ -23,6 +23,7 @@ public function up() $table->timestamps(); }); } + /** * Reverse the migrations. * From 8303fa3faf84dd9316ea1900bb37dfed8c4ba57b Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:27:55 +0100 Subject: [PATCH 06/17] run rector again after rebasing --- src/Fakes/FakeFilesystem.php | 2 +- src/Handlers/Eloquent/Schema/SchemaColumn.php | 2 +- src/Providers/ApplicationProvider.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Fakes/FakeFilesystem.php b/src/Fakes/FakeFilesystem.php index 96d900b4..9012e64b 100644 --- a/src/Fakes/FakeFilesystem.php +++ b/src/Fakes/FakeFilesystem.php @@ -19,7 +19,7 @@ final class FakeFilesystem extends Filesystem */ public function put($path, $contents, $lock = false) { - $destination = $this->destination !== null ? $this->destination : $path; + $destination = $this->destination ?? $path; $this->destination = null; diff --git a/src/Handlers/Eloquent/Schema/SchemaColumn.php b/src/Handlers/Eloquent/Schema/SchemaColumn.php index ef4533ae..bbf09fb2 100644 --- a/src/Handlers/Eloquent/Schema/SchemaColumn.php +++ b/src/Handlers/Eloquent/Schema/SchemaColumn.php @@ -40,6 +40,6 @@ public function __construct( $this->name = $name; $this->type = $type; $this->nullable = $nullable; - $this->options = $options !== null ? $options : []; + $this->options = $options ?? []; } } diff --git a/src/Providers/ApplicationProvider.php b/src/Providers/ApplicationProvider.php index cb6baf1d..bd6bcca0 100644 --- a/src/Providers/ApplicationProvider.php +++ b/src/Providers/ApplicationProvider.php @@ -22,7 +22,7 @@ final class ApplicationProvider use CreatesApplication; /** @var LaravelApplication|null */ - private static $app = null; + private static $app; public static function bootApp(): void { From 50432cf66860936ee5a93f449cd5dea211b27773 Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:29:21 +0100 Subject: [PATCH 07/17] run workflows on all branches this should trigger a run on my fork branch --- .github/workflows/psalm.yml | 1 - .github/workflows/test-laravel.yml | 1 - .github/workflows/test.yml | 1 - 3 files changed, 3 deletions(-) diff --git a/.github/workflows/psalm.yml b/.github/workflows/psalm.yml index 94c95788..c6670a31 100644 --- a/.github/workflows/psalm.yml +++ b/.github/workflows/psalm.yml @@ -2,7 +2,6 @@ name: Psalm on: push: - branches: [master] paths: - '**.php' - 'psalm*' diff --git a/.github/workflows/test-laravel.yml b/.github/workflows/test-laravel.yml index 0390b0d1..50851e44 100644 --- a/.github/workflows/test-laravel.yml +++ b/.github/workflows/test-laravel.yml @@ -2,7 +2,6 @@ name: Test laravel projects on: push: - branches: [master] paths: - '**.php' - '**.stubphp' diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2d3bb189..e76877be 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -3,7 +3,6 @@ name: Tests on: workflow_dispatch: push: - branches: [master] paths: - '**.php' - '**.stubphp' From 95c3e14caab61defe6bde35f6f955443ddb35177 Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:38:15 +0100 Subject: [PATCH 08/17] add types to trigger ci --- src/Fakes/FakeFilesystem.php | 7 ++----- src/Providers/ApplicationProvider.php | 3 ++- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Fakes/FakeFilesystem.php b/src/Fakes/FakeFilesystem.php index 9012e64b..afb38610 100644 --- a/src/Fakes/FakeFilesystem.php +++ b/src/Fakes/FakeFilesystem.php @@ -17,7 +17,7 @@ final class FakeFilesystem extends Filesystem * @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 ?? $path; @@ -26,10 +26,7 @@ public function put($path, $contents, $lock = false) return parent::put($destination, $contents, $lock); } - /** - * @return void - */ - public function setDestination(string $destination) + public function setDestination(string $destination): void { $this->destination = $destination; } diff --git a/src/Providers/ApplicationProvider.php b/src/Providers/ApplicationProvider.php index bd6bcca0..14334d18 100644 --- a/src/Providers/ApplicationProvider.php +++ b/src/Providers/ApplicationProvider.php @@ -5,6 +5,7 @@ namespace Psalm\LaravelPlugin\Providers; use Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider; +use Illuminate\Container\Container; use Illuminate\Contracts\Console\Kernel; use Illuminate\Foundation\Application as LaravelApplication; use Orchestra\Testbench\Concerns\CreatesApplication; @@ -38,7 +39,7 @@ public static function bootApp(): void public static function getApp(): LaravelApplication { - if (self::$app instanceof \Illuminate\Container\Container) { + if (self::$app instanceof Container) { return self::$app; } From b75833f1c6f26f7c81df53c5900b338fc59ddc4c Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:40:39 +0100 Subject: [PATCH 09/17] update baseline --- psalm-baseline.xml | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 812b8094..c79b61c1 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,24 +1,26 @@ - + + + + $abstract + + - - - config->get('auth.guards')]]> - + ?string |null]]> - config->get("auth.providers.$provider.model", null)]]> + config->get("auth.providers.{$provider}.model", null)]]> config->get('auth.defaults.guard')]]> - + null @@ -33,9 +35,9 @@ getFqClasslikeName()]]> - - - + array + array + array @@ -45,17 +47,12 @@ - - - - - - + new TLiteralString($result) - + new TLiteralString($concrete) From 54123ee333326d576d55b1f419d88d212963efad Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:44:35 +0100 Subject: [PATCH 10/17] trigger ci --- src/Fakes/FakeFilesystem.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Fakes/FakeFilesystem.php b/src/Fakes/FakeFilesystem.php index afb38610..341a3cd6 100644 --- a/src/Fakes/FakeFilesystem.php +++ b/src/Fakes/FakeFilesystem.php @@ -15,7 +15,6 @@ final class FakeFilesystem extends Filesystem * @param string $path * @param string $contents * @param bool $lock - * @return bool|int */ public function put($path, $contents, $lock = false): bool|int { From 58aa7207627cf0113c3e81559bccee04bd41e6f1 Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:46:32 +0100 Subject: [PATCH 11/17] show all psalm issues --- .github/workflows/psalm.yml | 2 +- psalm-baseline.xml | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/psalm.yml b/.github/workflows/psalm.yml index c6670a31..72466650 100644 --- a/.github/workflows/psalm.yml +++ b/.github/workflows/psalm.yml @@ -31,4 +31,4 @@ jobs: run: composer install -n --prefer-dist - name: Run Psalm - run: ./vendor/bin/psalm --threads=2 --output-format=github --shepherd + run: ./vendor/bin/psalm --threads=2 --output-format=github --shepherd --show-info=true diff --git a/psalm-baseline.xml b/psalm-baseline.xml index c79b61c1..6a3abd58 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -50,6 +50,11 @@ new TLiteralString($result) + + + + + new TLiteralString($concrete) From c086c4027f0b62bdf3f96a6cab25fb660fb064ec Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:57:45 +0100 Subject: [PATCH 12/17] add return types with Rector --- rector.php | 2 +- src/Fakes/FakeFilesystem.php | 3 +-- src/Fakes/FakeModelsCommand.php | 2 +- src/Handlers/Application/ContainerHandler.php | 2 +- src/Handlers/SuppressHandler.php | 2 +- src/Providers/ApplicationProvider.php | 11 ++++------- src/Providers/ModelStubProvider.php | 2 +- src/Util/ContainerResolver.php | 3 +-- tests/Application/app/Models/AbstractUuidModel.php | 2 +- .../2014_10_12_000000_create_users_table.php | 10 +++------- .../2014_10_12_000000_create_users_table.php | 10 +++------- .../2014_10_12_000000_create_users_table.php | 10 +++------- 12 files changed, 21 insertions(+), 38 deletions(-) diff --git a/rector.php b/rector.php index 98195313..ffecd1ed 100644 --- a/rector.php +++ b/rector.php @@ -17,7 +17,7 @@ ->withPaths(['src', 'tests']) ->withSkipPath('tests/Acceptance/_support/_generated') ->withPhpSets(php81: true) - ->withPreparedSets(deadCode: true, codingStyle: true) + ->withPreparedSets(deadCode: true, codingStyle: true, typeDeclarations: true) ->withSkip([ ReadOnlyPropertyRector::class, ClosureToArrowFunctionRector::class, diff --git a/src/Fakes/FakeFilesystem.php b/src/Fakes/FakeFilesystem.php index 341a3cd6..e6b20e89 100644 --- a/src/Fakes/FakeFilesystem.php +++ b/src/Fakes/FakeFilesystem.php @@ -6,8 +6,7 @@ final class FakeFilesystem extends Filesystem { - /** @var ?string */ - private $destination = ''; + private ?string $destination = ''; /** * Write the contents of a file. diff --git a/src/Fakes/FakeModelsCommand.php b/src/Fakes/FakeModelsCommand.php index d0cf0cc0..435c5bae 100644 --- a/src/Fakes/FakeModelsCommand.php +++ b/src/Fakes/FakeModelsCommand.php @@ -19,7 +19,7 @@ class FakeModelsCommand extends ModelsCommand { /** @var list> */ - private $model_classes = []; + private array $model_classes = []; public function __construct(Filesystem $files, private SchemaAggregator $schema) { diff --git a/src/Handlers/Application/ContainerHandler.php b/src/Handlers/Application/ContainerHandler.php index 45154dde..e588b6d1 100644 --- a/src/Handlers/Application/ContainerHandler.php +++ b/src/Handlers/Application/ContainerHandler.php @@ -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; diff --git a/src/Handlers/SuppressHandler.php b/src/Handlers/SuppressHandler.php index 26449d6d..17cc4f75 100644 --- a/src/Handlers/SuppressHandler.php +++ b/src/Handlers/SuppressHandler.php @@ -76,7 +76,7 @@ class SuppressHandler implements AfterClassLikeVisitInterface ] ]; - public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event) + public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event): void { $class = $event->getStorage(); diff --git a/src/Providers/ApplicationProvider.php b/src/Providers/ApplicationProvider.php index 14334d18..dd4564ed 100644 --- a/src/Providers/ApplicationProvider.php +++ b/src/Providers/ApplicationProvider.php @@ -22,8 +22,7 @@ final class ApplicationProvider { use CreatesApplication; - /** @var LaravelApplication|null */ - private static $app; + private static ?\Illuminate\Foundation\Application $app = null; public static function bootApp(): void { @@ -79,10 +78,8 @@ public static function getAppFullyQualifiedClassName(): string * Resolve application bootstrapper. * * @param LaravelApplication $app - * - * @return void */ - protected function resolveApplicationBootstrappers($app) + protected function resolveApplicationBootstrappers($app): void { // we want to keep the default psalm exception handler, otherwise the Laravel one will always return exit codes // of 0 @@ -115,7 +112,7 @@ protected function resolveApplicationBootstrappers($app) * @psalm-suppress MissingClosureParamType * @psalm-suppress UnusedClosureParam */ - $app->resolving('url', static function ($url, $app) use ($router) { + $app->resolving('url', static function ($url, $app) use ($router): void { $router->getRoutes()->refreshNameLookups(); }); } @@ -123,7 +120,7 @@ protected function resolveApplicationBootstrappers($app) /** * @param LaravelApplication $app */ - protected function getEnvironmentSetUp($app): void + protected function getEnvironmentSetUp(array $app): void { /** @var \Illuminate\Config\Repository $config */ $config = $app['config']; diff --git a/src/Providers/ModelStubProvider.php b/src/Providers/ModelStubProvider.php index 52ed780b..326dbc19 100644 --- a/src/Providers/ModelStubProvider.php +++ b/src/Providers/ModelStubProvider.php @@ -16,7 +16,7 @@ final class ModelStubProvider implements GeneratesStubs { /** @var list> */ - private static $model_classes = []; + private static array $model_classes = []; public static function generateStubFile(): void { diff --git a/src/Util/ContainerResolver.php b/src/Util/ContainerResolver.php index 0f45feb7..976f51b3 100644 --- a/src/Util/ContainerResolver.php +++ b/src/Util/ContainerResolver.php @@ -23,10 +23,9 @@ final class ContainerResolver { /** * map of abstract to concrete class fqn - * @var array * @psalm-var array */ - private static $cache = []; + private static array $cache = []; /** * @psalm-return class-string|string|null diff --git a/tests/Application/app/Models/AbstractUuidModel.php b/tests/Application/app/Models/AbstractUuidModel.php index 08b10058..49831c77 100644 --- a/tests/Application/app/Models/AbstractUuidModel.php +++ b/tests/Application/app/Models/AbstractUuidModel.php @@ -15,7 +15,7 @@ protected static function boot() { parent::boot(); - static::creating(function (Model $model) { + static::creating(function (Model $model): void { $model->setAttribute('uuid', Str::uuid()); }); } diff --git a/tests/Unit/Handlers/Eloquent/Schema/migrations/default_users_table_anon/2014_10_12_000000_create_users_table.php b/tests/Unit/Handlers/Eloquent/Schema/migrations/default_users_table_anon/2014_10_12_000000_create_users_table.php index beb06a36..33949925 100644 --- a/tests/Unit/Handlers/Eloquent/Schema/migrations/default_users_table_anon/2014_10_12_000000_create_users_table.php +++ b/tests/Unit/Handlers/Eloquent/Schema/migrations/default_users_table_anon/2014_10_12_000000_create_users_table.php @@ -7,12 +7,10 @@ return new class extends Migration { /** * Run the migrations. - * - * @return void */ - public function up() + public function up(): void { - Schema::create('users', function (Blueprint $table) { + Schema::create('users', function (Blueprint $table): void { $table->id(); $table->string('name'); $table->string('email')->unique(); @@ -25,10 +23,8 @@ public function up() /** * Reverse the migrations. - * - * @return void */ - public function down() + public function down(): void { Schema::dropIfExists('users'); } diff --git a/tests/Unit/Handlers/Eloquent/Schema/migrations/default_users_table_named/2014_10_12_000000_create_users_table.php b/tests/Unit/Handlers/Eloquent/Schema/migrations/default_users_table_named/2014_10_12_000000_create_users_table.php index 621a24eb..3abad593 100644 --- a/tests/Unit/Handlers/Eloquent/Schema/migrations/default_users_table_named/2014_10_12_000000_create_users_table.php +++ b/tests/Unit/Handlers/Eloquent/Schema/migrations/default_users_table_named/2014_10_12_000000_create_users_table.php @@ -8,12 +8,10 @@ class CreateUsersTable extends Migration { /** * Run the migrations. - * - * @return void */ - public function up() + public function up(): void { - Schema::create('users', function (Blueprint $table) { + Schema::create('users', function (Blueprint $table): void { $table->id(); $table->string('name'); $table->string('email')->unique(); @@ -26,10 +24,8 @@ public function up() /** * Reverse the migrations. - * - * @return void */ - public function down() + public function down(): void { Schema::dropIfExists('users'); } diff --git a/tests/Unit/Handlers/Eloquent/Schema/migrations/default_users_table_root_ns_facade/2014_10_12_000000_create_users_table.php b/tests/Unit/Handlers/Eloquent/Schema/migrations/default_users_table_root_ns_facade/2014_10_12_000000_create_users_table.php index f1110f10..470a9670 100644 --- a/tests/Unit/Handlers/Eloquent/Schema/migrations/default_users_table_root_ns_facade/2014_10_12_000000_create_users_table.php +++ b/tests/Unit/Handlers/Eloquent/Schema/migrations/default_users_table_root_ns_facade/2014_10_12_000000_create_users_table.php @@ -6,12 +6,10 @@ return new class extends Migration { /** * Run the migrations. - * - * @return void */ - public function up() + public function up(): void { - Schema::create('users', function (Blueprint $table) { + Schema::create('users', function (Blueprint $table): void { $table->id(); $table->string('name'); $table->string('email')->unique(); @@ -24,10 +22,8 @@ public function up() /** * Reverse the migrations. - * - * @return void */ - public function down() + public function down(): void { Schema::dropIfExists('users'); } From 6f96632a692fca4dcabfcb3e2d2cf72360070303 Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Wed, 13 Mar 2024 11:02:21 +0100 Subject: [PATCH 13/17] allow App too --- .github/workflows/psalm.yml | 2 +- src/Providers/ApplicationProvider.php | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/psalm.yml b/.github/workflows/psalm.yml index 72466650..c6670a31 100644 --- a/.github/workflows/psalm.yml +++ b/.github/workflows/psalm.yml @@ -31,4 +31,4 @@ jobs: run: composer install -n --prefer-dist - name: Run Psalm - run: ./vendor/bin/psalm --threads=2 --output-format=github --shepherd --show-info=true + run: ./vendor/bin/psalm --threads=2 --output-format=github --shepherd diff --git a/src/Providers/ApplicationProvider.php b/src/Providers/ApplicationProvider.php index dd4564ed..cd206abc 100644 --- a/src/Providers/ApplicationProvider.php +++ b/src/Providers/ApplicationProvider.php @@ -117,10 +117,7 @@ protected function resolveApplicationBootstrappers($app): void }); } - /** - * @param LaravelApplication $app - */ - protected function getEnvironmentSetUp(array $app): void + protected function getEnvironmentSetUp(array|LaravelApplication $app): void { /** @var \Illuminate\Config\Repository $config */ $config = $app['config']; From f9647aca17570f888186ed9680cfea8b2ee3769b Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Wed, 13 Mar 2024 11:07:42 +0100 Subject: [PATCH 14/17] disallow array --- src/Providers/ApplicationProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Providers/ApplicationProvider.php b/src/Providers/ApplicationProvider.php index cd206abc..b5562226 100644 --- a/src/Providers/ApplicationProvider.php +++ b/src/Providers/ApplicationProvider.php @@ -117,7 +117,7 @@ protected function resolveApplicationBootstrappers($app): void }); } - protected function getEnvironmentSetUp(array|LaravelApplication $app): void + protected function getEnvironmentSetUp(LaravelApplication $app): void { /** @var \Illuminate\Config\Repository $config */ $config = $app['config']; From 689fe8b0611f9dc0f02fb42dcbf3e307c856698a Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Mon, 18 Mar 2024 15:38:45 +0100 Subject: [PATCH 15/17] undo splitting double assignments --- rector.php | 2 ++ src/Fakes/FakeModelsCommand.php | 15 +++++---------- src/Providers/ApplicationProvider.php | 4 +--- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/rector.php b/rector.php index ffecd1ed..19d869c2 100644 --- a/rector.php +++ b/rector.php @@ -1,6 +1,7 @@ name; if (in_array($column_name, $model->getDates(), true)) { - $get_type = \Illuminate\Support\Carbon::class; - $set_type = \Illuminate\Support\Carbon::class; + $get_type = $set_type = \Illuminate\Support\Carbon::class; } else { switch ($column->type) { case SchemaColumn::TYPE_STRING: @@ -89,8 +88,7 @@ public function getPropertiesFromTable($model): void $get_type = '0|1'; break; default: - $get_type = 'bool'; - $set_type = 'bool'; + $get_type = $set_type = 'bool'; break; } @@ -98,18 +96,15 @@ public function getPropertiesFromTable($model): void case SchemaColumn::TYPE_ENUM: if (!$column->options) { - $get_type = 'string'; - $set_type = 'string'; + $get_type = $set_type = 'string'; } else { - $get_type = "'" . implode("'|'", $column->options) . "'"; - $set_type = "'" . implode("'|'", $column->options) . "'"; + $get_type = $set_type = "'" . implode("'|'", $column->options) . "'"; } break; default: - $get_type = SchemaColumn::TYPE_MIXED; - $set_type = SchemaColumn::TYPE_MIXED; + $get_type = $set_type = SchemaColumn::TYPE_MIXED; break; } } diff --git a/src/Providers/ApplicationProvider.php b/src/Providers/ApplicationProvider.php index b5562226..dacb4f15 100644 --- a/src/Providers/ApplicationProvider.php +++ b/src/Providers/ApplicationProvider.php @@ -76,10 +76,8 @@ public static function getAppFullyQualifiedClassName(): string /** * Overrides {@see \Orchestra\Testbench\Concerns\CreatesApplication::resolveApplicationBootstrappers} * Resolve application bootstrapper. - * - * @param LaravelApplication $app */ - protected function resolveApplicationBootstrappers($app): void + protected function resolveApplicationBootstrappers(LaravelApplication $app): void { // we want to keep the default psalm exception handler, otherwise the Laravel one will always return exit codes // of 0 From e0d54f14a5ad9c82fb2b8046a6f94bfb6ed9a7ce Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Mon, 18 Mar 2024 15:40:10 +0100 Subject: [PATCH 16/17] undo splitting double assignments --- src/Fakes/FakeModelsCommand.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Fakes/FakeModelsCommand.php b/src/Fakes/FakeModelsCommand.php index 3dfc01ad..555aff56 100644 --- a/src/Fakes/FakeModelsCommand.php +++ b/src/Fakes/FakeModelsCommand.php @@ -76,8 +76,7 @@ public function getPropertiesFromTable($model): void case SchemaColumn::TYPE_STRING: case SchemaColumn::TYPE_INT: case SchemaColumn::TYPE_FLOAT: - $get_type = $column->type; - $set_type = $column->type; + $get_type = $set_type = $column->type; break; case SchemaColumn::TYPE_BOOL: From 6fb166cb3d8be4d1088099852956cd6543fad1cd Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Tue, 19 Mar 2024 09:29:31 +0100 Subject: [PATCH 17/17] remove skipped path --- rector.php | 1 - 1 file changed, 1 deletion(-) diff --git a/rector.php b/rector.php index 19d869c2..ed2ea39f 100644 --- a/rector.php +++ b/rector.php @@ -16,7 +16,6 @@ return RectorConfig::configure() ->withPaths(['src', 'tests']) - ->withSkipPath('tests/Acceptance/_support/_generated') ->withPhpSets(php81: true) ->withPreparedSets(deadCode: true, codingStyle: true, typeDeclarations: true) ->withSkip([