Skip to content

Commit

Permalink
reverted to clean class definitions for event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
craue committed Mar 31, 2021
1 parent a416ea0 commit ce3710b
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 94 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Expand Up @@ -36,6 +36,10 @@ jobs:
-
php: 7.3
env: SYMFONY_VERSION='5.2.*' WITH_STATIC_ANALYSIS='yes'
# TODO remove as soon as Symfony >= 5.0 is required
-
php: 7.3
env: SYMFONY_VERSION='5.2.*' WITH_CONTAO_POLYFILL_SYMFONY='yes'
-
php: 7.3
env: SYMFONY_VERSION='5.x' MIN_STABILITY='dev'
Expand Down
5 changes: 5 additions & 0 deletions .travis_install_dependencies.sh
Expand Up @@ -31,4 +31,9 @@ if [ -n "${WITH_TRANSLATION_CONTRACTS:-}" ]; then
composer require --no-update --dev "symfony/translation-contracts:~1.1"
fi

# TODO remove as soon as Symfony >= 5.0 is required
if [ -n "${WITH_CONTAO_POLYFILL_SYMFONY:-}" ]; then
composer require --no-update --dev "contao/polyfill-symfony"
fi

composer update ${COMPOSER_UPDATE_ARGS:-}
38 changes: 38 additions & 0 deletions EventListener/EventListenerWithTranslatorTrait.php
@@ -0,0 +1,38 @@
<?php

namespace Craue\FormFlowBundle\EventListener;

use Craue\FormFlowBundle\Exception\InvalidTypeException;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @internal
*
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2021 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
trait EventListenerWithTranslatorTrait {

/**
* @var TranslatorInterface|LegacyTranslatorInterface
*/
protected $translator;

/**
* @param TranslatorInterface|LegacyTranslatorInterface $translator
* @throws InvalidTypeException
*/
public function setTranslator($translator) {
// TODO revert to type-hint with only TranslatorInterface as soon as Symfony >= 5.0 is required
if ($translator instanceof TranslatorInterface || $translator instanceof LegacyTranslatorInterface) {
$this->translator = $translator;

return;
}

throw new InvalidTypeException($translator, [TranslatorInterface::class, LegacyTranslatorInterface::class]);
}

}
44 changes: 7 additions & 37 deletions EventListener/FlowExpiredEventListener.php
Expand Up @@ -4,18 +4,17 @@

use Craue\FormFlowBundle\Event\FlowExpiredEvent;
use Symfony\Component\Form\FormError;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @internal
* Adds a validation error to the current step's form if an expired flow is detected.
*
* @author Tim Behrendsen <tim@siliconengine.com>
* @copyright 2011-2021 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
abstract class BaseFlowExpiredEventListener {
class FlowExpiredEventListener {

/**
* @var TranslatorInterface|LegacyTranslatorInterface
*/
protected $translator;
use EventListenerWithTranslatorTrait;

public function onFlowExpired(FlowExpiredEvent $event) {
$event->getCurrentStepForm()->addError($this->getFlowExpiredFormError());
Expand All @@ -32,32 +31,3 @@ protected function getFlowExpiredFormError() {
}

}

// TODO revert to one clean class definition as soon as Symfony >= 4.2 is required
if (!interface_exists(LegacyTranslatorInterface::class)) {
/**
* Adds a validation error to the current step's form if an expired flow is detected.
*
* @author Tim Behrendsen <tim@siliconengine.com>
* @copyright 2011-2021 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class FlowExpiredEventListener extends BaseFlowExpiredEventListener {
public function setTranslator(TranslatorInterface $translator) {
$this->translator = $translator;
}
}
} else {
/**
* Adds a validation error to the current step's form if an expired flow is detected.
*
* @author Tim Behrendsen <tim@siliconengine.com>
* @copyright 2011-2021 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class FlowExpiredEventListener extends BaseFlowExpiredEventListener {
public function setTranslator(LegacyTranslatorInterface $translator) {
$this->translator = $translator;
}
}
}
44 changes: 7 additions & 37 deletions EventListener/PreviousStepInvalidEventListener.php
Expand Up @@ -4,18 +4,17 @@

use Craue\FormFlowBundle\Event\PreviousStepInvalidEvent;
use Symfony\Component\Form\FormError;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @internal
* Adds a validation error to the current step's form if revalidating previous steps failed.
*
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2021 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
abstract class BasePreviousStepInvalidEventListener {
class PreviousStepInvalidEventListener {

/**
* @var TranslatorInterface|LegacyTranslatorInterface
*/
protected $translator;
use EventListenerWithTranslatorTrait;

public function onPreviousStepInvalid(PreviousStepInvalidEvent $event) {
$event->getCurrentStepForm()->addError($this->getPreviousStepInvalidFormError($event->getInvalidStepNumber()));
Expand All @@ -33,32 +32,3 @@ protected function getPreviousStepInvalidFormError($stepNumber) {
}

}

// TODO revert to one clean class definition as soon as Symfony >= 4.2 is required
if (!interface_exists(LegacyTranslatorInterface::class)) {
/**
* Adds a validation error to the current step's form if revalidating previous steps failed.
*
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2021 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class PreviousStepInvalidEventListener extends BasePreviousStepInvalidEventListener {
public function setTranslator(TranslatorInterface $translator) {
$this->translator = $translator;
}
}
} else {
/**
* Adds a validation error to the current step's form if revalidating previous steps failed.
*
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2021 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class PreviousStepInvalidEventListener extends BasePreviousStepInvalidEventListener {
public function setTranslator(LegacyTranslatorInterface $translator) {
$this->translator = $translator;
}
}
}
33 changes: 33 additions & 0 deletions Tests/EventListener/EventListenerWithTranslatorTestTrait.php
@@ -0,0 +1,33 @@
<?php

namespace Craue\FormFlowBundle\Tests\EventListener;

use Craue\FormFlowBundle\Exception\InvalidTypeException;

/**
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2021 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
trait EventListenerWithTranslatorTestTrait {

protected abstract function getListener();

/**
* @dataProvider dataSetTranslator_invalidArguments
*/
public function testSetTranslator_invalidArguments($translator) {
$listener = $this->getListener();

$this->expectException(InvalidTypeException::class);
$listener->setTranslator($translator);
}

public function dataSetTranslator_invalidArguments() {
return [
[null],
[new \stdClass()],
];
}

}
23 changes: 23 additions & 0 deletions Tests/EventListener/FlowExpiredEventListenerTest.php
@@ -0,0 +1,23 @@
<?php

namespace Craue\FormFlowBundle\Tests\EventListener;

use Craue\FormFlowBundle\EventListener\FlowExpiredEventListener;
use Craue\FormFlowBundle\Tests\UnitTestCase;

/**
* @group unit
*
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2021 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class FlowExpiredEventListenerTest extends UnitTestCase {

use EventListenerWithTranslatorTestTrait;

protected function getListener() {
return new FlowExpiredEventListener();
}

}
23 changes: 23 additions & 0 deletions Tests/EventListener/PreviousStepInvalidEventListenerTest.php
@@ -0,0 +1,23 @@
<?php

namespace Craue\FormFlowBundle\Tests\EventListener;

use Craue\FormFlowBundle\EventListener\PreviousStepInvalidEventListener;
use Craue\FormFlowBundle\Tests\UnitTestCase;

/**
* @group unit
*
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2021 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class PreviousStepInvalidEventListenerTest extends UnitTestCase {

use EventListenerWithTranslatorTestTrait;

protected function getListener() {
return new PreviousStepInvalidEventListener();
}

}
43 changes: 23 additions & 20 deletions phpstan-config.neon
Expand Up @@ -3,9 +3,6 @@ parameters:
paths:
- .
autoload_files:
# TODO remove as soon as Symfony >= 4.2 is required
- EventListener/FlowExpiredEventListener.php
- EventListener/PreviousStepInvalidEventListener.php
# TODO remove as soon as Symfony >= 5.0 is required
- Form/Extension/FormFlowFormExtension.php
- Form/Extension/FormFlowHiddenFieldExtension.php
Expand All @@ -23,38 +20,44 @@ parameters:
-
message: '#^Class Symfony\\Component\\HttpFoundation\\File\\UploadedFile constructor invoked with 6 parameters, 2-5 required\.$#'
path: Storage/SerializableFile.php
# TODO remove as soon as Symfony >= 4.2 is required
# TODO remove as soon as Symfony >= 4.3 is required
-
message: '#^Property Craue\\FormFlowBundle\\EventListener\\Base(FlowExpired|PreviousStepInvalid)EventListener::\$translator has unknown class Symfony\\Component\\Translation\\TranslatorInterface as its type\.$#'
message: '#^Comparison operation "<" between \d{5} and 40300 is always false\.$#'
paths:
- Event/FormFlowEvent.php
- Form/FormFlow.php
# TODO remove as soon as Symfony >= 4.3 is required
-
message: '#^Parameter \#1 \$event of method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface::dispatch\(\) expects object, string given\.$#'
path: Form/FormFlow.php
# TODO remove as soon as Symfony >= 4.3 is required
-
message: '#^Parameter \#2 \$eventName of method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface::dispatch\(\) expects string\|null, Craue\\FormFlowBundle\\Event\\FormFlowEvent given\.$#'
path: Form/FormFlow.php
# TODO remove as soon as Symfony >= 5.0 is required
-
message: '#^Property Craue\\FormFlowBundle\\EventListener\\(FlowExpired|PreviousStepInvalid)EventListener::\$translator has unknown class Symfony\\Component\\Translation\\TranslatorInterface as its type\.$#'
paths:
- EventListener/FlowExpiredEventListener.php
- EventListener/PreviousStepInvalidEventListener.php
# TODO remove as soon as Symfony >= 4.2 is required
# TODO remove as soon as Symfony >= 5.0 is required
-
message: '#^Parameter \$translator of method Craue\\FormFlowBundle\\EventListener\\(FlowExpired|PreviousStepInvalid)EventListener::setTranslator\(\) has invalid typehint type Symfony\\Component\\Translation\\TranslatorInterface\.$#'
paths:
- EventListener/FlowExpiredEventListener.php
- EventListener/PreviousStepInvalidEventListener.php
# TODO remove as soon as Symfony >= 4.2 is required
# TODO remove as soon as Symfony >= 5.0 is required
-
message: '#^Call to method trans\(\) on an unknown class Symfony\\Component\\Translation\\TranslatorInterface\.$#'
message: '#^Class Symfony\\Component\\Translation\\TranslatorInterface not found\.$#'
paths:
- EventListener/FlowExpiredEventListener.php
- EventListener/PreviousStepInvalidEventListener.php
# TODO remove as soon as Symfony >= 4.3 is required
# TODO remove as soon as Symfony >= 5.0 is required
-
message: '#^Comparison operation "<" between \d{5} and 40300 is always false\.$#'
message: '#^Call to method trans\(\) on an unknown class Symfony\\Component\\Translation\\TranslatorInterface\.$#'
paths:
- Event/FormFlowEvent.php
- Form/FormFlow.php
# TODO remove as soon as Symfony >= 4.3 is required
-
message: '#^Parameter \#1 \$event of method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface::dispatch\(\) expects object, string given\.$#'
path: Form/FormFlow.php
# TODO remove as soon as Symfony >= 4.3 is required
-
message: '#^Parameter \#2 \$eventName of method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface::dispatch\(\) expects string\|null, Craue\\FormFlowBundle\\Event\\FormFlowEvent given\.$#'
path: Form/FormFlow.php
- EventListener/FlowExpiredEventListener.php
- EventListener/PreviousStepInvalidEventListener.php
# TODO remove as soon as Doctrine DBAL >= 3.0 is required
-
message: '#^Cannot call method fetchColumn\(\) on Doctrine\\DBAL\\Result\|int\.$#'
Expand Down

0 comments on commit ce3710b

Please sign in to comment.