Skip to content

Commit

Permalink
Up the robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Oct 9, 2023
1 parent f0d297c commit 27c2d90
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 46 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"lexik/jwt-authentication-bundle": "^2.16",
"matthiasnoback/symfony-config-test": "^4.3",
"matthiasnoback/symfony-dependency-injection-test": "^4.3",
"php-http/message-factory": "^1.1",
"phpspec/phpspec": "^7.2",
"phpspec/prophecy-phpunit": "^2.0",
"phpunit/phpunit": "^9.6",
Expand Down
19 changes: 19 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<file src="src/Api/Controller/Action/UploadGiftCardConfigurationImageAction.php">
<DeprecatedInterface>
<code>LegacyIriConverterInterface|IriConverterInterface</code>
<code>LegacyIriConverterInterface|IriConverterInterface</code>
</DeprecatedInterface>
</file>
<file src="src/DependencyInjection/Configuration.php">
<UnusedPsalmSuppress>
<code>PossiblyUndefinedMethod</code>
</UnusedPsalmSuppress>
</file>
<file src="src/Generator/GiftCardCodeGenerator.php">
<UnusedPsalmSuppress>
<code>ArgumentTypeCoercion</code>
</UnusedPsalmSuppress>
</file>
</files>
16 changes: 12 additions & 4 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedPsalmSuppress="true"
findUnusedBaselineEntry="true"
findUnusedVariablesAndParams="false"
findUnusedCode="false"
errorLevel="1"
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<directory name="src"/>
Expand All @@ -16,10 +21,6 @@
</projectFiles>
<plugins>
<pluginClass class="Psalm\SymfonyPsalmPlugin\Plugin">
<!-- Symfony 4 container XML -->
<containerXml>tests/Application/var/cache/test/ApplicationTests_Setono_SyliusGiftCardPlugin_Application_KernelTestDebugContainer.xml</containerXml>

<!-- Symfony 5 container XML -->
<containerXml>tests/Application/var/cache/test/Tests_Setono_SyliusGiftCardPlugin_Application_KernelTestDebugContainer.xml</containerXml>
</pluginClass>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
Expand All @@ -28,9 +29,16 @@
<forbiddenFunctions>
<function name="dd"/>
<function name="dump"/>
<function name="var_dump"/>
<function name="print_r"/>
</forbiddenFunctions>
<issueHandlers>
<PluginIssue name="QueryBuilderSetParameter" errorLevel="suppress"/>
<RedundantConditionGivenDocblockType errorLevel="suppress"/>
<MissingTemplateParam>
<errorLevel type="suppress">
<directory name="src/Form"/>
</errorLevel>
</MissingTemplateParam>
</issueHandlers>
</psalm>
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Setono\SyliusGiftCardPlugin\Api\Controller\Action;

use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Core\Api\IriConverterInterface as LegacyIriConverterInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationImageInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationInterface;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -23,13 +23,17 @@ final class UploadGiftCardConfigurationImageAction

private ImageUploaderInterface $imageUploader;

private IriConverterInterface $iriConverter;
/** @var LegacyIriConverterInterface|IriConverterInterface */
private $iriConverter;

/**
* @param LegacyIriConverterInterface|IriConverterInterface $iriConverter
*/
public function __construct(
FactoryInterface $giftCardConfigurationImageFactory,
RepositoryInterface $giftCardConfigurationImageRepository,
ImageUploaderInterface $imageUploader,
IriConverterInterface $iriConverter
$iriConverter
) {
$this->giftCardConfigurationImageFactory = $giftCardConfigurationImageFactory;
$this->giftCardConfigurationImageRepository = $giftCardConfigurationImageRepository;
Expand All @@ -52,13 +56,10 @@ public function __invoke(Request $request): GiftCardConfigurationImageInterface

$image->setType($imageType);

/** @var string $ownerIri */
$ownerIri = $request->request->get('owner') ?? $request->query->get('type');
Assert::notEmpty($ownerIri);
Assert::stringNotEmpty($ownerIri);

/** @var ResourceInterface|GiftCardConfigurationInterface $owner */
$owner = $this->iriConverter->getItemFromIri($ownerIri);
Assert::isInstanceOf($owner, GiftCardConfigurationInterface::class);
$owner = $this->getOwner($ownerIri);

$oldImages = $owner->getImagesByType($imageType);
foreach ($oldImages as $oldImage) {
Expand All @@ -71,4 +72,17 @@ public function __invoke(Request $request): GiftCardConfigurationImageInterface

return $image;
}

private function getOwner(string $ownerIri): GiftCardConfigurationInterface
{
if ($this->iriConverter instanceof LegacyIriConverterInterface) {
$owner = $this->iriConverter->getItemFromIri($ownerIri);
} else {
$owner = $this->iriConverter->getResourceFromIri($ownerIri);
}

Assert::isInstanceOf($owner, GiftCardConfigurationInterface::class);

return $owner;
}
}
1 change: 0 additions & 1 deletion src/Controller/Action/DownloadGiftCardPdfAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ private function sendErrorResponse(Request $request, string $redirectUrl, string
{
$session = $request->getSession();

/** @psalm-suppress RedundantConditionGivenDocblockType */
if ($session instanceof Session) {
$flashBag = $session->getFlashBag();
$flashBag->add('error', $message);
Expand Down
18 changes: 5 additions & 13 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
use Setono\SyliusGiftCardPlugin\Form\Type\GiftCardType;
use Setono\SyliusGiftCardPlugin\Model\GiftCard;
use Setono\SyliusGiftCardPlugin\Model\GiftCardChannelConfiguration;
use Setono\SyliusGiftCardPlugin\Model\GiftCardChannelConfigurationInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfiguration;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationImage;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationImageInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardInterface;
use Setono\SyliusGiftCardPlugin\Provider\PdfRenderingOptionsProviderInterface;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
Expand All @@ -37,7 +33,7 @@ public function getConfigTreeBuilder(): TreeBuilder
$rootNode = $treeBuilder->getRootNode();

/**
* @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod
* @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod,UndefinedInterfaceMethod
*/
$rootNode
->addDefaultsIfNotSet()
Expand Down Expand Up @@ -95,7 +91,7 @@ private function addResourcesSection(ArrayNodeDefinition $node): void

private function addGiftCardSection(NodeBuilder $nodeBuilder): void
{
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod */
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,UndefinedInterfaceMethod */
$nodeBuilder
->arrayNode('gift_card')
->addDefaultsIfNotSet()
Expand All @@ -105,7 +101,6 @@ private function addGiftCardSection(NodeBuilder $nodeBuilder): void
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(GiftCard::class)->cannotBeEmpty()->end()
->scalarNode('interface')->defaultValue(GiftCardInterface::class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->defaultValue(GiftCardRepository::class)->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(GiftCardType::class)->end()
Expand All @@ -115,7 +110,7 @@ private function addGiftCardSection(NodeBuilder $nodeBuilder): void

private function addGiftCardConfigurationSection(NodeBuilder $nodeBuilder): void
{
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod */
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,UndefinedInterfaceMethod */
$nodeBuilder
->arrayNode('gift_card_configuration')
->addDefaultsIfNotSet()
Expand All @@ -125,7 +120,6 @@ private function addGiftCardConfigurationSection(NodeBuilder $nodeBuilder): void
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(GiftCardConfiguration::class)->cannotBeEmpty()->end()
->scalarNode('interface')->defaultValue(GiftCardConfigurationInterface::class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->defaultValue(GiftCardConfigurationRepository::class)->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(GiftCardConfigurationType::class)->end()
Expand All @@ -135,7 +129,7 @@ private function addGiftCardConfigurationSection(NodeBuilder $nodeBuilder): void

private function addGiftCardConfigurationImageSection(NodeBuilder $nodeBuilder): void
{
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod */
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,UndefinedInterfaceMethod */
$nodeBuilder
->arrayNode('gift_card_configuration_image')
->addDefaultsIfNotSet()
Expand All @@ -145,7 +139,6 @@ private function addGiftCardConfigurationImageSection(NodeBuilder $nodeBuilder):
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(GiftCardConfigurationImage::class)->cannotBeEmpty()->end()
->scalarNode('interface')->defaultValue(GiftCardConfigurationImageInterface::class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->defaultValue(EntityRepository::class)->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(GiftCardConfigurationImageType::class)->end()
Expand All @@ -155,7 +148,7 @@ private function addGiftCardConfigurationImageSection(NodeBuilder $nodeBuilder):

private function addChannelConfigurationSection(NodeBuilder $nodeBuilder): void
{
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod */
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,UndefinedInterfaceMethod */
$nodeBuilder
->arrayNode('gift_card_channel_configuration')
->addDefaultsIfNotSet()
Expand All @@ -165,7 +158,6 @@ private function addChannelConfigurationSection(NodeBuilder $nodeBuilder): void
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(GiftCardChannelConfiguration::class)->cannotBeEmpty()->end()
->scalarNode('interface')->defaultValue(GiftCardChannelConfigurationInterface::class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->defaultValue(EntityRepository::class)->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(GiftCardChannelConfigurationType::class)->end()
Expand Down
8 changes: 3 additions & 5 deletions src/DependencyInjection/SetonoSyliusGiftCardExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Webmozart\Assert\Assert;

final class SetonoSyliusGiftCardExtension extends AbstractResourceExtension
{
Expand Down Expand Up @@ -65,12 +66,9 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('services.xml');

if ($container->hasParameter('kernel.bundles')) {
/**
* @var array $bundles
*
* @psalm-suppress UndefinedDocblockClass
*/
$bundles = $container->getParameter('kernel.bundles');
Assert::isArray($bundles);

if (array_key_exists('SetonoSyliusCatalogPromotionPlugin', $bundles)) {
$loader->load('services/conditional/catalog_promotion_rule.xml');
}
Expand Down
2 changes: 0 additions & 2 deletions src/EmailManager/GiftCardEmailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public function sendEmailToCustomerWithGiftCard(CustomerInterface $customer, Gif
$channel = $this->customerChannelResolver->resolve($customer);

$this->wrapTemporaryLocale($localeCode, function () use ($email, $customer, $giftCard, $channel, $localeCode): void {
/** @psalm-suppress DeprecatedMethod */
$this->sender->send(

Check failure on line 59 in src/EmailManager/GiftCardEmailManager.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP 8.1 | Dependency versions: highest | Symfony: ^5.4)

DeprecatedMethod

src/EmailManager/GiftCardEmailManager.php:59:28: DeprecatedMethod: The method Sylius\Component\Mailer\Sender\SenderInterface::send has been marked as deprecated (see https://psalm.dev/001)
Emails::GIFT_CARD_CUSTOMER,
[$email],
Expand Down Expand Up @@ -92,7 +91,6 @@ public function sendEmailWithGiftCardsFromOrder(OrderInterface $order, array $gi
$localeCode = $this->localeResolver->resolveFromOrder($order);

$this->wrapTemporaryLocale($localeCode, function () use ($email, $giftCards, $order, $channel, $localeCode): void {
/** @psalm-suppress DeprecatedMethod */
$this->sender->send(

Check failure on line 94 in src/EmailManager/GiftCardEmailManager.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP 8.1 | Dependency versions: highest | Symfony: ^5.4)

DeprecatedMethod

src/EmailManager/GiftCardEmailManager.php:94:28: DeprecatedMethod: The method Sylius\Component\Mailer\Sender\SenderInterface::send has been marked as deprecated (see https://psalm.dev/001)
Emails::GIFT_CARD_ORDER,
[$email],
Expand Down
3 changes: 3 additions & 0 deletions src/Form/Type/AddGiftCardToOrderType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @extends AbstractType<AddGiftCardToOrderCommand>
*/
final class AddGiftCardToOrderType extends AbstractType
{
private DataTransformerInterface $giftCardToCodeDataTransformer;
Expand Down
3 changes: 3 additions & 0 deletions src/Model/GiftCardBalanceCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use OutOfBoundsException;
use Webmozart\Assert\Assert;

/**
* @implements Iterator<array-key, GiftCardBalance>
*/
final class GiftCardBalanceCollection implements Countable, Iterator
{
/** @var GiftCardBalance[] */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Setono\SyliusGiftCardPlugin\Serializer\Normalizer;

use ArrayObject;
use Setono\MainRequestTrait\MainRequestTrait;
use Setono\SyliusGiftCardPlugin\Exception\UnexpectedTypeException;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationInterface;
use Symfony\Component\HttpFoundation\RequestStack;
Expand All @@ -15,8 +14,6 @@

final class GiftCardConfigurationNormalizer implements ContextAwareNormalizerInterface
{
use MainRequestTrait;

private ObjectNormalizer $objectNormalizer;

private RequestStack $requestStack;
Expand Down Expand Up @@ -50,7 +47,7 @@ public function normalize($object, $format = null, array $context = [])

$data['image'] = '';

$request = $this->getMainRequestFromRequestStack($this->requestStack);
$request = $this->requestStack->getMainRequest();
if (null !== $request) {
$data['image'] = $request->getSchemeAndHttpHost() . '/bundles/setonosyliusgiftcardplugin/setono-logo.png';
}
Expand Down
8 changes: 0 additions & 8 deletions tests/Unit/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@
use Setono\SyliusGiftCardPlugin\Form\Type\GiftCardType;
use Setono\SyliusGiftCardPlugin\Model\GiftCard;
use Setono\SyliusGiftCardPlugin\Model\GiftCardChannelConfiguration;
use Setono\SyliusGiftCardPlugin\Model\GiftCardChannelConfigurationInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfiguration;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationImage;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationImageInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardInterface;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
Expand Down Expand Up @@ -94,7 +90,6 @@ public function processed_configuration_for_array_node_1(): void
'gift_card' => [
'classes' => [
'model' => GiftCard::class,
'interface' => GiftCardInterface::class,
'controller' => ResourceController::class,
'repository' => GiftCardRepository::class,
'form' => GiftCardType::class,
Expand All @@ -104,7 +99,6 @@ public function processed_configuration_for_array_node_1(): void
'gift_card_configuration' => [
'classes' => [
'model' => GiftCardConfiguration::class,
'interface' => GiftCardConfigurationInterface::class,
'controller' => ResourceController::class,
'repository' => GiftCardConfigurationRepository::class,
'form' => GiftCardConfigurationType::class,
Expand All @@ -114,7 +108,6 @@ public function processed_configuration_for_array_node_1(): void
'gift_card_configuration_image' => [
'classes' => [
'model' => GiftCardConfigurationImage::class,
'interface' => GiftCardConfigurationImageInterface::class,
'controller' => ResourceController::class,
'repository' => EntityRepository::class,
'form' => GiftCardConfigurationImageType::class,
Expand All @@ -124,7 +117,6 @@ public function processed_configuration_for_array_node_1(): void
'gift_card_channel_configuration' => [
'classes' => [
'model' => GiftCardChannelConfiguration::class,
'interface' => GiftCardChannelConfigurationInterface::class,
'controller' => ResourceController::class,
'repository' => EntityRepository::class,
'form' => GiftCardChannelConfigurationType::class,
Expand Down

0 comments on commit 27c2d90

Please sign in to comment.