Skip to content

Commit

Permalink
refactor #768 Move some namespaces on Exceptions (loic425)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.11 branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Bug fix?        | no
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | 
| License         | MIT


Commits
-------

2a61679 Move some namespaces on Exceptions
bfa6df9 Fix Psam errors
165873a Fix PHPStan errors
16cdabb Remove translatable factory spec
109800f Move spec file
b96ee2b Test the alias
cc473c0 Add InvalidArgumentException & Exception
e945ee9 Test the aliases
  • Loading branch information
lchrusciel authored Apr 17, 2024
2 parents ae389bf + e945ee9 commit 643c575
Show file tree
Hide file tree
Showing 29 changed files with 327 additions and 156 deletions.
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ parameters:
- %currentWorkingDirectory%/src/Bundle/Tests/*
- %currentWorkingDirectory%/src/Component/legacy/spec/*
- %currentWorkingDirectory%/src/Component/legacy/src/Annotation/*
- %currentWorkingDirectory%/src/Component/legacy/src/Exception/*
- %currentWorkingDirectory%/src/Component/legacy/src/Factory/*
- %currentWorkingDirectory%/src/Component/legacy/src/Generator/*
- %currentWorkingDirectory%/src/Component/legacy/src/Metadata/*
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
<TypeDoesNotContainType>
<errorLevel type="suppress">
<directory name="src/Component/legacy/src/Annotation" />
<directory name="src/Component/legacy/src/Exception" />
<directory name="src/Component/legacy/src/Factory" />
<directory name="src/Component/legacy/src/Generator" />
<directory name="src/Component/legacy/src/Metadata" />
Expand Down
4 changes: 2 additions & 2 deletions src/Bundle/Controller/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
use Doctrine\Persistence\ObjectManager;
use FOS\RestBundle\View\View;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use Sylius\Component\Resource\Exception\DeleteHandlingException;
use Sylius\Component\Resource\Exception\UpdateHandlingException;
use Sylius\Resource\Doctrine\Persistence\RepositoryInterface;
use Sylius\Resource\Exception\DeleteHandlingException;
use Sylius\Resource\Exception\UpdateHandlingException;
use Sylius\Resource\Factory\FactoryInterface;
use Sylius\Resource\Metadata\MetadataInterface;
use Sylius\Resource\Model\ResourceInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/Form/EventSubscriber/AddCodeFormSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sylius\Bundle\ResourceBundle\Form\EventSubscriber;

use Sylius\Component\Resource\Exception\UnexpectedTypeException;
use Sylius\Resource\Exception\UnexpectedTypeException;
use Sylius\Resource\Model\CodeAwareInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/spec/Controller/ResourceControllerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
use Sylius\Bundle\ResourceBundle\Controller\StateMachineInterface;
use Sylius\Bundle\ResourceBundle\Controller\ViewHandlerInterface;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use Sylius\Component\Resource\Exception\DeleteHandlingException;
use Sylius\Resource\Doctrine\Persistence\RepositoryInterface;
use Sylius\Resource\Exception\DeleteHandlingException;
use Sylius\Resource\Factory\FactoryInterface;
use Sylius\Resource\Metadata\MetadataInterface;
use Sylius\Resource\Model\ResourceInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
use Sylius\Resource\Exception\UnexpectedTypeException;
use Sylius\Resource\Model\CodeAwareInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\FormType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,12 @@
namespace spec\Sylius\Component\Resource\Exception;

use PhpSpec\ObjectBehavior;
use Sylius\Resource\Exception\DeleteHandlingException as NewDeleteHandlingException;

final class DeleteHandlingExceptionSpec extends ObjectBehavior
{
function it_extends_an_exception(): void
function it_should_be_an_alias_of_delete_handling_exception(): void
{
$this->shouldHaveType(\Exception::class);
}

function it_has_a_message(): void
{
$this->getMessage()->shouldReturn('Ups, something went wrong during deleting a resource, please try again.');
}

function it_has_a_flash(): void
{
$this->getFlash()->shouldReturn('something_went_wrong_error');
}

function it_has_an_api_response_code(): void
{
$this->getApiResponseCode()->shouldReturn(500);
$this->shouldBeAnInstanceOf(NewDeleteHandlingException::class);
}
}
21 changes: 3 additions & 18 deletions src/Component/legacy/spec/Exception/RaceConditionExceptionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,12 @@
namespace spec\Sylius\Component\Resource\Exception;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Resource\Exception\UpdateHandlingException;
use Sylius\Resource\Exception\RaceConditionException as NewRaceConditionException;

final class RaceConditionExceptionSpec extends ObjectBehavior
{
function it_extends_an_update_handling_exception(): void
function it_should_be_an_alias_of_race_condition_exception(): void
{
$this->shouldHaveType(UpdateHandlingException::class);
}

function it_has_a_message(): void
{
$this->getMessage()->shouldReturn('Operated entity was previously modified.');
}

function it_has_a_flash(): void
{
$this->getFlash()->shouldReturn('race_condition_error');
}

function it_has_an_api_response_code(): void
{
$this->getApiResponseCode()->shouldReturn(409);
$this->shouldBeAnInstanceOf(NewRaceConditionException::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace spec\Sylius\Component\Resource\Exception;

use PhpSpec\ObjectBehavior;
use Sylius\Resource\Exception\UnexpectedTypeException as NewUnexpectedTypeException;

final class UnexpectedTypeExceptionSpec extends ObjectBehavior
{
Expand All @@ -22,13 +23,8 @@ function let(): void
$this->beConstructedWith('stringValue', '\ExpectedType');
}

function it_extends_invalid_argument_exception(): void
function it_should_be_an_alias_of_unexpected_type_exception(): void
{
$this->shouldHaveType(\InvalidArgumentException::class);
}

function it_has_a_message(): void
{
$this->getMessage()->shouldReturn('Expected argument of type "\ExpectedType", "string" given.');
$this->shouldBeAnInstanceOf(NewUnexpectedTypeException::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace spec\Sylius\Component\Resource\Exception;

use PhpSpec\ObjectBehavior;
use Sylius\Resource\Exception\UnsupportedMethodException as NewUnsupportedMethodException;

final class UnsupportedMethodExceptionSpec extends ObjectBehavior
{
Expand All @@ -22,13 +23,8 @@ function let(): void
$this->beConstructedWith('methodName');
}

function it_extends_exception(): void
function it_should_be_an_alias_of_unsupported_method_exception(): void
{
$this->shouldHaveType(\Exception::class);
}

function it_has_a_message(): void
{
$this->getMessage()->shouldReturn('The method "methodName" is not supported.');
$this->shouldBeAnInstanceOf(NewUnsupportedMethodException::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,12 @@
namespace spec\Sylius\Component\Resource\Exception;

use PhpSpec\ObjectBehavior;
use Sylius\Resource\Exception\UpdateHandlingException as NewUpdateHandlingException;

final class UpdateHandlingExceptionSpec extends ObjectBehavior
{
function it_extends_an_exception(): void
function it_should_be_an_alias_of_unsupported_method_exception(): void
{
$this->shouldHaveType(\Exception::class);
}

function it_has_a_message(): void
{
$this->getMessage()->shouldReturn('Ups, something went wrong during updating a resource, please try again.');
}

function it_has_a_flash(): void
{
$this->getFlash()->shouldReturn('something_went_wrong_error');
}

function it_has_an_api_response_code(): void
{
$this->getApiResponseCode()->shouldReturn(400);
$this->shouldBeAnInstanceOf(NewUpdateHandlingException::class);
}
}
28 changes: 3 additions & 25 deletions src/Component/legacy/src/Exception/DeleteHandlingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,10 @@

namespace Sylius\Component\Resource\Exception;

class DeleteHandlingException extends \Exception
{
protected string $flash;
class_exists(\Sylius\Resource\Exception\DeleteHandlingException::class);

protected int $apiResponseCode;

public function __construct(
string $message = 'Ups, something went wrong during deleting a resource, please try again.',
string $flash = 'something_went_wrong_error',
int $apiResponseCode = 500,
int $code = 0,
?\Exception $previous = null,
) {
parent::__construct($message, $code, $previous);

$this->flash = $flash;
$this->apiResponseCode = $apiResponseCode;
}

public function getFlash(): string
{
return $this->flash;
}

public function getApiResponseCode(): int
if (false) {
class DeleteHandlingException extends \Sylius\Resource\Exception\DeleteHandlingException
{
return $this->apiResponseCode;
}
}
14 changes: 4 additions & 10 deletions src/Component/legacy/src/Exception/RaceConditionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@

namespace Sylius\Component\Resource\Exception;

class RaceConditionException extends UpdateHandlingException
{
public function __construct(?\Exception $previous = null)
class_exists(\Sylius\Resource\Exception\RaceConditionException::class);

if (false) {
class RaceConditionException extends \Sylius\Resource\Exception\RaceConditionException
{
parent::__construct(
'Operated entity was previously modified.',
'race_condition_error',
409,
null !== $previous ? (int) $previous->getCode() : 0,
$previous,
);
}
}
15 changes: 4 additions & 11 deletions src/Component/legacy/src/Exception/UnexpectedTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,10 @@

namespace Sylius\Component\Resource\Exception;

class UnexpectedTypeException extends \InvalidArgumentException
{
/**
* @param mixed $value
*/
public function __construct($value, string $expectedType)
class_exists(\Sylius\Resource\Exception\UnexpectedTypeException::class);

if (false) {
class UnexpectedTypeException extends \Sylius\Resource\Exception\UnexpectedTypeException
{
parent::__construct(sprintf(
'Expected argument of type "%s", "%s" given.',
$expectedType,
is_object($value) ? get_class($value) : gettype($value),
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@

namespace Sylius\Component\Resource\Exception;

class UnsupportedMethodException extends \Exception
{
public function __construct(string $methodName)
class_exists(\Sylius\Resource\Exception\UnsupportedMethodException::class);

if (false) {
class UnsupportedMethodException extends \Sylius\Resource\Exception\UnsupportedMethodException
{
parent::__construct(sprintf(
'The method "%s" is not supported.',
$methodName,
));
}
}
28 changes: 3 additions & 25 deletions src/Component/legacy/src/Exception/UpdateHandlingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,10 @@

namespace Sylius\Component\Resource\Exception;

class UpdateHandlingException extends \Exception
{
protected string $flash;
class_exists(\Sylius\Resource\Exception\UpdateHandlingException::class);

protected int $apiResponseCode;

public function __construct(
string $message = 'Ups, something went wrong during updating a resource, please try again.',
string $flash = 'something_went_wrong_error',
int $apiResponseCode = 400,
int $code = 0,
?\Exception $previous = null,
) {
parent::__construct($message, $code, $previous);

$this->flash = $flash;
$this->apiResponseCode = $apiResponseCode;
}

public function getFlash(): string
{
return $this->flash;
}

public function getApiResponseCode(): int
if (false) {
class UpdateHandlingException extends \Sylius\Resource\Exception\UpdateHandlingException
{
return $this->apiResponseCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

namespace Sylius\Component\Resource\Exception;

final class VariantWithNoOptionsValuesException extends \Exception
{
public function __construct()
class_exists(\Sylius\Resource\Exception\VariantWithNoOptionsValuesException::class);

if (false) {
final class VariantWithNoOptionsValuesException extends \Sylius\Resource\Exception\VariantWithNoOptionsValuesException
{
parent::__construct('sylius.product_variant.cannot_generate_variants');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
use Pagerfanta\Pagerfanta;
use PhpSpec\ObjectBehavior;
use spec\Sylius\Component\Resource\Fixtures\SampleBookResourceInterface;
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
use Sylius\Resource\Doctrine\Persistence\Exception\ResourceExistsException;
use Sylius\Resource\Doctrine\Persistence\RepositoryInterface;
use Sylius\Resource\Exception\UnexpectedTypeException;
use Sylius\Resource\Model\ResourceInterface;

require_once dirname(__DIR__, 3) . '/legacy/spec/Fixtures/SampleBookResourceInterface.php';
Expand Down
40 changes: 40 additions & 0 deletions src/Component/spec/Exception/RaceConditionExceptionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Resource\Exception;

use PhpSpec\ObjectBehavior;
use Sylius\Resource\Exception\UpdateHandlingException;

final class RaceConditionExceptionSpec extends ObjectBehavior
{
function it_extends_an_update_handling_exception(): void
{
$this->shouldHaveType(UpdateHandlingException::class);
}

function it_has_a_message(): void
{
$this->getMessage()->shouldReturn('Operated entity was previously modified.');
}

function it_has_a_flash(): void
{
$this->getFlash()->shouldReturn('race_condition_error');
}

function it_has_an_api_response_code(): void
{
$this->getApiResponseCode()->shouldReturn(409);
}
}
Loading

0 comments on commit 643c575

Please sign in to comment.