Skip to content

Commit 8ca2053

Browse files
author
Philo Hamel
committed
Adding phan check, fixing CS issues
1 parent 7a2c9fc commit 8ca2053

7 files changed

Lines changed: 439 additions & 16 deletions

File tree

.phan/config.php

Lines changed: 414 additions & 0 deletions
Large diffs are not rendered by default.

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ matrix:
3030
- php: 7.1
3131
env:
3232
- PHP_STAN=1
33+
- php: 7.1
34+
env:
35+
- PHP_PHAN=1
3336
- php: 7.1
3437
env:
3538
- PHP_COVERAGE=1

.travis/before-script.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ elif [ "${PHP_MD}" = '1' ]; then
1919
exit 0;
2020
elif [ "${PHP_STAN}" = '1' ]; then
2121
exit 0;
22+
elif [ "${PHP_PHAN}" = '1' ]; then
23+
exit 0;
2224
fi
2325

2426
composer require "cakephp/cakephp:${CAKE_VERSION}" --dev --no-ansi --no-progress --no-interaction;

.travis/script.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ elif [ "${PHP_MD}" = '1' ]; then
3131

3232
vendor/bin/phpmd . text phpmd.xml --suffixes php --exclude "${excludePathsJoined}" || true;
3333
elif [ "${PHP_STAN}" = '1' ]; then
34-
vendor/bin/phpstan analyse -c phpstan.neon -l 5 src/
34+
vendor/bin/phpstan analyse -c phpstan.neon -l 5 src/;
35+
elif [ "${PHP_PHAN}" = '1' ]; then
36+
vendor/bin/phan;
3537
elif [ "${PHP_COVERAGE}" = '1' ]; then
3638
vendor/bin/phpunit --coverage-clover=clover.xml;
3739
else

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"phpunit/phpunit": "^5.7.14|^6.0",
2525
"psy/psysh": "@stable",
2626
"sebastian/phpcpd": "^3.0",
27-
"dereuromark/cakephp-tools": "^1.9"
27+
"dereuromark/cakephp-tools": "^1.9",
28+
"phan/phan": "^1.3"
2829
},
2930
"support": {
3031
"issues": "https://github.com/Oefenweb/cakephp-queue/issues",

src/Model/Table/QueuedTasksTable.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $opti
8585
*
8686
* @param string $taskName Task name
8787
* @param array|null $data Array of data
88-
* @param string $notBefore A datetime which indicates when the job may be executed
88+
* @param string|null $notBefore A datetime which indicates when the job may be executed
8989
* @return \Queue\Model\Entity\QueuedTask Saved job entity
9090
*/
9191
public function createJob($taskName, array $data = null, string $notBefore = null)
@@ -157,7 +157,7 @@ public function getStats()
157157
{
158158
$driverName = $this->_getDriverName();
159159
$options = [
160-
'fields' => function (Query $query) use ($driverName) {
160+
'fields' => function (Query $query) use ($driverName): array {
161161
$alltime = $query->func()->avg('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(created)');
162162
$runtime = $query->func()->avg('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(fetched)');
163163
$fetchdelay = $query->func()->avg('UNIX_TIMESTAMP(fetched) - IF(not_before is NULL, UNIX_TIMESTAMP(created), UNIX_TIMESTAMP(not_before))');
@@ -205,7 +205,7 @@ public function getStats()
205205
public function getFullStats($taskName = null)
206206
{
207207
$driverName = $this->_getDriverName();
208-
$fields = function (Query $query) use ($driverName) {
208+
$fields = function (Query $query) use ($driverName): array {
209209
$runtime = $query->newExpr('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(fetched)');
210210
switch ($driverName) {
211211
case static::DRIVER_SQLSERVER:
@@ -339,7 +339,7 @@ public function requestJob(array $capabilities, array $types = [])
339339
}
340340

341341
/** @var \Queue\Model\Entity\QueuedTask|null $task */
342-
$task = $this->getConnection()->transactional(function () use ($query, $options, $now) {
342+
$task = $this->getConnection()->transactional(function () use ($query, $options, $now): ?QueuedTask {
343343
$task = $query->find('all', $options)
344344
->enableAutoFields(true)
345345
->epilog('FOR UPDATE')
@@ -350,6 +350,7 @@ public function requestJob(array $capabilities, array $types = [])
350350
}
351351

352352
$key = sha1(microtime());
353+
/* @phan-suppress-next-line PhanPartialTypeMismatchArgument */
353354
$task = $this->patchEntity($task, [
354355
'worker_key' => $key,
355356
'fetched' => $now
@@ -507,7 +508,7 @@ public function truncate()
507508
protected function _getDriverName()
508509
{
509510
$className = explode('\\', $this->getConnection()->config()['driver']);
510-
$name = end($className);
511+
$name = end($className) ?: '';
511512

512513
return $name;
513514
}

src/Shell/QueueShell.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Cake\Log\Log;
88
use Cake\Utility\Inflector;
99
use Cake\Utility\Text;
10-
use Exception;
1110
use Queue\Model\Entity\QueuedTask;
1211
use Queue\Model\QueueException;
1312
use Queue\Queue\Config;
@@ -142,7 +141,7 @@ public function add()
142141
protected function _taskName($task)
143142
{
144143
if (strpos($task, 'Queue') === 0) {
145-
return substr($task, 5);
144+
return substr($task, 5) ?: '';
146145
}
147146

148147
return $task;
@@ -182,7 +181,9 @@ public function runworker()
182181
$this->_exit = false;
183182

184183
$startTime = time();
185-
$types = $this->_stringToArray($this->param('type'));
184+
185+
$typesParam = $this->param('type');
186+
$types = is_string($typesParam) ? $this->_stringToArray($typesParam) : [];
186187

187188
while (!$this->_exit) {
188189
$this->out(__d('queue', 'Looking for a job.'), 1, Shell::VERBOSE);
@@ -237,6 +238,7 @@ protected function runJob(QueuedTask $queuedTask)
237238
throw new RuntimeException('Task must implement ' . QueueTaskInterface::class);
238239
}
239240

241+
/* @phan-suppress-next-line PhanTypeVoidAssignment */
240242
$return = $task->run((array)$data, $queuedTask->id);
241243
if ($return !== null) {
242244
trigger_error('run() should be void and throw exception in error case now.', E_USER_DEPRECATED);
@@ -251,11 +253,6 @@ protected function runJob(QueuedTask $queuedTask)
251253
}
252254

253255
$this->_logError($taskName . ' (job ' . $queuedTask->id . ')' . "\n" . $failureMessage);
254-
} catch (Exception $e) {
255-
$return = false;
256-
257-
$failureMessage = get_class($e) . ': ' . $e->getMessage();
258-
$this->_logError($taskName . "\n" . $failureMessage);
259256
}
260257

261258
if ($return === false) {
@@ -473,7 +470,7 @@ protected function _getTaskConf()
473470
*/
474471
protected function _exit($signal)
475472
{
476-
$this->out(__d('queue', 'Caught signal {0}, exiting.', $signal));
473+
$this->out(__d('queue', 'Caught signal {0}, exiting.', [$signal]));
477474
$this->_exit = true;
478475
}
479476

@@ -544,6 +541,9 @@ protected function _stringToArray($param)
544541
}
545542

546543
$array = Text::tokenize($param);
544+
if (is_string($array)) {
545+
return [$array];
546+
}
547547

548548
return array_filter($array);
549549
}

0 commit comments

Comments
 (0)