Skip to content

Commit

Permalink
Setup psalm level to 2 (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
s1lver authored Nov 23, 2023
1 parent bd3ce76 commit f915784
Show file tree
Hide file tree
Showing 59 changed files with 417 additions and 254 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
/phpunit.xml.dist export-ignore
/support export-ignore
/psalm.xml export-ignore
/stubs export-ignore
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"enqueue/amqp-bunny": "^0.10.0",
"enqueue/amqp-ext": "^0.10.8",
"enqueue/stomp": "^0.10.0",
"pda/pheanstalk": "^v5.0.0",
"aws/aws-sdk-php": ">=2.4",
"pda/pheanstalk": "^5.0.0",
"aws/aws-sdk-php": "3.285.0",
"vimeo/psalm": "^5.10.0"
},
"suggest": {
Expand Down
30 changes: 17 additions & 13 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@
displayDetailsOnSkippedTests="true"
executionOrder="random"
>
<testsuites>
<testsuite name="Yii2-Queue">
<directory>./tests</directory>
<exclude>./tests/app</exclude>
<exclude>./tests/docker</exclude>
<exclude>./tests/runtime</exclude>
</testsuite>
</testsuites>
<source>
<include>
<directory>./src</directory>
</include>
</source>
<testsuites>
<testsuite name="Yii2-Queue">
<directory>./tests</directory>
<exclude>./tests/app</exclude>
<exclude>./tests/docker</exclude>
<exclude>./tests/runtime</exclude>
</testsuite>
</testsuites>
<source>
<include>
<directory>./src</directory>
</include>
<exclude>
<directory>./src/debug</directory>
<directory>./src/gii</directory>
</exclude>
</source>
</phpunit>
6 changes: 5 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
errorLevel="2"
findUnusedBaselineEntry="true"
findUnusedCode="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -20,4 +20,8 @@
<extraFiles>
<directory name="vendor"/>
</extraFiles>
<stubs>
<file name="stubs/psalm/BaseYii.php" preloadClasses="true"/>
<file name="support/ide-helper.php"/>
</stubs>
</psalm>
14 changes: 8 additions & 6 deletions src/ExecEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace yii\queue;

use Throwable;

/**
* Exec Event.
*
Expand All @@ -23,23 +25,23 @@ class ExecEvent extends JobEvent
* @see Queue::EVENT_AFTER_EXEC
* @see Queue::EVENT_AFTER_ERROR
*/
public int $attempt;
public int $attempt = 0;
/**
* @var mixed result of a job execution in case job is done.
* @see Queue::EVENT_AFTER_EXEC
* @since 2.1.1
*/
public $result;
public mixed $result = null;
/**
* @var null|\Exception|\Throwable
* @var null|Throwable
* @see Queue::EVENT_AFTER_ERROR
* @since 2.1.1
*/
public $error;
public ?Throwable $error = null;
/**
* @var null|bool
* @var bool
* @see Queue::EVENT_AFTER_ERROR
* @since 2.1.1
*/
public $retry;
public bool $retry = true;
}
8 changes: 4 additions & 4 deletions src/InvalidJobException.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class InvalidJobException extends Exception
* @param Throwable|null $previous
*/
public function __construct(
private string $serialized,
string $message = '',
int $code = 0,
Throwable $previous = null
private readonly string $serialized,
string $message = '',
int $code = 0,
Throwable $previous = null
)
{
parent::__construct($message, $code, $previous);
Expand Down
11 changes: 8 additions & 3 deletions src/JobEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,25 @@
abstract class JobEvent extends Event
{
/**
* @var Queue
* @inheritdoc
*/
public $name = '';
/**
* @var Queue|null|object
* @inheritdoc
* @psalm-suppress PropertyNotSetInConstructor
*/
public $sender;
/**
* @var int|string|null unique id of a job
*/
public string|int|null $id;
public string|int|null $id = null;
/**
* @var Closure|JobInterface|null|mixed
*/
public mixed $job;
/**
* @var int time to reserve in seconds of the job
*/
public int $ttr;
public int $ttr = 0;
}
5 changes: 3 additions & 2 deletions src/LogBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Yii;
use yii\base\Behavior;
use yii\base\Component;

/**
* Log Behavior.
Expand All @@ -21,7 +22,7 @@
class LogBehavior extends Behavior
{
/**
* @var Queue
* @var Queue|null|Component
* @inheritdoc
*/
public $owner;
Expand Down Expand Up @@ -138,7 +139,7 @@ protected function getExecTitle(ExecEvent $event): string
{
$title = $this->getJobTitle($event);
$extra = "attempt: $event->attempt";
if ($pid = $event->sender->getWorkerPid()) {
if ($pid = $event->sender?->getWorkerPid()) {
$extra .= ", PID: $pid";
}
return "$title ($extra)";
Expand Down
4 changes: 2 additions & 2 deletions src/PushEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class PushEvent extends JobEvent
/**
* @var int
*/
public int $delay;
public int $delay = 0;
/**
* @var int|string|null
*/
public string|int|null $priority;
public string|int|null $priority = null;
}
20 changes: 13 additions & 7 deletions src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace yii\queue;

use Throwable;
use yii\base\Component;
use yii\base\InvalidArgumentException;
use yii\base\InvalidConfigException;
Expand Down Expand Up @@ -88,8 +89,6 @@ public function init(): void
{
parent::init();

$this->serializer = Instance::ensure($this->serializer, SerializerInterface::class);

if ($this->ttr <= 0) {
throw new InvalidConfigException('Default TTR must be greater that zero.');
}
Expand Down Expand Up @@ -141,7 +140,7 @@ public function priority(int|string|null $value): static
* @param JobInterface|mixed $job
* @return int|string|null id of a job message
*/
public function push($job): int|string|null
public function push(mixed $job): int|string|null
{
$event = new PushEvent([
'job' => $job,
Expand Down Expand Up @@ -174,7 +173,7 @@ public function push($job): int|string|null
throw new InvalidArgumentException('Job delay must be positive.');
}

$message = $this->serializer->serialize($event->job);
$message = $this->getSerializer()->serialize($event->job);
$event->id = $this->pushMessage($message, $event->ttr, $event->delay, $event->priority);
$this->trigger(self::EVENT_AFTER_PUSH, $event);

Expand Down Expand Up @@ -225,8 +224,9 @@ protected function handleMessage(int|string $id, string $message, int $ttr, int
return $this->handleError($event);
}
try {
$event->result = $event->job->execute($this);
} catch (\Exception|\Throwable $error) {
/** @psalm-suppress PossiblyUndefinedMethod */
$event->result = $event->job?->execute($this);
} catch (Throwable $error) {
$event->error = $error;
return $this->handleError($event);
}
Expand All @@ -243,7 +243,7 @@ protected function handleMessage(int|string $id, string $message, int $ttr, int
public function unserializeMessage(string $serialized): array
{
try {
$job = $this->serializer->unserialize($serialized);
$job = $this->getSerializer()->unserialize($serialized);
} catch (\Exception $e) {
return [null, new InvalidJobException($serialized, $e->getMessage(), 0, $e)];
}
Expand Down Expand Up @@ -307,4 +307,10 @@ public function isDone(int|string $id): bool
* @return int status code
*/
abstract public function status(int|string $id): int;

private function getSerializer(): SerializerInterface
{
/** @psalm-var SerializerInterface */
return Instance::ensure($this->serializer, SerializerInterface::class);
}
}
5 changes: 2 additions & 3 deletions src/RetryableJobInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace yii\queue;

use Exception;
use Throwable;

/**
Expand All @@ -27,8 +26,8 @@ public function getTtr(): int;

/**
* @param int $attempt number
* @param Exception|Throwable $error from last execute of the job
* @param Throwable|null $error from last execute of the job
* @return bool
*/
public function canRetry(int $attempt, $error): bool;
public function canRetry(int $attempt, ?Throwable $error): bool;
}
2 changes: 1 addition & 1 deletion src/cli/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class Action extends BaseAction
*/
public Queue $queue;
/**
* @var Command|ConsoleController
* @inheritdoc
*/
public $controller;

Expand Down
12 changes: 7 additions & 5 deletions src/cli/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ public function beforeAction($action): bool
if ($this->phpBinary === null) {
$this->phpBinary = PHP_BINARY;
}
$this->queue->messageHandler = function ($id, $message, $ttr, $attempt) {
return $this->handleMessage($id, $message, (int)$ttr, (int)$attempt);
/** @psalm-suppress MissingClosureReturnType */
$this->queue->messageHandler = function (int|string|null $id, string $message, int $ttr, int $attempt) {
return $this->handleMessage($id, $message, $ttr, $attempt);
};
}

Expand All @@ -136,14 +137,14 @@ public function beforeAction($action): bool
* Executes a job.
* The command is internal, and used to isolate a job execution. Manual usage is not provided.
*
* @param string|null $id of a message
* @param string $id of a message
* @param int $ttr time to reserve
* @param int $attempt number
* @param int $pid of a worker
* @return int exit code
* @internal It is used with isolate mode.
*/
public function actionExec(?string $id, int $ttr, int $attempt, int $pid): int
public function actionExec(string $id, int $ttr, int $attempt, int $pid): int
{
if ($this->queue->execute($id, file_get_contents('php://stdin'), $ttr, $attempt, $pid ?: null)) {
return self::EXEC_DONE;
Expand All @@ -164,6 +165,7 @@ public function actionExec(?string $id, int $ttr, int $attempt, int $pid): int
protected function handleMessage(int|string|null $id, string $message, ?int $ttr, int $attempt): bool
{
// Child process command: php yii queue/exec "id" "ttr" "attempt" "pid"
/** @psalm-suppress PossiblyUndefinedArrayOffset */
$cmd = [
$this->phpBinary,
$_SERVER['SCRIPT_FILENAME'],
Expand All @@ -185,7 +187,7 @@ protected function handleMessage(int|string|null $id, string $message, ?int $ttr

$process = new Process($cmd, null, null, $message, $ttr);
try {
$result = $process->run(function ($type, $buffer) {
$result = $process->run(function (string $type, string $buffer) {
if ($type === Process::ERR) {
$this->stderr($buffer);
} else {
Expand Down
2 changes: 0 additions & 2 deletions src/cli/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ abstract class Queue extends BaseQueue implements BootstrapInterface
*/
protected function getCommandId(): string
{
/** @psalm-suppress UndefinedClass */
foreach (Yii::$app->getComponents(false) as $id => $component) {
if ($component === $this) {
return Inflector::camel2id($id);
Expand Down Expand Up @@ -103,7 +102,6 @@ protected function runWorker(callable $handler): ?int
{
$this->_workerPid = getmypid();
/** @var LoopInterface $loop */
/** @psalm-suppress UndefinedClass */
$loop = Yii::createObject($this->loopConfig, [$this]);

$event = new WorkerEvent(['loop' => $loop]);
Expand Down
Loading

0 comments on commit f915784

Please sign in to comment.