Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup Psalm error level to 1 #505

Merged
merged 14 commits into from
Dec 4, 2023
10 changes: 7 additions & 3 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
errorLevel="1"
findUnusedBaselineEntry="true"
findUnusedCode="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -12,8 +12,9 @@
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
<directory name="src/debug" />
<directory name="src/gii" />
<directory name="src/debug/views" />
<directory name="src/gii/default" />
<file name="src/gii/form.php" />
<directory name="src/drivers/db/migrations" />
</ignoreFiles>
</projectFiles>
Expand All @@ -24,4 +25,7 @@
<file name="stubs/psalm/BaseYii.php" preloadClasses="true"/>
<file name="support/ide-helper.php"/>
</stubs>
<issueHandlers>
<MixedAssignment errorLevel="suppress"/>
</issueHandlers>
</psalm>
4 changes: 2 additions & 2 deletions src/JobEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ abstract class JobEvent extends Event
*/
public $name = '';
/**
* @var Queue|null|object
* @var Queue
* @inheritdoc
* @psalm-suppress PropertyNotSetInConstructor
* @psalm-suppress PropertyNotSetInConstructor, NonInvariantDocblockPropertyType
*/
public $sender;
/**
Expand Down
24 changes: 17 additions & 7 deletions src/LogBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,13 @@ public function afterError(ExecEvent $event): void
*/
public function workerStart(cli\WorkerEvent $event): void
{
$title = 'Worker ' . $event->sender->getWorkerPid();
Yii::info("$title is started.", Queue::class);
Yii::beginProfile($title, Queue::class);
$workerPid = $event->sender->getWorkerPid();
if (null !== $workerPid) {
$title = 'Worker ' . $workerPid;
Yii::info("$title is started.", Queue::class);
Yii::beginProfile($title, Queue::class);
}

s1lver marked this conversation as resolved.
Show resolved Hide resolved
if ($this->autoFlush) {
Yii::getLogger()->flush(true);
}
Expand All @@ -111,9 +115,13 @@ public function workerStart(cli\WorkerEvent $event): void
*/
public function workerStop(cli\WorkerEvent $event): void
{
$title = 'Worker ' . $event->sender->getWorkerPid();
Yii::endProfile($title, Queue::class);
Yii::info("$title is stopped.", Queue::class);
$workerPid = $event->sender->getWorkerPid();
if (null !== $workerPid) {
$title = 'Worker ' . $workerPid;
Yii::endProfile($title, Queue::class);
Yii::info("$title is stopped.", Queue::class);
}

if ($this->autoFlush) {
Yii::getLogger()->flush(true);
}
Expand All @@ -139,7 +147,9 @@ protected function getExecTitle(ExecEvent $event): string
{
$title = $this->getJobTitle($event);
$extra = "attempt: $event->attempt";
if ($pid = $event->sender?->getWorkerPid()) {

$pid = $event->sender->getWorkerPid();
if (null !== $pid) {
$extra .= ", PID: $pid";
}
return "$title ($extra)";
Expand Down
2 changes: 1 addition & 1 deletion src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ protected function handleMessage(int|string $id, string $message, int $ttr, int
return $this->handleError($event);
}
try {
/** @psalm-suppress PossiblyUndefinedMethod */
/** @psalm-suppress PossiblyUndefinedMethod, MixedMethodCall */
$event->result = $event->job?->execute($this);
} catch (Throwable $error) {
$event->error = $error;
Expand Down
1 change: 1 addition & 0 deletions src/cli/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function init(): void
/**
* @param string $string
* @return string
* @psalm-suppress MixedInferredReturnType, MixedReturnStatement
*/
protected function format(string $string): string
{
Expand Down
9 changes: 7 additions & 2 deletions src/cli/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,12 @@ public function beforeAction($action): bool
if ($this->phpBinary === null) {
$this->phpBinary = PHP_BINARY;
}
/** @psalm-suppress MissingClosureReturnType */
$this->queue->messageHandler = function (int|string|null $id, string $message, int $ttr, int $attempt) {
$this->queue->messageHandler = function (
int|string|null $id,
string $message,
int $ttr,
int $attempt
): bool {
return $this->handleMessage($id, $message, $ttr, $attempt);
};
}
Expand Down Expand Up @@ -178,6 +182,7 @@ protected function handleMessage(int|string|null $id, string $message, ?int $ttr

foreach ($this->getPassedOptions() as $name) {
if (in_array($name, $this->options('exec'), true)) {
/** @psalm-suppress MixedOperand */
$cmd[] = '--' . $name . '=' . $this->$name;
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/cli/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ abstract class Queue extends BaseQueue implements BootstrapInterface
* @var int|null current process ID of a worker.
* @since 2.0.2
*/
private ?int $_workerPid = null;
private ?int $workerPid = null;

/**
* @return string command id
Expand All @@ -72,7 +72,7 @@ protected function getCommandId(): string
{
foreach (Yii::$app->getComponents(false) as $id => $component) {
if ($component === $this) {
return Inflector::camel2id($id);
return Inflector::camel2id((string)$id);
}
}
throw new InvalidConfigException('Queue must be an application component.');
Expand Down Expand Up @@ -100,7 +100,7 @@ public function bootstrap($app): void
*/
protected function runWorker(callable $handler): ?int
{
$this->_workerPid = getmypid();
$this->workerPid = getmypid();
/** @var LoopInterface $loop */
$loop = Yii::createObject($this->loopConfig, [$this]);

Expand All @@ -117,7 +117,7 @@ protected function runWorker(callable $handler): ?int
});
} finally {
$this->trigger(self::EVENT_WORKER_STOP, $event);
$this->_workerPid = null;
$this->workerPid = null;
}

return $event->exitCode;
Expand All @@ -132,11 +132,12 @@ protected function runWorker(callable $handler): ?int
*/
public function getWorkerPid(): ?int
{
return $this->_workerPid;
return $this->workerPid;
}

/**
* @inheritdoc
* @psalm-suppress MixedReturnStatement, MixedInferredReturnType
*/
protected function handleMessage(int|string $id, string $message, int $ttr, int $attempt): bool
{
Expand All @@ -158,7 +159,7 @@ protected function handleMessage(int|string $id, string $message, int $ttr, int
*/
public function execute(string $id, string $message, int $ttr, int $attempt, ?int $workerPid): bool
{
$this->_workerPid = $workerPid;
$this->workerPid = $workerPid;
return parent::handleMessage($id, $message, $ttr, $attempt);
}
}
6 changes: 3 additions & 3 deletions src/cli/SignalLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class SignalLoop extends BaseObject implements LoopInterface
{
/**
* @var array of signals to exit from listening of the queue.
* @var array<int> of signals to exit from listening of the queue.
*/
public array $exitSignals = [
15, // SIGTERM
Expand All @@ -30,12 +30,12 @@ class SignalLoop extends BaseObject implements LoopInterface
1, // SIGHUP
];
/**
* @var array of signals to suspend listening of the queue.
* @var array<int> of signals to suspend listening of the queue.
* For example: SIGTSTP
*/
public array $suspendSignals = [];
/**
* @var array of signals to resume listening of the queue.
* @var array<int> of signals to resume listening of the queue.
* For example: SIGCONT
*/
public array $resumeSignals = [];
Expand Down
11 changes: 6 additions & 5 deletions src/cli/VerboseBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ class VerboseBehavior extends Behavior
public Controller $command;

/**
* @var float|null timestamp
* @var float timestamp
*/
private ?float $jobStartedAt = null;
private float $jobStartedAt = 0;
/**
* @var int|null timestamp
* @var int timestamp
*/
private ?int $workerStartedAt = null;
private int $workerStartedAt = 0;

/**
* @inheritdoc
Expand Down Expand Up @@ -121,7 +121,8 @@ protected function jobTitle(ExecEvent $event): string
{
$name = $event->job instanceof JobInterface ? get_class($event->job) : 'unknown job';
$extra = "attempt: $event->attempt";
if ($pid = $event->sender?->getWorkerPid()) {
$pid = $event->sender->getWorkerPid();
if (null !== $pid) {
$extra .= ", pid: $pid";
}
return " [$event->id] $name ($extra)";
Expand Down
3 changes: 1 addition & 2 deletions src/closure/Behavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ class Behavior extends \yii\base\Behavior
{
/**
* @var Queue
* @psalm-suppress NonInvariantDocblockPropertyType
* @psalm-suppress PropertyNotSetInConstructor
* @psalm-suppress PropertyNotSetInConstructor, NonInvariantDocblockPropertyType
*/
public $owner;

Expand Down
6 changes: 5 additions & 1 deletion src/closure/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace yii\queue\closure;

use Laravel\SerializableClosure\SerializableClosure;
use Laravel\SerializableClosure\Serializers\Native;
use yii\queue\JobInterface;
use yii\queue\Queue;
Expand All @@ -32,13 +33,16 @@ class Job implements JobInterface
*/
public function execute(Queue $queue)
{
$closure = unserialize($this->serialized)->getClosure();
/** @var SerializableClosure $unserialize */
$unserialize = unserialize($this->serialized);
$closure = $unserialize->getClosure();
$nativeClosure = $closure();

if ($nativeClosure instanceof Native) {
return $nativeClosure();
}

/** @psalm-var JobInterface $nativeClosure */
return $nativeClosure->execute($queue);
}
}
14 changes: 10 additions & 4 deletions src/debug/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Yii;
use yii\base\NotSupportedException;
use yii\base\ViewContextInterface;
use yii\debug\Panel as BasePanel;
use yii\helpers\VarDumper;
use yii\queue\JobInterface;
use yii\queue\PushEvent;
Expand All @@ -23,10 +24,11 @@
* Debug Panel.
*
* @author Roman Zhuravlev <zhuravljov@gmail.com>
* @psalm-suppress PropertyNotSetInConstructor
*/
class Panel extends \yii\debug\Panel implements ViewContextInterface
class Panel extends BasePanel implements ViewContextInterface
{
private array $_jobs = [];
private array $jobs = [];

/**
* @inheritdoc
Expand All @@ -43,7 +45,7 @@ public function init(): void
{
parent::init();
PushEvent::on(Queue::class, Queue::EVENT_AFTER_PUSH, function (PushEvent $event) {
$this->_jobs[] = $this->getPushData($event);
$this->jobs[] = $this->getPushData($event);
});
}

Expand Down Expand Up @@ -82,7 +84,7 @@ protected function getPushData(PushEvent $event): array
*/
public function save()
{
return ['jobs' => $this->_jobs];
return ['jobs' => $this->jobs];
}

/**
Expand All @@ -98,6 +100,7 @@ public function getViewPath(): string
*/
public function getSummary(): string
{
/** @psalm-var array{jobs: array} $this->data */
return Yii::$app->view->render('summary', [
'url' => $this->getUrl(),
'count' => isset($this->data['jobs']) ? count($this->data['jobs']) : 0,
Expand All @@ -109,12 +112,15 @@ public function getSummary(): string
*/
public function getDetail(): string
{
/** @psalm-var array{jobs: array} $this->data */
$jobs = $this->data['jobs'] ?? [];
foreach ($jobs as &$job) {
/** @psalm-var array{sender: string, id: string|int} $job */
$job['status'] = 'unknown';
/** @var Queue $queue */
if ($queue = Yii::$app->get($job['sender'], false)) {
try {
/** @psalm-var Queue $queue */
if ($queue->isWaiting($job['id'])) {
$job['status'] = 'waiting';
} elseif ($queue->isReserved($job['id'])) {
Expand Down
4 changes: 3 additions & 1 deletion src/debug/views/detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
declare(strict_types=1);

/**
* @var \yii\web\View $this
* @var View $this
* @var array $jobs
*/

use yii\helpers\Html;
use yii\web\View;

$styles = [
'unknown' => 'default',
Expand Down
5 changes: 4 additions & 1 deletion src/debug/views/summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
declare(strict_types=1);

/**
* @var \yii\web\View $this
* @var View $this
* @var string $url
* @var int $count
*/

use yii\web\View;

?>
<div class="yii-debug-toolbar__block">
<a href="<?= $url ?>">
Expand Down
3 changes: 1 addition & 2 deletions src/drivers/amqp_interop/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class Command extends CliCommand
{
/**
* @var Queue
* @psalm-suppress NonInvariantDocblockPropertyType
* @psalm-suppress PropertyNotSetInConstructor
* @psalm-suppress PropertyNotSetInConstructor, NonInvariantDocblockPropertyType
*/
public CliQueue $queue;

Expand Down
Loading
Loading