Skip to content

Commit 669cd13

Browse files
authored
Merge pull request #76 from matomo-org/spice-psr
Adds test for PHPCS
2 parents 241d8cf + 1c8546e commit 669cd13

11 files changed

Lines changed: 123 additions & 41 deletions

.github/workflows/phpcs.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: PHPCS 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+
jobs:
18+
phpcs:
19+
name: PHPCS
20+
runs-on: ubuntu-latest
21+
steps:
22+
- uses: actions/checkout@v4
23+
with:
24+
lfs: false
25+
persist-credentials: false
26+
- name: Setup PHP
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
php-version: '7.4'
30+
tools: cs2pr
31+
- name: Install dependencies
32+
run:
33+
composer init --name=matomo/devicedetectorcache --quiet;
34+
composer --no-plugins config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true -n;
35+
composer config repositories.matomo-coding-standards vcs https://github.com/matomo-org/matomo-coding-standards -n;
36+
composer require matomo-org/matomo-coding-standards:dev-master;
37+
composer install --dev --prefer-dist --no-progress --no-suggest
38+
- name: Check PHP code styles
39+
id: phpcs
40+
run: ./vendor/bin/phpcs --report-full --standard=phpcs.xml --report-checkstyle=./phpcs-report.xml
41+
- name: Show PHPCS results in PR
42+
if: ${{ always() && steps.phpcs.outcome == 'failure' }}
43+
run: cs2pr ./phpcs-report.xml --prepend-filename

Commands/WarmDeviceDetectorCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
class WarmDeviceDetectorCache extends ConsoleCommand
2020
{
21-
const COMMAND_NAME = 'device-detector-cache:warm-cache';
21+
public const COMMAND_NAME = 'device-detector-cache:warm-cache';
2222
/**
2323
* @var Configuration
2424
*/

Configuration.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313

1414
class Configuration
1515
{
16-
const KEY_NumEntriesToCache = 'num_cache_entries';
17-
const DEFAULT_NumEntriesToCache = 200000;
16+
public const KEY_NUM_ENTRIES_TO_CACHE = 'num_cache_entries';
17+
public const DEFAULT_NUM_ENTRIES_TO_CACHE = 200000;
1818

19-
const KEY_AccessLogRegex = 'access_log_regex';
20-
const DEFAULT_AccessLogRegex = '/^(\S+) (\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+) ([^\]]+)\] \"(\S+) (.*?) (\S+)\" (\S+) (\S+) "([^"]*)" "([^"]*)" (\d+)$/';
19+
public const KEY_ACCESS_LOG_REGEX = 'access_log_regex';
20+
public const DEFAULT_ACCESS_LOG_REGEX = '/^(\S+) (\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+) ([^\]]+)\] \"(\S+) (.*?) (\S+)\" (\S+) (\S+) "([^"]*)" "([^"]*)" (\d+)$/';
2121

22-
const KEY_AccessLogRegexMatchEntry = 'regex_match_entry';
23-
const DEFAULT_AccessLogRegexMatchEntry = 14;
22+
public const KEY_ACCESS_LOG_REGEX_MATCH_ENTRY = 'regex_match_entry';
23+
public const DEFAULT_ACCESS_LOG_REGEX_MATCH_ENTRY = 14;
2424

25-
const KEY_AccessLogPath = 'access_log_path';
26-
const DEFAULT_AccessLogPath = '/var/log/httpd/access_log';
25+
public const KEY_ACCESS_LOG_PATH = 'access_log_path';
26+
public const DEFAULT_ACCESS_LOG_PATH = '/var/log/httpd/access_log';
2727

2828
public function install()
2929
{
@@ -36,17 +36,17 @@ public function install()
3636
$cache = $config->DeviceDetectorCache;
3737

3838
// we make sure to set a value only if none has been configured yet, eg in common config.
39-
if (empty($cache[self::KEY_NumEntriesToCache])) {
40-
$cache[self::KEY_NumEntriesToCache] = self::DEFAULT_NumEntriesToCache;
39+
if (empty($cache[self::KEY_NUM_ENTRIES_TO_CACHE])) {
40+
$cache[self::KEY_NUM_ENTRIES_TO_CACHE] = self::DEFAULT_NUM_ENTRIES_TO_CACHE;
4141
}
42-
if (empty($cache[self::KEY_AccessLogPath])) {
43-
$cache[self::KEY_AccessLogPath] = self::DEFAULT_AccessLogPath;
42+
if (empty($cache[self::KEY_ACCESS_LOG_PATH])) {
43+
$cache[self::KEY_ACCESS_LOG_PATH] = self::DEFAULT_ACCESS_LOG_PATH;
4444
}
45-
if (empty($cache[self::KEY_AccessLogRegex])) {
46-
$cache[self::KEY_AccessLogRegex] = self::DEFAULT_AccessLogRegex;
45+
if (empty($cache[self::KEY_ACCESS_LOG_REGEX])) {
46+
$cache[self::KEY_ACCESS_LOG_REGEX] = self::DEFAULT_ACCESS_LOG_REGEX;
4747
}
48-
if (empty($cache[self::KEY_AccessLogRegexMatchEntry])) {
49-
$cache[self::KEY_AccessLogRegexMatchEntry] = self::DEFAULT_AccessLogRegexMatchEntry;
48+
if (empty($cache[self::KEY_ACCESS_LOG_REGEX_MATCH_ENTRY])) {
49+
$cache[self::KEY_ACCESS_LOG_REGEX_MATCH_ENTRY] = self::DEFAULT_ACCESS_LOG_REGEX_MATCH_ENTRY;
5050
}
5151

5252
$config->DeviceDetectorCache = $cache;
@@ -66,31 +66,31 @@ public function uninstall()
6666
*/
6767
public function getAccessLogPath()
6868
{
69-
return $this->getConfigValue(self::KEY_AccessLogPath, self::DEFAULT_AccessLogPath);
69+
return $this->getConfigValue(self::KEY_ACCESS_LOG_PATH, self::DEFAULT_ACCESS_LOG_PATH);
7070
}
7171

7272
/**
7373
* @return string
7474
*/
7575
public function getAccessLogRegex()
7676
{
77-
return $this->getConfigValue(self::KEY_AccessLogRegex, self::DEFAULT_AccessLogRegex);
77+
return $this->getConfigValue(self::KEY_ACCESS_LOG_REGEX, self::DEFAULT_ACCESS_LOG_REGEX);
7878
}
7979

8080
/**
8181
* @return string
8282
*/
8383
public function getRegexMatchEntry()
8484
{
85-
return (int)$this->getConfigValue(self::KEY_AccessLogRegexMatchEntry, self::DEFAULT_AccessLogRegexMatchEntry);
85+
return (int)$this->getConfigValue(self::KEY_ACCESS_LOG_REGEX_MATCH_ENTRY, self::DEFAULT_ACCESS_LOG_REGEX_MATCH_ENTRY);
8686
}
8787

8888
/**
8989
* @return string
9090
*/
9191
public function getNumEntriesToCache()
9292
{
93-
return (int)$this->getConfigValue(self::KEY_NumEntriesToCache, self::DEFAULT_NumEntriesToCache);
93+
return (int)$this->getConfigValue(self::KEY_NUM_ENTRIES_TO_CACHE, self::DEFAULT_NUM_ENTRIES_TO_CACHE);
9494
}
9595

9696
private function getConfig()

DeviceDetector/CachedBrowserParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ protected function parseBrowserFromUserAgent(): array
2929

3030
return parent::parseBrowserFromUserAgent();
3131
}
32-
}
32+
}

DeviceDetector/CachedOperatingSystemParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ protected function parseOsFromUserAgent(): array
2929

3030
return parent::parseOsFromUserAgent();
3131
}
32-
}
32+
}

Factory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Matomo - free/libre analytics platform
45
*
@@ -21,4 +22,4 @@ protected function getDeviceDetectionInfo($userAgent, array $clientHints = [])
2122

2223
return parent::getDeviceDetectionInfo($userAgent, $clientHints);
2324
}
24-
}
25+
}

phpcs.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="deviceDetectorCache" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
3+
4+
<description>Matomo Coding Standard for DeviceDetectorCache plugin</description>
5+
6+
<arg name="extensions" value="php" />
7+
8+
<file>.</file>
9+
10+
<exclude-pattern>tests/javascript/*</exclude-pattern>
11+
<exclude-pattern>*/vendor/*</exclude-pattern>
12+
13+
<rule ref="Matomo"></rule>
14+
15+
<rule ref="Generic.Files.LineLength">
16+
<properties>
17+
<property name="lineLimit" value="250" />
18+
</properties>
19+
<exclude-pattern>tests/*</exclude-pattern>
20+
</rule>
21+
22+
<rule ref="Squiz.Classes.ValidClassName.NotCamelCaps">
23+
<!-- Classnames for our update files don't match PascalCase, this can't be changed easily -->
24+
<exclude-pattern>Updates/*</exclude-pattern>
25+
</rule>
26+
27+
<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
28+
<!-- Allow using method name without camel caps in tests as long as some methods are named test_* -->
29+
<exclude-pattern>tests/*</exclude-pattern>
30+
</rule>
31+
32+
<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
33+
<!-- Allow using multiple classes in one file for tests -->
34+
<exclude-pattern>tests/*</exclude-pattern>
35+
</rule>
36+
</ruleset>

tests/Integration/CachedEntryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public function testConstructSomeValues()
4646
$values = [
4747
'bot' => 'testBot',
4848
'brand' => 'testBrand',
49-
'client'=> 'testClient',
50-
'device'=> 2,
51-
'model'=> 'testModel',
49+
'client' => 'testClient',
50+
'device' => 2,
51+
'model' => 'testModel',
5252
'os' => 'testOs',
5353
];
5454

tests/Integration/ConfigurationTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,59 +42,59 @@ public function test_shouldInstallConfig()
4242
$this->assertEquals([
4343
'num_cache_entries' => '200000',
4444
'access_log_path' => '/var/log/httpd/access_log',
45-
'access_log_regex' => Configuration::DEFAULT_AccessLogRegex,
45+
'access_log_regex' => Configuration::DEFAULT_ACCESS_LOG_REGEX,
4646
'regex_match_entry' => 14,
4747
], $configs);
4848
}
4949

5050
public function test_getRegexMatchEntry()
5151
{
52-
$this->assertSame(Configuration::DEFAULT_AccessLogRegexMatchEntry, $this->configuration->getRegexMatchEntry());
52+
$this->assertSame(Configuration::DEFAULT_ACCESS_LOG_REGEX_MATCH_ENTRY, $this->configuration->getRegexMatchEntry());
5353
}
5454

5555
public function test_getRegexMatchEntry_customValue()
5656
{
5757
Config::getInstance()->DeviceDetectorCache = [
58-
Configuration::KEY_AccessLogRegexMatchEntry => '5',
58+
Configuration::KEY_ACCESS_LOG_REGEX_MATCH_ENTRY => '5',
5959
];
6060
$this->assertEquals(5, $this->configuration->getRegexMatchEntry());
6161
}
6262

6363
public function test_getAccessLogPath()
6464
{
65-
$this->assertSame(Configuration::DEFAULT_AccessLogPath, $this->configuration->getAccessLogPath());
65+
$this->assertSame(Configuration::DEFAULT_ACCESS_LOG_PATH, $this->configuration->getAccessLogPath());
6666
}
6767

6868
public function test_getAccessLogPath_customValue()
6969
{
7070
Config::getInstance()->DeviceDetectorCache = [
71-
Configuration::KEY_AccessLogPath => '/var/log/foo',
71+
Configuration::KEY_ACCESS_LOG_PATH => '/var/log/foo',
7272
];
7373
$this->assertEquals('/var/log/foo', $this->configuration->getAccessLogPath());
7474
}
7575

7676
public function test_getNumEntriesToCache()
7777
{
78-
$this->assertSame(Configuration::DEFAULT_NumEntriesToCache, $this->configuration->getNumEntriesToCache());
78+
$this->assertSame(Configuration::DEFAULT_NUM_ENTRIES_TO_CACHE, $this->configuration->getNumEntriesToCache());
7979
}
8080

8181
public function test_getNumEntriesToCache_customValue()
8282
{
8383
Config::getInstance()->DeviceDetectorCache = [
84-
Configuration::KEY_NumEntriesToCache => '145',
84+
Configuration::KEY_NUM_ENTRIES_TO_CACHE => '145',
8585
];
8686
$this->assertEquals(145, $this->configuration->getNumEntriesToCache());
8787
}
8888

8989
public function test_getAccessLogRegex()
9090
{
91-
$this->assertSame(Configuration::DEFAULT_AccessLogRegex, $this->configuration->getAccessLogRegex());
91+
$this->assertSame(Configuration::DEFAULT_ACCESS_LOG_REGEX, $this->configuration->getAccessLogRegex());
9292
}
9393

9494
public function test_getAccessLogRegex_customValue()
9595
{
9696
Config::getInstance()->DeviceDetectorCache = [
97-
Configuration::KEY_AccessLogRegex => '(.*)',
97+
Configuration::KEY_ACCESS_LOG_REGEX => '(.*)',
9898
];
9999
$this->assertEquals('(.*)', $this->configuration->getAccessLogRegex());
100100
}

tests/Integration/WarmDeviceDetectorCacheTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Matomo - free/libre analytics platform
45
*
@@ -26,7 +27,7 @@ class WarmDeviceDetectorCacheTest extends ConsoleCommandTestCase
2627
public function setUp(): void
2728
{
2829
parent::setUp();
29-
CachedEntry::setCacheDir(PIWIK_DOCUMENT_ROOT. '/tmp/devicecachetests/');
30+
CachedEntry::setCacheDir(PIWIK_DOCUMENT_ROOT . '/tmp/devicecachetests/');
3031
CachedEntry::clearCacheDir();
3132
}
3233

@@ -41,15 +42,15 @@ private function setAccessLogFile($file)
4142
{
4243
$config = \Piwik\Config::getInstance();
4344
$d = $config->DeviceDetectorCache;
44-
$d[Configuration::KEY_AccessLogPath] = $file;
45+
$d[Configuration::KEY_ACCESS_LOG_PATH] = $file;
4546
$config->DeviceDetectorCache = $d;
4647
}
4748

4849
private function setCountProcessNumEntries($numEntries)
4950
{
5051
$config = \Piwik\Config::getInstance();
5152
$d = $config->DeviceDetectorCache;
52-
$d[Configuration::KEY_NumEntriesToCache] = $numEntries;
53+
$d[Configuration::KEY_NUM_ENTRIES_TO_CACHE] = $numEntries;
5354
$config->DeviceDetectorCache = $d;
5455
}
5556

@@ -203,4 +204,4 @@ private function assertUserAgentWrittenToFile($userAgent)
203204
$this->assertEquals($deviceDetectionParsed->getModel(), $deviceDetectionFromFile->getModel());
204205
$this->assertEquals($deviceDetectionParsed->getOs(), $deviceDetectionFromFile->getOs());
205206
}
206-
}
207+
}

0 commit comments

Comments
 (0)