Skip to content

Commit 832db07

Browse files
Merge pull request #266 from matomo-org/spice-psr
Adds test for PHPCS
2 parents 9ddbab2 + d09ab4e commit 832db07

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+306
-176
lines changed

.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/queuedtracking --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/LockStatus.php

Lines changed: 1 addition & 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
*
@@ -15,7 +16,6 @@
1516

1617
class LockStatus extends ConsoleCommand
1718
{
18-
1919
protected function configure()
2020
{
2121
$this->setName('queuedtracking:lock-status');

Commands/Monitor.php

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Matomo - free/libre analytics platform
45
*
@@ -15,7 +16,6 @@
1516

1617
class Monitor extends ConsoleCommand
1718
{
18-
1919
protected function configure()
2020
{
2121
$this->setName('queuedtracking:monitor');
@@ -38,7 +38,7 @@ protected function doExecute(): int
3838
}
3939

4040
$output->write(str_repeat("\r\n", 100));
41-
$output->write("\e[".(100)."A");
41+
$output->write("\e[" . (100) . "A");
4242

4343
$iterations = $this->getIterationsFromArg();
4444
if ($iterations !== null) {
@@ -63,22 +63,25 @@ protected function doExecute(): int
6363
}
6464

6565
$output->writeln(sprintf('Up to <info>%d</> workers will be used', $manager->getNumberOfAvailableQueues()));
66-
$output->writeln(sprintf('Processor will start once there are at least <info>%s</> request sets in the queue',
67-
$manager->getNumberOfRequestsToProcessAtSameTime()));
66+
$output->writeln(sprintf(
67+
'Processor will start once there are at least <info>%s</> request sets in the queue',
68+
$manager->getNumberOfRequestsToProcessAtSameTime()
69+
));
6870
$iterationCount = 0;
69-
71+
7072
$qCurrentPage = 1;
7173
$qCount = count($queues);
7274
$qPerPAge = min(max($this->getPerPageFromArg(), 1), $qCount);
7375
$qPageCount = ceil($qCount / $qPerPAge);
74-
75-
readline_callback_handler_install('', function() {});
76-
stream_set_blocking (STDIN, false);
76+
77+
readline_callback_handler_install('', function () {
78+
});
79+
stream_set_blocking(STDIN, false);
7780

7881
$output->writeln(str_repeat("-", 30));
79-
$output->writeln("<fg=black;bg=white;options=bold>".str_pad(" Q INDEX", 10).str_pad(" | REQUEST SETS", 20)."</>");
82+
$output->writeln("<fg=black;bg=white;options=bold>" . str_pad(" Q INDEX", 10) . str_pad(" | REQUEST SETS", 20) . "</>");
8083
$output->writeln(str_repeat("-", 30));
81-
84+
8285
$lastStatsTimer = microtime(true) - 2;
8386
$lastSumInQueue = false;
8487
$diffSumInQueue = 0;
@@ -87,13 +90,12 @@ protected function doExecute(): int
8790
$output->write(str_repeat("\r\n", $qPerPAge + 5));
8891

8992
while (1) {
90-
if (microtime(true) - $lastStatsTimer >= 2 || $keyPressed != "")
91-
{
92-
$output->write("\e[".($qPerPAge + 5)."A");
93+
if (microtime(true) - $lastStatsTimer >= 2 || $keyPressed != "") {
94+
$output->write("\e[" . ($qPerPAge + 5) . "A");
9395

9496
$qCurrentPage = min(max($qCurrentPage, 1), $qPageCount);
9597
$memory = $backend->getMemoryStats(); // I know this will only work with redis currently as it is not defined in backend interface etc. needs to be refactored once we add another backend
96-
98+
9799
$sumInQueue = 0;
98100
foreach ($queues as $sumQ) {
99101
$sumInQueue += $sumQ->getNumberOfRequestSetsInQueue();
@@ -102,31 +104,34 @@ protected function doExecute(): int
102104
if ($lastSumInQueue !== false) {
103105
$diffSumInQueue = $lastSumInQueue - $sumInQueue;
104106
$diffRps = round($diffSumInQueue / (microtime(true) - $lastStatsTimer), 2);
105-
$diffSumInQueue = $diffSumInQueue < 0 ? "<fg=red;options=bold>".abs($diffRps)."</>" : "<fg=green;options=bold>{$diffRps}</>";
107+
$diffSumInQueue = $diffSumInQueue < 0 ? "<fg=red;options=bold>" . abs($diffRps) . "</>" : "<fg=green;options=bold>{$diffRps}</>";
106108
}
107-
109+
108110
$numInQueue = 0;
109111
for ($idxPage = 0; $idxPage < $qPerPAge; $idxPage++) {
110112
$idx = ($qCurrentPage - 1) * $qPerPAge + $idxPage;
111113
if (isset($queues[$idx])) {
112114
$q = $queues[$idx]->getNumberOfRequestSetsInQueue();
113115
$numInQueue += (int)$q;
114-
$output->writeln(str_pad($idx, 10, " ", STR_PAD_LEFT)." | ".str_pad(number_format($q), 16, " ", STR_PAD_LEFT));
116+
$output->writeln(str_pad($idx, 10, " ", STR_PAD_LEFT) . " | " . str_pad(number_format($q), 16, " ", STR_PAD_LEFT));
115117
} else {
116-
$output->writeln(str_pad("", 10)." | ".str_pad("", 16));
118+
$output->writeln(str_pad("", 10) . " | " . str_pad("", 16));
117119
}
118120
}
119-
121+
120122
$output->writeln(str_repeat("-", 30));
121-
$output->writeln("<fg=black;bg=white;options=bold>".str_pad(" ".($qCount)." Q", 10)." | ".str_pad(number_format($sumInQueue)." R", 16)."</>");
123+
$output->writeln("<fg=black;bg=white;options=bold>" . str_pad(" " . ($qCount) . " Q", 10) . " | " . str_pad(number_format($sumInQueue) . " R", 16) . "</>");
122124
$output->writeln(str_repeat("-", 30));
123125
$output->writeln(sprintf(
124-
"Q [%s-%s] | <info>page %s/%s</> | <comment>press (0-9.,q) or arrow(L,R,U,D)</> | diff/sec %s \n".
125-
"%s used memory (%s peak). <info>%d</> workers active.".str_repeat(" ", 15),
126-
($idx - $qPerPAge + 1),
127-
$idx, $qCurrentPage, $qPageCount, $diffSumInQueue,
128-
$memory['used_memory_human'] ?? 'Unknown',
129-
$memory['used_memory_peak_human'] ?? 'Unknown',
126+
"Q [%s-%s] | <info>page %s/%s</> | <comment>press (0-9.,q) or arrow(L,R,U,D)</> | diff/sec %s \n" .
127+
"%s used memory (%s peak). <info>%d</> workers active." . str_repeat(" ", 15),
128+
($idx - $qPerPAge + 1),
129+
$idx,
130+
$qCurrentPage,
131+
$qPageCount,
132+
$diffSumInQueue,
133+
$memory['used_memory_human'] ?? 'Unknown',
134+
$memory['used_memory_peak_human'] ?? 'Unknown',
130135
$lock->getNumberOfAcquiredLocks()
131136
));
132137

@@ -145,26 +150,35 @@ protected function doExecute(): int
145150
$keyPressed = strlen($keyStroke) == 3 ? $keyStroke[2] : (strlen($keyStroke) > 0 ? $keyStroke[0] : "");
146151
if ($keyPressed != "" and in_array($keyPressed, array(".", ",", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "q"))) {
147152
switch ($keyPressed) {
148-
case "0": case "1": case "2": case "3": case "4":
149-
case "5": case "6": case "7": case "8": case "9":
150-
$keyPressed = $keyPressed != "0" ? $keyPressed : "10";
151-
$qCurrentPage = floor(($qCurrentPage - 0.1) / 10) * 10 + (int)$keyPressed; break;
152-
case "C":
153+
case "0":
154+
case "1":
155+
case "2":
156+
case "3":
157+
case "4":
158+
case "5":
159+
case "6":
160+
case "7":
161+
case "8":
162+
case "9":
163+
$keyPressed = $keyPressed != "0" ? $keyPressed : "10";
164+
$qCurrentPage = floor(($qCurrentPage - 0.1) / 10) * 10 + (int)$keyPressed;
165+
break;
166+
case "C":
153167
$qCurrentPage++;
154168
break;
155-
case "D":
169+
case "D":
156170
$qCurrentPage--;
157171
break;
158-
case "A":
172+
case "A":
159173
$qCurrentPage += 10;
160174
break;
161-
case "B":
175+
case "B":
162176
$qCurrentPage -= 10;
163177
break;
164-
case ",":
178+
case ",":
165179
$qCurrentPage = 1;
166180
break;
167-
case ".":
181+
case ".":
168182
$qCurrentPage = $qPageCount;
169183
break;
170184
case "q":
@@ -204,7 +218,7 @@ private function getIterationsFromArg()
204218
* Loads the `perpage` argument from the commands arguments.
205219
*
206220
* @return int|null
207-
*/
221+
*/
208222
private function getPerPageFromArg()
209223
{
210224
$perPage = $this->getInput()->getOption('perpage');

Commands/PrintQueuedRequests.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Matomo - free/libre analytics platform
45
*
@@ -15,7 +16,6 @@
1516

1617
class PrintQueuedRequests extends ConsoleCommand
1718
{
18-
1919
protected function configure()
2020
{
2121
$this->setName('queuedtracking:print-queued-requests');
@@ -54,7 +54,6 @@ protected function doExecute(): int
5454
$output->writeln(var_export($requests, 1));
5555

5656
$output->writeln(sprintf('<info>These were the requests of queue %s. Use <comment>--queue-id=%s</comment> to print only information for this queue.</info>', $thisQueueId, $thisQueueId));
57-
5857
}
5958

6059
return self::SUCCESS;

Commands/Process.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Matomo - free/libre analytics platform
45
*
@@ -20,16 +21,26 @@
2021

2122
class Process extends ConsoleCommand
2223
{
23-
2424
protected function configure()
2525
{
2626
$this->setName('queuedtracking:process');
27-
$this->addRequiredValueOption('queue-id', null, 'If set, will only work on that specific queue. For example "0" or "1" (if there are multiple queues). Not recommended when only one worker is in use. If for example 4 workers are in use, you may want to use 0, 1, 2, or 3.');
28-
$this->addRequiredValueOption('force-num-requests-process-at-once', null, 'If defined, it overwrites the setting of how many requests will be picked out of the queue and processed at once. Must be a number which is >= 1. By default, the configured value from the settings will be used. This can be useful for example if you want to process every single request within the queue. If otherwise a batch size of say 100 is configured, then there may be otherwise 99 requests left in the queue. It can be also useful for testing purposes.');
27+
$this->addRequiredValueOption(
28+
'queue-id',
29+
null,
30+
'If set, will only work on that specific queue. For example "0" or "1" (if there are multiple queues). Not recommended when only one worker is in use. If for example 4 workers are in use, you may want to use 0, 1, 2, or 3.'
31+
);
32+
$this->addRequiredValueOption(
33+
'force-num-requests-process-at-once',
34+
null,
35+
'If defined, it overwrites the setting of how many requests will be picked out of the queue and processed at once. Must be a number which is >= 1. By default, the configured value from the settings will be used.' .
36+
' This can be useful for example if you want to process every single request within the queue.' .
37+
' If otherwise a batch size of say 100 is configured, then there may be otherwise 99 requests left in the queue. It can be also useful for testing purposes.'
38+
);
2939
$this->addRequiredValueOption('cycle', 'c', 'The proccess will automatically loop for "n" cycle time(s), set "0" to infinite.', 1);
3040
$this->addRequiredValueOption('sleep', 's', 'Take a nap for "n" second(s) before recycle, minimum is 1 second.', 1);
3141
$this->addRequiredValueOption('delay', 'd', 'Delay before finished', 0);
32-
$this->setDescription('Processes all queued tracking requests in case there are enough requests in the queue and in case they are not already in process by another script. To keep track of the queue use the <comment>--verbose</comment> option or execute the <comment>queuedtracking:monitor</comment> command.');
42+
$this->setDescription('Processes all queued tracking requests in case there are enough requests in the queue and in case they are not already in process by another script. To keep track of the queue use the <comment>--verbose</comment>' .
43+
' option or execute the <comment>queuedtracking:monitor</comment> command.');
3344
}
3445

3546
/**
@@ -91,7 +102,7 @@ protected function doExecute(): int
91102
}
92103
$numberOfProcessCycle = (int)$numberOfProcessCycle;
93104
$infiniteCycle = $numberOfProcessCycle == 0;
94-
105+
95106
$delayedBeforeFinish = (int)$input->getOption('delay');
96107

97108
$napster = max(1, $input->getOption('sleep'));
@@ -156,7 +167,7 @@ protected function doExecute(): int
156167
if ($delayedBeforeFinish > 0) {
157168
sleep($delayedBeforeFinish);
158169
}
159-
170+
160171
return self::SUCCESS;
161172
}
162173

Commands/Test.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Matomo - free/libre analytics platform
45
*
@@ -91,7 +92,7 @@ protected function doExecute(): int
9192

9293
$extension = new \ReflectionExtension('redis');
9394
$output->writeln('PHPRedis version: ' . $extension->getVersion());
94-
} catch(\Exception $e) {
95+
} catch (\Exception $e) {
9596
$output->writeln('No PHPRedis extension (not a problem if sentinel is used):' . $e->getMessage());
9697
}
9798
}
@@ -107,12 +108,14 @@ protected function doExecute(): int
107108

108109
$redis = $backend->getConnection();
109110
if ($isUsingRedis && !$shouldSkipCheckingMemoryConfigValues) {
110-
111111
$evictionPolicy = $this->getRedisConfig($redis, 'maxmemory-policy');
112112
$output->writeln('MaxMemory Eviction Policy config: ' . $evictionPolicy);
113113

114114
if ($evictionPolicy !== 'allkeys-lru' && $evictionPolicy !== 'noeviction') {
115-
$output->writeln('<error>The eviction policy can likely lead to errors when memory is low. We recommend to use eviction policy <comment>allkeys-lru</comment> or alternatively <comment>noeviction</comment>. Read more here: http://redis.io/topics/lru-cache</error>');
115+
$output->writeln(
116+
'<error>The eviction policy can likely lead to errors when memory is low. We recommend to use eviction policy <comment>allkeys-lru</comment> or alternatively <comment>noeviction</comment>.' .
117+
' Read more here: http://redis.io/topics/lru-cache</error>'
118+
);
116119
}
117120

118121
$evictionPolicy = $this->getRedisConfig($redis, 'maxmemory');
@@ -126,7 +129,7 @@ protected function doExecute(): int
126129
$output->writeln('Redis is connected: ' . (int) $redis->isConnected());
127130
}
128131

129-
if ($backend->testConnection()){
132+
if ($backend->testConnection()) {
130133
$output->writeln('Connection works in general');
131134
} else {
132135
$output->writeln('Connection does not actually work: ' . $redis->getLastError());
@@ -144,7 +147,7 @@ protected function doExecute(): int
144147
if (!$backend->setIfNotExists('foo', 'bar', 5)) {
145148
$message = "setIfNotExists(foo, bar, 1) does not work, most likely we won't be able to acquire a lock: " . $backend->getLastError();
146149
$output->writeln($message);
147-
} else{
150+
} else {
148151
$initialTtl = $backend->getTimeToLive('foo');
149152
if ($initialTtl >= 3000 && $initialTtl <= 5000) {
150153
$output->writeln('Initial expire seems to be set correctly');
@@ -195,7 +198,6 @@ protected function doExecute(): int
195198
$backend->appendValuesToList('fooList', array('value1', 'value2', 'value3'));
196199
$values = $backend->getFirstXValuesFromList('fooList', 2);
197200
if ($values == array('value1', 'value2')) {
198-
199201
$backend->removeFirstXValuesFromList('fooList', 1);
200202
$backend->removeFirstXValuesFromList('fooList', 1);
201203
$values = $backend->getFirstXValuesFromList('fooList', 2);
@@ -204,7 +206,6 @@ protected function doExecute(): int
204206
} else {
205207
$output->writeln('List feature seems to work only partially: ' . var_export($values, 1));
206208
}
207-
208209
} else {
209210
$output->writeln('<error>List feature seems to not work fine: ' . $redis->getLastError() . '</error>');
210211
}
@@ -247,11 +248,11 @@ private function testRedis($redis, $method, $params, $keyToCleanUp)
247248

248249
$result = call_user_func_array(array($redis, $method), $params);
249250

250-
$paramsMapped = array_map(function($item) {
251+
$paramsMapped = array_map(function ($item) {
251252
if (is_string($item)) {
252253
return $item;
253254
}
254-
255+
255256
return str_replace(["\r", "\n", " "], '', var_export($item, true));
256257
}, $params);
257258
$paramsInline = implode(', ', $paramsMapped);

0 commit comments

Comments
 (0)