Skip to content

Commit

Permalink
fix: correct several PHPStan errors (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
tinect committed Sep 14, 2023
1 parent ac7f524 commit 3e2e55e
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 12 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/test.yaml
@@ -0,0 +1,33 @@
name: Test
on:
workflow_dispatch:
push:
branches:
- main
pull_request_target:
schedule:
- cron: '0 0 * * *'

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
shopware-version:
- "~6.5.0.0"
- "~6.5.1.0"
- "~6.5.2.0"
- "~6.5.3.0"
- "~6.5.4.0"
- "~6.5.5.0"
- "dev-trunk"
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: 'PHPStan'
run: |
composer require shopware/core:${{ matrix.shopware-version }} --no-interaction --no-progress --optimize-autoloader
composer show shopware/core | grep versions
./vendor/bin/phpstan analyse
3 changes: 3 additions & 0 deletions composer.json
Expand Up @@ -47,5 +47,8 @@
"allow-plugins": {
"symfony/runtime": true
}
},
"require-dev": {
"phpstan/phpstan": "^1.10"
}
}
6 changes: 6 additions & 0 deletions phpstan.neon
@@ -0,0 +1,6 @@
parameters:
checkGenericClassInNonGenericObjectType: false
level: max
paths:
- src

5 changes: 4 additions & 1 deletion src/Command/MigrateMailCommand.php
Expand Up @@ -47,7 +47,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$progressBar->start();

while ($ids = $iterator->fetch()) {
$message = new MigrateMailMessage(\array_values($ids));
/** @var array<int, string> $ids */
$ids = \array_values($ids);

$message = new MigrateMailMessage($ids);

if ($input->getOption('sync')) {
$this->migrateMailHandler->__invoke($message);
Expand Down
14 changes: 14 additions & 0 deletions src/Content/MailArchive/MailArchiveEntity.php
Expand Up @@ -12,8 +12,10 @@ class MailArchiveEntity extends Entity
{
use EntityIdTrait;

/** @var array<string, string> */
protected array $sender;

/** @var array<string, string> */
protected array $receiver;

protected string $subject;
Expand All @@ -40,21 +42,33 @@ class MailArchiveEntity extends Entity
/** @var EntityCollection<MailArchiveAttachmentEntity>|null $attachments */
protected ?EntityCollection $attachments = null;

/**
* @return array<string, string>
*/
public function getSender(): array
{
return $this->sender;
}

/**
* @param array<string, string> $sender
*/
public function setSender(array $sender): void
{
$this->sender = $sender;
}

/**
* @return array<string, string>
*/
public function getReceiver(): array
{
return $this->receiver;
}

/**
* @param array<string, string> $receiver
*/
public function setReceiver(array $receiver): void
{
$this->receiver = $receiver;
Expand Down
48 changes: 43 additions & 5 deletions src/Controller/Api/MailArchiveController.php
Expand Up @@ -10,7 +10,6 @@
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\PlatformRequest;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand Down Expand Up @@ -102,12 +101,20 @@ public function download(Request $request): JsonResponse
throw new \RuntimeException('Cannot read eml file or file is empty');
}

$fileName = $mailArchive->getCreatedAt()->format('Y-m-d_H-i-s') . ' ' . $mailArchive->getSubject() . '.eml';
$fileNameParts = [];

if ($mailArchive->getCreatedAt() !== null) {
$fileNameParts[] = $mailArchive->getCreatedAt()->format('Y-m-d_H-i-s');
}

$fileNameParts[] = $mailArchive->getSubject();

$fileName = $this->getFileName($fileNameParts) . '.eml';

return new JsonResponse([
'success' => true,
'content' => $content,
'fileName' => preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $fileName),
'fileName' => $fileName,
]);
}

Expand All @@ -128,6 +135,9 @@ public function attachment(Request $request): JsonResponse
}

$mailArchive = $attachment->getMailArchive();
if (!$mailArchive instanceof MailArchiveEntity) {
throw MailArchiveException::notFound();
}

$emlPath = $mailArchive->getEmlPath();
$isEml = !empty($emlPath) && \is_string($emlPath);
Expand Down Expand Up @@ -156,13 +166,22 @@ public function attachment(Request $request): JsonResponse
throw new \RuntimeException('Cannot find attachment in eml file');
}

$fileName = $mailArchive->getCreatedAt()->format('Y-m-d_H-i-s') . ' ' . $mailArchive->getSubject() . ' ' . $attachment->getFileName();
$fileNameParts = [];

if ($mailArchive->getCreatedAt() !== null) {
$fileNameParts[] = $mailArchive->getCreatedAt()->format('Y-m-d_H-i-s');
}

$fileNameParts[] = $mailArchive->getSubject();
$fileNameParts[] = $attachment->getFileName();

$fileName = $this->getFileName($fileNameParts);

return new JsonResponse([
'success' => true,
'content' => \base64_encode($content),
'contentType' => $attachment->getContentType(),
'fileName' => preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $fileName),
'fileName' => $fileName,
]);
}

Expand All @@ -189,6 +208,10 @@ private function enrichFromEml(string $emlPath, Email $email): void
}

foreach ($message->getAllAttachmentParts() as $attachment) {
if ($attachment->getContent() === null) {
continue;
}

$email->attach($attachment->getContent(), $attachment->getFilename(), $attachment->getContentType());
}
}
Expand All @@ -209,6 +232,9 @@ private function enrichFromDatabase(MailArchiveEntity $mailArchive, Email $email
$email->text($mailArchive->getPlainText());
}

/**
* @return string|array<Address>|\DateTimeImmutable|null
*/
private function getHeaderValue(IHeader $header): string|array|null|\DateTimeImmutable
{
if ($header instanceof AddressHeader) {
Expand All @@ -226,4 +252,16 @@ private function getHeaderValue(IHeader $header): string|array|null|\DateTimeImm

return $header->getValue();
}

/**
* @param array<string> $fileNameParts
*/
private function getFileName(array $fileNameParts): string
{
return (string) preg_replace(
'/[\x00-\x1F\x7F-\xFF]/',
'',
\implode(' ', $fileNameParts)
);
}
}
14 changes: 12 additions & 2 deletions src/FroshPlatformMailArchive.php
Expand Up @@ -14,8 +14,18 @@ public function uninstall(UninstallContext $uninstallContext): void
return;
}

$this->container->get(Connection::class)->executeStatement('DROP TABLE IF EXISTS frosh_mail_archive');
$this->container->get(Connection::class)->executeStatement('DROP TABLE IF EXISTS frosh_mail_archive_attachment');
$container = $this->container;
if ($container === null) {
return;
}

$connection = $container->get(Connection::class);
if (!$connection instanceof Connection) {
return;
}

$connection->executeStatement('DROP TABLE IF EXISTS frosh_mail_archive');
$connection->executeStatement('DROP TABLE IF EXISTS frosh_mail_archive_attachment');
}

public function executeComposerCommands(): bool
Expand Down
2 changes: 1 addition & 1 deletion src/MessageQueue/MigrateMailMessage.php
Expand Up @@ -7,7 +7,7 @@
class MigrateMailMessage implements AsyncMessageInterface
{
/**
* @param string[] $ids
* @param array<int, string> $ids
*/
public function __construct(
public readonly array $ids
Expand Down
5 changes: 5 additions & 0 deletions src/Services/EmlFileManager.php
Expand Up @@ -20,6 +20,11 @@ public function __construct(
public function writeFile(string $id, string $content): string
{
$content = \gzcompress($content, 9);

if ($content === false) {
throw new \RuntimeException('Cannot compress eml file');
}

$emlFilePath = 'mails/' . $id . '.eml.gz';

$this->filesystem->write($emlFilePath, $content);
Expand Down
1 change: 1 addition & 0 deletions src/Services/MailSender.php
Expand Up @@ -103,6 +103,7 @@ private function getCustomerIdByMail(array $to): ?string

/**
* @param Address[] $addresses
* @return array<string, string>
*/
private function convertAddress(array $addresses): array
{
Expand Down
4 changes: 2 additions & 2 deletions src/Subscriber/MailArchiveDeleteSubscriber.php
Expand Up @@ -27,7 +27,7 @@ public static function getSubscribedEvents(): array

public function beforeDelete(BeforeDeleteEvent $event): void
{
/** @var list<string> $affected */
/** @var array<string> $ids */
$ids = array_values($event->getIds(MailArchiveDefinition::ENTITY_NAME));
if (empty($ids)) {
return;
Expand All @@ -40,7 +40,7 @@ public function beforeDelete(BeforeDeleteEvent $event): void
/** @var PartialEntity $mail */
foreach ($mails as $mail) {
$emlPath = $mail->get('emlPath');
if (empty($emlPath)) {
if (empty($emlPath) || !\is_string($emlPath)) {
continue;
}

Expand Down
5 changes: 4 additions & 1 deletion src/Task/CleanupTaskHandler.php
Expand Up @@ -8,7 +8,6 @@
use Shopware\Core\Defaults;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

Expand Down Expand Up @@ -53,6 +52,10 @@ public function run(): void
}

foreach ($result as $item) {
if (empty($item['eml_path']) || !\is_string($item['eml_path'])) {
continue;
}

$this->emlFileManager->deleteEmlFile($item['eml_path']);
}

Expand Down

0 comments on commit 3e2e55e

Please sign in to comment.