Skip to content

Commit aa04362

Browse files
authored
PHPLIB-610: Psalm Integration (#973)
* Add base psalm configuration * Run psalm on GitHub Actions * Fix psalm errors * Use development version of psalm The upcoming release will include an updated callmap for the MongoDB classes. * Add unfixable error to psalm baseline * Enforce psalm errorLevel 1 for new code Existing errors are added to the baseline, meaning that new code needs to adhere to the strictest psalm level. * Run coding standards and static analysis checks on feature branches * Add contribution docs for psalm * Improve ChangeStreamIterator handling of wrapped iterator * Specify callable explicitly in ChangeStream * Make null coalesce within ternary more readable * Rewrite conditional to avoid suppressing psalm errors * Move psalm config to dist file * Add comment about ChangeStreamIterator::getInnerIterator
1 parent 07b48fc commit aa04362

32 files changed

+1071
-50
lines changed

.github/workflows/coding-standards.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ on:
55
branches:
66
- "v*.*"
77
- "master"
8+
- "feature/*"
89
push:
910
branches:
1011
- "v*.*"
1112
- "master"
13+
- "feature/*"
1214

1315
jobs:
14-
coding-standards:
15-
name: "Coding Standards"
16+
phpcs:
17+
name: "phpcs"
1618
runs-on: "ubuntu-20.04"
1719

1820
strategy:

.github/workflows/static-analysis.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: "Static Analysis"
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- "v*.*"
7+
- "master"
8+
- "feature/*"
9+
push:
10+
branches:
11+
- "v*.*"
12+
- "master"
13+
- "feature/*"
14+
15+
jobs:
16+
psalm:
17+
name: "Psalm"
18+
runs-on: "ubuntu-20.04"
19+
20+
strategy:
21+
matrix:
22+
php-version:
23+
- "7.4"
24+
driver-version:
25+
- "mongodb/mongo-php-driver@master"
26+
27+
steps:
28+
- name: "Checkout"
29+
uses: "actions/checkout@v2"
30+
31+
- name: Setup cache environment
32+
id: extcache
33+
uses: shivammathur/cache-extensions@v1
34+
with:
35+
php-version: ${{ matrix.php-version }}
36+
extensions: "mongodb-${{ matrix.driver-version }}"
37+
key: "extcache-v1"
38+
39+
- name: Cache extensions
40+
uses: actions/cache@v2
41+
with:
42+
path: ${{ steps.extcache.outputs.dir }}
43+
key: ${{ steps.extcache.outputs.key }}
44+
restore-keys: ${{ steps.extcache.outputs.key }}
45+
46+
- name: "Install PHP"
47+
uses: "shivammathur/setup-php@v2"
48+
with:
49+
coverage: "none"
50+
extensions: "mongodb-${{ matrix.driver-version }}"
51+
php-version: "${{ matrix.php-version }}"
52+
tools: "cs2pr"
53+
54+
- name: "Show driver information"
55+
run: "php --ri mongodb"
56+
57+
- name: "Cache dependencies installed with Composer"
58+
uses: "actions/cache@v2"
59+
with:
60+
path: "~/.composer/cache"
61+
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"
62+
restore-keys: "php-${{ matrix.php-version }}-composer-locked-"
63+
64+
- name: "Install dependencies with Composer"
65+
run: "composer install --no-interaction --no-progress --no-suggest"
66+
67+
- name: "Run Psalm"
68+
run: "vendor/bin/psalm --show-info=false --stats --output-format=github --threads=$(nproc)"

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ phpunit.xml
1212
.phpcs-cache
1313
phpcs.xml
1414

15+
# psalm
16+
psalm.xml
17+
1518
mongocryptd.pid

CONTRIBUTING.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,32 @@ To automatically fix all fixable errors, use the `phpcbf` binary:
122122
$ vendor/bin/phpcbf
123123
```
124124

125+
## Running static analysis
126+
127+
The library uses [psalm](https://psalm.dev) to run static analysis on the code
128+
and ensure an additional level of type safety. New code is expected to adhere
129+
to level 1, with a baseline covering existing issues. To run static analysis
130+
checks, run the `psalm` binary:
131+
132+
```
133+
$ vendor/bin/psalm
134+
```
135+
136+
To remove fixed errors from the baseline, you can use the `update-baseline`
137+
command-line argument:
138+
139+
```
140+
$ vendor/bin/psalm --update-baseline
141+
```
142+
143+
Note that this will not add new errors to the baseline. New errors should be
144+
fixed instead of being added to the technical debt, but in case this isn't
145+
possible it can be added to the baseline using `set-baseline`:
146+
147+
```
148+
$ vendor/bin/psalm --set-baseline=psalm-baseline.xml
149+
```
150+
125151
## Documentation
126152

127153
Documentation for the library lives in the `docs/` directory and is built with

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"require-dev": {
2020
"squizlabs/php_codesniffer": "^3.6",
2121
"doctrine/coding-standard": "^9.0",
22-
"symfony/phpunit-bridge": "^5.2"
22+
"symfony/phpunit-bridge": "^5.2",
23+
"vimeo/psalm": "^4.x-dev"
2324
},
2425
"autoload": {
2526
"psr-4": { "MongoDB\\": "src/" },

0 commit comments

Comments
 (0)