Skip to content

Commit

Permalink
Fix phpstan behaviors to allow an interface inherited from Translatab…
Browse files Browse the repository at this point in the history
…leInterface (KnpLabs#706)
  • Loading branch information
nicolassing authored Jun 29, 2022
1 parent e0d5386 commit c998ea6
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Knp\DoctrineBehaviors\Tests\Fixtures\Contract\Translatable;

use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;

interface ExtendedTranslatableInterface extends TranslatableInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Knp\DoctrineBehaviors\Tests\Fixtures\Contract\Translatable;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;

#[Entity]
class TranslatableEntityWithCustomInterface implements ExtendedTranslatableInterface
{
use TranslatableTrait;

#[Id]
#[Column(type: 'integer')]
#[GeneratedValue(strategy: 'AUTO')]
private int $id;

/**
* @return mixed
*/
public function __call(string $method, array $arguments)
{
return $this->proxyCurrentLocaleTranslation($method, $arguments);
}

public function getId(): int
{
return $this->id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Knp\DoctrineBehaviors\Tests\Fixtures\Contract\Translatable;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Knp\DoctrineBehaviors\Tests\Fixtures\Entity\Translatable\AbstractTranslatableEntityTranslation;

#[Entity]
class TranslatableEntityWithCustomInterfaceTranslation extends AbstractTranslatableEntityTranslation
{
#[Id]
#[Column(type: 'integer')]
#[GeneratedValue(strategy: 'AUTO')]
private int $id;

public function getId(): int
{
return $this->id;
}
}
11 changes: 11 additions & 0 deletions tests/ORM/Translatable/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
use Knp\DoctrineBehaviors\Tests\AbstractBehaviorTestCase;
use Knp\DoctrineBehaviors\Tests\Fixtures\Contract\Translatable\TranslatableEntityWithCustomInterface;
use Knp\DoctrineBehaviors\Tests\Fixtures\Entity\TranslatableCustomIdentifierEntity;
use Knp\DoctrineBehaviors\Tests\Fixtures\Entity\TranslatableCustomizedEntity;
use Knp\DoctrineBehaviors\Tests\Fixtures\Entity\TranslatableEntity;
Expand Down Expand Up @@ -383,6 +384,16 @@ public function testTranslationIsNotEmptyWithZeroAsValue(): void
$this->assertSame('0', $translatableEntity->translate('en')->getTitle());
}

public function testCustomInterface(): void
{
$translatableEntityWithCustom = new TranslatableEntityWithCustomInterface();
$translatableEntityWithCustom->translate('en')
->setTitle('awesome');
$translatableEntityWithCustom->mergeNewTranslations();

$this->assertSame('awesome', $translatableEntityWithCustom->translate('en')->getTitle());
}

/**
* @param class-string $translatableClass
* @param class-string $translationClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public static function getTranslationClass(
}

if ($reflectionClass->isInterface()) {
if ($reflectionClass->getName() === TranslatableInterface::class) {
if ($reflectionClass->getName() === TranslatableInterface::class || $reflectionClass->implementsInterface(
TranslatableInterface::class
)) {
return TranslationInterface::class;
}

Expand All @@ -59,7 +61,9 @@ public static function getTranslatableClass(
->getNativeReflection();

if ($nativeReflection->isInterface()) {
if ($nativeReflection->getName() === TranslationInterface::class) {
if ($nativeReflection->getName() === TranslationInterface::class || $nativeReflection->implementsInterface(
TranslationInterface::class
)) {
return TranslatableInterface::class;
}

Expand Down

0 comments on commit c998ea6

Please sign in to comment.