From 9ac70cc26d94e92ab0fe22de13311d40e6843f8f Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Thu, 2 Nov 2023 18:59:11 +0100 Subject: [PATCH 01/43] add PHPStan package (#1930) --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9f605c667..b04425751 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,8 @@ "orchestra/testbench": "^8.0", "mockery/mockery": "^1.4.4", "doctrine/coding-standard": "12.0.x-dev", - "spatie/laravel-query-builder": "^5.6" + "spatie/laravel-query-builder": "^5.6", + "phpstan/phpstan": "^1.10" }, "replace": { "jenssegers/mongodb": "self.version" From 9bc72451204a04e1f1f24b87098591df938e3d0c Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Thu, 2 Nov 2023 18:59:50 +0100 Subject: [PATCH 02/43] #1930 add PHPStan config --- .gitignore | 2 ++ phpstan.neon.dist | 9 +++++++++ 2 files changed, 11 insertions(+) create mode 100644 phpstan.neon.dist diff --git a/.gitignore b/.gitignore index d69c89d6f..f29120648 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ composer.lock composer.phar phpunit.xml +phpstan.neon +/.cache/phpstan/ diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 000000000..efd97ed7a --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,9 @@ +parameters: + tmpDir: .cache/phpstan + + paths: + - src + + level: 1 + + editorUrl: 'phpstorm://open?file=%%file%%&line=%%line%%' From 2f182cf9adbb07e6c4d038f068ffde2f76d54f8c Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Thu, 2 Nov 2023 19:09:32 +0100 Subject: [PATCH 03/43] #1930 add PHPStan to the coding-standards Github Workflow --- .editorconfig | 5 ++++- .github/workflows/coding-standards.yml | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index fcdf61edc..80ce1de38 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,4 +6,7 @@ end_of_line = lf insert_final_newline = true indent_style = space indent_size = 4 -trim_trailing_whitespace = true \ No newline at end of file +trim_trailing_whitespace = true + +[*.yml] +indent_size = 2 diff --git a/.github/workflows/coding-standards.yml b/.github/workflows/coding-standards.yml index e75ca3c53..94272a298 100644 --- a/.github/workflows/coding-standards.yml +++ b/.github/workflows/coding-standards.yml @@ -53,3 +53,28 @@ jobs: # The -q option is required until phpcs v4 is released - name: "Run PHP_CodeSniffer" run: "vendor/bin/phpcs -q --no-colors --report=checkstyle | cs2pr" + + analysis: + runs-on: "ubuntu-22.04" + strategy: + matrix: + php: + - '8.1' + - '8.2' + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: curl, mbstring + tools: composer:v2 + coverage: none + + - name: Install dependencies + run: composer install + + - name: Run PHPStan + run: ./vendor/bin/phpstan analyse --no-interaction --no-progress --ansi From 314cd14dd4f10f2388f999d839700887794b9187 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Thu, 2 Nov 2023 19:10:16 +0100 Subject: [PATCH 04/43] #1930 remove redundant config settings --- .github/workflows/build-ci.yml | 2 -- .github/workflows/coding-standards.yml | 2 -- 2 files changed, 4 deletions(-) diff --git a/.github/workflows/build-ci.yml b/.github/workflows/build-ci.yml index 97b1e8a32..646b58362 100644 --- a/.github/workflows/build-ci.yml +++ b/.github/workflows/build-ci.yml @@ -2,8 +2,6 @@ name: CI on: push: - branches: - tags: pull_request: jobs: diff --git a/.github/workflows/coding-standards.yml b/.github/workflows/coding-standards.yml index 94272a298..c53b43db2 100644 --- a/.github/workflows/coding-standards.yml +++ b/.github/workflows/coding-standards.yml @@ -2,8 +2,6 @@ name: "Coding Standards" on: push: - branches: - tags: pull_request: env: From e985e8e6e4fa12966d10250937aa5f42a87d3ccf Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Thu, 2 Nov 2023 19:24:04 +0100 Subject: [PATCH 05/43] #1930 continue job on error --- .github/workflows/coding-standards.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/coding-standards.yml b/.github/workflows/coding-standards.yml index c53b43db2..f7ad2a101 100644 --- a/.github/workflows/coding-standards.yml +++ b/.github/workflows/coding-standards.yml @@ -54,6 +54,7 @@ jobs: analysis: runs-on: "ubuntu-22.04" + continue-on-error: true strategy: matrix: php: From 1aad84a99975da068b6f94d48798bbd860529a6e Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Thu, 2 Nov 2023 23:25:16 +0100 Subject: [PATCH 06/43] #1930 add required return statements --- src/Relations/BelongsToMany.php | 1 + src/Schema/Blueprint.php | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/Relations/BelongsToMany.php b/src/Relations/BelongsToMany.php index a1b028c9f..7eed228fc 100644 --- a/src/Relations/BelongsToMany.php +++ b/src/Relations/BelongsToMany.php @@ -171,6 +171,7 @@ public function sync($ids, $detaching = true) public function updateExistingPivot($id, array $attributes, $touch = true) { // Do nothing, we have no pivot table. + return $this; } /** @inheritdoc */ diff --git a/src/Schema/Blueprint.php b/src/Schema/Blueprint.php index 6dd28d3b2..a15d9502c 100644 --- a/src/Schema/Blueprint.php +++ b/src/Schema/Blueprint.php @@ -275,6 +275,8 @@ public function create($options = []) public function drop() { $this->collection->drop(); + + return $this; } /** @inheritdoc */ From 4e7d4e000b277fde69ce8f3dac91973a9c54616b Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Thu, 2 Nov 2023 23:27:33 +0100 Subject: [PATCH 07/43] #1930 rename arguments, keep in line with the parent name --- src/Relations/BelongsToMany.php | 4 ++-- src/Relations/EmbedsMany.php | 8 ++++---- src/Schema/Blueprint.php | 10 +++++----- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Relations/BelongsToMany.php b/src/Relations/BelongsToMany.php index 7eed228fc..9e496bed7 100644 --- a/src/Relations/BelongsToMany.php +++ b/src/Relations/BelongsToMany.php @@ -82,11 +82,11 @@ protected function setWhere() } /** @inheritdoc */ - public function save(Model $model, array $joining = [], $touch = true) + public function save(Model $model, array $pivotAttributes = [], $touch = true) { $model->save(['touch' => false]); - $this->attach($model, $joining, $touch); + $this->attach($model, $pivotAttributes, $touch); return $model; } diff --git a/src/Relations/EmbedsMany.php b/src/Relations/EmbedsMany.php index b97849f24..a72ae2f0c 100644 --- a/src/Relations/EmbedsMany.php +++ b/src/Relations/EmbedsMany.php @@ -322,13 +322,13 @@ protected function getEmbedded() } /** @inheritdoc */ - protected function setEmbedded($models) + protected function setEmbedded($records) { - if (! is_array($models)) { - $models = [$models]; + if (! is_array($records)) { + $records = [$records]; } - return parent::setEmbedded(array_values($models)); + return parent::setEmbedded(array_values($records)); } /** @inheritdoc */ diff --git a/src/Schema/Blueprint.php b/src/Schema/Blueprint.php index a15d9502c..cc6d2d850 100644 --- a/src/Schema/Blueprint.php +++ b/src/Schema/Blueprint.php @@ -82,11 +82,11 @@ public function primary($columns = null, $name = null, $algorithm = null, $optio } /** @inheritdoc */ - public function dropIndex($indexOrColumns = null) + public function dropIndex($index = null) { - $indexOrColumns = $this->transformColumns($indexOrColumns); + $index = $this->transformColumns($index); - $this->collection->dropIndex($indexOrColumns); + $this->collection->dropIndex($index); return $this; } @@ -341,11 +341,11 @@ protected function fluent($columns = null) * Allows the use of unsupported schema methods. * * @param string $method - * @param array $args + * @param array $parameters * * @return Blueprint */ - public function __call($method, $args) + public function __call($method, $parameters) { // Dummy. return $this; From 0168dcee240337c21ae6a16c21572231b9cdfb72 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Sat, 4 Nov 2023 01:31:22 +0100 Subject: [PATCH 08/43] #1930 add missing parent calls --- src/Schema/Blueprint.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Schema/Blueprint.php b/src/Schema/Blueprint.php index cc6d2d850..52a5762f5 100644 --- a/src/Schema/Blueprint.php +++ b/src/Schema/Blueprint.php @@ -44,6 +44,8 @@ class Blueprint extends SchemaBlueprint */ public function __construct(Connection $connection, string $collection) { + parent::__construct($collection); + $this->connection = $connection; $this->collection = $this->connection->getCollection($collection); From 7349d4d4a44f72055ec49802c6ed00ccde055797 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Sat, 4 Nov 2023 01:39:45 +0100 Subject: [PATCH 09/43] #1930 add parent call and simplify setting the properties --- src/Relations/EmbedsOneOrMany.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Relations/EmbedsOneOrMany.php b/src/Relations/EmbedsOneOrMany.php index 46f4f1e72..1804dec48 100644 --- a/src/Relations/EmbedsOneOrMany.php +++ b/src/Relations/EmbedsOneOrMany.php @@ -42,8 +42,8 @@ abstract class EmbedsOneOrMany extends Relation */ public function __construct(Builder $query, Model $parent, Model $related, string $localKey, string $foreignKey, string $relation) { - $this->query = $query; - $this->parent = $parent; + parent::__construct($query, $parent); + $this->related = $related; $this->localKey = $localKey; $this->foreignKey = $foreignKey; @@ -54,8 +54,6 @@ public function __construct(Builder $query, Model $parent, Model $related, strin if ($parentRelation) { $this->query = $parentRelation->getQuery(); } - - $this->addConstraints(); } /** @inheritdoc */ From 6b7d0474c1877386d798d8c507cea99a5be3a108 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Thu, 2 Nov 2023 23:28:43 +0100 Subject: [PATCH 10/43] #1930 update return type --- src/Relations/BelongsToMany.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Relations/BelongsToMany.php b/src/Relations/BelongsToMany.php index 9e496bed7..5b3cc2a8b 100644 --- a/src/Relations/BelongsToMany.php +++ b/src/Relations/BelongsToMany.php @@ -267,7 +267,7 @@ public function newPivotQuery() /** * Create a new query builder for the related model. * - * @return \Illuminate\Database\Query\Builder + * @return Builder|Model */ public function newRelatedQuery() { From b19813aa3dd590aba1a3144bde59285043205cd8 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Fri, 3 Nov 2023 00:03:07 +0100 Subject: [PATCH 11/43] #1930 update parameter type --- src/Relations/EmbedsMany.php | 15 +++++++++++---- src/Relations/EmbedsOne.php | 14 ++++++++++++-- src/Relations/EmbedsOneOrMany.php | 18 ++++++++++++++---- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/Relations/EmbedsMany.php b/src/Relations/EmbedsMany.php index a72ae2f0c..9d2996acf 100644 --- a/src/Relations/EmbedsMany.php +++ b/src/Relations/EmbedsMany.php @@ -9,6 +9,8 @@ use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\Paginator; use MongoDB\BSON\ObjectID; +use MongoDB\Driver\Exception\LogicException; +use MongoDB\Laravel\Eloquent\Model as MongoDBModel; use function array_key_exists; use function array_values; @@ -195,10 +197,15 @@ public function destroy($ids = []) /** * Delete all embedded models. * - * @return int + * @param null $id + * + * @throws LogicException|\Throwable + * + * @note The $id is not used to delete embedded models. */ - public function delete() + public function delete($id = null): int { + throw_if($id !== null, new LogicException('The id parameter should not be used.')); // Overwrite the local key with an empty array. $result = $this->query->update([$this->localKey => []]); @@ -224,9 +231,9 @@ public function detach($ids = []) /** * Save alias. * - * @return Model + * @return MongoDBModel */ - public function attach(Model $model) + public function attach(MongoDBModel $model) { return $this->save($model); } diff --git a/src/Relations/EmbedsOne.php b/src/Relations/EmbedsOne.php index 196415a55..233f6cdba 100644 --- a/src/Relations/EmbedsOne.php +++ b/src/Relations/EmbedsOne.php @@ -6,6 +6,10 @@ use Illuminate\Database\Eloquent\Model; use MongoDB\BSON\ObjectID; +use MongoDB\Driver\Exception\LogicException; +use Throwable; + +use function throw_if; class EmbedsOne extends EmbedsOneOrMany { @@ -133,10 +137,16 @@ public function dissociate() /** * Delete all embedded models. * - * @return int + * @param ?string $id + * + * @throws LogicException|Throwable + * + * @note The $id is not used to delete embedded models. */ - public function delete() + public function delete($id = null): int { + throw_if($id !== null, new LogicException('The id parameter should not be used.')); + return $this->performDelete(); } diff --git a/src/Relations/EmbedsOneOrMany.php b/src/Relations/EmbedsOneOrMany.php index 1804dec48..d810bcfa5 100644 --- a/src/Relations/EmbedsOneOrMany.php +++ b/src/Relations/EmbedsOneOrMany.php @@ -8,11 +8,15 @@ use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model as EloquentModel; use Illuminate\Database\Eloquent\Relations\Relation; +use Illuminate\Database\Query\Expression; +use MongoDB\Driver\Exception\LogicException; use MongoDB\Laravel\Eloquent\Model; +use Throwable; use function array_merge; use function count; use function is_array; +use function throw_if; abstract class EmbedsOneOrMany extends Relation { @@ -99,10 +103,16 @@ public function get($columns = ['*']) /** * Get the number of embedded models. * - * @return int + * @param Expression|string $columns + * + * @throws LogicException|Throwable + * + * @note The $column parameter is not used to count embedded models. */ - public function count() + public function count($columns = '*'): int { + throw_if($columns !== '*', new LogicException('The columns parameter should not be used.')); + return count($this->getEmbedded()); } @@ -392,8 +402,8 @@ public function getQualifiedForeignKeyName() /** * Get the name of the "where in" method for eager loading. * - * @param \Illuminate\Database\Eloquent\Model $model - * @param string $key + * @param EloquentModel $model + * @param string $key * * @return string */ From f2ea91fe045bc095fe1525d939c235622d59711b Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Fri, 3 Nov 2023 00:04:58 +0100 Subject: [PATCH 12/43] #1930 remove default config option values --- phpunit.xml.dist | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 7a38678eb..64aa1694a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,13 +1,9 @@ @@ -23,7 +19,7 @@ - ./src + ./src From d80979d60e3d0ffe58c98910953f536c2bc84957 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Fri, 3 Nov 2023 00:25:52 +0100 Subject: [PATCH 13/43] #1930 ignore two errors --- phpstan.neon.dist | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index efd97ed7a..45c55350a 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -7,3 +7,7 @@ parameters: level: 1 editorUrl: 'phpstorm://open?file=%%file%%&line=%%line%%' + + ignoreErrors: + - '#Unsafe usage of new static#' + - '#Call to an undefined method [a-zA-Z0-9\\_\<\>]+::[a-zA-Z]+\(\)#' From 7c4340a7963b8b16f51ccfad54267cdd7bdaf1e5 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Fri, 3 Nov 2023 21:30:57 +0100 Subject: [PATCH 14/43] #1930 add baseline for level 2 --- phpstan-baseline.neon | 46 +++++++++++++++++++++++++++++++++++++++++++ phpstan.neon.dist | 5 ++++- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 phpstan-baseline.neon diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 000000000..193615179 --- /dev/null +++ b/phpstan-baseline.neon @@ -0,0 +1,46 @@ +parameters: + ignoreErrors: + - + message: "#^Method MongoDB\\\\Laravel\\\\Connection\\:\\:transaction\\(\\) has invalid return type Illuminate\\\\Database\\\\Concerns\\\\TCallbackReturnType\\.$#" + count: 1 + path: src/Connection.php + + - + message: "#^Parameter \\$callback of method MongoDB\\\\Laravel\\\\Connection\\:\\:transaction\\(\\) has invalid type Illuminate\\\\Database\\\\Concerns\\\\TCallbackReturnType\\.$#" + count: 1 + path: src/Connection.php + + - + message: "#^Method Illuminate\\\\Database\\\\Query\\\\Builder\\:\\:update\\(\\) invoked with 2 parameters, 1 required\\.$#" + count: 1 + path: src/Eloquent/Builder.php + + - + message: "#^Parameter \\$offset of method MongoDB\\\\Laravel\\\\Eloquent\\\\Model\\:\\:offsetSet\\(\\) has invalid type Illuminate\\\\Database\\\\Eloquent\\\\TKey\\.$#" + count: 1 + path: src/Eloquent/Model.php + + - + message: "#^Parameter \\$offset of method MongoDB\\\\Laravel\\\\Eloquent\\\\Model\\:\\:offsetUnset\\(\\) has invalid type Illuminate\\\\Database\\\\Eloquent\\\\TKey\\.$#" + count: 1 + path: src/Eloquent/Model.php + + - + message: "#^Parameter \\$value of method MongoDB\\\\Laravel\\\\Eloquent\\\\Model\\:\\:offsetSet\\(\\) has invalid type Illuminate\\\\Database\\\\Eloquent\\\\TValue\\.$#" + count: 1 + path: src/Eloquent/Model.php + + - + message: "#^Cannot call method modelKeys\\(\\) on array\\|Illuminate\\\\Database\\\\Eloquent\\\\Model\\|Illuminate\\\\Support\\\\Collection\\.$#" + count: 1 + path: src/Relations/BelongsToMany.php + + - + message: "#^Method Illuminate\\\\Database\\\\Eloquent\\\\Model\\:\\:push\\(\\) invoked with 3 parameters, 0 required\\.$#" + count: 2 + path: src/Relations/BelongsToMany.php + + - + message: "#^Method Illuminate\\\\Database\\\\Schema\\\\Blueprint\\:\\:create\\(\\) invoked with 1 parameter, 0 required\\.$#" + count: 1 + path: src/Schema/Builder.php diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 45c55350a..518fe9ab8 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,10 +1,13 @@ +includes: + - ./phpstan-baseline.neon + parameters: tmpDir: .cache/phpstan paths: - src - level: 1 + level: 2 editorUrl: 'phpstorm://open?file=%%file%%&line=%%line%%' From e3e5d2e200473d8d129ef0f11d01989a3f8b5888 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Fri, 3 Nov 2023 21:42:20 +0100 Subject: [PATCH 15/43] #1930 add PHPStan to the coding-standards Github Workflow --- .github/workflows/coding-standards.yml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/.github/workflows/coding-standards.yml b/.github/workflows/coding-standards.yml index f7ad2a101..83857d5cc 100644 --- a/.github/workflows/coding-standards.yml +++ b/.github/workflows/coding-standards.yml @@ -72,8 +72,32 @@ jobs: tools: composer:v2 coverage: none + - name: Cache dependencies + id: composer-cache + uses: actions/cache@v3 + with: + path: ./vendor + key: composer-${{ hashFiles('**/composer.lock') }} + - name: Install dependencies run: composer install + - name: Restore cache PHPStan results + id: phpstan-cache-restore + uses: actions/cache/restore@v3 + with: + path: .cache + key: "phpstan-result-cache-${{ github.run_id }}" + restore-keys: | + phpstan-result-cache- + - name: Run PHPStan run: ./vendor/bin/phpstan analyse --no-interaction --no-progress --ansi + + - name: Save cache PHPStan results + id: phpstan-cache-save + if: always() + uses: actions/cache/save@v3 + with: + path: .cache + key: ${{ steps.phpstan-cache-restore.outputs.cache-primary-key }} From 8b2b05e1e38cd7ca5934951abad347ba9767bbdb Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Fri, 3 Nov 2023 01:23:05 +0100 Subject: [PATCH 16/43] #1930 add formatting to the coding-standards Github Workflow --- .github/workflows/coding-standards.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.github/workflows/coding-standards.yml b/.github/workflows/coding-standards.yml index 83857d5cc..bb693c1b2 100644 --- a/.github/workflows/coding-standards.yml +++ b/.github/workflows/coding-standards.yml @@ -13,6 +13,11 @@ jobs: name: "phpcs" runs-on: "ubuntu-22.04" + permissions: + # Give the default GITHUB_TOKEN write permission to commit and push the + # added or changed files to the repository. + contents: write + steps: - name: "Checkout" uses: "actions/checkout@v4" @@ -52,6 +57,14 @@ jobs: - name: "Run PHP_CodeSniffer" run: "vendor/bin/phpcs -q --no-colors --report=checkstyle | cs2pr" + - name: "Format the code" + run: ./vendor/bin/phpcbf + + - name: "Commit the changes" + uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: "apply phpcbf formatting" + analysis: runs-on: "ubuntu-22.04" continue-on-error: true From 829c4107c294b168803bf7e5936da4188138712e Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Fri, 3 Nov 2023 12:17:33 +0100 Subject: [PATCH 17/43] #1930 rename argument names, in line with parent method Also remove second argument, only one argument needed --- src/Schema/Builder.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index af311df6c..bfa0e4715 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -44,9 +44,9 @@ public function hasCollection($name) } /** @inheritdoc */ - public function hasTable($collection) + public function hasTable($table) { - return $this->hasCollection($collection); + return $this->hasCollection($table); } /** @@ -66,15 +66,15 @@ public function collection($collection, Closure $callback) } /** @inheritdoc */ - public function table($collection, Closure $callback) + public function table($table, Closure $callback) { - $this->collection($collection, $callback); + $this->collection($table, $callback); } /** @inheritdoc */ - public function create($collection, ?Closure $callback = null, array $options = []) + public function create($table, ?Closure $callback = null, array $options = []) { - $blueprint = $this->createBlueprint($collection); + $blueprint = $this->createBlueprint($table); $blueprint->create($options); @@ -84,17 +84,17 @@ public function create($collection, ?Closure $callback = null, array $options = } /** @inheritdoc */ - public function dropIfExists($collection) + public function dropIfExists($table) { - if ($this->hasCollection($collection)) { - $this->drop($collection); + if ($this->hasCollection($table)) { + $this->drop($table); } } /** @inheritdoc */ - public function drop($collection) + public function drop($table) { - $blueprint = $this->createBlueprint($collection); + $blueprint = $this->createBlueprint($table); $blueprint->drop(); } @@ -108,9 +108,9 @@ public function dropAllTables() } /** @inheritdoc */ - protected function createBlueprint($collection, ?Closure $callback = null) + protected function createBlueprint($table, ?Closure $callback = null) { - return new Blueprint($this->connection, $collection); + return new Blueprint($this->connection, $table); } /** From 28436c1b1d4d32476ecbe0efffc1e16672fac920 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Fri, 3 Nov 2023 23:50:24 +0100 Subject: [PATCH 18/43] #1930 convert to short closure --- src/Eloquent/Builder.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php index 948182ad3..89f9fcf1a 100644 --- a/src/Eloquent/Builder.php +++ b/src/Eloquent/Builder.php @@ -219,16 +219,15 @@ protected function ensureOrderForCursorPagination($shouldReverse = false) } if ($shouldReverse) { - $this->query->orders = collect($this->query->orders)->map(function ($direction) { - return $direction === 1 ? -1 : 1; - })->toArray(); + $this->query->orders = collect($this->query->orders) + ->map(fn (int $direction) => $direction === 1 ? -1 : 1) + ->toArray(); } - return collect($this->query->orders)->map(function ($direction, $column) { - return [ + return collect($this->query->orders) + ->map(fn ($direction, $column) => [ 'column' => $column, 'direction' => $direction === 1 ? 'asc' : 'desc', - ]; - })->values(); + ])->values(); } } From 5885486ab70686a5d6b61355cd22461e47c61483 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Fri, 3 Nov 2023 15:49:35 +0100 Subject: [PATCH 19/43] #1930 updated the baseline --- phpstan-baseline.neon | 31 +++---------------------------- 1 file changed, 3 insertions(+), 28 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 193615179..7ceb63a8a 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,34 +1,9 @@ parameters: ignoreErrors: - - message: "#^Method MongoDB\\\\Laravel\\\\Connection\\:\\:transaction\\(\\) has invalid return type Illuminate\\\\Database\\\\Concerns\\\\TCallbackReturnType\\.$#" + message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Builder\\|Illuminate\\\\Database\\\\Eloquent\\\\Model\\:\\:pull\\(\\)\\.$#" count: 1 - path: src/Connection.php - - - - message: "#^Parameter \\$callback of method MongoDB\\\\Laravel\\\\Connection\\:\\:transaction\\(\\) has invalid type Illuminate\\\\Database\\\\Concerns\\\\TCallbackReturnType\\.$#" - count: 1 - path: src/Connection.php - - - - message: "#^Method Illuminate\\\\Database\\\\Query\\\\Builder\\:\\:update\\(\\) invoked with 2 parameters, 1 required\\.$#" - count: 1 - path: src/Eloquent/Builder.php - - - - message: "#^Parameter \\$offset of method MongoDB\\\\Laravel\\\\Eloquent\\\\Model\\:\\:offsetSet\\(\\) has invalid type Illuminate\\\\Database\\\\Eloquent\\\\TKey\\.$#" - count: 1 - path: src/Eloquent/Model.php - - - - message: "#^Parameter \\$offset of method MongoDB\\\\Laravel\\\\Eloquent\\\\Model\\:\\:offsetUnset\\(\\) has invalid type Illuminate\\\\Database\\\\Eloquent\\\\TKey\\.$#" - count: 1 - path: src/Eloquent/Model.php - - - - message: "#^Parameter \\$value of method MongoDB\\\\Laravel\\\\Eloquent\\\\Model\\:\\:offsetSet\\(\\) has invalid type Illuminate\\\\Database\\\\Eloquent\\\\TValue\\.$#" - count: 1 - path: src/Eloquent/Model.php + path: src/Relations/BelongsToMany.php - message: "#^Cannot call method modelKeys\\(\\) on array\\|Illuminate\\\\Database\\\\Eloquent\\\\Model\\|Illuminate\\\\Support\\\\Collection\\.$#" @@ -37,7 +12,7 @@ parameters: - message: "#^Method Illuminate\\\\Database\\\\Eloquent\\\\Model\\:\\:push\\(\\) invoked with 3 parameters, 0 required\\.$#" - count: 2 + count: 3 path: src/Relations/BelongsToMany.php - From 51b0bb9ee230684083b1862590fd40d554ae9b95 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Fri, 3 Nov 2023 23:46:28 +0100 Subject: [PATCH 20/43] #1930 assert that a instance of the Mongo query builder is used The base `update(..)` method and the MongoDB `update(..)` differ in the amount of arguments. Adding a assertion to make sure the correct one is used. --- src/Eloquent/Builder.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php index 89f9fcf1a..90beb323f 100644 --- a/src/Eloquent/Builder.php +++ b/src/Eloquent/Builder.php @@ -8,10 +8,12 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use MongoDB\Driver\Cursor; use MongoDB\Laravel\Helpers\QueriesRelationships; +use MongoDB\Laravel\Query\Builder as MongoDBQueryBuilder; use MongoDB\Model\BSONDocument; use function array_key_exists; use function array_merge; +use function assert; use function collect; use function is_array; use function iterator_to_array; @@ -62,7 +64,10 @@ public function update(array $values, array $options = []) return 1; } - return $this->toBase()->update($this->addUpdatedAtColumn($values), $options); + $builder = $this->toBase(); + assert($builder instanceof MongoDBQueryBuilder); + + return $builder->update($this->addUpdatedAtColumn($values), $options); } /** @inheritdoc */ From 29101df8a1ba3a7157db43637131a557e97646a8 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Sat, 4 Nov 2023 01:54:10 +0100 Subject: [PATCH 21/43] #1930 update phpunit config using `phpunit --generate-configuration` --- .github/workflows/coding-standards.yml | 8 +++++--- .gitignore | 4 +--- phpcs.xml.dist | 2 +- phpunit.xml.dist | 15 ++++++++++----- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/.github/workflows/coding-standards.yml b/.github/workflows/coding-standards.yml index bb693c1b2..8ccf13f58 100644 --- a/.github/workflows/coding-standards.yml +++ b/.github/workflows/coding-standards.yml @@ -53,13 +53,15 @@ jobs: with: composer-options: "--no-suggest" + - name: "Format the code" + run: | + mkdir .cache + ./vendor/bin/phpcbf + # The -q option is required until phpcs v4 is released - name: "Run PHP_CodeSniffer" run: "vendor/bin/phpcs -q --no-colors --report=checkstyle | cs2pr" - - name: "Format the code" - run: ./vendor/bin/phpcbf - - name: "Commit the changes" uses: stefanzweifel/git-auto-commit-action@v5 with: diff --git a/.gitignore b/.gitignore index f29120648..80f343333 100644 --- a/.gitignore +++ b/.gitignore @@ -3,11 +3,9 @@ *.sublime-workspace .DS_Store .idea/ -.phpunit.cache/ -.phpcs-cache /vendor composer.lock composer.phar phpunit.xml phpstan.neon -/.cache/phpstan/ +/.cache/ diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 23bc44ab7..5f402d4ce 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -3,7 +3,7 @@ - + diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 64aa1694a..55c198ba9 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -2,10 +2,12 @@ - + cacheDirectory=".cache/phpunit" + executionOrder="depends,defects" + beStrictAboutCoverageMetadata="true" + beStrictAboutOutputDuringTests="true" + failOnRisky="true" + failOnWarning="true"> tests/ @@ -17,7 +19,10 @@ - + + ./src From aa05e9cbf5c60b6d79339e27987e6a19318a6d72 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Wed, 15 Nov 2023 19:54:02 +0100 Subject: [PATCH 22/43] #1930 increase the php memory limit --- phpunit.xml.dist | 2 ++ 1 file changed, 2 insertions(+) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 55c198ba9..8e5e9d3d6 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -18,6 +18,8 @@ + + Date: Wed, 15 Nov 2023 22:15:18 +0100 Subject: [PATCH 23/43] #1930 formatting/readability GH Action --- .github/workflows/build-ci.yml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build-ci.yml b/.github/workflows/build-ci.yml index 646b58362..99482610a 100644 --- a/.github/workflows/build-ci.yml +++ b/.github/workflows/build-ci.yml @@ -53,7 +53,7 @@ jobs: - name: Show Docker version run: if [[ "$DEBUG" == "true" ]]; then docker version && env; fi env: - DEBUG: ${{secrets.DEBUG}} + DEBUG: ${{ secrets.DEBUG }} - name: Download Composer cache dependencies from cache id: composer-cache run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT @@ -64,11 +64,9 @@ jobs: key: ${{ matrix.os }}-composer-${{ hashFiles('**/composer.json') }} restore-keys: ${{ matrix.os }}-composer- - name: Install dependencies - run: | - composer update --no-interaction $([[ "${{ matrix.mode }}" == low-deps ]] && echo ' --prefer-lowest --prefer-stable') + run: composer update --no-interaction $([[ "${{ matrix.mode }}" == low-deps ]] && echo ' --prefer-lowest --prefer-stable') - name: Run tests - run: | - ./vendor/bin/phpunit --coverage-clover coverage.xml + run: ./vendor/bin/phpunit --coverage-clover coverage.xml env: MONGODB_URI: 'mongodb://127.0.0.1/?replicaSet=rs' - uses: codecov/codecov-action@v3 From ef81fd794dbeed929a80619a2f3380d7318e7bdc Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Wed, 15 Nov 2023 22:15:34 +0100 Subject: [PATCH 24/43] #1930 remove default value --- .github/workflows/build-ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build-ci.yml b/.github/workflows/build-ci.yml index 99482610a..306d20fc8 100644 --- a/.github/workflows/build-ci.yml +++ b/.github/workflows/build-ci.yml @@ -72,4 +72,3 @@ jobs: - uses: codecov/codecov-action@v3 with: token: ${{ secrets.CODECOV_TOKEN }} - fail_ci_if_error: false From d6e8a8561bcffb68ad4ebed31bbe5f1838c1e7ea Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Thu, 16 Nov 2023 10:53:45 +0100 Subject: [PATCH 25/43] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jérôme Tamarelle --- src/Eloquent/Builder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php index 90beb323f..dba92b632 100644 --- a/src/Eloquent/Builder.php +++ b/src/Eloquent/Builder.php @@ -225,12 +225,12 @@ protected function ensureOrderForCursorPagination($shouldReverse = false) if ($shouldReverse) { $this->query->orders = collect($this->query->orders) - ->map(fn (int $direction) => $direction === 1 ? -1 : 1) + ->map(static fn (int $direction) => $direction === 1 ? -1 : 1) ->toArray(); } return collect($this->query->orders) - ->map(fn ($direction, $column) => [ + ->map(static fn ($direction, $column) => [ 'column' => $column, 'direction' => $direction === 1 ? 'asc' : 'desc', ])->values(); From e06145285bad6ec67274bbcba62cc042583e1359 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Thu, 16 Nov 2023 12:09:39 +0100 Subject: [PATCH 26/43] continue workflow if `phpcbf` finds/fixes issues --- .github/workflows/coding-standards.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/coding-standards.yml b/.github/workflows/coding-standards.yml index 8ccf13f58..c6f730d33 100644 --- a/.github/workflows/coding-standards.yml +++ b/.github/workflows/coding-standards.yml @@ -54,6 +54,7 @@ jobs: composer-options: "--no-suggest" - name: "Format the code" + continue-on-error: true run: | mkdir .cache ./vendor/bin/phpcbf From b221fd2b52d86f24afd17e802f5cb2ce0fea8f78 Mon Sep 17 00:00:00 2001 From: Treggats Date: Thu, 16 Nov 2023 11:11:30 +0000 Subject: [PATCH 27/43] apply phpcbf formatting --- src/Relations/EmbedsMany.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Relations/EmbedsMany.php b/src/Relations/EmbedsMany.php index 9d2996acf..0bdad8e4c 100644 --- a/src/Relations/EmbedsMany.php +++ b/src/Relations/EmbedsMany.php @@ -11,6 +11,7 @@ use MongoDB\BSON\ObjectID; use MongoDB\Driver\Exception\LogicException; use MongoDB\Laravel\Eloquent\Model as MongoDBModel; +use Throwable; use function array_key_exists; use function array_values; @@ -18,6 +19,7 @@ use function in_array; use function is_array; use function method_exists; +use function throw_if; class EmbedsMany extends EmbedsOneOrMany { @@ -199,7 +201,7 @@ public function destroy($ids = []) * * @param null $id * - * @throws LogicException|\Throwable + * @throws LogicException|Throwable * * @note The $id is not used to delete embedded models. */ From 9148217cba0b160d59fc77e63147856092680779 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Thu, 16 Nov 2023 12:28:25 +0100 Subject: [PATCH 28/43] cs --- src/Relations/EmbedsMany.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Relations/EmbedsMany.php b/src/Relations/EmbedsMany.php index 0bdad8e4c..1d84dcf35 100644 --- a/src/Relations/EmbedsMany.php +++ b/src/Relations/EmbedsMany.php @@ -208,6 +208,7 @@ public function destroy($ids = []) public function delete($id = null): int { throw_if($id !== null, new LogicException('The id parameter should not be used.')); + // Overwrite the local key with an empty array. $result = $this->query->update([$this->localKey => []]); From 306476c016a8faeb2fff235bb5098a75af49c7d1 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Thu, 16 Nov 2023 12:30:35 +0100 Subject: [PATCH 29/43] use static call for static method --- src/Relations/EmbedsMany.php | 2 +- src/Relations/EmbedsOne.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Relations/EmbedsMany.php b/src/Relations/EmbedsMany.php index 1d84dcf35..36fa7cbba 100644 --- a/src/Relations/EmbedsMany.php +++ b/src/Relations/EmbedsMany.php @@ -86,7 +86,7 @@ public function performUpdate(Model $model) // Get the correct foreign key value. $foreignKey = $this->getForeignKeyValue($model); - $values = $this->getUpdateValues($model->getDirty(), $this->localKey . '.$.'); + $values = self::getUpdateValues($model->getDirty(), $this->localKey . '.$.'); // Update document in database. $result = $this->toBase()->where($this->localKey . '.' . $model->getKeyName(), $foreignKey) diff --git a/src/Relations/EmbedsOne.php b/src/Relations/EmbedsOne.php index 233f6cdba..678141cf1 100644 --- a/src/Relations/EmbedsOne.php +++ b/src/Relations/EmbedsOne.php @@ -77,7 +77,7 @@ public function performUpdate(Model $model) return $this->parent->save(); } - $values = $this->getUpdateValues($model->getDirty(), $this->localKey . '.'); + $values = self::getUpdateValues($model->getDirty(), $this->localKey . '.'); $result = $this->toBase()->update($values); From c4755b1e6995d18c0b656d414a03aa5c610c8dac Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Thu, 16 Nov 2023 12:32:01 +0100 Subject: [PATCH 30/43] let early return return the expected data --- src/Relations/EmbedsOneOrMany.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Relations/EmbedsOneOrMany.php b/src/Relations/EmbedsOneOrMany.php index d810bcfa5..56fc62041 100644 --- a/src/Relations/EmbedsOneOrMany.php +++ b/src/Relations/EmbedsOneOrMany.php @@ -269,21 +269,21 @@ protected function toCollection(array $records = []) /** * Create a related model instanced. * - * @param array $attributes + * @param mixed $attributes * - * @return Model + * @return Model | null */ - protected function toModel($attributes = []) + protected function toModel(mixed $attributes = []): Model|null { if ($attributes === null) { - return; + return null; } $connection = $this->related->getConnection(); $model = $this->related->newFromBuilder( (array) $attributes, - $connection ? $connection->getName() : null, + $connection?->getName(), ); $model->setParentRelation($this); From 6d0e49ee4245d88bf332d426f1913d773b4c3656 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Mon, 20 Nov 2023 12:27:32 +0100 Subject: [PATCH 31/43] unused, removing The codecov.io service is no longer used. --- .github/workflows/build-ci.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/build-ci.yml b/.github/workflows/build-ci.yml index 306d20fc8..3664d752e 100644 --- a/.github/workflows/build-ci.yml +++ b/.github/workflows/build-ci.yml @@ -69,6 +69,3 @@ jobs: run: ./vendor/bin/phpunit --coverage-clover coverage.xml env: MONGODB_URI: 'mongodb://127.0.0.1/?replicaSet=rs' - - uses: codecov/codecov-action@v3 - with: - token: ${{ secrets.CODECOV_TOKEN }} From 1cc60c23b2efddb6a9acb11be7c847b25a214482 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Mon, 20 Nov 2023 12:33:14 +0100 Subject: [PATCH 32/43] update PHPStan baseline (level 2) --- phpstan-baseline.neon | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 7ceb63a8a..f452b8aa4 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,12 +1,22 @@ parameters: ignoreErrors: + - + message: "#^Variable \\$value on left side of \\?\\? always exists and is always null\\.$#" + count: 1 + path: src/Eloquent/Model.php + + - + message: "#^Variable \\$value on left side of \\?\\? always exists and is not nullable\\.$#" + count: 1 + path: src/Eloquent/Model.php + - message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Builder\\|Illuminate\\\\Database\\\\Eloquent\\\\Model\\:\\:pull\\(\\)\\.$#" count: 1 path: src/Relations/BelongsToMany.php - - message: "#^Cannot call method modelKeys\\(\\) on array\\|Illuminate\\\\Database\\\\Eloquent\\\\Model\\|Illuminate\\\\Support\\\\Collection\\.$#" + message: "#^Cannot call method modelKeys\\(\\) on array\\|Illuminate\\\\Support\\\\Collection\\.$#" count: 1 path: src/Relations/BelongsToMany.php From 8bce1bff19a5f516162e5dc7638b2c6e77e15f84 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Wed, 22 Nov 2023 12:18:48 +0100 Subject: [PATCH 33/43] replace assert with docblock --- src/Eloquent/Builder.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php index dba92b632..b9005c442 100644 --- a/src/Eloquent/Builder.php +++ b/src/Eloquent/Builder.php @@ -8,16 +8,15 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use MongoDB\Driver\Cursor; use MongoDB\Laravel\Helpers\QueriesRelationships; -use MongoDB\Laravel\Query\Builder as MongoDBQueryBuilder; use MongoDB\Model\BSONDocument; use function array_key_exists; use function array_merge; -use function assert; use function collect; use function is_array; use function iterator_to_array; +/** @method \MongoDB\Laravel\Query\Builder toBase() */ class Builder extends EloquentBuilder { use QueriesRelationships; @@ -64,10 +63,7 @@ public function update(array $values, array $options = []) return 1; } - $builder = $this->toBase(); - assert($builder instanceof MongoDBQueryBuilder); - - return $builder->update($this->addUpdatedAtColumn($values), $options); + return $this->toBase()->update($this->addUpdatedAtColumn($values), $options); } /** @inheritdoc */ From 9b4de896a52a3e8cb3c21e93cb25089e9d1d2575 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Wed, 22 Nov 2023 12:20:43 +0100 Subject: [PATCH 34/43] remove `@throw` docblock comment --- src/Relations/EmbedsMany.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Relations/EmbedsMany.php b/src/Relations/EmbedsMany.php index 36fa7cbba..2d68af70b 100644 --- a/src/Relations/EmbedsMany.php +++ b/src/Relations/EmbedsMany.php @@ -11,7 +11,6 @@ use MongoDB\BSON\ObjectID; use MongoDB\Driver\Exception\LogicException; use MongoDB\Laravel\Eloquent\Model as MongoDBModel; -use Throwable; use function array_key_exists; use function array_values; @@ -201,8 +200,6 @@ public function destroy($ids = []) * * @param null $id * - * @throws LogicException|Throwable - * * @note The $id is not used to delete embedded models. */ public function delete($id = null): int From a7b572edf579a8e871c1e16baf3283252d3b1dd4 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Wed, 22 Nov 2023 14:23:24 +0100 Subject: [PATCH 35/43] fix PHPStan error; unknown method `pull()` --- phpstan-baseline.neon | 5 ----- src/Relations/BelongsToMany.php | 3 +++ 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index f452b8aa4..9f5e094b8 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -10,11 +10,6 @@ parameters: count: 1 path: src/Eloquent/Model.php - - - message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Builder\\|Illuminate\\\\Database\\\\Eloquent\\\\Model\\:\\:pull\\(\\)\\.$#" - count: 1 - path: src/Relations/BelongsToMany.php - - message: "#^Cannot call method modelKeys\\(\\) on array\\|Illuminate\\\\Support\\\\Collection\\.$#" count: 1 diff --git a/src/Relations/BelongsToMany.php b/src/Relations/BelongsToMany.php index 5b3cc2a8b..2241680ca 100644 --- a/src/Relations/BelongsToMany.php +++ b/src/Relations/BelongsToMany.php @@ -15,6 +15,7 @@ use function array_map; use function array_merge; use function array_values; +use function assert; use function count; use function is_array; use function is_numeric; @@ -230,6 +231,8 @@ public function detach($ids = [], $touch = true) } // Remove the relation to the parent. + assert($this->parent instanceof \MongoDB\Laravel\Eloquent\Model); + assert($query instanceof \MongoDB\Laravel\Eloquent\Model); $query->pull($this->foreignPivotKey, $this->parent->getKey()); if ($touch) { From 58d7c63b99743116454230353c0d367cf5f66148 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Wed, 22 Nov 2023 15:58:30 +0100 Subject: [PATCH 36/43] replace array_merge with array unpacking --- src/Relations/MorphToMany.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Relations/MorphToMany.php b/src/Relations/MorphToMany.php index 9c9576d90..92a308e28 100644 --- a/src/Relations/MorphToMany.php +++ b/src/Relations/MorphToMany.php @@ -85,11 +85,11 @@ protected function setWhere() } /** @inheritdoc */ - public function save(Model $model, array $joining = [], $touch = true) + public function save(Model $model, array $pivotAttributes = [], $touch = true) { $model->save(['touch' => false]); - $this->attach($model, $joining, $touch); + $this->attach($model, $pivotAttributes, $touch); return $model; } @@ -272,12 +272,13 @@ public function detach($ids = [], $touch = true) // Remove the relation from the parent. $data = []; foreach ($ids as $item) { - $data = array_merge($data, [ + $data = [ + ...$data, [ $this->relatedPivotKey => $item, - $this->morphType => $this->related->getMorphClass(), + $this->morphType => $this->related->getMorphClass(), ], - ]); + ]; } $this->parent->pull($this->table, $data); @@ -378,8 +379,8 @@ protected function whereInMethod(Model $model, $key) /** * Extract ids from given pivot table data * - * @param array $data - * @param string|null $relatedPivotKey + * @param array $data + * @param string|null $relatedPivotKey * * @return mixed */ From 6c8f41adada436c4af36dd0f6793679782461543 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Wed, 22 Nov 2023 16:01:26 +0100 Subject: [PATCH 37/43] add return type to match the userland use case --- src/Relations/MorphToMany.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Relations/MorphToMany.php b/src/Relations/MorphToMany.php index 92a308e28..5dffbab5a 100644 --- a/src/Relations/MorphToMany.php +++ b/src/Relations/MorphToMany.php @@ -175,7 +175,7 @@ public function sync($ids, $detaching = true) } /** @inheritdoc */ - public function updateExistingPivot($id, array $attributes, $touch = true) + public function updateExistingPivot($id, array $attributes, $touch = true): void { // Do nothing, we have no pivot table. } From 02b7acbeb8e9fad486673bfbf0793352e7d799c2 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Wed, 22 Nov 2023 16:01:52 +0100 Subject: [PATCH 38/43] ignore Eloquent magically passing arguments --- phpstan-baseline.neon | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 9f5e094b8..51b641bc1 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -20,6 +20,11 @@ parameters: count: 3 path: src/Relations/BelongsToMany.php + - + message: "#^Method Illuminate\\\\Database\\\\Eloquent\\\\Model\\:\\:push\\(\\) invoked with 3 parameters, 0 required\\.$#" + count: 6 + path: src/Relations/MorphToMany.php + - message: "#^Method Illuminate\\\\Database\\\\Schema\\\\Blueprint\\:\\:create\\(\\) invoked with 1 parameter, 0 required\\.$#" count: 1 From 48adaeb1e3a278ca59398ed76d7b1277645beab0 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Wed, 22 Nov 2023 16:11:38 +0100 Subject: [PATCH 39/43] replace with the suggested "new" method --- src/Relations/BelongsToMany.php | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/src/Relations/BelongsToMany.php b/src/Relations/BelongsToMany.php index 2241680ca..eff667ebb 100644 --- a/src/Relations/BelongsToMany.php +++ b/src/Relations/BelongsToMany.php @@ -17,7 +17,6 @@ use function array_values; use function assert; use function count; -use function is_array; use function is_numeric; class BelongsToMany extends EloquentBelongsToMany @@ -132,7 +131,7 @@ public function sync($ids, $detaching = true) $current = $ids->modelKeys(); } - $records = $this->formatSyncList($ids); + $records = $this->formatRecordsList($ids); $current = Arr::wrap($current); @@ -299,28 +298,6 @@ public function getQualifiedRelatedPivotKeyName() return $this->relatedPivotKey; } - /** - * Format the sync list so that it is keyed by ID. (Legacy Support) - * The original function has been renamed to formatRecordsList since Laravel 5.3. - * - * @deprecated - * - * @return array - */ - protected function formatSyncList(array $records) - { - $results = []; - foreach ($records as $id => $attributes) { - if (! is_array($attributes)) { - [$id, $attributes] = [$attributes, []]; - } - - $results[$id] = $attributes; - } - - return $results; - } - /** * Get the name of the "where in" method for eager loading. * From 395ed5c78ba43ad014fe65127ba636333a13e4d2 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Thu, 23 Nov 2023 12:16:43 +0100 Subject: [PATCH 40/43] Apply suggestions from code review Co-authored-by: Mohammad Mortazavi <39920372+hans-thomas@users.noreply.github.com> --- src/Relations/BelongsToMany.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Relations/BelongsToMany.php b/src/Relations/BelongsToMany.php index eff667ebb..84c9007f4 100644 --- a/src/Relations/BelongsToMany.php +++ b/src/Relations/BelongsToMany.php @@ -231,7 +231,7 @@ public function detach($ids = [], $touch = true) // Remove the relation to the parent. assert($this->parent instanceof \MongoDB\Laravel\Eloquent\Model); - assert($query instanceof \MongoDB\Laravel\Eloquent\Model); + assert($query instanceof \MongoDB\Laravel\Eloquent\Builder); $query->pull($this->foreignPivotKey, $this->parent->getKey()); if ($touch) { From d73f9d902c930cb28a1a999d8b572fac616d4303 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Thu, 23 Nov 2023 14:19:42 +0100 Subject: [PATCH 41/43] fix null coalescing Co-authored-by: Mohammad Mortazavi <39920372+hans-thomas@users.noreply.github.com> --- phpstan-baseline.neon | 10 ---------- src/Eloquent/Model.php | 4 ++-- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 51b641bc1..06d94424b 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,15 +1,5 @@ parameters: ignoreErrors: - - - message: "#^Variable \\$value on left side of \\?\\? always exists and is always null\\.$#" - count: 1 - path: src/Eloquent/Model.php - - - - message: "#^Variable \\$value on left side of \\?\\? always exists and is not nullable\\.$#" - count: 1 - path: src/Eloquent/Model.php - - message: "#^Cannot call method modelKeys\\(\\) on array\\|Illuminate\\\\Support\\\\Collection\\.$#" count: 1 diff --git a/src/Eloquent/Model.php b/src/Eloquent/Model.php index bbd45a32b..bcb672a3c 100644 --- a/src/Eloquent/Model.php +++ b/src/Eloquent/Model.php @@ -297,10 +297,10 @@ protected function asDecimal($value, $decimals) public function fromJson($value, $asObject = false) { if (! is_string($value)) { - $value = Json::encode($value ?? ''); + $value = Json::encode($value); } - return Json::decode($value ?? '', ! $asObject); + return Json::decode($value, ! $asObject); } /** @inheritdoc */ From 0db7601a14ae00da2443325064cc081c5e0c7fe3 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Thu, 23 Nov 2023 14:25:05 +0100 Subject: [PATCH 42/43] remove Collection check Co-authored-by: Mohammad Mortazavi <39920372+hans-thomas@users.noreply.github.com> --- phpstan-baseline.neon | 5 ----- src/Relations/BelongsToMany.php | 5 ----- 2 files changed, 10 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 06d94424b..71a44a395 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,10 +1,5 @@ parameters: ignoreErrors: - - - message: "#^Cannot call method modelKeys\\(\\) on array\\|Illuminate\\\\Support\\\\Collection\\.$#" - count: 1 - path: src/Relations/BelongsToMany.php - - message: "#^Method Illuminate\\\\Database\\\\Eloquent\\\\Model\\:\\:push\\(\\) invoked with 3 parameters, 0 required\\.$#" count: 3 diff --git a/src/Relations/BelongsToMany.php b/src/Relations/BelongsToMany.php index 84c9007f4..1d6b84ba8 100644 --- a/src/Relations/BelongsToMany.php +++ b/src/Relations/BelongsToMany.php @@ -126,11 +126,6 @@ public function sync($ids, $detaching = true) // if they exist in the array of current ones, and if not we will insert. $current = $this->parent->{$this->relatedPivotKey} ?: []; - // See issue #256. - if ($current instanceof Collection) { - $current = $ids->modelKeys(); - } - $records = $this->formatRecordsList($ids); $current = Arr::wrap($current); From 6bfe7199a4bba53c50f26df3fd9237633ff60a68 Mon Sep 17 00:00:00 2001 From: Tonko Mulder Date: Thu, 23 Nov 2023 15:43:35 +0100 Subject: [PATCH 43/43] remove Collection check Co-authored-by: Mohammad Mortazavi <39920372+hans-thomas@users.noreply.github.com> --- src/Relations/MorphToMany.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Relations/MorphToMany.php b/src/Relations/MorphToMany.php index 5dffbab5a..a2c55969f 100644 --- a/src/Relations/MorphToMany.php +++ b/src/Relations/MorphToMany.php @@ -133,11 +133,6 @@ public function sync($ids, $detaching = true) $current = $this->parent->{$this->relatedPivotKey} ?: []; } - // See issue #256. - if ($current instanceof Collection) { - $current = $this->parseIds($current); - } - $records = $this->formatRecordsList($ids); $current = Arr::wrap($current);