Skip to content

Commit

Permalink
feat: add phpstan (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim authored Nov 10, 2023
1 parent 628f8bb commit 0aef4a9
Show file tree
Hide file tree
Showing 23 changed files with 200 additions and 95 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/code-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ on:
jobs:
php-cs-fixer:
uses: FriendsOfShopware/actions/.github/workflows/php-cs-fixer.yml@main
phpstan:
uses: FriendsOfShopware/actions/.github/workflows/phpstan.yml@main
85 changes: 43 additions & 42 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
{
"name": "frosh/tools",
"version": "1.2.3",
"description": "Provides some basic things for managing the Shopware Installation",
"type": "shopware-platform-plugin",
"license": "MIT",
"autoload": {
"psr-4": {
"Frosh\\Tools\\": "src/"
}
},
"authors": [
{
"name": "FriendsOfShopware",
"homepage": "https://friendsofshopware.com"
}
],
"extra": {
"shopware-plugin-class": "Frosh\\Tools\\FroshTools",
"label": {
"de-DE": "Tools",
"en-GB": "Tools"
"name": "frosh/tools",
"version": "1.2.3",
"description": "Provides some basic things for managing the Shopware Installation",
"type": "shopware-platform-plugin",
"license": "MIT",
"autoload": {
"psr-4": {
"Frosh\\Tools\\": "src/"
}
},
"description": {
"de-DE": "Die kostenlose Shopware 6 App beinhaltet mehrere Werkzeuge um den Shop besser zu verwalten. Optimierter Cache Manager, Log Viewer um die Logeinträge zu sehen oder Tasks auszuführen.",
"en-GB": "The free Shopware 6 app include several tools to manage the store much better like a cache manager, log viewer to see the log entries, run the tasks or see the system status."
"authors": [
{
"name": "FriendsOfShopware",
"homepage": "https://friendsofshopware.com"
}
],
"extra": {
"shopware-plugin-class": "Frosh\\Tools\\FroshTools",
"label": {
"de-DE": "Tools",
"en-GB": "Tools"
},
"description": {
"de-DE": "Die kostenlose Shopware 6 App beinhaltet mehrere Werkzeuge um den Shop besser zu verwalten. Optimierter Cache Manager, Log Viewer um die Logeinträge zu sehen oder Tasks auszuführen.",
"en-GB": "The free Shopware 6 app include several tools to manage the store much better like a cache manager, log viewer to see the log entries, run the tasks or see the system status."
},
"manufacturerLink": {
"de-DE": "https://github.com/FriendsOfShopware/FroshTools",
"en-GB": "https://github.com/FriendsOfShopware/FroshTools"
},
"supportLink": {
"de-DE": "https://github.com/FriendsOfShopware/FroshTools/issues",
"en-GB": "https://github.com/FriendsOfShopware/FroshTools/issues"
}
},
"manufacturerLink": {
"de-DE": "https://github.com/FriendsOfShopware/FroshTools",
"en-GB": "https://github.com/FriendsOfShopware/FroshTools"
"require": {

Check warning on line 37 in composer.json

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Security advisories for Composer packages

\[EA\] Please add roave/security-advisories:dev-latest into require-dev as a firewall for vulnerable components.
"shopware/core": "~6.5.2"
},
"supportLink": {
"de-DE": "https://github.com/FriendsOfShopware/FroshTools/issues",
"en-GB": "https://github.com/FriendsOfShopware/FroshTools/issues"
}
},
"require": {
"shopware/core": "~6.5.2"
},
"config": {
"allow-plugins": {
"symfony/runtime": true
"config": {
"allow-plugins": {
"symfony/runtime": true
}
},
"scripts": {
"cs-fix": "docker run --rm -v $(pwd):$(pwd) -w $(pwd) oskarstark/php-cs-fixer-ga --rules @PER-CS2.0,@PER-CS2.0:risky --allow-risky=yes .",
"phpstan": "docker run --rm -v $(pwd):/app aragon999/phpstan-shopware:v6.5.6 analyse --level max ."
}
},
"scripts": {
"cs-fix": "docker run --rm -v $(pwd):$(pwd) -w $(pwd) oskarstark/php-cs-fixer-ga --rules @PER-CS2.0,@PER-CS2.0:risky --allow-risky=yes ."
}
}
4 changes: 4 additions & 0 deletions src/Command/ChangeUserPasswordCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\User\UserCollection;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -18,6 +19,9 @@
#[AsCommand('frosh:user:change:password', 'Change user password')]
class ChangeUserPasswordCommand extends Command
{
/**
* @param EntityRepository<UserCollection> $userRepository
*/
public function __construct(private readonly EntityRepository $userRepository)
{
parent::__construct();
Expand Down
9 changes: 6 additions & 3 deletions src/Command/DevRobotsTxtCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$robotsFile = fopen($robotsPath, 'wb');
\assert($robotsFile !== false);

$robotsContent = "#soc\nUser-agent: *\nDisallow: /\n#eoc";
fwrite($robotsFile, $robotsContent);
fclose($robotsFile);
Expand All @@ -60,11 +62,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return self::SUCCESS;
}

private function revertToOriginal($input, $output, $robotsPath): int
private function revertToOriginal(InputInterface $input, OutputInterface $output, string $robotsPath): int
{
// returns robots.txt to original state
$io = new SymfonyStyle($input, $output);
$file = file_get_contents($robotsPath);

$file = (string) file_get_contents($robotsPath);
$createdString = "#soc\nUser-agent: *\nDisallow: /\n#eoc";

// If only input from command is present
Expand All @@ -89,7 +92,7 @@ private function changeRobotsTxt(InputInterface $input, OutputInterface $output,
// change robots.txt to disable crawlers
$io = new SymfonyStyle($input, $output);

$file = file_get_contents($robotsPath);
$file = (string) file_get_contents($robotsPath);
$commandString = "#soc\nUser-agent: *\nDisallow: /\n#eoc";

// If command is called multiple times
Expand Down
8 changes: 7 additions & 1 deletion src/Command/MonitorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskCollection;
use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskDefinition;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\Console\Attribute\AsCommand;
Expand All @@ -30,6 +31,9 @@ class MonitorCommand extends Command
private const MONITOR_EMAIL_OPTION = 'email';
private const MONITOR_SALESCHANNEL_ARG = 'sales-channel';

/**
* @param EntityRepository<ScheduledTaskCollection> $scheduledTaskRepository
*/
public function __construct(
#[Autowire(service: MailService::class)]
private readonly AbstractMailService $mailService,
Expand Down Expand Up @@ -103,7 +107,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int

private function queueFailed(): bool
{
$oldestMessage = (int) $this->connection->fetchOne('SELECT IFNULL(MIN(created_at), 0) FROM messenger_messages');
/** @var string $createdAt */
$createdAt = $this->connection->fetchOne('SELECT IFNULL(MIN(created_at), 0) FROM messenger_messages');
$oldestMessage = (int)$createdAt;
$oldestMessage /= 10000;
$minutes = $this->configService->getInt(
'FroshTools.config.monitorQueueGraceTime'
Expand Down
3 changes: 2 additions & 1 deletion src/Components/CacheAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ private function getCacheAdapter(AdapterInterface $adapter): AdapterInterface
return $adapter;
}

private function getRedis(AdapterInterface $adapter): ?\Redis
private function getRedis(AdapterInterface $adapter): \Redis
{
if ($adapter instanceof RedisTagAwareAdapter) {
$redisProxyGetter = \Closure::bind(fn() => $adapter->redis, $adapter, RedisTagAwareAdapter::class);
} else {
// @phpstan-ignore-next-line
$redisProxyGetter = \Closure::bind(fn() => $adapter->redis, $adapter, RedisAdapter::class);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Components/CacheRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public function addAdapter(string $name, CacheAdapter $adapter): void
$this->adapters[$name] = $adapter;
}

/**
* @return CacheAdapter[]
*/
public function all(): array
{
return $this->adapters;
Expand Down
19 changes: 17 additions & 2 deletions src/Components/Elasticsearch/ElasticsearchManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public function isEnabled(): bool
return $this->enabled;
}

/**
* @return array<mixed>
*/
public function info(): array
{
return [
Expand All @@ -43,6 +46,9 @@ public function info(): array
];
}

/**
* @return array<mixed>
*/
public function indices(): array
{
$indices = $this->client->indices()->get(['index' => '*']);
Expand All @@ -64,12 +70,21 @@ public function indices(): array
return $list;
}

/**
* @return array<mixed>
*/
public function deleteIndex(string $name): array
{
return $this->client->indices()->delete(['index' => $name]);
}

public function proxy(string $method, string $path, array $params, array $body): array
/**
* @param array<mixed> $body
* @param array<mixed> $params
*
* @return array<mixed>|callable
*/
public function proxy(string $method, string $path, array $params, array $body): array|callable
{
if ($body === []) {
$body = null;

Check warning on line 90 in src/Components/Elasticsearch/ElasticsearchManager.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Callable parameter usage violates definition

\[EA\] New value type (null) is not matching the resolved parameter type and might introduce types-related false-positives.
Expand Down Expand Up @@ -101,7 +116,7 @@ public function switchAlias(): void

public function deleteUnusedIndices(): void
{
$indices = $this->outdatedIndexDetector->get();
$indices = $this->outdatedIndexDetector->get() ?? [];

foreach ($indices as $index) {
$this->client->indices()->delete(['index' => $index]);
Expand Down
8 changes: 7 additions & 1 deletion src/Components/Environment/EnvironmentFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class EnvironmentFile implements \Stringable
{
/**
* @param array<string, EnvironmentLine> $items
* @param list<EnvironmentLine> $items
*/
public function __construct(private array $items) {}

Expand Down Expand Up @@ -60,6 +60,9 @@ public function delete(string $key): void
}
}

/**
* @return array<string>
*/
public function keys(): array
{
$keys = [];
Expand All @@ -73,6 +76,9 @@ public function keys(): array
return $keys;
}

/**
* @return array<string, string>
*/
public function values(): array
{
$values = [];
Expand Down
5 changes: 3 additions & 2 deletions src/Components/Environment/EnvironmentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ public function read(string $path): EnvironmentFile
throw new \RuntimeException(sprintf('Cannot read file %s', $path));
}

/** @var array<string> $lines */
$lines = preg_split('/\r\n|\r|\n/', $content);
$parsedLines = [];
$lineCount = (is_countable($lines) ? \count($lines) : 0) - 1;
$lineCount = \count($lines) - 1;
foreach ($lines as $line) {
$line = trim((string) $line);
$line = trim($line);

if ($line === '' || $line[0] === '#') {
$parsedLines[] = EnvironmentCommentLine::parse($line);
Expand Down
3 changes: 3 additions & 0 deletions src/Components/Health/Checker/HealthChecker/MysqlChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ private function checkMysqlVersion(HealthCollection $collection, string $version
));
}

/**
* @return array{mysql?: string, mariadb?: string}
*/
private function extract(string $versionString): array
{
if (mb_stripos($versionString, 'mariadb') === false) {
Expand Down
12 changes: 0 additions & 12 deletions src/Components/Health/Checker/HealthChecker/PhpChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,6 @@ private function checkMemoryLimit(HealthCollection $collection): void
{
$minMemoryLimit = $this->parseQuantity('512m');
$currentMemoryLimit = \ini_get('memory_limit');
if ($currentMemoryLimit === false) {
$collection->add(
SettingsResult::error(
'php-memory-limit',
'Memory-Limit',
'unknown',
'min ' . $this->formatSize($minMemoryLimit)
)
);

return;
}

$currentMemoryLimit = $this->parseQuantity($currentMemoryLimit);
if ($currentMemoryLimit < $minMemoryLimit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __construct(

public function collect(HealthCollection $collection): void
{
/** @var array{scheduled_task_class: class-string, next_execution_time: string}[] $data */
$data = $this->connection->createQueryBuilder()
->select('s.scheduled_task_class', 's.next_execution_time')
->from('scheduled_task', 's')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function collect(HealthCollection $collection): void
return;
}

if (version_compare($extractedVersion['mysql'], '8.0.0', '>=')) {
if (version_compare($extractedVersion['mysql'] ?? '', '8.0.0', '>=')) {
return;
}

Expand All @@ -39,6 +39,9 @@ public function collect(HealthCollection $collection): void
);
}

/**
* @return array{mysql?: string, mariadb?: string}
*/
private function extract(string $versionString): array
{
if (mb_stripos($versionString, 'mariadb') === false) {
Expand Down
Loading

0 comments on commit 0aef4a9

Please sign in to comment.