From d1317916dc19f7e316bfd3e27eb27287887c13e3 Mon Sep 17 00:00:00 2001 From: James Hill Date: Wed, 25 Mar 2026 13:13:06 +1300 Subject: [PATCH] PHPStan [no_release] Types are messed up in level 1, maybe tricky to safely fix. --- .git-hooks-matomo/.pre-push.swp | Bin 0 -> 12288 bytes .git-hooks-matomo/pre-push | 23 +++++++ .git-hooks-matomo/pre-push.d/phpstan.sh | 69 +++++++++++++++++++ .github/workflows/phpstan.yml | 85 ++++++++++++++++++++++++ Commands/WarmDeviceDetectorCache.php | 2 +- phpstan.neon | 21 ++++++ 6 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 .git-hooks-matomo/.pre-push.swp create mode 100755 .git-hooks-matomo/pre-push create mode 100755 .git-hooks-matomo/pre-push.d/phpstan.sh create mode 100644 .github/workflows/phpstan.yml create mode 100644 phpstan.neon diff --git a/.git-hooks-matomo/.pre-push.swp b/.git-hooks-matomo/.pre-push.swp new file mode 100644 index 0000000000000000000000000000000000000000..100a8000a9b095b2775f6c25c27335fac5c8f0ea GIT binary patch literal 12288 zcmeI2-D(p-6vwB<3lXZK7v7#Gp|-+g+lo+Y6$C{iZCYxh7h20CJIRinomqEgllDgO z4ZIN_!Y2?!P%nHGA3z_#GusUTDHbo4Gw^3V&)09SLQZ$_QELOPH!rRpDT|3}mm+4}ljEim? zYYtOexGgnO`yD3+azhGN3&oTA`^$GP6^%~Vd zJw%;Boko2)%h)^ATht!v8EO~x6xBiPpypARQQy)3w<^x(5Q_#8AOb{y2oM1xKm>>Y z5g-CYfC&60f!G?!0GxPWvHVL0BhRzghq@0 zt?}Bb7}wa?ZN026s!h0Ib+q$|1m}f`MIK|$S>Z$G;S_60SMf#5gq85h8W3HJ55jHF zrm6A{Mv3YrAaV&^smuUEqqsy*4qTCCTH#ta(8@>$-j*R-{qj|KJyJdi9t#(C0*pw} zpvn=iux^nLiqM#jp_JAb1>}oD=w|7vv5Z`xv%QYKt5iaAA&HQ}QA*{Pt3zWcylrCy zu>G=zah#J(5yDP_?m}05aEq8VrWum2NNH_HW%VLQ|6b;96+g;s)Kw + + + +set -e + +ROOT_DIR="$(git rev-parse --show-toplevel)" + +for script in "$ROOT_DIR/.git-hooks-matomo/pre-push.d/"*.sh; do + [ -x "$script" ] && "$script" +done diff --git a/.git-hooks-matomo/pre-push.d/phpstan.sh b/.git-hooks-matomo/pre-push.d/phpstan.sh new file mode 100755 index 00000000..b57b53e9 --- /dev/null +++ b/.git-hooks-matomo/pre-push.d/phpstan.sh @@ -0,0 +1,69 @@ +#!/bin/bash + +# This hook is called with the following parameters: +# +# $1 -- Name of the remote to which the push is being done +# $2 -- URL to which the push is being done +# +# If pushing without using a named remote those arguments will be equal. +# +# Information about the commits which are being pushed is supplied as lines to +# the standard input in the form: +# +# + + + +### Check we're running in the context of a plugin and get helpful dir variables ### + +REPO_DIR="$(git rev-parse --show-toplevel)" +echo "Running pre-commit hook in repo: $REPO_DIR" + +if [[ "$REPO_DIR" =~ /plugins/(.*) ]]; then + PLUGIN_PATH="plugins/${BASH_REMATCH[1]}/" +else + echo "Not a plugin, not running any further checks" + exit 1 +fi +MATOMO_DIR=$(echo "$REPO_DIR" | sed -E 's|/plugins/.*$||') + + + +### Figure out how to run PHPStan - ddev or not. ### + +COMMAND="" +# Use local PHP if setup +if command -v php >/dev/null 2>&1; then + if [ -f "${MATOMO_DIR}/vendor/bin/phpstan" ]; then + COMMAND="${MATOMO_DIR}/vendor/bin/phpstan" + PLUGIN_PATH='' + fi +elif command -v ddev >/dev/null 2>&1; then + # Use ddev if setup (overridding local setup) + if [ -d "$MATOMO_DIR/.ddev" ]; then + cd "$MATOMO_DIR" || exit 1 + if ddev status 2>&1 > /dev/null; then + COMMAND="ddev exec phpstan" + fi + fi +fi +# If no command, exit +if [[ -z "$COMMAND" ]]; then + echo "No way to run phpstan found." + exit 1 +fi + + + +# Basic setup +cd "$REPO_DIR" +STATUS=0 + + +PHPSTAN_BASE_CONFIG=phpstan.neon +if [[ -f "$PHPSTAN_BASE_CONFIG" ]]; then + echo "Running PHPstan at a base level on all plugin files" + $COMMAND analyse -c ${PLUGIN_PATH}/${PHPSTAN_BASE_CONFIG} || STATUS=1 +fi + +exit $STATUS diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 00000000..5490cf1e --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,85 @@ +name: PHPStan check + +on: pull_request + +permissions: + actions: read + checks: read + contents: read + deployments: none + issues: read + packages: none + pull-requests: read + repository-projects: none + security-events: none + statuses: read + +env: + PLUGIN_NAME: DeviceDetectorCache + DEPENDENT_PLUGINS: + +jobs: + phpstan: + name: PHPStan + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + lfs: false + persist-credentials: false + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '7.2' + + - name: Check out github-action-tests repository + uses: actions/checkout@v4 + with: + repository: matomo-org/github-action-tests + ref: main + path: github-action-tests + + - name: checkout matomo for plugin builds + shell: bash + run: ${{ github.workspace }}/github-action-tests/scripts/bash/checkout_matomo.sh + env: + PLUGIN_NAME: ${{ env.PLUGIN_NAME }} + WORKSPACE: ${{ github.workspace }} + ACTION_PATH: ${{ github.workspace }}/github-action-tests + MATOMO_TEST_TARGET: maximum_supported_matomo + + - name: prepare setup + shell: bash + run: | + cd ${{ github.workspace }}/matomo + echo -e "composer install" + composer install --ignore-platform-reqs + + - name: checkout additional plugins + if: ${{ env.DEPENDENT_PLUGINS != '' }} + shell: bash + working-directory: ${{ github.workspace }}/matomo + run: ${{ github.workspace }}/github-action-tests/scripts/bash/checkout_dependent_plugins.sh + + env: + DEPENDENT_PLUGINS: ${{ env.DEPENDENT_PLUGINS }} + GITHUB_USER_TOKEN: ${{ secrets.TESTS_ACCESS_TOKEN || secrets.GITHUB_TOKEN }} + + - name: "Restore result cache" + uses: actions/cache/restore@v4 + with: + path: /tmp/phpstan # same as in phpstan.neon + key: "phpstan-result-cache-${{ github.run_id }}" + restore-keys: | + phpstan-result-cache- + + - name: PHPStan whole repo + id: phpstan-all + run: cd ${{ github.workspace }}/matomo && composer run phpstan -- -vvv -c plugins/${{ env.PLUGIN_NAME }}/phpstan.neon + + - name: "Save result cache" + uses: actions/cache/save@v4 + if: ${{ !cancelled() }} + with: + path: /tmp/phpstan # same as in phpstan.neon + key: "phpstan-result-cache-${{ github.run_id }}" diff --git a/Commands/WarmDeviceDetectorCache.php b/Commands/WarmDeviceDetectorCache.php index 336d9c49..681589f7 100644 --- a/Commands/WarmDeviceDetectorCache.php +++ b/Commands/WarmDeviceDetectorCache.php @@ -164,7 +164,7 @@ protected function doExecute(): int if ($i <= 10) { $this->log('Found user agent ' . $agent . ' count: ' . $val); } - CachedEntry::writeToCache($agent, []); + CachedEntry::writeToCache($agent); // sleep 2ms to let CPU do something else // this will make things about 10m slower for 200K entries but at least sudden CPU increase for instance // can be prevented when there are only few CPUs available diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 00000000..5aee8c76 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,21 @@ +parameters: + level: 0 + phpVersion: 70200 + tmpDir: /tmp/phpstan/DeviceDetectorCache/main + paths: + - . + excludePaths: + - tests/* + - github-action-tests + bootstrapFiles: + - ../../bootstrap-phpstan.php + universalObjectCratesClasses: + - Piwik\Config + - Piwik\View + - Piwik\ViewDataTable\Config + scanDirectories: + # ../../ does not actually seem to give us anything + # that ../plugins/ does not, but including it for + # completeness. It does not seem to slow down performance. + - . +