Skip to content

Commit

Permalink
Merge pull request #63 from innocraft/spice-psr
Browse files Browse the repository at this point in the history
Adds test for PHPCS
  • Loading branch information
snake14 authored Oct 20, 2024
2 parents d869d5f + 1e10a93 commit 06579b4
Show file tree
Hide file tree
Showing 23 changed files with 134 additions and 46 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/phpcs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: PHPCS 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

jobs:
phpcs:
name: PHPCS
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.4'
tools: cs2pr
- name: Install dependencies
run:
composer init --name=matomo/customtranslations --quiet;
composer --no-plugins config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true -n;
composer config repositories.matomo-coding-standards vcs https://github.com/matomo-org/matomo-coding-standards -n;
composer require matomo-org/matomo-coding-standards:dev-master;
composer install --dev --prefer-dist --no-progress --no-suggest
- name: Check PHP code styles
id: phpcs
run: ./vendor/bin/phpcs --report-full --standard=phpcs.xml --report-checkstyle=./phpcs-report.xml
- name: Show PHPCS results in PR
if: ${{ always() && steps.phpcs.outcome == 'failure' }}
run: cs2pr ./phpcs-report.xml --prepend-filename
2 changes: 1 addition & 1 deletion API.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* InnoCraft - the company of the makers of Matomo Analytics, the free/libre analytics platform
*
Expand Down Expand Up @@ -102,5 +103,4 @@ public function getTranslatableTypes()

return $metadata;
}

}
1 change: 1 addition & 0 deletions Controller.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* InnoCraft - the company of the makers of Matomo Analytics, the free/libre analytics platform
*
Expand Down
2 changes: 1 addition & 1 deletion CustomTranslations.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* InnoCraft - the company of the makers of Matomo Analytics, the free/libre analytics platform
*
Expand Down Expand Up @@ -78,5 +79,4 @@ public function updateEvents(&$returnedValue, $extraInfo)
$returnedValue = $type->translate($returnedValue, $method, $extraInfo);
}
}

}
3 changes: 2 additions & 1 deletion Dao/TranslationsDao.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* InnoCraft - the company of the makers of Matomo Analytics, the free/libre analytics platform
*
Expand All @@ -12,7 +13,7 @@

class TranslationsDao
{
CONST OPTION_LANG_PREFIX = 'CustomTranslations_lang_';
public const OPTION_LANG_PREFIX = 'CustomTranslations_lang_';

public function get($typeId, $lang)
{
Expand Down
4 changes: 2 additions & 2 deletions DataTable/Filter/RenameLabelFilter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* InnoCraft - the company of the makers of Matomo Analytics, the free/libre analytics platform
*
Expand Down Expand Up @@ -52,7 +53,6 @@ private function renameLabels($table, $level)
}

foreach ($table->getRowsWithoutSummaryRow() as $row) {

$label = $row->getColumn('label');
if ($label && isset($map[$label])) {
$row->setColumn('label', $map[$label]);
Expand All @@ -65,4 +65,4 @@ private function renameLabels($table, $level)
}
}
}
}
}
2 changes: 1 addition & 1 deletion Menu.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* InnoCraft - the company of the makers of Matomo Analytics, the free/libre analytics platform
*
Expand All @@ -13,7 +14,6 @@

class Menu extends \Piwik\Plugin\Menu
{

public function configureAdminMenu(MenuAdmin $menu)
{
if (Piwik::hasUserSuperUserAccess()) {
Expand Down
11 changes: 6 additions & 5 deletions TranslationTypes/CustomDimensionEntity.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* InnoCraft - the company of the makers of Matomo Analytics, the free/libre analytics platform
*
Expand All @@ -14,7 +15,7 @@

class CustomDimensionEntity extends TranslationType
{
const ID = 'customDimensionEntity';
public const ID = 'customDimensionEntity';

public function getName()
{
Expand All @@ -35,9 +36,10 @@ public function getTranslationKeys()

public function translate($returnedValue, $method, $extraInfo)
{
if ($method === 'CustomDimensions.getConfiguredCustomDimensions'
&& is_array($returnedValue)) {

if (
$method === 'CustomDimensions.getConfiguredCustomDimensions'
&& is_array($returnedValue)
) {
if ($this->isRequestingAPIwithinUI('CustomDimensions.getConfiguredCustomDimensions')) {
// make sure in manage custom dimensions the correct names are shown
return $returnedValue;
Expand All @@ -53,5 +55,4 @@ public function translate($returnedValue, $method, $extraInfo)
}
return $returnedValue;
}

}
4 changes: 2 additions & 2 deletions TranslationTypes/CustomDimensionLabel.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* InnoCraft - the company of the makers of Matomo Analytics, the free/libre analytics platform
*
Expand All @@ -13,7 +14,7 @@

class CustomDimensionLabel extends TranslationType
{
const ID = 'customDimensionLabel';
public const ID = 'customDimensionLabel';

public function getName()
{
Expand All @@ -38,5 +39,4 @@ public function translate($returnedValue, $method, $extraInfo)
}
return $returnedValue;
}

}
24 changes: 14 additions & 10 deletions TranslationTypes/CustomReportEntity.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* InnoCraft - the company of the makers of Matomo Analytics, the free/libre analytics platform
*
Expand All @@ -13,12 +14,11 @@
use Piwik\DataTable\DataTableInterface;
use Piwik\Db;
use Piwik\Piwik;
use Piwik\Plugin;
use Piwik\Plugins\CustomTranslations\Dao\TranslationsDao;

class CustomReportEntity extends TranslationType
{
const ID = 'customReportEntity';
public const ID = 'customReportEntity';

/**
* @var CustomDimensionLabel
Expand Down Expand Up @@ -55,9 +55,10 @@ public function getTranslationKeys()

public function translate($returnedValue, $method, $extraInfo)
{
if ($method === 'CustomReports.getConfiguredReports'
&& is_array($returnedValue)) {

if (
$method === 'CustomReports.getConfiguredReports'
&& is_array($returnedValue)
) {
if ($this->isRequestingAPIwithinUI('CustomReports.getConfiguredReports')) {
// make sure in manage reports screen we show original name... but not when API is called independently
return $returnedValue;
Expand All @@ -71,8 +72,10 @@ public function translate($returnedValue, $method, $extraInfo)
}
}

if ($method === 'CustomReports.getConfiguredReport'
&& is_array($returnedValue)) {
if (
$method === 'CustomReports.getConfiguredReport'
&& is_array($returnedValue)
) {
if ($this->isRequestingAPIwithinUI('CustomReports.getConfiguredReport')) {
return $returnedValue;
}
Expand All @@ -83,8 +86,10 @@ public function translate($returnedValue, $method, $extraInfo)
}
}

if ($method === 'CustomReports.getCustomReport'
&& $returnedValue instanceof DataTableInterface) {
if (
$method === 'CustomReports.getCustomReport'
&& $returnedValue instanceof DataTableInterface
) {
$params = ['idSite' => $extraInfo['parameters']['idSite'], 'idCustomReport' => $extraInfo['parameters']['idCustomReport']];
$customReport = Request::processRequest('CustomReports.getConfiguredReport', $params, []);

Expand All @@ -108,5 +113,4 @@ public function translate($returnedValue, $method, $extraInfo)

return $returnedValue;
}

}
5 changes: 2 additions & 3 deletions TranslationTypes/DashboardEntity.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* InnoCraft - the company of the makers of Matomo Analytics, the free/libre analytics platform
*
Expand All @@ -14,7 +15,7 @@

class DashboardEntity extends TranslationType
{
const ID = 'dashboardEntity';
public const ID = 'dashboardEntity';

public function getName()
{
Expand All @@ -36,7 +37,6 @@ public function getTranslationKeys()
public function translate($returnedValue, $method, $extraInfo)
{
if ($method === 'Dashboard.getDashboards' && is_array($returnedValue)) {

if ($this->isRequestingAPIwithinUI('Dashboard.getDashboards')) {
// we make sure that when using renaming Dashboard feature, to show the original dashboard name, and also
// that when moving around a dashboard, to keep the original name
Expand All @@ -52,5 +52,4 @@ public function translate($returnedValue, $method, $extraInfo)
}
return $returnedValue;
}

}
4 changes: 2 additions & 2 deletions TranslationTypes/EventLabel.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* InnoCraft - the company of the makers of Matomo Analytics, the free/libre analytics platform
*
Expand All @@ -13,7 +14,7 @@

class EventLabel extends TranslationType
{
const ID = 'eventLabel';
public const ID = 'eventLabel';

public function getName()
{
Expand All @@ -38,5 +39,4 @@ public function translate($returnedValue, $method, $extraInfo)
}
return $returnedValue;
}

}
10 changes: 6 additions & 4 deletions TranslationTypes/TranslationType.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* InnoCraft - the company of the makers of Matomo Analytics, the free/libre analytics platform
*
Expand All @@ -12,11 +13,10 @@
use Piwik\Container\StaticContainer;
use Piwik\DataTable\DataTableInterface;
use Piwik\Plugins\CustomTranslations\Dao\TranslationsDao;
use Piwik\Translate;

abstract class TranslationType
{
const ID = '';
public const ID = '';

/**
* @var TranslationsDao
Expand Down Expand Up @@ -55,9 +55,11 @@ public function getTranslationKeys()
protected function isRequestingAPIwithinUI($method)
{
if (Request::getRootApiRequestMethod() === $method) {
if (!empty($_SERVER['HTTP_REFERER'])
if (
!empty($_SERVER['HTTP_REFERER'])
&& strpos($_SERVER['HTTP_REFERER'], 'module=') !== false
&& strpos($_SERVER['HTTP_REFERER'], 'action=') !== false) {
&& strpos($_SERVER['HTTP_REFERER'], 'action=') !== false
) {
// the API method was requested from within the UI... in this case we usually don't want to apply
// the renamings... but we want to apply it when the API was requested directly
return true;
Expand Down
1 change: 1 addition & 0 deletions TranslationTypes/TranslationTypeProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* InnoCraft - the company of the makers of Matomo Analytics, the free/libre analytics platform
*
Expand Down
36 changes: 36 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0"?>
<ruleset name="customTranslations" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">

<description>Matomo Coding Standard for CustomTranslations plugin</description>

<arg name="extensions" value="php" />

<file>.</file>

<exclude-pattern>tests/javascript/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>

<rule ref="Matomo"></rule>

<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="250" />
</properties>
<exclude-pattern>tests/*</exclude-pattern>
</rule>

<rule ref="Squiz.Classes.ValidClassName.NotCamelCaps">
<!-- Classnames for our update files don't match PascalCase, this can't be changed easily -->
<exclude-pattern>Updates/*</exclude-pattern>
</rule>

<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
<!-- Allow using method name without camel caps in tests as long as some methods are named test_* -->
<exclude-pattern>tests/*</exclude-pattern>
</rule>

<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
<!-- Allow using multiple classes in one file for tests -->
<exclude-pattern>tests/*</exclude-pattern>
</rule>
</ruleset>
4 changes: 2 additions & 2 deletions tests/Fixtures/CustomTranslationsFixture.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* InnoCraft - the company of the makers of Matomo Analytics, the free/libre analytics platform
*
Expand Down Expand Up @@ -195,5 +196,4 @@ public static function hasCustomReports()
{
return file_exists(PIWIK_DOCUMENT_ROOT . '/plugins/CustomReports/CustomReports.php');
}

}
}
1 change: 1 addition & 0 deletions tests/Integration/ApiTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* InnoCraft - the company of the makers of Matomo Analytics, the free/libre analytics platform
*
Expand Down
Loading

0 comments on commit 06579b4

Please sign in to comment.