Skip to content

Commit

Permalink
Merge 389d92f into 5ae2b30
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaicher committed Aug 28, 2020
2 parents 5ae2b30 + 389d92f commit 8d181ac
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 31 deletions.
7 changes: 6 additions & 1 deletion .phpstan/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ parameters:
-
# will be fixed in v4. Code is marked as deprecated
message: "#^Result of \\&\\& is always false\\.$#"
count: 1
count: 2
path: ../src/Admin/AbstractAdmin.php

-
Expand Down Expand Up @@ -271,3 +271,8 @@ parameters:
message: "#^Method Symfony\\\\Contracts\\\\EventDispatcher\\\\EventDispatcherInterface\\:\\:dispatch\\(\\) invoked with 2 parameters, 1 required\\.$#"
count: 1
path: ../src/Menu/MenuBuilder.php

-
message: '#Else branch is unreachable because ternary operator condition is always true#'
count: 1
path: ../src/Admin/AbstractAdmin.php
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"symfony/security-core": "^4.4",
"symfony/security-csrf": "^4.4",
"symfony/string": "^5.1",
"symfony/translation": "^4.4",
"symfony/translation": "^4.4 || ^5.1",
"symfony/twig-bridge": "^4.4",
"symfony/twig-bundle": "^4.4",
"symfony/validator": "^4.4",
Expand Down
25 changes: 22 additions & 3 deletions src/Admin/AbstractAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@
use Symfony\Component\Routing\Generator\UrlGeneratorInterface as RoutingUrlGeneratorInterface;
use Symfony\Component\Security\Acl\Model\DomainObjectInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Component\Validator\Mapping\GenericMetadata;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
Expand Down Expand Up @@ -309,7 +310,7 @@ abstract class AbstractAdmin implements AdminInterface, DomainObjectInterface, A
*
* NEXT_MAJOR: remove this property
*
* @var \Symfony\Component\Translation\TranslatorInterface
* @var TranslatorInterface|LegacyTranslatorInterface
*
* @deprecated since sonata-project/admin-bundle 3.9, to be removed with 4.0
*/
Expand Down Expand Up @@ -2441,6 +2442,10 @@ public function transChoice($id, $count, array $parameters = [], $domain = null,
__METHOD__
), E_USER_DEPRECATED);

if (!method_exists($this->translator, 'transChoice')) {
throw new \RuntimeException('AbstractAdmin::transChoice is only supported with symfony/translation 4.4');
}

$domain = $domain ?: $this->getTranslationDomain();

return $this->translator->transChoice($id, $count, $parameters, $domain, $locale);
Expand All @@ -2461,9 +2466,11 @@ public function getTranslationDomain()
*
* NEXT_MAJOR: remove this method
*
* @param LegacyTranslatorInterface|TranslatorInterface $translator
*
* @deprecated since sonata-project/admin-bundle 3.9, to be removed with 4.0
*/
public function setTranslator(TranslatorInterface $translator)
public function setTranslator($translator)
{
$args = \func_get_args();
if (isset($args[1]) && $args[1]) {
Expand All @@ -2473,6 +2480,16 @@ public function setTranslator(TranslatorInterface $translator)
), E_USER_DEPRECATED);
}

if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf(
'Argument 1 passed to "%s()" must be an instance of "%s" or "%s", %s given.',
__METHOD__,
LegacyTranslatorInterface::class,
TranslatorInterface::class,
\is_object($translator) ? 'instance of '.\get_class($translator) : \gettype($translator)
));
}

$this->translator = $translator;
}

Expand All @@ -2482,6 +2499,8 @@ public function setTranslator(TranslatorInterface $translator)
* NEXT_MAJOR: remove this method
*
* @deprecated since sonata-project/admin-bundle 3.9, to be removed with 4.0
*
* @return LegacyTranslatorInterface|TranslatorInterface
*/
public function getTranslator()
{
Expand Down
17 changes: 14 additions & 3 deletions src/Form/Type/Filter/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
use Symfony\Component\Form\Extension\Core\Type\ChoiceType as FormChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @final since sonata-project/admin-bundle 3.52
Expand Down Expand Up @@ -47,12 +48,22 @@ class ChoiceType extends AbstractType
*
* @deprecated since sonata-project/admin-bundle 3.5, to be removed with 4.0
*
* @var TranslatorInterface
* @var TranslatorInterface|LegacyTranslatorInterface
*/
protected $translator;

public function __construct(TranslatorInterface $translator)
public function __construct($translator)
{
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf(
'Argument 1 passed to "%s()" must be an instance of "%s" or "%s", %s given.',
__METHOD__,
LegacyTranslatorInterface::class,
TranslatorInterface::class,
\is_object($translator) ? 'instance of '.\get_class($translator) : \gettype($translator)
));
}

$this->translator = $translator;
}

Expand Down
17 changes: 14 additions & 3 deletions src/Form/Type/Filter/DateRangeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @final since sonata-project/admin-bundle 3.52
Expand All @@ -42,12 +43,22 @@ class DateRangeType extends AbstractType
*
* @deprecated since sonata-project/admin-bundle 3.5, to be removed with 4.0
*
* @var TranslatorInterface
* @var TranslatorInterface|LegacyTranslatorInterface
*/
protected $translator;

public function __construct(TranslatorInterface $translator)
public function __construct($translator)
{
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf(
'Argument 1 passed to "%s()" must be an instance of "%s" or "%s", %s given.',
__METHOD__,
LegacyTranslatorInterface::class,
TranslatorInterface::class,
\is_object($translator) ? 'instance of '.\get_class($translator) : \gettype($translator)
));
}

$this->translator = $translator;
}

Expand Down
17 changes: 14 additions & 3 deletions src/Form/Type/Filter/DateTimeRangeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @final since sonata-project/admin-bundle 3.52
Expand All @@ -42,12 +43,22 @@ class DateTimeRangeType extends AbstractType
*
* @deprecated since sonata-project/admin-bundle 3.5, to be removed with 4.0
*
* @var TranslatorInterface
* @var TranslatorInterface|LegacyTranslatorInterface
*/
protected $translator;

public function __construct(TranslatorInterface $translator)
public function __construct($translator)
{
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf(
'Argument 1 passed to "%s()" must be an instance of "%s" or "%s", %s given.',
__METHOD__,
LegacyTranslatorInterface::class,
TranslatorInterface::class,
\is_object($translator) ? 'instance of '.\get_class($translator) : \gettype($translator)
));
}

$this->translator = $translator;
}

Expand Down
17 changes: 14 additions & 3 deletions src/Form/Type/Filter/DateTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
use Symfony\Component\Form\Extension\Core\Type\DateTimeType as FormDateTimeType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @final since sonata-project/admin-bundle 3.52
Expand Down Expand Up @@ -67,12 +68,22 @@ class DateTimeType extends AbstractType
*
* @deprecated since sonata-project/admin-bundle 3.5, to be removed with 4.0
*
* @var TranslatorInterface
* @var TranslatorInterface|LegacyTranslatorInterface
*/
protected $translator;

public function __construct(TranslatorInterface $translator)
public function __construct($translator)
{
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf(
'Argument 1 passed to "%s()" must be an instance of "%s" or "%s", %s given.',
__METHOD__,
LegacyTranslatorInterface::class,
TranslatorInterface::class,
\is_object($translator) ? 'instance of '.\get_class($translator) : \gettype($translator)
));
}

$this->translator = $translator;
}

Expand Down
17 changes: 14 additions & 3 deletions src/Form/Type/Filter/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
use Symfony\Component\Form\Extension\Core\Type\DateType as FormDateType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @final since sonata-project/admin-bundle 3.52
Expand Down Expand Up @@ -67,12 +68,22 @@ class DateType extends AbstractType
*
* @deprecated since sonata-project/admin-bundle 3.5, to be removed with 4.0
*
* @var TranslatorInterface
* @var TranslatorInterface|LegacyTranslatorInterface
*/
protected $translator;

public function __construct(TranslatorInterface $translator)
public function __construct($translator)
{
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf(
'Argument 1 passed to "%s()" must be an instance of "%s" or "%s", %s given.',
__METHOD__,
LegacyTranslatorInterface::class,
TranslatorInterface::class,
\is_object($translator) ? 'instance of '.\get_class($translator) : \gettype($translator)
));
}

$this->translator = $translator;
}

Expand Down
17 changes: 14 additions & 3 deletions src/Form/Type/Filter/NumberType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
use Symfony\Component\Form\Extension\Core\Type\NumberType as FormNumberType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @final since sonata-project/admin-bundle 3.52
Expand Down Expand Up @@ -57,12 +58,22 @@ class NumberType extends AbstractType
*
* @deprecated since sonata-project/admin-bundle 3.5, to be removed with 4.0
*
* @var TranslatorInterface
* @var TranslatorInterface|LegacyTranslatorInterface
*/
protected $translator;

public function __construct(TranslatorInterface $translator)
public function __construct($translator)
{
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf(
'Argument 1 passed to "%s()" must be an instance of "%s" or "%s", %s given.',
__METHOD__,
LegacyTranslatorInterface::class,
TranslatorInterface::class,
\is_object($translator) ? 'instance of '.\get_class($translator) : \gettype($translator)
));
}

$this->translator = $translator;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Twig/Extension/SonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use Symfony\Component\Security\Acl\Voter\FieldVote;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslationInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
use Twig\Error\LoaderError;
Expand Down Expand Up @@ -104,11 +104,11 @@ public function __construct(
), E_USER_DEPRECATED);
}

if (!$translator instanceof TranslatorInterface && !$translator instanceof LegacyTranslationInterface) {
if (!$translator instanceof TranslatorInterface && !$translator instanceof LegacyTranslatorInterface) {
throw new \TypeError(sprintf(
'Argument 2 must be an instance of "%s" or preferably "%s", "%s given"',
TranslatorInterface::class,
LegacyTranslationInterface::class,
LegacyTranslatorInterface::class,
\get_class($translator)
));
}
Expand Down
19 changes: 16 additions & 3 deletions tests/Admin/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Component\Validator\Mapping\MemberMetadata;
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class AdminTest extends TestCase
{
Expand Down Expand Up @@ -1445,10 +1446,16 @@ public function testTransWithMessageDomain(): void
*/
public function testTransChoice(): void
{
if (!interface_exists(LegacyTranslatorInterface::class)) {
$this->markTestSkipped('This test is only available using Symfony 4');

return;
}

$admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
$admin->setTranslationDomain('fooMessageDomain');

$translator = $this->createMock(TranslatorInterface::class);
$translator = $this->createMock(LegacyTranslatorInterface::class);
$admin->setTranslator($translator);

$translator->expects($this->once())
Expand All @@ -1464,9 +1471,15 @@ public function testTransChoice(): void
*/
public function testTransChoiceWithMessageDomain(): void
{
if (!interface_exists(LegacyTranslatorInterface::class)) {
$this->markTestSkipped('This test is only available using Symfony 4');

return;
}

$admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');

$translator = $this->createMock(TranslatorInterface::class);
$translator = $this->createMock(LegacyTranslatorInterface::class);
$admin->setTranslator($translator);

$translator->expects($this->once())
Expand Down
2 changes: 1 addition & 1 deletion tests/Form/Type/Filter/DateTimeRangeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Sonata\Form\Type\DateTimeRangeType as FormDateTimeRangeType;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class DateTimeRangeTypeTest extends TypeTestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Form/Widget/FormSonataFilterChoiceWidgetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Sonata\AdminBundle\Tests\Fixtures\TestExtension;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType as SymfonyChoiceType;
use Symfony\Component\Form\FormTypeGuesserInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class FormSonataFilterChoiceWidgetTest extends BaseWidgetTest
{
Expand Down
Loading

0 comments on commit 8d181ac

Please sign in to comment.