Skip to content

Commit ebbbc48

Browse files
Adds PHPStan to Slack plugin (#2)
* Adds PHPStan to Slack plugin * Changed the level to 8 * Bumped phpstan level up to 5 Can bump the level that high with (almost) no errors. * Don't check github action with phpstan * Test changes for multiple channels * Reverted changes * Adds the multiple channel logic back * Fixes for pre-push hook to work locally * Changes to work on all branches and reduced the phpstan level to 5 for both created and updated * Force to 8 to ensure github action is working * test for GH action * More fixes around pre-push and reverted to level 5 * Changed level to 5 for update * Get correct branch for comparison * PHPStan work correctly with DDEV * Fixed context of cd for non ddev phpstan * Changes for pre-push to work for non-dev --------- Co-authored-by: James Hill <james-hill-matomo@users.noreply.github.com>
1 parent 35cd206 commit ebbbc48

6 files changed

Lines changed: 221 additions & 4 deletions

File tree

.git-hooks-matomo/pre-push

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
fi
42+
# Use ddev if setup (overridding local setup)
43+
if command -v ddev >/dev/null 2>&1; then
44+
if [ -d "$MATOMO_DIR/.ddev" ]; then
45+
cd "$MATOMO_DIR" || exit 1
46+
if ddev status 2>&1 > /dev/null; then
47+
COMMAND="ddev exec phpstan"
48+
fi
49+
fi
50+
fi
51+
# If no command, exit
52+
if [[ -z "$COMMAND" ]]; then
53+
echo "No way to run phpstan found."
54+
exit 1
55+
fi
56+
57+
58+
59+
# Basic setup
60+
cd "$REPO_DIR"
61+
STATUS=0
62+
63+
64+
65+
66+
### Run PHPStan on newly created files. ###
67+
68+
PHPSTAN_CREATED_CONFIG=phpstan/phpstan.created.neon
69+
MAIN_BRANCH='5.x-dev'
70+
if [[ -f "$PHPSTAN_CREATED_CONFIG" ]]; then
71+
CHANGED_FILES=$(git diff --name-only ${MAIN_BRANCH} --diff-filter=A | grep '\.php$' || true)
72+
if [ -z "$CHANGED_FILES" ]; then
73+
echo "No created PHP files"
74+
else
75+
echo "Running PHPstan at a very high level on new files"
76+
CHANGED_FILES=`echo "$CHANGED_FILES" | sed -e 's/^\(.*\)$/"\1"/' | xargs -I{} echo "${PLUGIN_PATH}{}"`
77+
echo "$CHANGED_FILES" | xargs $COMMAND analyse -c ${PLUGIN_PATH}${PHPSTAN_CREATED_CONFIG} || STATUS=1
78+
fi
79+
fi
80+
81+
82+
83+
### Run PHPStan on modified files. ###
84+
PHPSTAN_MODIFIED_CONFIG=phpstan/phpstan.modified.neon
85+
if [[ -f "$PHPSTAN_MODIFIED_CONFIG" ]]; then
86+
CHANGED_FILES=$(git diff --name-only ${MAIN_BRANCH} --diff-filter=CM | grep '\.php$' || true)
87+
if [ -z "$CHANGED_FILES" ]; then
88+
echo "No changed PHP files"
89+
else
90+
echo "Running PHPstan on modified files"
91+
CHANGED_FILES=`echo "$CHANGED_FILES" | sed -e 's/^\(.*\)$/"\1"/' | xargs -I{} echo "${PLUGIN_PATH}{}"`
92+
echo "$CHANGED_FILES" | xargs $COMMAND analyse -c ${PLUGIN_PATH}${PHPSTAN_MODIFIED_CONFIG} || STATUS=1
93+
fi
94+
fi
95+
96+
# Don't bother running the full check, as we check changes files already, and
97+
# can assume that the unchanged files don't need rechecking.
98+
#
99+
# Github will check this anyway.
100+
#
101+
# PHPSTAN_BASE_CONFIG=phpstan.neon
102+
# if [[ -f "$PHPSTAN_BASE_CONFIG" ]]; then
103+
# echo "Running PHPstan at a base level on all plugin files"
104+
# $COMMAND analyse -c ${PLUGIN_PATH}/${PHPSTAN_BASE_CONFIG} || STATUS=1
105+
# fi
106+
107+
exit $STATUS

.github/workflows/phpstan.yml

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

ScheduleReportSlack.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ class ScheduleReportSlack
3737
*/
3838
private $token;
3939

40-
/**
41-
* @var string
42-
*/
43-
4440
public function __construct(
4541
string $subject,
4642
string $fileName,

phpstan.neon

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
parameters:
2+
level: 5
3+
phpVersion: 70200
4+
tmpDir: /tmp/phpstan/Slack/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+

phpstan/phpstan.created.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
includes:
2+
- ../phpstan.neon
3+
parameters:
4+
level: 5
5+
tmpDir: /tmp/phpstan/Slack/created

phpstan/phpstan.modified.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
includes:
2+
- ../phpstan.neon
3+
parameters:
4+
level: 5
5+
tmpDir: /tmp/phpstan/Slack/modified

0 commit comments

Comments
 (0)