diff --git a/src/Command/AddCommand.php b/src/Command/AddCommand.php index dc68fcd0..6aae0932 100644 --- a/src/Command/AddCommand.php +++ b/src/Command/AddCommand.php @@ -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); } diff --git a/src/Command/BakeQueueTaskCommand.php b/src/Command/BakeQueueTaskCommand.php index 1ae64f98..7296f7fe 100644 --- a/src/Command/BakeQueueTaskCommand.php +++ b/src/Command/BakeQueueTaskCommand.php @@ -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 = <<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 .= ' [addable via CLI]'; } diff --git a/src/Controller/Admin/LoadHelperTrait.php b/src/Controller/Admin/LoadHelperTrait.php index a1337169..f40d6814 100644 --- a/src/Controller/Admin/LoadHelperTrait.php +++ b/src/Controller/Admin/LoadHelperTrait.php @@ -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'; diff --git a/src/Controller/Admin/QueueController.php b/src/Controller/Admin/QueueController.php index 56035a10..71d5676a 100644 --- a/src/Controller/Admin/QueueController.php +++ b/src/Controller/Admin/QueueController.php @@ -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; } diff --git a/src/Controller/Admin/QueueProcessesController.php b/src/Controller/Admin/QueueProcessesController.php index 7ad23a82..a1c1c876 100644 --- a/src/Controller/Admin/QueueProcessesController.php +++ b/src/Controller/Admin/QueueProcessesController.php @@ -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.')); } diff --git a/src/Controller/Admin/QueuedJobsController.php b/src/Controller/Admin/QueuedJobsController.php index ae47f5ab..ad2476c4 100644 --- a/src/Controller/Admin/QueuedJobsController.php +++ b/src/Controller/Admin/QueuedJobsController.php @@ -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; } @@ -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; } diff --git a/src/Generator/Task/QueuedJobTask.php b/src/Generator/Task/QueuedJobTask.php index 09d1dcc9..5e9877d5 100644 --- a/src/Generator/Task/QueuedJobTask.php +++ b/src/Generator/Task/QueuedJobTask.php @@ -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'"; } @@ -44,9 +44,8 @@ public function collect(): array { */ protected function collectQueuedJobTasks(): array { $taskFinder = new TaskFinder(); - $tasks = $taskFinder->all(); - return $tasks; + return $taskFinder->all(); } } diff --git a/src/Mailer/Transport/SimpleQueueTransport.php b/src/Mailer/Transport/SimpleQueueTransport.php index 9c6fb1de..be209351 100644 --- a/src/Mailer/Transport/SimpleQueueTransport.php +++ b/src/Mailer/Transport/SimpleQueueTransport.php @@ -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]); } } diff --git a/src/Model/Behavior/JsonableBehavior.php b/src/Model/Behavior/JsonableBehavior.php index 9b875eeb..4c43e3c2 100644 --- a/src/Model/Behavior/JsonableBehavior.php +++ b/src/Model/Behavior/JsonableBehavior.php @@ -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)) { return null; diff --git a/src/Model/Table/QueueProcessesTable.php b/src/Model/Table/QueueProcessesTable.php index 4e29e908..49f2c0b8 100644 --- a/src/Model/Table/QueueProcessesTable.php +++ b/src/Model/Table/QueueProcessesTable.php @@ -242,6 +242,7 @@ public function status(): array { ->enableHydration(false) ->all() ->toArray(); + /** @var array $results */ if (!$results) { return []; diff --git a/src/Model/Table/QueuedJobsTable.php b/src/Model/Table/QueuedJobsTable.php index 077a3e5d..ab4398d8 100644 --- a/src/Model/Table/QueuedJobsTable.php +++ b/src/Model/Table/QueuedJobsTable.php @@ -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(), @@ -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(); } @@ -451,6 +452,7 @@ public function getFullStats(?string $jobTask = null): array { ->limit(static::STATS_LIMIT) ->all() ->toArray(); + /** @var array $jobs */ $result = []; @@ -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) @@ -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) ?: ''; } /** @@ -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; diff --git a/src/Queue/Config.php b/src/Queue/Config.php index baa4a2c5..0fd209ec 100644 --- a/src/Queue/Config.php +++ b/src/Queue/Config.php @@ -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.'); diff --git a/src/Queue/Processor.php b/src/Queue/Processor.php index 6c75ca58..36b2412d 100644 --- a/src/Queue/Processor.php +++ b/src/Queue/Processor.php @@ -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 @@ -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; @@ -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(); } /** diff --git a/src/Queue/Task/EmailTask.php b/src/Queue/Task/EmailTask.php index e01298ea..498b4363 100644 --- a/src/Queue/Task/EmailTask.php +++ b/src/Queue/Task/EmailTask.php @@ -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); diff --git a/src/Queue/Task/ExecuteTask.php b/src/Queue/Task/ExecuteTask.php index 39b8971b..bd0ffc2b 100644 --- a/src/Queue/Task/ExecuteTask.php +++ b/src/Queue/Task/ExecuteTask.php @@ -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']; diff --git a/src/Queue/Task/MonitorExampleTask.php b/src/Queue/Task/MonitorExampleTask.php index a0400c00..a9c75216 100644 --- a/src/Queue/Task/MonitorExampleTask.php +++ b/src/Queue/Task/MonitorExampleTask.php @@ -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); diff --git a/src/Queue/Task/ProgressExampleTask.php b/src/Queue/Task/ProgressExampleTask.php index 81ab7cea..a1e59f9b 100644 --- a/src/Queue/Task/ProgressExampleTask.php +++ b/src/Queue/Task/ProgressExampleTask.php @@ -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++) { diff --git a/src/Queue/TaskFinder.php b/src/Queue/TaskFinder.php index 2582bb6f..ce1573ee 100644 --- a/src/Queue/TaskFinder.php +++ b/src/Queue/TaskFinder.php @@ -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; } diff --git a/src/View/Helper/QueueHelper.php b/src/View/Helper/QueueHelper.php index 0a31d67f..9977c79f 100644 --- a/src/View/Helper/QueueHelper.php +++ b/src/View/Helper/QueueHelper.php @@ -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']); } /** @@ -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']; } /** @@ -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; } /** diff --git a/tests/TestCase/Command/RunCommandTest.php b/tests/TestCase/Command/RunCommandTest.php index f2cef917..e9a99166 100644 --- a/tests/TestCase/Command/RunCommandTest.php +++ b/tests/TestCase/Command/RunCommandTest.php @@ -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.'); } diff --git a/tests/TestCase/Controller/Admin/QueueControllerTest.php b/tests/TestCase/Controller/Admin/QueueControllerTest.php index 9ff026fc..28610d6d 100644 --- a/tests/TestCase/Controller/Admin/QueueControllerTest.php +++ b/tests/TestCase/Controller/Admin/QueueControllerTest.php @@ -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.'); } diff --git a/tests/TestCase/Controller/Admin/QueuedJobsControllerTest.php b/tests/TestCase/Controller/Admin/QueuedJobsControllerTest.php index 3604a7aa..5c933724 100644 --- a/tests/TestCase/Controller/Admin/QueuedJobsControllerTest.php +++ b/tests/TestCase/Controller/Admin/QueuedJobsControllerTest.php @@ -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.'); } diff --git a/tests/TestCase/Model/Table/QueuedJobsTableTest.php b/tests/TestCase/Model/Table/QueuedJobsTableTest.php index 8f65f0b3..3dd24c0d 100644 --- a/tests/TestCase/Model/Table/QueuedJobsTableTest.php +++ b/tests/TestCase/Model/Table/QueuedJobsTableTest.php @@ -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'])); @@ -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.'); } diff --git a/tests/TestCase/Queue/ProcessorTest.php b/tests/TestCase/Queue/ProcessorTest.php index 156a23d5..1170e168 100644 --- a/tests/TestCase/Queue/ProcessorTest.php +++ b/tests/TestCase/Queue/ProcessorTest.php @@ -222,7 +222,7 @@ public function testRunVerboseShowsIdlePollOutput() { */ 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.'); } diff --git a/tests/TestCase/Queue/Task/EmailTaskTest.php b/tests/TestCase/Queue/Task/EmailTaskTest.php index e6523879..c51ee31e 100644 --- a/tests/TestCase/Queue/Task/EmailTaskTest.php +++ b/tests/TestCase/Queue/Task/EmailTaskTest.php @@ -415,7 +415,7 @@ public function testRunWithAttachments() { */ protected function _skipPostgres() { $config = ConnectionManager::getConfig('test'); - $skip = strpos($config['driver'], 'Postgres') !== false; + $skip = str_contains((string)$config['driver'], 'Postgres'); $this->skipIf($skip, 'Only non Postgres is working yet for this.'); } diff --git a/tests/TestCase/View/Helper/QueueProgressHelperTest.php b/tests/TestCase/View/Helper/QueueProgressHelperTest.php index 0281e50d..0a1925fa 100644 --- a/tests/TestCase/View/Helper/QueueProgressHelperTest.php +++ b/tests/TestCase/View/Helper/QueueProgressHelperTest.php @@ -227,7 +227,7 @@ public function testHtmlTimeoutProgressBar() { */ 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.'); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index dcfe5881..641b4f58 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -19,7 +19,7 @@ define('DS', DIRECTORY_SEPARATOR); } if (!defined('WINDOWS')) { - if (DS === '\\' || substr(PHP_OS, 0, 3) === 'WIN') { + if (DS === '\\' || str_starts_with(PHP_OS, 'WIN')) { define('WINDOWS', true); } else { define('WINDOWS', false); diff --git a/tests/schema.php b/tests/schema.php index 324e5ec8..163ac34d 100644 --- a/tests/schema.php +++ b/tests/schema.php @@ -21,7 +21,7 @@ $fieldsObject = (new ReflectionClass($class))->getProperty('fields'); $tableObject = (new ReflectionClass($class))->getProperty('table'); $tableName = $tableObject->getDefaultValue(); - } catch (ReflectionException $e) { + } catch (ReflectionException) { continue; }