From e780aea97f11038c604d58c78e2414849fe41e78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Fri, 6 Sep 2024 10:07:51 +0200 Subject: [PATCH 1/3] Add rector --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e7d2f09cd..8c715bcd3 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,8 @@ "mockery/mockery": "^1.4.4", "doctrine/coding-standard": "12.0.x-dev", "spatie/laravel-query-builder": "^5.6", - "phpstan/phpstan": "^1.10" + "phpstan/phpstan": "^1.10", + "rector/rector": "^1.2" }, "suggest": { "league/flysystem-gridfs": "Filesystem storage in MongoDB with GridFS", From f9b160020cc569abd08a6c7d4e54b8969daf0752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Fri, 6 Sep 2024 13:50:50 +0200 Subject: [PATCH 2/3] Modernize code with rector --- rector.php | 26 ++++++++++++++++++++++++++ src/Bus/MongoBatchRepository.php | 2 +- src/Helpers/QueriesRelationships.php | 7 +++---- src/Query/Builder.php | 2 +- tests/PHPStan/SarifErrorFormatter.php | 6 +++--- 5 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 rector.php diff --git a/rector.php b/rector.php new file mode 100644 index 000000000..c07b67f63 --- /dev/null +++ b/rector.php @@ -0,0 +1,26 @@ +withPaths([ + __FILE__, + __DIR__ . '/docs', + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + // uncomment to reach your current PHP version + ->withPhpSets(php82: true) + ->withTypeCoverageLevel(0) + ->withSkip([ + RemoveExtraParametersRector::class, + ClosureToArrowFunctionRector::class, + NullToStrictStringFuncCallArgRector::class, + MixedTypeRector::class, + AddClosureVoidReturnTypeWhereNoReturnRector::class, + ]); diff --git a/src/Bus/MongoBatchRepository.php b/src/Bus/MongoBatchRepository.php index dd0713f97..e0690149c 100644 --- a/src/Bus/MongoBatchRepository.php +++ b/src/Bus/MongoBatchRepository.php @@ -28,7 +28,7 @@ // are called by PruneBatchesCommand class MongoBatchRepository extends DatabaseBatchRepository implements PrunableBatchRepository { - private Collection $collection; + private readonly Collection $collection; public function __construct( BatchFactory $factory, diff --git a/src/Helpers/QueriesRelationships.php b/src/Helpers/QueriesRelationships.php index 933b6ec32..1f1ffa34b 100644 --- a/src/Helpers/QueriesRelationships.php +++ b/src/Helpers/QueriesRelationships.php @@ -21,12 +21,11 @@ use function array_map; use function class_basename; use function collect; -use function get_class; use function in_array; use function is_array; use function is_string; use function method_exists; -use function strpos; +use function str_contains; trait QueriesRelationships { @@ -45,7 +44,7 @@ trait QueriesRelationships public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', ?Closure $callback = null) { if (is_string($relation)) { - if (strpos($relation, '.') !== false) { + if (str_contains($relation, '.')) { return $this->hasNested($relation, $operator, $count, $boolean, $callback); } @@ -139,7 +138,7 @@ private function handleMorphToMany($hasQuery, $relation) { // First we select the parent models that have a relation to our related model, // Then extracts related model's ids from the pivot column - $hasQuery->where($relation->getTable() . '.' . $relation->getMorphType(), get_class($relation->getParent())); + $hasQuery->where($relation->getTable() . '.' . $relation->getMorphType(), $relation->getParent()::class); $relations = $hasQuery->pluck($relation->getTable()); $relations = $relation->extractIds($relations->flatten(1)->toArray(), $relation->getForeignPivotKeyName()); diff --git a/src/Query/Builder.php b/src/Query/Builder.php index e2f8867b3..5deea32e6 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -1079,7 +1079,7 @@ protected function performUpdate(array $update, array $options = []) $wheres = $this->aliasIdForQuery($wheres); $result = $this->collection->updateMany($wheres, $update, $options); if ($result->isAcknowledged()) { - return $result->getModifiedCount() ? $result->getModifiedCount() : $result->getUpsertedCount(); + return $result->getModifiedCount() ?: $result->getUpsertedCount(); } return 0; diff --git a/tests/PHPStan/SarifErrorFormatter.php b/tests/PHPStan/SarifErrorFormatter.php index 1fb814cde..92c0255cc 100644 --- a/tests/PHPStan/SarifErrorFormatter.php +++ b/tests/PHPStan/SarifErrorFormatter.php @@ -26,9 +26,9 @@ class SarifErrorFormatter implements ErrorFormatter private const URI_BASE_ID = 'WORKINGDIR'; public function __construct( - private RelativePathHelper $relativePathHelper, - private string $currentWorkingDirectory, - private bool $pretty, + private readonly RelativePathHelper $relativePathHelper, + private readonly string $currentWorkingDirectory, + private readonly bool $pretty, ) { } From 09403e584e4dcdcbfab8cc56cae6f8d85db63dde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Fri, 6 Sep 2024 20:38:13 +0200 Subject: [PATCH 3/3] Run rector with composer --- composer.json | 3 ++- rector.php | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 8c715bcd3..6d02c9b96 100644 --- a/composer.json +++ b/composer.json @@ -74,7 +74,8 @@ "test": "phpunit", "test:coverage": "phpunit --coverage-clover ./coverage.xml", "cs": "phpcs", - "cs:fix": "phpcbf" + "cs:fix": "phpcbf", + "rector": "rector" }, "config": { "allow-plugins": { diff --git a/rector.php b/rector.php index c07b67f63..23afcb2ea 100644 --- a/rector.php +++ b/rector.php @@ -14,8 +14,7 @@ __DIR__ . '/src', __DIR__ . '/tests', ]) - // uncomment to reach your current PHP version - ->withPhpSets(php82: true) + ->withPhpSets() ->withTypeCoverageLevel(0) ->withSkip([ RemoveExtraParametersRector::class,