Skip to content

Commit

Permalink
Code analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
rimi-itk committed Oct 3, 2023
1 parent 7305397 commit 42192c5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 36 deletions.
30 changes: 29 additions & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ jobs:
strategy:
matrix:
php-versions: [ '8.1' ]
dependency-version: [ prefer-lowest, prefer-stable ]
steps:
- uses: actions/checkout@master
- name: Setup PHP, with composer and extensions
Expand All @@ -83,6 +82,35 @@ jobs:
run: |
composer coding-standards-check/phpcs
php-code-analysis:
name: PHP code analysis
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '8.1' ]
steps:
- uses: actions/checkout@master
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: json
coverage: none
tools: composer:v2
# https://github.com/shivammathur/setup-php#cache-composer-dependencies
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: Code analysis
run: |
./scripts/code-analysis
markdownlint:
runs-on: ubuntu-latest
name: markdownlint
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,3 @@ we use a helper script to run the analysis:
```sh
./scripts/code-analysis
```

**TODO**: Make the code analysis run in GitHub Actions (cf.
<https://github.com/itk-dev/os2forms_digital_post/blob/main/.github/workflows/pr.yaml#L86-L144>).
58 changes: 26 additions & 32 deletions scripts/code-analysis
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
#!/usr/bin/env bash
set -o errexit -o errtrace -o nounset -o pipefail
IFS=$'\n\t'

script_dir=$(pwd)
module_name=$(basename "$script_dir")
drupal_dir=vendor/drupal-module-code-analysis
# Relative to $drupal_dir
module_path=web/modules/contrib/$module_name

# @see https://unix.stackexchange.com/a/13474
upsearch () {
slashes=${PWD//[^\/]/}
directory="$PWD"
for (( n=${#slashes}; n>0; --n ))
do
test -e "$directory/$1" && echo "$directory/$1" && return
directory="$directory/.."
done
}

cd "$script_dir" || exit

# The Drupal root contains a `web` folder.
drupal_root=$(dirname "$(cd "$(upsearch web)" && pwd)")
# Module path from drupal_root (note trailing slash) (cf. https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html)
module_path=${script_dir#"${drupal_root}/"}
drupal_composer() {
composer --working-dir="$drupal_dir" --no-interaction "$@"
}

echo "Drupal root: $drupal_root"
echo "Module path: $module_path"
# Create new Drupal 9 project
if [ ! -f "$drupal_dir/composer.json" ]; then
composer --no-interaction create-project drupal/recommended-project:^9 "$drupal_dir"
fi
# Copy our code into the modules folder
mkdir -p "$drupal_dir/$module_path"
# https://stackoverflow.com/a/15373763
rsync --archive --compress . --filter=':- .gitignore' --exclude "$drupal_dir" --exclude .git "$drupal_dir/$module_path"

# Work around
# PHP Fatal error: Cannot redeclare drupal_get_filename() (previously declared in /Users/rimi/ITK/github/itk-dev/os2forms_selvbetjening/web/sites/default/modules/os2forms/vendor/drupal/core/includes/bootstrap.inc:190) in /Users/rimi/ITK/github/itk-dev/os2forms_selvbetjening/web/core/includes/bootstrap.inc on line 223
# Remove our non-develop requirements and a develop dependency requiring drupal/core.
docker run --volume ${PWD}:/app --workdir /app --rm efrecon/jq:1.7 'del(.require, ."require-dev"["drupal/maillog"])' composer.json > drupal-module-code-analysis-composer.json
drupal_composer config minimum-stability dev

# It seems that the file system needs a little time to sync.
sleep 1
# Allow ALL plugins
# https://getcomposer.org/doc/06-config.md#allow-plugins
drupal_composer config --no-plugins allow-plugins true

docker run --rm --env COMPOSER=drupal-module-code-analysis-composer.json --volume "${script_dir}":/app itkdev/php8.1-fpm:latest composer install
# Clean up
rm drupal-module-code-analysis-composer.*
drupal_composer require wikimedia/composer-merge-plugin
drupal_composer config extra.merge-plugin.include "$module_path/composer.json"
# https://www.drupal.org/project/drupal/issues/3220043#comment-14845434
drupal_composer require --dev symfony/phpunit-bridge

# https://getcomposer.org/doc/03-cli.md#global-options
# docker run --rm --interactive --tty --volume ${drupal_root}:/app itkdev/php8.1-fpm:latest composer --working-dir $module_path code-analysis/drupal-check
docker run --rm --env PHP_MEMORY_LIMIT=-1 --interactive --tty --volume "${drupal_root}":/app itkdev/php8.1-fpm:latest composer --working-dir "$module_path" code-analysis/phpstan
# Run PHPStan
(cd "$drupal_dir" && vendor/bin/phpstan --configuration="$module_path/phpstan.neon")

0 comments on commit 42192c5

Please sign in to comment.