Skip to content

Commit d131791

Browse files
PHPStan [no_release]
Types are messed up in level 1, maybe tricky to safely fix.
1 parent a92e044 commit d131791

6 files changed

Lines changed: 199 additions & 1 deletion

File tree

.git-hooks-matomo/.pre-push.swp

12 KB
Binary file not shown.

.git-hooks-matomo/pre-push

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
# This hook is called with the following parameters:
4+
#
5+
# $1 -- Name of the remote to which the push is being done
6+
# $2 -- URL to which the push is being done
7+
#
8+
# If pushing without using a named remote those arguments will be equal.
9+
#
10+
# Information about the commits which are being pushed is supplied as lines to
11+
# the standard input in the form:
12+
#
13+
# <local ref> <local oid> <remote ref> <remote oid>
14+
15+
16+
17+
set -e
18+
19+
ROOT_DIR="$(git rev-parse --show-toplevel)"
20+
21+
for script in "$ROOT_DIR/.git-hooks-matomo/pre-push.d/"*.sh; do
22+
[ -x "$script" ] && "$script"
23+
done
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/bash
2+
3+
# This hook is called with the following parameters:
4+
#
5+
# $1 -- Name of the remote to which the push is being done
6+
# $2 -- URL to which the push is being done
7+
#
8+
# If pushing without using a named remote those arguments will be equal.
9+
#
10+
# Information about the commits which are being pushed is supplied as lines to
11+
# the standard input in the form:
12+
#
13+
# <local ref> <local oid> <remote ref> <remote oid>
14+
15+
16+
17+
### Check we're running in the context of a plugin and get helpful dir variables ###
18+
19+
REPO_DIR="$(git rev-parse --show-toplevel)"
20+
echo "Running pre-commit hook in repo: $REPO_DIR"
21+
22+
if [[ "$REPO_DIR" =~ /plugins/(.*) ]]; then
23+
PLUGIN_PATH="plugins/${BASH_REMATCH[1]}/"
24+
else
25+
echo "Not a plugin, not running any further checks"
26+
exit 1
27+
fi
28+
MATOMO_DIR=$(echo "$REPO_DIR" | sed -E 's|/plugins/.*$||')
29+
30+
31+
32+
### Figure out how to run PHPStan - ddev or not. ###
33+
34+
COMMAND=""
35+
# Use local PHP if setup
36+
if command -v php >/dev/null 2>&1; then
37+
if [ -f "${MATOMO_DIR}/vendor/bin/phpstan" ]; then
38+
COMMAND="${MATOMO_DIR}/vendor/bin/phpstan"
39+
PLUGIN_PATH=''
40+
fi
41+
elif command -v ddev >/dev/null 2>&1; then
42+
# Use ddev if setup (overridding local setup)
43+
if [ -d "$MATOMO_DIR/.ddev" ]; then
44+
cd "$MATOMO_DIR" || exit 1
45+
if ddev status 2>&1 > /dev/null; then
46+
COMMAND="ddev exec phpstan"
47+
fi
48+
fi
49+
fi
50+
# If no command, exit
51+
if [[ -z "$COMMAND" ]]; then
52+
echo "No way to run phpstan found."
53+
exit 1
54+
fi
55+
56+
57+
58+
# Basic setup
59+
cd "$REPO_DIR"
60+
STATUS=0
61+
62+
63+
PHPSTAN_BASE_CONFIG=phpstan.neon
64+
if [[ -f "$PHPSTAN_BASE_CONFIG" ]]; then
65+
echo "Running PHPstan at a base level on all plugin files"
66+
$COMMAND analyse -c ${PLUGIN_PATH}/${PHPSTAN_BASE_CONFIG} || STATUS=1
67+
fi
68+
69+
exit $STATUS

.github/workflows/phpstan.yml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
name: PHPStan check
2+
3+
on: pull_request
4+
5+
permissions:
6+
actions: read
7+
checks: read
8+
contents: read
9+
deployments: none
10+
issues: read
11+
packages: none
12+
pull-requests: read
13+
repository-projects: none
14+
security-events: none
15+
statuses: read
16+
17+
env:
18+
PLUGIN_NAME: DeviceDetectorCache
19+
DEPENDENT_PLUGINS:
20+
21+
jobs:
22+
phpstan:
23+
name: PHPStan
24+
runs-on: ubuntu-latest
25+
steps:
26+
- uses: actions/checkout@v4
27+
with:
28+
lfs: false
29+
persist-credentials: false
30+
- name: Setup PHP
31+
uses: shivammathur/setup-php@v2
32+
with:
33+
php-version: '7.2'
34+
35+
- name: Check out github-action-tests repository
36+
uses: actions/checkout@v4
37+
with:
38+
repository: matomo-org/github-action-tests
39+
ref: main
40+
path: github-action-tests
41+
42+
- name: checkout matomo for plugin builds
43+
shell: bash
44+
run: ${{ github.workspace }}/github-action-tests/scripts/bash/checkout_matomo.sh
45+
env:
46+
PLUGIN_NAME: ${{ env.PLUGIN_NAME }}
47+
WORKSPACE: ${{ github.workspace }}
48+
ACTION_PATH: ${{ github.workspace }}/github-action-tests
49+
MATOMO_TEST_TARGET: maximum_supported_matomo
50+
51+
- name: prepare setup
52+
shell: bash
53+
run: |
54+
cd ${{ github.workspace }}/matomo
55+
echo -e "composer install"
56+
composer install --ignore-platform-reqs
57+
58+
- name: checkout additional plugins
59+
if: ${{ env.DEPENDENT_PLUGINS != '' }}
60+
shell: bash
61+
working-directory: ${{ github.workspace }}/matomo
62+
run: ${{ github.workspace }}/github-action-tests/scripts/bash/checkout_dependent_plugins.sh
63+
64+
env:
65+
DEPENDENT_PLUGINS: ${{ env.DEPENDENT_PLUGINS }}
66+
GITHUB_USER_TOKEN: ${{ secrets.TESTS_ACCESS_TOKEN || secrets.GITHUB_TOKEN }}
67+
68+
- name: "Restore result cache"
69+
uses: actions/cache/restore@v4
70+
with:
71+
path: /tmp/phpstan # same as in phpstan.neon
72+
key: "phpstan-result-cache-${{ github.run_id }}"
73+
restore-keys: |
74+
phpstan-result-cache-
75+
76+
- name: PHPStan whole repo
77+
id: phpstan-all
78+
run: cd ${{ github.workspace }}/matomo && composer run phpstan -- -vvv -c plugins/${{ env.PLUGIN_NAME }}/phpstan.neon
79+
80+
- name: "Save result cache"
81+
uses: actions/cache/save@v4
82+
if: ${{ !cancelled() }}
83+
with:
84+
path: /tmp/phpstan # same as in phpstan.neon
85+
key: "phpstan-result-cache-${{ github.run_id }}"

Commands/WarmDeviceDetectorCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ protected function doExecute(): int
164164
if ($i <= 10) {
165165
$this->log('Found user agent ' . $agent . ' count: ' . $val);
166166
}
167-
CachedEntry::writeToCache($agent, []);
167+
CachedEntry::writeToCache($agent);
168168
// sleep 2ms to let CPU do something else
169169
// this will make things about 10m slower for 200K entries but at least sudden CPU increase for instance
170170
// can be prevented when there are only few CPUs available

phpstan.neon

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
parameters:
2+
level: 0
3+
phpVersion: 70200
4+
tmpDir: /tmp/phpstan/DeviceDetectorCache/main
5+
paths:
6+
- .
7+
excludePaths:
8+
- tests/*
9+
- github-action-tests
10+
bootstrapFiles:
11+
- ../../bootstrap-phpstan.php
12+
universalObjectCratesClasses:
13+
- Piwik\Config
14+
- Piwik\View
15+
- Piwik\ViewDataTable\Config
16+
scanDirectories:
17+
# ../../ does not actually seem to give us anything
18+
# that ../plugins/ does not, but including it for
19+
# completeness. It does not seem to slow down performance.
20+
- .
21+

0 commit comments

Comments
 (0)