Skip to content

Commit

Permalink
ci(phpstan): fixing type errors - found by phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
sgc-fireball committed Feb 22, 2025
1 parent 6ae4200 commit 57fbf1a
Show file tree
Hide file tree
Showing 31 changed files with 123 additions and 103 deletions.
7 changes: 6 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ parameters:
dynamicConstantNames:
- SWOOLE
ignoreErrors:
- '#is never assigned null so it can be removed from the property type.#'
- '#of anonymous function has typehint with deprecated#'
- '#has invalid type Composer\\Script\\Event\.#'
- '#Result of && is always false#'
- '#Right side of && is always false#'
- '#Left side of && is always false#'
- '#Ternary operator condition is always true#'
- '#If condition is always true#'
- '#Used function Swoole\\.*#'
-
Expand All @@ -27,6 +29,9 @@ parameters:
-
message: '#Constructor of class TinyFramework\\Broadcast\\SwooleBroadcast has an unused parameter \$config\.#'
path: src/Broadcast/SwooleBroadcast.php
-
message: '#Call to function is_object\(\) with stdClass will always evaluate to true.#'
path: src/Mail/IMAP.php

level: 6
paths:
Expand Down
14 changes: 0 additions & 14 deletions src/Console/Commands/TinyframeworkCacheClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,4 @@ public function run(InputInterface $input, OutputInterface $output): int
return $exitCode;
}

private function clearFile(string $title, string $path): void
{
$this->output->write('[<green>....</green>] ' . $title);
if (file_exists($path)) {
if (unlink($path)) {
$this->output->write("\r[<green>DONE</green>]\n");
} else {
$this->output->write("\r[<red>FAIL</red>]\n");
}
} else {
$this->output->write("\r[<yellow>DONE</yellow>]\n");
}
}

}
4 changes: 2 additions & 2 deletions src/Console/Commands/TinyframeworkCronjobCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ private function runCronjobs(InputInterface $input, OutputInterface $output): in
function (CronjobInterface|CronjobAwesome $job) use ($now) {
$cron = new CronExpression($job->expression());
$mustRun = $cron->isDue($now);
/** @var int $verbosity */
if ($mustRun && $job instanceof CronjobAwesome) {
$mustRun = !$job->skip();
}
/** @var int $verbosity */
$verbosity = $this->output->verbosity();
if (!$mustRun && $verbosity >= OutputInterface::VERBOSITY_VERBOSE) {
$this->output->writeln(
Expand All @@ -72,8 +72,8 @@ function (CronjobInterface|CronjobAwesome $job) use ($now) {
);

foreach ($jobs as $job) {
$start = microtime(true);
try {
$start = microtime(true);
if ($job instanceof CronjobAwesome) {
$this->output->write(sprintf("\r[INIT ] <yellow>%s</yellow>", get_class($job)));
$job->onStart();
Expand Down
14 changes: 8 additions & 6 deletions src/Console/Commands/TinyframeworkSystemdInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ public function run(InputInterface $input, OutputInterface $output): int
}
$this->output->write("\r[<green>DONE</green>]\n");

$this->execute('systemctl daemon-reload');
$this->execute('systemctl enable ' . escapeshellarg($name));
$this->execute('systemctl restart ' . escapeshellarg($name));
$output = [];
$exitCode = 0;
$this->execute('systemctl daemon-reload', $output, $exitCode);
$this->execute('systemctl enable ' . escapeshellarg($name), $output, $exitCode);
$this->execute('systemctl restart ' . escapeshellarg($name), $output, $exitCode);

return 0;
}
Expand Down Expand Up @@ -95,11 +97,11 @@ private function getSystemdServiceFile(InputInterface $input): string
return implode("\n", $content);
}

private function execute(string $command, array &$output = null, int &$result_code = null): string|false
private function execute(string $command, array &$output = [], int &$result_code = 0): string|false
{
$this->output->write('[<green>....</green>] Run: ' . $command);
$result = exec($command, $output, $exitCode);
if ($exitCode) {
$result = exec($command, $output, $result_code);
if ($result_code) {
$this->output->write("\r[<red>FAIL</red>]\n");
return $result;
}
Expand Down
5 changes: 4 additions & 1 deletion src/FileSystem/LocalFileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public function __construct(array $config)
private function getAbsolutePath(string $path): string
{
$path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path);
$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
$parts = array_filter(
explode(DIRECTORY_SEPARATOR, $path),
fn(string $value): bool => (bool)strlen($value)
);
$absolutes = [];
foreach ($parts as $part) {
if ('.' == $part) {
Expand Down
18 changes: 9 additions & 9 deletions src/Helpers/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ class DateTime extends \DateTime implements JsonSerializable, Stringable, DateTi

public static function setDefaultTimeZone(string $timezone = 'UTC'): void
{
static::$defaultTimeZone = $timezone;
self::$defaultTimeZone = $timezone;
}

public static function setFakeNow(DateTime $fakeNow): void
{
static::$fakeNow = $fakeNow;
self::$fakeNow = $fakeNow;
}

public static function clearFakeNow(): void
{
static::$fakeNow = null;
self::$fakeNow = null;
}

/**
* @throws Throwable
*/
public static function now(DateTimeZone|null $timezone = null): DateTime
{
if (static::$fakeNow) {
return static::$fakeNow->clone()->setTimezone($timezone ?? new DateTimeZone(static::$defaultTimeZone));
if (self::$fakeNow) {
return self::$fakeNow->clone()->setTimezone($timezone ?? new DateTimeZone(self::$defaultTimeZone));
}
return new DateTime('now', $timezone);
}
Expand All @@ -47,10 +47,10 @@ public function __construct(
string $datetime = 'now',
DateTimeZone|null $timezone = null
) {
if ($datetime === 'now' && static::$fakeNow instanceof DateTime) {
$datetime = static::$fakeNow->__toString();
if ($datetime === 'now' && self::$fakeNow instanceof DateTime) {
$datetime = self::$fakeNow->__toString();
}
parent::__construct($datetime, $timezone ?? new DateTimeZone(static::$defaultTimeZone));
parent::__construct($datetime, $timezone ?? new DateTimeZone(self::$defaultTimeZone));
}

public function getYear(): int
Expand Down Expand Up @@ -376,7 +376,7 @@ public function addYears(int $years): self
$this->modify('+' . $years . ' year');
return $this;
}

public function subSecond(int $second = 1): self
{
return $this->subSeconds($second);
Expand Down
16 changes: 8 additions & 8 deletions src/Helpers/Macroable.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function addStaticMacro(string $name, Closure|callable $callback):
);
}
}
static::$staticMacros[$name] = $callback;
self::$staticMacros[$name] = $callback;
}

public static function addMacro(string $name, Closure|callable $callback): void
Expand All @@ -50,25 +50,25 @@ public static function addMacro(string $name, Closure|callable $callback): void
);
}
}
static::$macros[$name] = $callback;
self::$macros[$name] = $callback;
}

public static function hasStaticMacro(string $name): bool
{
return array_key_exists($name, static::$staticMacros);
return array_key_exists($name, self::$staticMacros);
}

public static function hasMacro(string $name): bool
{
return array_key_exists($name, static::$macros);
return array_key_exists($name, self::$macros);
}

public static function __callStatic(string $name, array $arguments): mixed
{
if (static::hasStaticMacro($name)) {
$callback = static::$staticMacros[$name];
$callback = self::$staticMacros[$name];
if ($callback instanceof Closure) {
$callback->bindTo(null, static::class);
return $callback->bindTo(null, static::class)();
}
return $callback(...$arguments);
}
Expand All @@ -84,9 +84,9 @@ public static function __callStatic(string $name, array $arguments): mixed
public function __call(string $name, array $arguments): mixed
{
if (static::hasMacro($name)) {
$callback = static::$macros[$name];
$callback = self::$macros[$name];
if ($callback instanceof Closure) {
return $callback->bindTo($this, static::class);
return $callback->bindTo($this, static::class)();
}
return $callback(...$arguments);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Helpers/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(string $path)
throw new RuntimeException('Please install the image ext-imagick first.');
}
if (!command_exists('convert')) {
$this->markTestSkipped('Missing imagick cli programms.');
throw new RuntimeException('Please install the imagick cli programms first.');
}
if (!is_readable($path)) {
throw new RuntimeException('Argument #1 $path must be a valid and readable filepath.');
Expand Down Expand Up @@ -82,7 +82,7 @@ public function thumbnail(int $page = 1, int $width = 1920): ?Image
$imagick->setResolution(300, 300);
$imagick->readImage($this->path . '[' . ($page - 1) . ']');
$imagick->scaleImage($width, (int)($width / $this->getWidth() * $this->getHeight()));
$imagick->setImageColorspace(255);
$imagick->setImageColorspace(Imagick::COLORSPACE_SRGB);
$imagick->setImageFormat('png');
$imagick->setCompressionQuality(100);
$imagick->setImageBackgroundColor(new ImagickPixel('white'));
Expand Down
4 changes: 2 additions & 2 deletions src/Helpers/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private static function wrap(mixed $result): Promise

/**
* @param mixed $value
* @return Promise<mixed>
* @return Promise
*/
public static function resolve(mixed $value = null): Promise
{
Expand All @@ -31,7 +31,7 @@ public static function resolve(mixed $value = null): Promise

/**
* @param Throwable $value
* @return Promise<Throwable>
* @return Promise
*/
public static function reject(Throwable $value = null): Promise
{
Expand Down
4 changes: 2 additions & 2 deletions src/Helpers/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,13 @@ public function padRight(int $length, string $pad_string = " "): static
return $this;
}

public function replace(array|string $search, array|string $replace, int &$count = null): static
public function replace(array|string $search, array|string $replace, int &$count = 0): static
{
$this->value = \str_replace($search, $replace, $this->value, $count);
return $this;
}

public function ireplace(array|string $search, array|string $replace, int &$count = null): static
public function ireplace(array|string $search, array|string $replace, int &$count = 0): static
{
$this->value = \str_ireplace($search, $replace, $this->value, $count);
return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/Helpers/Video.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function frame(string|int $second = 5): Image
* @link https://developer.bitmovin.com/playback/docs/webvtt-based-thumbnails
* @link https://bitmovin.com/demos/thumbnail-seeking
* @param int $second default is zero, that means we are creating automatically 100 positions, but maximal one per second.
* @return array<Image, string>
* @return array{0: Image, 1: string}
*/
public function webvtt(string $target, int $second = 0, int $width = 120): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Helpers/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ function datetime(string $datetime = 'now', DateTimeZone|null $timezone = null):
}

if (!function_exists('old')) {
function old(string $field, $default = null): mixed
function old(string $field, mixed $default = null): mixed
{
$request = container('request');
assert($request instanceof RequestInterface);
Expand Down
6 changes: 5 additions & 1 deletion src/Logger/DioLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class DioLogger extends LoggerAwesome implements LoggerInterface
{
private string $path;
/** @var resource|null */
private $fp;

public function __construct(#[\SensitiveParameter] array $config)
Expand All @@ -26,6 +27,9 @@ public function __construct(#[\SensitiveParameter] array $config)

public function log(string $level, string $message, array $context = []): static
{
if (!$message) {
return $this;
}
$message = json_encode([
'type' => 'log',
'host' => gethostname(),
Expand All @@ -43,7 +47,7 @@ public function log(string $level, string $message, array $context = []): static
return $this;
}
$bytes = dio_write($this->fp, $message);
if ($bytes === false) {
if ($bytes === 0) {
trigger_error('Could not write log message.', E_USER_WARNING);
}
return $this;
Expand Down
12 changes: 4 additions & 8 deletions src/Mail/IMAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function close(): self
return $this;
}

public function setOption(string $option, $value = null): self
public function setOption(string $option, mixed $value = null): self
{
$option = trim(strtolower($option));
switch ($option) {
Expand Down Expand Up @@ -185,7 +185,7 @@ public function listEmails(): array
public function getFolderInformation(): array
{
$result = @imap_mailboxmsginfo($this->connection);
if ($result === false) {
if (!is_object($result)) {
throw new RuntimeException(@imap_last_error());
}
return [
Expand All @@ -211,11 +211,7 @@ public function getUIDByMsgNo(int $messageNumber): int

public function getMsgNoByUID(int $uid): int
{
$result = @imap_msgno($this->connection, $uid);
if ($result === false) {
throw new RuntimeException(@imap_last_error());
}
return $result;
return @imap_msgno($this->connection, $uid);
}

public function getMessageOverviewByUID(int $uid): array
Expand Down Expand Up @@ -388,7 +384,7 @@ public function getPermissions(string $folder): array
throw new RuntimeException('Unknown folder.');
}
$result = @imap_getacl($this->connection, @imap_utf7_encode($folder));
if ($result === false || !is_array($result)) {
if (!is_array($result)) {
throw new RuntimeException(@imap_last_error());
}
return $result;
Expand Down
2 changes: 1 addition & 1 deletion src/OpenAPI/Objects/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class Callback extends AbstractObject
{

/** @var ?object<string, PathItem|Reference> */
/** @var null|stdClass<PathItem|Reference> */
public ?object $paths = null;

public static function parse(array $arr): Callback
Expand Down
Loading

0 comments on commit 57fbf1a

Please sign in to comment.