Skip to content

Commit

Permalink
chore: Fix PHPStan issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mitelg committed Aug 5, 2024
1 parent 1bd7796 commit 7291cb9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 75 deletions.
35 changes: 0 additions & 35 deletions .phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1380,41 +1380,11 @@ parameters:
count: 1
path: engine/Library/Enlight/Components/Auth/Adapter/DbTable.php

-
message: "#^Method Enlight_Components_Cron_Adapter\\:\\:startJob\\(\\) has no return type specified\\.$#"
count: 1
path: engine/Library/Enlight/Components/Cron/Adapter.php

-
message: "#^Argument \\#0 expects a constant string, got string$#"
count: 4
path: engine/Library/Enlight/Components/Cron/Adapter/DBAL.php

-
message: "#^Method Enlight_Components_Cron_Adapter_DBAL\\:\\:isJobStillOverdue\\(\\) has parameter \\$jobId with no type specified\\.$#"
count: 1
path: engine/Library/Enlight/Components/Cron/Adapter/DBAL.php

-
message: "#^Method Enlight_Components_Cron_Adapter_DBAL\\:\\:startJob\\(\\) has no return type specified\\.$#"
count: 1
path: engine/Library/Enlight/Components/Cron/Adapter/DBAL.php

-
message: "#^Method Enlight_Components_Cron_Adapter_DBAL\\:\\:updateJob\\(\\) should return Enlight_Components_Cron_Manager but return statement is missing\\.$#"
count: 1
path: engine/Library/Enlight/Components/Cron/Adapter/DBAL.php

-
message: "#^Negated boolean expression is always false\\.$#"
count: 1
path: engine/Library/Enlight/Components/Cron/Adapter/DBAL.php

-
message: "#^Parameter \\#2 \\$value of method Enlight_Components_Cron_Adapter_DBAL\\:\\:getJobByColumn\\(\\) expects string, int given\\.$#"
count: 1
path: engine/Library/Enlight/Components/Cron/Adapter/DBAL.php

-
message: "#^Method Enlight_Components_Cron_Job\\:\\:__construct\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -1500,11 +1470,6 @@ parameters:
count: 1
path: engine/Library/Enlight/Components/Cron/Job.php

-
message: "#^Method Enlight_Components_Cron_Manager\\:\\:disableJob\\(\\) should return Enlight_Components_Cron_Adapter but returns Enlight_Components_Cron_Manager\\.$#"
count: 1
path: engine/Library/Enlight/Components/Cron/Manager.php

-
message: "#^Method Enlight_Components_Cron_Manager\\:\\:endJob\\(\\) has no return type specified\\.$#"
count: 1
Expand Down
2 changes: 2 additions & 0 deletions engine/Library/Enlight/.php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

use PhpCsFixer\Config;
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
use PhpCsFixerCustomFixers\Fixer\NoSuperfluousConcatenationFixer;
use PhpCsFixerCustomFixers\Fixer\NoUselessCommentFixer;
use PhpCsFixerCustomFixers\Fixer\NoUselessDirnameCallFixer;
Expand Down Expand Up @@ -56,6 +57,7 @@
->registerCustomFixers(new Fixers())
->setRiskyAllowed(true)
->setCacheFile('var/cache/php-cs-fixer-enlight')
->setParallelConfig(ParallelConfigFactory::detect())
->setRules([
'@PSR12' => true,
'@Symfony' => true,
Expand Down
7 changes: 5 additions & 2 deletions engine/Library/Enlight/Components/Cron/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,19 @@ interface Enlight_Components_Cron_Adapter
/**
* Updates a cron job in the cron tab
*
* @return Enlight_Components_Cron_Manager
* @return void
*/
public function updateJob(Enlight_Components_Cron_Job $job);

/**
* @return bool
*/
public function startJob(Enlight_Components_Cron_Job $job);

/**
* Returns an array of Enlight_Components_Cron_Job from the crontab
*
* @return Enlight_Components_Cron_Job[]
* @return array<int, Enlight_Components_Cron_Job>
*/
public function getAllJobs();

Expand Down
44 changes: 16 additions & 28 deletions engine/Library/Enlight/Components/Cron/Adapter/DBAL.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* Enlight
*
Expand Down Expand Up @@ -34,20 +36,17 @@ class Enlight_Components_Cron_Adapter_DBAL implements Enlight_Components_Cron_Ad
*/
protected $tableName = 's_crontab';

/**
* @var Connection
*/
private $connection;
private Connection $connection;

/**
* @var Enlight_Components_Cron_Job[]|null
* @var array<int, Enlight_Components_Cron_Job>|null
*/
private $allJobsList;
private ?array $allJobsList = null;

/**
* @var Enlight_Components_Cron_Job[]|null
* @var array<int, Enlight_Components_Cron_Job>|null
*/
private $overdueJobsList;
private ?array $overdueJobsList = null;

public function __construct(Connection $connection)
{
Expand Down Expand Up @@ -106,17 +105,12 @@ public function getAllJobs()
$qb->select('*')
->from($this->tableName, 'c');

$ignoreActive = true;
if (!$ignoreActive) {
$qb->andWhere('c.active = true');
}

$rows = $qb->execute()->fetchAll();
$rows = $qb->execute()->fetchAllAssociative();

$jobs = [];
foreach ($rows as $row) {
$row['data'] = unserialize($row['data'], ['allowed_classes' => false]);
$jobs[$row['id']] = new Enlight_Components_Cron_Job($row);
$jobs[(int) $row['id']] = new Enlight_Components_Cron_Job($row);
}

return $jobs;
Expand All @@ -140,7 +134,7 @@ public function getNextJob($force = false)
}

while (($nextJob = array_pop($this->overdueJobsList)) !== null) {
if ($this->isJobStillOverdue($nextJob->getId())) {
if ($this->isJobStillOverdue((int) $nextJob->getId())) {
return $nextJob;
}
}
Expand Down Expand Up @@ -194,12 +188,9 @@ public function deleteJob(Enlight_Components_Cron_Job $job)
/**
* Internal helper method to grep data based on a given column name.
*
* @param string $column
* @param string $value
*
* @return Enlight_Components_Cron_Job|null
* @param string|int $value
*/
private function getJobByColumn($column, $value)
private function getJobByColumn(string $column, $value): ?Enlight_Components_Cron_Job
{
$qb = $this->connection->createQueryBuilder();

Expand All @@ -222,9 +213,9 @@ private function getJobByColumn($column, $value)
}

/**
* @return Enlight_Components_Cron_Job[]
* @return array<int, Enlight_Components_Cron_Job>
*/
private function getOverdueJobs()
private function getOverdueJobs(): array
{
$qb = $this->connection->createQueryBuilder();

Expand All @@ -241,16 +232,13 @@ private function getOverdueJobs()
$overdueJobsList = [];
foreach ($rows as $row) {
$row['data'] = unserialize($row['data'], ['allowed_classes' => false]);
$overdueJobsList[$row['id']] = new Enlight_Components_Cron_Job($row);
$overdueJobsList[(int) $row['id']] = new Enlight_Components_Cron_Job($row);
}

return $overdueJobsList;
}

/**
* @return bool
*/
private function isJobStillOverdue($jobId)
private function isJobStillOverdue(int $jobId): bool
{
$qb = $this->connection->createQueryBuilder();
$qb->select('*')
Expand Down
4 changes: 2 additions & 2 deletions engine/Library/Enlight/Components/Cron/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ public function getEventManager()
/**
* Deactivate a given Cron Job in the crontab
*
* @return Enlight_Components_Cron_Adapter
* @return void
*/
public function disableJob(Enlight_Components_Cron_Job $job)
{
$job->setActive(false);

return $this->adapter->updateJob($job);
$this->adapter->updateJob($job);
}

/**
Expand Down
19 changes: 11 additions & 8 deletions engine/Shopware/Components/Log/Parser/LogfileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
use LimitIterator;
use SplFileObject;

/**
* @phpstan-type Log array{date?: string, channel?: string, level?: string, message?: string, context?: string, extra?: string, raw: string}
*/
class LogfileParser
{
/**
Expand All @@ -37,7 +40,7 @@ class LogfileParser
* @param int|null $limit
* @param bool $reverse
*
* @return array<array{data?: string, channel?: string, level?: string, message?: string, context?: string, extra?: string, raw: string}>
* @return list<Log>
*/
public function parseLogFile($file, $offset = null, $limit = null, $reverse = false)
{
Expand Down Expand Up @@ -78,7 +81,7 @@ public function countLogFile($filePath)
}

/**
* @return array{data?: string, channel?: string, level?: string, message?: string, context?: string, extra?: string, raw: string}
* @return Log
*/
private function parseLine(string $log): array
{
Expand All @@ -93,12 +96,12 @@ private function parseLine(string $log): array
}

return [
'date' => (string) $data['date'],
'channel' => (string) $data['channel'],
'level' => (string) $data['level'],
'message' => (string) $data['message'],
'context' => json_decode($data['context'], true),
'extra' => json_decode($data['extra'], true),
'date' => $data['date'],
'channel' => $data['channel'] ?? '',
'level' => $data['level'] ?? '',
'message' => $data['message'] ?? '',
'context' => json_decode($data['context'] ?? '', true),
'extra' => json_decode($data['extra'] ?? '', true),
'raw' => $log,
];
}
Expand Down

0 comments on commit 7291cb9

Please sign in to comment.