Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .git-hooks-matomo/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/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:
#
# <local ref> <local oid> <remote ref> <remote oid>



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
69 changes: 69 additions & 0 deletions .git-hooks-matomo/pre-push.d/phpstan.sh
Original file line number Diff line number Diff line change
@@ -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:
#
# <local ref> <local oid> <remote ref> <remote oid>



### 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
85 changes: 85 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -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: CustomVariables
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 }}"
2 changes: 1 addition & 1 deletion API.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ public function getUsagesOfSlots($idSite)
'page' => array_fill(1, $numVars, array()),
);

/** @var DataTable $customVarUsages */
$today = StaticContainer::get('CustomVariables.today');
$date = '2008-12-12,' . $today;
/** @var DataTable $customVarUsages */
$customVarUsages = Request::processRequest(
'CustomVariables.getCustomVariables',
array('idSite' => $idSite, 'period' => 'range', 'date' => $date,
Expand Down
1 change: 1 addition & 0 deletions RecordBuilders/CustomVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ protected function aggregateCustomVariable(DataTable $record, array &$metadata,
$query = $logAggregator->queryActionsByDimension($dimensions, $where, $additionalSelects);
$this->aggregateFromActions($record, $metadata, $metadataFlat, $query, $keyField, $valueField);

// @phpstan-ignore-next-line Ternary operator condition is always true.
$query = version_compare(Version::VERSION, '5.2.0-b6', '>=')
? $logAggregator->queryConversionsByDimension($dimensions, $where, [], [], false, false, true)
: $logAggregator->queryConversionsByDimension($dimensions, $where);
Expand Down
12 changes: 5 additions & 7 deletions Reports/GetCustomVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ class GetCustomVariables extends Base
protected function init()
{
parent::init();
$this->dimension = new CustomVariableName();
$this->name = Piwik::translate('CustomVariables_CustomVariables');
$this->dimension = new CustomVariableName();
$this->name = Piwik::translate('CustomVariables_CustomVariables');
$this->documentation = Piwik::translate(
'CustomVariables_CustomVariablesReportDocumentation',
array('<br />', '<a href="https://matomo.org/docs/custom-variables/" rel="noreferrer noopener" target="_blank">', '</a>')
);
$this->actionToLoadSubTables = 'getCustomVariablesValuesFromNameId';
$this->order = 10;

$this->subcategoryId = 'CustomVariables_CustomVariables';
$this->subcategoryId = 'CustomVariables_CustomVariables';
$this->hasGoalMetrics = true;
}

Expand All @@ -38,7 +38,7 @@ public function configureView(ViewDataTable $view)
$view->config->columns_to_display = array('label', 'nb_actions', 'nb_visits');
$view->config->addTranslation('label', Piwik::translate('CustomVariables_ColumnCustomVariableName'));
$view->requestConfig->filter_sort_column = 'nb_actions';
$view->requestConfig->filter_sort_order = 'desc';
$view->requestConfig->filter_sort_order = 'desc';

$that = $this;
$view->config->filters[] = function (DataTable $table) use ($view, $that) {
Expand Down Expand Up @@ -71,9 +71,7 @@ public function getFooterMessageExplanationMissingMetrics()
// no footer message for subtables
$out = '';
Piwik::postEvent('Template.afterCustomVariablesReport', array(&$out));
if (!empty($message)) {
$message .= $out;
}
$message .= $out;
}

return $message;
Expand Down
4 changes: 3 additions & 1 deletion Tracker/CustomVariablesRequestProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function processRequestParams(VisitProperties $visitProperties, Request $
}

$request->setMetadata('CustomVariables', 'visitCustomVariables', $visitorCustomVariables);
return false;
}

public function onNewVisit(VisitProperties $visitProperties, Request $request)
Expand All @@ -71,7 +72,7 @@ public function afterRequestProcessed(VisitProperties $visitProperties, Request
$action = $request->getMetadata('Actions', 'action');

if (empty($action) || !($action instanceof Action)) {
return;
return false;
}

$customVariables = self::getCustomVariablesInPageScope($request);
Expand All @@ -84,6 +85,7 @@ public function afterRequestProcessed(VisitProperties $visitProperties, Request
$action->setCustomField($field, $value);
}
}
return false;
}

public static function getCustomVariablesInVisitScope(Request $request)
Expand Down
21 changes: 21 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
parameters:
level: 4
phpVersion: 70200
tmpDir: /tmp/phpstan/CustomVariables/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.
- .

Loading