Skip to content

Commit 973b6b9

Browse files
authored
[feature] add static analysis tool (mongodb#2664)
1 parent 82ddb83 commit 973b6b9

18 files changed

+221
-125
lines changed

Diff for: .editorconfig

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ end_of_line = lf
66
insert_final_newline = true
77
indent_style = space
88
indent_size = 4
9-
trim_trailing_whitespace = true
9+
trim_trailing_whitespace = true
10+
11+
[*.yml]
12+
indent_size = 2

Diff for: .github/workflows/build-ci.yml

+3-11
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ name: CI
22

33
on:
44
push:
5-
branches:
6-
tags:
75
pull_request:
86

97
jobs:
@@ -55,7 +53,7 @@ jobs:
5553
- name: Show Docker version
5654
run: if [[ "$DEBUG" == "true" ]]; then docker version && env; fi
5755
env:
58-
DEBUG: ${{secrets.DEBUG}}
56+
DEBUG: ${{ secrets.DEBUG }}
5957
- name: Download Composer cache dependencies from cache
6058
id: composer-cache
6159
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
@@ -66,14 +64,8 @@ jobs:
6664
key: ${{ matrix.os }}-composer-${{ hashFiles('**/composer.json') }}
6765
restore-keys: ${{ matrix.os }}-composer-
6866
- name: Install dependencies
69-
run: |
70-
composer update --no-interaction $([[ "${{ matrix.mode }}" == low-deps ]] && echo ' --prefer-lowest --prefer-stable')
67+
run: composer update --no-interaction $([[ "${{ matrix.mode }}" == low-deps ]] && echo ' --prefer-lowest --prefer-stable')
7168
- name: Run tests
72-
run: |
73-
./vendor/bin/phpunit --coverage-clover coverage.xml
69+
run: ./vendor/bin/phpunit --coverage-clover coverage.xml
7470
env:
7571
MONGODB_URI: 'mongodb://127.0.0.1/?replicaSet=rs'
76-
- uses: codecov/codecov-action@v3
77-
with:
78-
token: ${{ secrets.CODECOV_TOKEN }}
79-
fail_ci_if_error: false

Diff for: .github/workflows/coding-standards.yml

+66-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ name: "Coding Standards"
22

33
on:
44
push:
5-
branches:
6-
tags:
75
pull_request:
86

97
env:
@@ -15,6 +13,11 @@ jobs:
1513
name: "phpcs"
1614
runs-on: "ubuntu-22.04"
1715

16+
permissions:
17+
# Give the default GITHUB_TOKEN write permission to commit and push the
18+
# added or changed files to the repository.
19+
contents: write
20+
1821
steps:
1922
- name: "Checkout"
2023
uses: "actions/checkout@v4"
@@ -50,6 +53,67 @@ jobs:
5053
with:
5154
composer-options: "--no-suggest"
5255

56+
- name: "Format the code"
57+
continue-on-error: true
58+
run: |
59+
mkdir .cache
60+
./vendor/bin/phpcbf
61+
5362
# The -q option is required until phpcs v4 is released
5463
- name: "Run PHP_CodeSniffer"
5564
run: "vendor/bin/phpcs -q --no-colors --report=checkstyle | cs2pr"
65+
66+
- name: "Commit the changes"
67+
uses: stefanzweifel/git-auto-commit-action@v5
68+
with:
69+
commit_message: "apply phpcbf formatting"
70+
71+
analysis:
72+
runs-on: "ubuntu-22.04"
73+
continue-on-error: true
74+
strategy:
75+
matrix:
76+
php:
77+
- '8.1'
78+
- '8.2'
79+
steps:
80+
- name: Checkout
81+
uses: actions/checkout@v4
82+
83+
- name: Setup PHP
84+
uses: shivammathur/setup-php@v2
85+
with:
86+
php-version: ${{ matrix.php }}
87+
extensions: curl, mbstring
88+
tools: composer:v2
89+
coverage: none
90+
91+
- name: Cache dependencies
92+
id: composer-cache
93+
uses: actions/cache@v3
94+
with:
95+
path: ./vendor
96+
key: composer-${{ hashFiles('**/composer.lock') }}
97+
98+
- name: Install dependencies
99+
run: composer install
100+
101+
- name: Restore cache PHPStan results
102+
id: phpstan-cache-restore
103+
uses: actions/cache/restore@v3
104+
with:
105+
path: .cache
106+
key: "phpstan-result-cache-${{ github.run_id }}"
107+
restore-keys: |
108+
phpstan-result-cache-
109+
110+
- name: Run PHPStan
111+
run: ./vendor/bin/phpstan analyse --no-interaction --no-progress --ansi
112+
113+
- name: Save cache PHPStan results
114+
id: phpstan-cache-save
115+
if: always()
116+
uses: actions/cache/save@v3
117+
with:
118+
path: .cache
119+
key: ${{ steps.phpstan-cache-restore.outputs.cache-primary-key }}

Diff for: .gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
*.sublime-workspace
44
.DS_Store
55
.idea/
6-
.phpunit.cache/
7-
.phpcs-cache
86
/vendor
97
composer.lock
108
composer.phar
119
phpunit.xml
10+
phpstan.neon
11+
/.cache/

Diff for: composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"orchestra/testbench": "^8.0",
3636
"mockery/mockery": "^1.4.4",
3737
"doctrine/coding-standard": "12.0.x-dev",
38-
"spatie/laravel-query-builder": "^5.6"
38+
"spatie/laravel-query-builder": "^5.6",
39+
"phpstan/phpstan": "^1.10"
3940
},
4041
"replace": {
4142
"jenssegers/mongodb": "self.version"

Diff for: phpcs.xml.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<arg name="basepath" value="." />
44
<arg name="extensions" value="php" />
55
<arg name="parallel" value="80" />
6-
<arg name="cache" value=".phpcs-cache" />
6+
<arg name="cache" value=".cache/phpcs" />
77
<arg name="colors" />
88

99
<!-- Ignore warnings (n), show progress of the run (p), and show sniff names (s) -->

Diff for: phpstan-baseline.neon

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: "#^Method Illuminate\\\\Database\\\\Eloquent\\\\Model\\:\\:push\\(\\) invoked with 3 parameters, 0 required\\.$#"
5+
count: 3
6+
path: src/Relations/BelongsToMany.php
7+
8+
-
9+
message: "#^Method Illuminate\\\\Database\\\\Eloquent\\\\Model\\:\\:push\\(\\) invoked with 3 parameters, 0 required\\.$#"
10+
count: 6
11+
path: src/Relations/MorphToMany.php
12+
13+
-
14+
message: "#^Method Illuminate\\\\Database\\\\Schema\\\\Blueprint\\:\\:create\\(\\) invoked with 1 parameter, 0 required\\.$#"
15+
count: 1
16+
path: src/Schema/Builder.php

Diff for: phpstan.neon.dist

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
includes:
2+
- ./phpstan-baseline.neon
3+
4+
parameters:
5+
tmpDir: .cache/phpstan
6+
7+
paths:
8+
- src
9+
10+
level: 2
11+
12+
editorUrl: 'phpstorm://open?file=%%file%%&line=%%line%%'
13+
14+
ignoreErrors:
15+
- '#Unsafe usage of new static#'
16+
- '#Call to an undefined method [a-zA-Z0-9\\_\<\>]+::[a-zA-Z]+\(\)#'

Diff for: phpunit.xml.dist

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
4-
backupGlobals="false"
54
bootstrap="vendor/autoload.php"
6-
colors="true"
7-
processIsolation="false"
8-
stopOnFailure="false"
9-
cacheDirectory=".phpunit.cache"
10-
backupStaticProperties="false"
11-
>
12-
<coverage/>
5+
cacheDirectory=".cache/phpunit"
6+
executionOrder="depends,defects"
7+
beStrictAboutCoverageMetadata="true"
8+
beStrictAboutOutputDuringTests="true"
9+
failOnRisky="true"
10+
failOnWarning="true">
1311
<testsuites>
1412
<testsuite name="Test Suite">
1513
<directory>tests/</directory>
@@ -20,10 +18,15 @@
2018
<env name="MONGODB_DATABASE" value="unittest"/>
2119
<env name="SQLITE_DATABASE" value=":memory:"/>
2220
<env name="QUEUE_CONNECTION" value="database"/>
21+
22+
<ini name="memory_limit" value="-1"/>
2323
</php>
24-
<source>
24+
25+
<source restrictDeprecations="true"
26+
restrictNotices="true"
27+
restrictWarnings="true">
2528
<include>
26-
<directory suffix=".php">./src</directory>
29+
<directory>./src</directory>
2730
</include>
2831
</source>
2932
</phpunit>

Diff for: src/Eloquent/Builder.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use function is_array;
1717
use function iterator_to_array;
1818

19+
/** @method \MongoDB\Laravel\Query\Builder toBase() */
1920
class Builder extends EloquentBuilder
2021
{
2122
use QueriesRelationships;
@@ -219,16 +220,15 @@ protected function ensureOrderForCursorPagination($shouldReverse = false)
219220
}
220221

221222
if ($shouldReverse) {
222-
$this->query->orders = collect($this->query->orders)->map(function ($direction) {
223-
return $direction === 1 ? -1 : 1;
224-
})->toArray();
223+
$this->query->orders = collect($this->query->orders)
224+
->map(static fn (int $direction) => $direction === 1 ? -1 : 1)
225+
->toArray();
225226
}
226227

227-
return collect($this->query->orders)->map(function ($direction, $column) {
228-
return [
228+
return collect($this->query->orders)
229+
->map(static fn ($direction, $column) => [
229230
'column' => $column,
230231
'direction' => $direction === 1 ? 'asc' : 'desc',
231-
];
232-
})->values();
232+
])->values();
233233
}
234234
}

Diff for: src/Eloquent/Model.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,10 @@ protected function asDecimal($value, $decimals)
297297
public function fromJson($value, $asObject = false)
298298
{
299299
if (! is_string($value)) {
300-
$value = Json::encode($value ?? '');
300+
$value = Json::encode($value);
301301
}
302302

303-
return Json::decode($value ?? '', ! $asObject);
303+
return Json::decode($value, ! $asObject);
304304
}
305305

306306
/** @inheritdoc */

Diff for: src/Relations/BelongsToMany.php

+8-32
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
use function array_map;
1616
use function array_merge;
1717
use function array_values;
18+
use function assert;
1819
use function count;
19-
use function is_array;
2020
use function is_numeric;
2121

2222
class BelongsToMany extends EloquentBelongsToMany
@@ -82,11 +82,11 @@ protected function setWhere()
8282
}
8383

8484
/** @inheritdoc */
85-
public function save(Model $model, array $joining = [], $touch = true)
85+
public function save(Model $model, array $pivotAttributes = [], $touch = true)
8686
{
8787
$model->save(['touch' => false]);
8888

89-
$this->attach($model, $joining, $touch);
89+
$this->attach($model, $pivotAttributes, $touch);
9090

9191
return $model;
9292
}
@@ -126,12 +126,7 @@ public function sync($ids, $detaching = true)
126126
// if they exist in the array of current ones, and if not we will insert.
127127
$current = $this->parent->{$this->relatedPivotKey} ?: [];
128128

129-
// See issue #256.
130-
if ($current instanceof Collection) {
131-
$current = $ids->modelKeys();
132-
}
133-
134-
$records = $this->formatSyncList($ids);
129+
$records = $this->formatRecordsList($ids);
135130

136131
$current = Arr::wrap($current);
137132

@@ -171,6 +166,7 @@ public function sync($ids, $detaching = true)
171166
public function updateExistingPivot($id, array $attributes, $touch = true)
172167
{
173168
// Do nothing, we have no pivot table.
169+
return $this;
174170
}
175171

176172
/** @inheritdoc */
@@ -229,6 +225,8 @@ public function detach($ids = [], $touch = true)
229225
}
230226

231227
// Remove the relation to the parent.
228+
assert($this->parent instanceof \MongoDB\Laravel\Eloquent\Model);
229+
assert($query instanceof \MongoDB\Laravel\Eloquent\Builder);
232230
$query->pull($this->foreignPivotKey, $this->parent->getKey());
233231

234232
if ($touch) {
@@ -266,7 +264,7 @@ public function newPivotQuery()
266264
/**
267265
* Create a new query builder for the related model.
268266
*
269-
* @return \Illuminate\Database\Query\Builder
267+
* @return Builder|Model
270268
*/
271269
public function newRelatedQuery()
272270
{
@@ -295,28 +293,6 @@ public function getQualifiedRelatedPivotKeyName()
295293
return $this->relatedPivotKey;
296294
}
297295

298-
/**
299-
* Format the sync list so that it is keyed by ID. (Legacy Support)
300-
* The original function has been renamed to formatRecordsList since Laravel 5.3.
301-
*
302-
* @deprecated
303-
*
304-
* @return array
305-
*/
306-
protected function formatSyncList(array $records)
307-
{
308-
$results = [];
309-
foreach ($records as $id => $attributes) {
310-
if (! is_array($attributes)) {
311-
[$id, $attributes] = [$attributes, []];
312-
}
313-
314-
$results[$id] = $attributes;
315-
}
316-
317-
return $results;
318-
}
319-
320296
/**
321297
* Get the name of the "where in" method for eager loading.
322298
*

0 commit comments

Comments
 (0)