Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/Command/AddCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function execute(Arguments $args, ConsoleIo $io) {
$taskName = $args->getArgument('task');
if (!$taskName) {
$io->out(count($tasks) . ' tasks available:');
foreach ($tasks as $task => $className) {
foreach (array_keys($tasks) as $task) {
$io->out(' - ' . $task);
}

Expand Down
8 changes: 3 additions & 5 deletions src/Command/BakeQueueTaskCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ protected function generateTaskTestContent(string $name, string $namespace): str
}
$taskClassNamespace = $namespace . '\Queue\\Task\\' . str_replace(DS, '\\', $name);

if (strpos($name, '/') !== false) {
if (str_contains($name, '/')) {
$parts = explode('/', $name);
$name = array_pop($parts);
}

$content = <<<TXT
return <<<TXT
<?php

namespace $namespace\Test\TestCase\Queue\Task$subNamespace;
Expand Down Expand Up @@ -122,8 +122,6 @@ public function testRun(): void {
}

TXT;

return $content;
}

/**
Expand All @@ -148,7 +146,7 @@ public function templateData(Arguments $arguments): array {
$namespace .= '\\Queue\\Task';

$namespacePart = null;
if (strpos($name, '/') !== false) {
if (str_contains($name, '/')) {
$parts = explode('/', $name);
$name = array_pop($parts);
$namespacePart = implode('\\', $parts);
Expand Down
2 changes: 1 addition & 1 deletion src/Command/InfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function execute(Arguments $args, ConsoleIo $io) {
$addableTasks = $this->getAddableTasks();

$io->out(count($tasks) . ' tasks available:');
foreach ($tasks as $task => $className) {
foreach (array_keys($tasks) as $task) {
if (array_key_exists($task, $addableTasks)) {
$task .= ' <info>[addable via CLI]</info>';
}
Expand Down
6 changes: 1 addition & 5 deletions src/Controller/Admin/LoadHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ protected function loadHelpers(): void {
}

// Configure helper: prefer Shim, fallback to Queue's own
if (Plugin::isLoaded('Shim')) {
$helpers[] = 'Shim.Configure';
} else {
$helpers[] = 'Queue.Configure';
}
$helpers[] = Plugin::isLoaded('Shim') ? 'Shim.Configure' : 'Queue.Configure';

if (Configure::read('Icon.sets') && class_exists(IconHelper::class)) {
$helpers[] = 'Templating.Icon';
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Admin/QueueController.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ protected function refererRedirect(array|string $default) {
if (is_array($url)) {
throw new NotFoundException('Invalid array in query string');
}
if ($url && (mb_substr($url, 0, 1) !== '/' || mb_substr($url, 0, 2) === '//')) {
if ($url && (mb_substr((string)$url, 0, 1) !== '/' || mb_substr((string)$url, 0, 2) === '//')) {
$url = null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Admin/QueueProcessesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function terminate(?int $id = null) {
$queueProcess->terminate = true;
$this->QueueProcesses->saveOrFail($queueProcess);
$this->Flash->success(__d('queue', 'The queue process has been deleted.'));
} catch (Exception $exception) {
} catch (Exception) {
$this->Flash->error(__d('queue', 'The queue process could not be deleted. Please, try again.'));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Controller/Admin/QueuedJobsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public function test() {
$taskFinder = new TaskFinder();
$allTasks = $taskFinder->all();
$tasks = [];
foreach ($allTasks as $task => $className) {
foreach (array_keys($allTasks) as $task) {
if (!str_starts_with($task, 'Queue.')) {
continue;
}
Expand Down Expand Up @@ -458,7 +458,7 @@ public function migrate() {
->toArray();

$tasks = [];
foreach ($allTasks as $task => $className) {
foreach (array_keys($allTasks) as $task) {
if (!str_starts_with($task, 'Queue.')) {
continue;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Generator/Task/QueuedJobTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function collect(): array {
$list = [];

$names = $this->collectQueuedJobTasks();
foreach ($names as $name => $className) {
foreach (array_keys($names) as $name) {
$list[$name] = "'$name'";
}

Expand All @@ -44,9 +44,8 @@ public function collect(): array {
*/
protected function collectQueuedJobTasks(): array {
$taskFinder = new TaskFinder();
$tasks = $taskFinder->all();

return $tasks;
return $taskFinder->all();
}

}
3 changes: 1 addition & 2 deletions src/Mailer/Transport/SimpleQueueTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public function send(Message $message): array {
];

foreach ($settings as $setting => $value) {
/** @phpstan-ignore-next-line */
if (array_key_exists(0, $value) && ($value[0] === null || $value[0] === [])) {
if ($value[0] === null || $value[0] === []) {
unset($settings[$setting]);
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/Model/Behavior/JsonableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,11 @@ protected function _getMappedFields() {
* @return string|null
*/
public function _encode($val) {
if (!empty($this->_config['fields'])) {
if ($this->_config['input'] === 'json') {
if (!is_string($val)) {
throw new InvalidArgumentException('Only accepts JSON string for input type `json`');
}
$val = $this->_fromJson($val);
if (!empty($this->_config['fields']) && $this->_config['input'] === 'json') {
if (!is_string($val)) {
throw new InvalidArgumentException('Only accepts JSON string for input type `json`');
}
$val = $this->_fromJson($val);
}
if (!is_array($val)) {
Comment thread
dereuromark marked this conversation as resolved.
return null;
Expand Down
1 change: 1 addition & 0 deletions src/Model/Table/QueueProcessesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public function status(): array {
->enableHydration(false)
->all()
->toArray();
/** @var array<array{modified: \Cake\I18n\DateTime}> $results */

if (!$results) {
return [];
Expand Down
13 changes: 7 additions & 6 deletions src/Model/Table/QueuedJobsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public function createJob(string $jobTask, array|object|null $data = null, array
throw new InvalidArgumentException('createJob() with `unique` requires a `reference` to dedupe on.');
}

/** @var \Queue\Model\Entity\QueuedJob|null $existing */
$existing = $this->find()
->where([
'reference' => $config->getReferenceOrFail(),
Expand Down Expand Up @@ -267,7 +268,7 @@ public function createJob(string $jobTask, array|object|null $data = null, array
* @return string
*/
protected function jobTask(string $jobType): string {
if ($this->taskFinder === null) {
if (!($this->taskFinder instanceof TaskFinder)) {
$this->taskFinder = new TaskFinder();
}

Expand Down Expand Up @@ -451,6 +452,7 @@ public function getFullStats(?string $jobTask = null): array {
->limit(static::STATS_LIMIT)
->all()
->toArray();
/** @var array<array{created: \DateTime, duration: int|float|null, job_task: string}> $jobs */

$result = [];

Expand Down Expand Up @@ -981,7 +983,7 @@ public function getHeatmapData(string $field = 'created', int $days = 30, ?strin

// MySQL DAYOFWEEK returns 1=Sunday, 2=Monday, etc. Adjust to 0=Sunday
if ($driverName === static::DRIVER_MYSQL) {
$day = $day - 1;
$day -= 1;
}

// Ensure day is in valid range (0-6)
Expand Down Expand Up @@ -1260,10 +1262,9 @@ public function truncate(): void {
* @return string
*/
protected function getDriverName(): string {
$className = explode('\\', $this->getConnection()->config()['driver']);
$name = end($className) ?: '';
$className = explode('\\', (string)$this->getConnection()->config()['driver']);

return $name;
return end($className) ?: '';
}

/**
Expand All @@ -1277,7 +1278,7 @@ protected function addFilter(array $conditions, string $key, array $values): arr
$include = [];
$exclude = [];
foreach ($values as $value) {
if (substr($value, 0, 1) === '-') {
if (str_starts_with($value, '-')) {
$exclude[] = substr($value, 1);
} else {
$include[] = $value;
Expand Down
2 changes: 1 addition & 1 deletion src/Queue/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function defaultworkertimeout(): int {
);
}
}
$timeout = $timeout ?? 600; // 10min default
$timeout ??= 600; // 10min default

if ($timeout <= 0) {
throw new InvalidArgumentException('Queue.defaultRequeueTimeout (or deprecated defaultworkertimeout) is less or equal than zero. Indefinite running of jobs is not supported.');
Expand Down
20 changes: 7 additions & 13 deletions src/Queue/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ public function run(array $args): int {
pcntl_async_signals(true);
}
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGTERM, [&$this, 'exit']);
pcntl_signal(SIGINT, [&$this, 'abort']);
pcntl_signal(SIGTSTP, [&$this, 'abort']);
pcntl_signal(SIGQUIT, [&$this, 'abort']);
pcntl_signal(SIGTERM, $this->exit(...));
pcntl_signal(SIGINT, $this->abort(...));
pcntl_signal(SIGTSTP, $this->abort(...));
pcntl_signal(SIGQUIT, $this->abort(...));
if (Configure::read('Queue.canInterruptSleep')) {
// Defining a signal handler here will make the worker wake up
// from its sleep() when SIGUSR1 is received. Since waking it
Expand All @@ -222,12 +222,12 @@ public function run(array $args): int {

try {
$this->updatePid($pid);
} catch (RecordNotFoundException $exception) {
} catch (RecordNotFoundException) {
// Manually killed
$this->exit = true;

continue;
} catch (ProcessEndingException $exception) {
} catch (ProcessEndingException) {
// Soft killed, e.g. during deploy update
$this->exit = true;

Expand Down Expand Up @@ -533,13 +533,7 @@ protected function initPid(): string {
* @return string
*/
protected function retrievePid(): string {
if (function_exists('posix_getpid')) {
$pid = (string)posix_getpid();
} else {
$pid = $this->QueuedJobs->key();
}

return $pid;
return function_exists('posix_getpid') ? (string)posix_getpid() : $this->QueuedJobs->key();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Queue/Task/EmailTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function run(array $data, int $jobId): void {
}

foreach ($settings as $method => $setting) {
$setter = 'set' . ucfirst($method);
$setter = 'set' . ucfirst((string)$method);
if (in_array($method, ['theme', 'template', 'layout'], true)) {
call_user_func_array([$this->mailer->viewBuilder(), $setter], (array)$setting);

Expand Down
6 changes: 1 addition & 5 deletions src/Queue/Task/ExecuteTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ public function run(array $data, int $jobId): void {
}
}

if ($data['escape']) {
$command = escapeshellarg($rawCommand);
} else {
$command = $rawCommand;
}
$command = $data['escape'] ? escapeshellarg($rawCommand) : $rawCommand;

if ($data['params']) {
$params = $data['params'];
Expand Down
2 changes: 1 addition & 1 deletion src/Queue/Task/MonitorExampleTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected function getSystemMemInfo(): array {
$data = explode("\n", file_get_contents('/proc/meminfo') ?: '');
$meminfo = [];
foreach ($data as $line) {
if (strpos($line, ':') === false) {
if (!str_contains($line, ':')) {
continue;
}
[$key, $val] = explode(':', $line);
Expand Down
2 changes: 1 addition & 1 deletion src/Queue/Task/ProgressExampleTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function description(): ?string {
public function run(array $data, int $jobId): void {
$this->io->hr();
$this->io->out('CakePHP Queue ProgressExample task.');
$seconds = !empty($data['duration']) ? (int)$data['duration'] : 2 * static::MINUTE;
$seconds = empty($data['duration']) ? 2 * static::MINUTE : (int)$data['duration'];

$this->io->out('A total of ' . $seconds . ' seconds need to pass...');
for ($i = 0; $i < $seconds; $i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/Queue/TaskFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function resolve(string $jobTask): string {

if (!str_contains($jobTask, '\\')) {
// Let's try matching without plugin prefix
foreach ($all as $name => $className) {
foreach ($all as $name => $_) {
if (!str_contains($name, '.')) {
continue;
}
Expand Down
16 changes: 3 additions & 13 deletions src/View/Helper/QueueHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ public function hasFailed(QueuedJob $queuedJob): bool {

// Requeued
$taskConfig = $this->taskConfig($queuedJob->job_task);
if ($taskConfig && $queuedJob->attempts <= $taskConfig['retries']) {
return false;
}

return true;
return !($taskConfig && $queuedJob->attempts <= $taskConfig['retries']);
}
Comment thread
dereuromark marked this conversation as resolved.

/**
Expand Down Expand Up @@ -85,11 +82,8 @@ public function isRequeued(QueuedJob $queuedJob): bool {
}

$taskConfig = $this->taskConfig($queuedJob->job_task);
if ($taskConfig && $queuedJob->attempts <= $taskConfig['retries']) {
return true;
}

return false;
return $taskConfig && $queuedJob->attempts <= $taskConfig['retries'];
}
Comment thread
dereuromark marked this conversation as resolved.

/**
Expand All @@ -113,11 +107,7 @@ public function isRestarted(QueuedJob $queuedJob): bool {
}

// Must NOT have a failure_message (it was cleared by reset)
if ($queuedJob->failure_message) {
return false;
}

return true;
return !$queuedJob->failure_message;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Command/RunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function testServiceInjection(): void {
*/
protected function _needsConnection() {
$config = ConnectionManager::getConfig('test');
$skip = strpos($config['driver'], 'Mysql') === false && strpos($config['driver'], 'Postgres') === false;
$skip = !str_contains((string)$config['driver'], 'Mysql') && !str_contains((string)$config['driver'], 'Postgres');
$this->skipIf($skip, 'Only Mysql/Postgres is working yet for this.');
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Controller/Admin/QueueControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public function testIndexStandalone(): void {
*/
protected function _needsConnection() {
$config = ConnectionManager::getConfig('test');
$skip = strpos($config['driver'], 'Mysql') === false && strpos($config['driver'], 'Postgres') === false;
$skip = !str_contains((string)$config['driver'], 'Mysql') && !str_contains((string)$config['driver'], 'Postgres');
$this->skipIf($skip, 'Only Mysql/Postgres is working yet for this.');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public function testImport() {
*/
protected function _needsConnection() {
$config = ConnectionManager::getConfig('test');
$skip = strpos($config['driver'], 'Mysql') === false && strpos($config['driver'], 'Postgres') === false;
$skip = !str_contains((string)$config['driver'], 'Mysql') && !str_contains((string)$config['driver'], 'Postgres');
$this->skipIf($skip, 'Only Mysql/Postgres is working yet for this.');
}

Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Model/Table/QueuedJobsTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public function testSequence() {
* Test creating Jobs to run close to a specified time, and strtotime parsing.
* Using toUnixString() function to convert Time object to timestamp, instead of strtotime
*
* @return null
* @return void
*/
public function testNotBefore() {
$this->assertTrue((bool)$this->QueuedJobs->createJob('Foo', null, ['notBefore' => '+ 1 Min']));
Expand Down Expand Up @@ -945,7 +945,7 @@ public function testJobCreatedEventFired(): void {
*/
protected function _needsConnection() {
$config = ConnectionManager::getConfig('test');
$skip = strpos($config['driver'], 'Mysql') === false && strpos($config['driver'], 'Postgres') === false;
$skip = !str_contains((string)$config['driver'], 'Mysql') && !str_contains((string)$config['driver'], 'Postgres');
$this->skipIf($skip, 'Only Mysql/Postgres is working yet for this.');
}

Expand Down
Loading
Loading