Repository navigation
Add PHPStan analysis for PHP blocks in feature files #354
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
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,72 @@ | ||
| #!/bin/sh | ||
|
|
||
| # Run the code style check only if a configuration file exists. | ||
| EXIT_CODE=0 | ||
|
|
||
| # 1. Run the static analysis only if a configuration file exists. | ||
| if [ -f "phpstan.dist.neon" ] || [ -f "phpstan.neon.dist" ] || [ -f "phpstan.neon" ] | ||
| then | ||
| vendor/bin/phpstan --memory-limit=2048M analyse "$@" | ||
| vendor/bin/phpstan --memory-limit=2048M analyse "$@" || EXIT_CODE=$? | ||
| fi | ||
|
|
||
| # 2. Run PHPStan over the PHP blocks in .feature files if the package opted in. | ||
| DIR="$(cd "$(dirname "$0")/.." && pwd)" | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| FEATURE_CONFIG="" | ||
| for CANDIDATE in "phpstan-feature-files.neon" "phpstan-feature-files.neon.dist" | ||
| do | ||
| if [ -f "$CANDIDATE" ] | ||
| then | ||
| FEATURE_CONFIG="$(pwd)/$CANDIDATE" | ||
| break | ||
| fi | ||
| done | ||
|
|
||
| if [ -d "features" ] && [ -n "$FEATURE_CONFIG" ] && [ -f "$DIR/utils/phpstan-feature-files.php" ] | ||
| then | ||
| TEMP_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'feature_phpstan') | ||
| trap 'rm -rf "$TEMP_DIR"' EXIT HUP INT TERM | ||
|
|
||
| # Results are only reported when the extraction they are based on succeeded. | ||
| if php "$DIR/utils/phpstan-feature-files.php" extract features "$TEMP_DIR/blocks" | ||
| then | ||
| { | ||
| echo "includes:" | ||
| echo " - $DIR/phpstan/feature-files.neon" | ||
| echo " - $FEATURE_CONFIG" | ||
|
|
||
| # The functions in the `WP_CLI\Utils` namespace are pulled in through the | ||
| # `files` autoloader, which does not make them known to PHPStan. | ||
| if [ -d "vendor/wp-cli/wp-cli" ] | ||
| then | ||
| echo "parameters:" | ||
| echo " scanDirectories:" | ||
| echo " - $(pwd)/vendor/wp-cli/wp-cli" | ||
| fi | ||
| } > "$TEMP_DIR/phpstan-feature-files.neon" | ||
|
|
||
| # Blocks are spread over batches that do not declare the same symbol twice, | ||
| # so that PHPStan does not resolve a name to another block's declaration. | ||
| for BATCH in "$TEMP_DIR"/blocks/batch* | ||
| do | ||
| [ -d "$BATCH" ] || continue | ||
|
|
||
| vendor/bin/phpstan --memory-limit=2048M analyse \ | ||
| --configuration="$TEMP_DIR/phpstan-feature-files.neon" \ | ||
| --error-format=json \ | ||
| --no-progress \ | ||
| "$BATCH" > "$TEMP_DIR/$(basename "$BATCH").json" 2>"$TEMP_DIR/$(basename "$BATCH").stderr" | ||
|
|
||
| # Anything PHPStan itself has to say goes to the terminal, while its | ||
| # findings are reported against the feature files they came from. | ||
| if [ -s "$TEMP_DIR/$(basename "$BATCH").stderr" ] | ||
| then | ||
| cat "$TEMP_DIR/$(basename "$BATCH").stderr" >&2 | ||
| fi | ||
| done | ||
|
|
||
| php "$DIR/utils/phpstan-feature-files.php" report "$TEMP_DIR/blocks" "$TEMP_DIR"/batch*.json || EXIT_CODE=$? | ||
| else | ||
| EXIT_CODE=1 | ||
| fi | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| fi | ||
|
|
||
| exit $EXIT_CODE | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Opts this package into the static analysis of the PHP blocks in its feature files. | ||
| # | ||
| # The defaults in `phpstan/feature-files.neon` are applied first. Packages using the | ||
| # testing framework pick up `extension.neon` through phpstan/extension-installer, so | ||
| # they do not have to include it themselves. | ||
|
|
||
| includes: | ||
| - extension.neon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Defaults for the static analysis of the PHP blocks embedded in Behat feature files. | ||
| # | ||
| # A package opts into the analysis by adding a `phpstan-feature-files.neon` (or | ||
| # `phpstan-feature-files.neon.dist`) file to its root. These defaults are always | ||
| # applied first, so that file only needs to hold what it wants to change. | ||
| # | ||
| # The snippets in feature files are fixtures rather than production code, so the | ||
| # level is kept below the one that asks for type declarations everywhere. | ||
|
|
||
| parameters: | ||
| level: 5 | ||
| treatPhpDocTypesAsCertain: false | ||
|
|
||
| # Most packages will not run into all of the errors ignored below. | ||
| reportUnmatchedIgnoredErrors: false | ||
|
|
||
| ignoreErrors: | ||
| # `wp eval-file` passes the remaining positional arguments to the file it runs. | ||
| - | ||
| identifier: variable.undefined | ||
| message: '#^Variable \$args might not be defined\.$#' | ||
|
|
||
| # Files a snippet pulls in are created by other steps while the scenario runs. | ||
| - identifier: include.fileNotFound | ||
| - identifier: includeOnce.fileNotFound | ||
| - identifier: require.fileNotFound | ||
| - identifier: requireOnce.fileNotFound |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.