Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PHPStan for Static Analysis #40

Merged
merged 4 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ If your work fixes existing functionality, check if a test exists. Either update

Tests are written in PHP using [PHPUnit](https://phpunit.de/), and the existing `tests/ConvertKitAPITest.php` is a good place to start as a guide.

## Run PHPStan

[PHPStan](https://phpstan.org) performs static analysis on the Plugin's PHP code. This ensures:

- DocBlocks declarations are valid and uniform
- DocBlocks declarations for WordPress `do_action()` and `apply_filters()` calls are valid
- Typehinting variables and return types declared in DocBlocks are correctly cast
- Any unused functions are detected
- Unnecessary checks / code is highlighted for possible removal
- Conditions that do not evaluate can be fixed/removed as necessary

In the Plugin's directory, run the following command to run PHPStan:

```bash
vendor/bin/phpstan --memory-limit=1G
```

Any errors should be corrected by making applicable code changes.

False positives [can be excluded by configuring](https://phpstan.org/user-guide/ignoring-errors) the `phpstan.neon` file.

## Run PHPUnit

Once you have written your code and tests, run the tests to make sure there are no errors.
Expand Down
14 changes: 14 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Parameters
parameters:
# Paths to scan
paths:
- src/

# Should not need to edit anything below here
# Rule Level: https://phpstan.org/user-guide/rule-levels
level: 8

# Ignore the following errors, as PHPStan either does not have registered symbols for them yet,
# or the symbols are inaccurate.
ignoreErrors:
- '#\$headers of class GuzzleHttp\\Psr7\\Request constructor expects#'
14 changes: 14 additions & 0 deletions phpstan.neon.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Parameters
parameters:
# Paths to scan
paths:
- src/

# Should not need to edit anything below here
# Rule Level: https://phpstan.org/user-guide/rule-levels
level: 8

# Ignore the following errors, as PHPStan either does not have registered symbols for them yet,
# or the symbols are inaccurate.
ignoreErrors:
- '#\$headers of class GuzzleHttp\\Psr7\\Request constructor expects#'
Loading