diff --git a/src/Contract/Entity/TranslatableInterface.php b/src/Contract/Entity/TranslatableInterface.php index 2de877e9..f868f7c9 100644 --- a/src/Contract/Entity/TranslatableInterface.php +++ b/src/Contract/Entity/TranslatableInterface.php @@ -6,20 +6,29 @@ use Doctrine\Common\Collections\Collection; +/** + * @template T of TranslationInterface + */ interface TranslatableInterface { /** - * @return Collection + * @return Collection */ public function getTranslations(); /** - * @return Collection + * @return Collection */ public function getNewTranslations(): Collection; + /** + * @param T $translation + */ public function addTranslation(TranslationInterface $translation): void; + /** + * @param T $translation + */ public function removeTranslation(TranslationInterface $translation): void; /** @@ -28,6 +37,8 @@ public function removeTranslation(TranslationInterface $translation): void; * newTranslations collection. In order to persist new translations, call mergeNewTranslations method, before flush * * @param string $locale The locale (en, ru, fr) | null If null, will try with current locale + * + * @return T */ public function translate(?string $locale = null, bool $fallbackToDefault = true): TranslationInterface; @@ -44,5 +55,8 @@ public function setDefaultLocale(string $locale): void; public function getDefaultLocale(): string; + /** + * @return class-string + */ public static function getTranslationEntityClass(): string; } diff --git a/src/Contract/Entity/TranslationInterface.php b/src/Contract/Entity/TranslationInterface.php index 6423431c..caf9650c 100644 --- a/src/Contract/Entity/TranslationInterface.php +++ b/src/Contract/Entity/TranslationInterface.php @@ -4,12 +4,24 @@ namespace Knp\DoctrineBehaviors\Contract\Entity; +/** + * @template T of TranslatableInterface + */ interface TranslationInterface { + /** + * @return class-string + */ public static function getTranslatableEntityClass(): string; + /** + * @param T $translatable + */ public function setTranslatable(TranslatableInterface $translatable): void; + /** + * @return T + */ public function getTranslatable(): TranslatableInterface; public function setLocale(string $locale): void; diff --git a/src/Model/Translatable/TranslatableMethodsTrait.php b/src/Model/Translatable/TranslatableMethodsTrait.php index a1374b20..e23dc5c6 100644 --- a/src/Model/Translatable/TranslatableMethodsTrait.php +++ b/src/Model/Translatable/TranslatableMethodsTrait.php @@ -9,10 +9,13 @@ use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface; use Knp\DoctrineBehaviors\Exception\TranslatableException; +/** + * @template T of TranslationInterface + */ trait TranslatableMethodsTrait { /** - * @return Collection + * @return Collection */ public function getTranslations() { @@ -25,8 +28,8 @@ public function getTranslations() } /** - * @param Collection $translations - * @phpstan-param iterable $translations + * @param Collection $translations + * @phpstan-param iterable $translations */ public function setTranslations(iterable $translations): void { @@ -38,7 +41,7 @@ public function setTranslations(iterable $translations): void } /** - * @return Collection + * @return Collection */ public function getNewTranslations(): Collection { @@ -50,6 +53,9 @@ public function getNewTranslations(): Collection return $this->newTranslations; } + /** + * @param T $translation + */ public function addTranslation(TranslationInterface $translation): void { $this->getTranslations() @@ -57,6 +63,9 @@ public function addTranslation(TranslationInterface $translation): void $translation->setTranslatable($this); } + /** + * @param T $translation + */ public function removeTranslation(TranslationInterface $translation): void { $this->getTranslations() @@ -69,6 +78,8 @@ public function removeTranslation(TranslationInterface $translation): void * newTranslations collection. In order to persist new translations, call mergeNewTranslations method, before flush * * @param string $locale The locale (en, ru, fr) | null If null, will try with current locale + * + * @return T */ public function translate(?string $locale = null, bool $fallbackToDefault = true): TranslationInterface { @@ -117,6 +128,9 @@ public function getDefaultLocale(): string return $this->defaultLocale; } + /** + * @return class-string + */ public static function getTranslationEntityClass(): string { return static::class . 'Translation'; @@ -128,6 +142,8 @@ public static function getTranslationEntityClass(): string * newTranslations collection. In order to persist new translations, call mergeNewTranslations method, before flush * * @param string $locale The locale (en, ru, fr) | null If null, will try with current locale + * + * @return T */ protected function doTranslate(?string $locale = null, bool $fallbackToDefault = true): TranslationInterface { @@ -183,6 +199,8 @@ protected function proxyCurrentLocaleTranslation(string $method, array $argument /** * Finds specific translation in collection by its locale. + * + * @return T|null */ protected function findTranslationByLocale(string $locale, bool $withNewTranslations = true): ?TranslationInterface { @@ -228,6 +246,9 @@ private function ensureIsIterableOrCollection($translations): void ); } + /** + * @return T|null + */ private function resolveFallbackTranslation(string $locale): ?TranslationInterface { $fallbackLocale = $this->computeFallbackLocale($locale); diff --git a/src/Model/Translatable/TranslatablePropertiesTrait.php b/src/Model/Translatable/TranslatablePropertiesTrait.php index 4c6f0fa4..6e8f2414 100644 --- a/src/Model/Translatable/TranslatablePropertiesTrait.php +++ b/src/Model/Translatable/TranslatablePropertiesTrait.php @@ -5,18 +5,20 @@ namespace Knp\DoctrineBehaviors\Model\Translatable; use Doctrine\Common\Collections\Collection; -use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface; +/** + * @template T of \Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface + */ trait TranslatablePropertiesTrait { /** - * @var Collection + * @var Collection */ protected $translations; /** * @see mergeNewTranslations - * @var Collection + * @var Collection */ protected $newTranslations; diff --git a/src/Model/Translatable/TranslatableTrait.php b/src/Model/Translatable/TranslatableTrait.php index 83f6d9fb..1a37aa78 100644 --- a/src/Model/Translatable/TranslatableTrait.php +++ b/src/Model/Translatable/TranslatableTrait.php @@ -4,8 +4,18 @@ namespace Knp\DoctrineBehaviors\Model\Translatable; +/** + * @template T of \Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface + */ trait TranslatableTrait { + /** + * @template-use TranslatablePropertiesTrait + */ use TranslatablePropertiesTrait; + + /** + * @template-use TranslatableMethodsTrait + */ use TranslatableMethodsTrait; } diff --git a/src/Model/Translatable/TranslationMethodsTrait.php b/src/Model/Translatable/TranslationMethodsTrait.php index 4e5410c1..73f592db 100644 --- a/src/Model/Translatable/TranslationMethodsTrait.php +++ b/src/Model/Translatable/TranslationMethodsTrait.php @@ -7,8 +7,14 @@ use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface; use Nette\Utils\Strings; +/** + * @template T of TranslatableInterface + */ trait TranslationMethodsTrait { + /** + * @return class-string + */ public static function getTranslatableEntityClass(): string { // By default, the translatable class has the same name but without the "Translation" suffix @@ -17,6 +23,8 @@ public static function getTranslatableEntityClass(): string /** * Sets entity, that this translation should be mapped to. + * + * @param T $translatable */ public function setTranslatable(TranslatableInterface $translatable): void { @@ -25,6 +33,8 @@ public function setTranslatable(TranslatableInterface $translatable): void /** * Returns entity, that this translation is mapped to. + * + * @return T */ public function getTranslatable(): TranslatableInterface { diff --git a/src/Model/Translatable/TranslationPropertiesTrait.php b/src/Model/Translatable/TranslationPropertiesTrait.php index c3773e27..077f9012 100644 --- a/src/Model/Translatable/TranslationPropertiesTrait.php +++ b/src/Model/Translatable/TranslationPropertiesTrait.php @@ -4,8 +4,9 @@ namespace Knp\DoctrineBehaviors\Model\Translatable; -use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface; - +/** + * @template T of \Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface + */ trait TranslationPropertiesTrait { /** @@ -16,7 +17,7 @@ trait TranslationPropertiesTrait /** * Will be mapped to translatable entity by TranslatableSubscriber * - * @var TranslatableInterface + * @var T */ protected $translatable; } diff --git a/src/Model/Translatable/TranslationTrait.php b/src/Model/Translatable/TranslationTrait.php index 765aa519..a2622bdb 100644 --- a/src/Model/Translatable/TranslationTrait.php +++ b/src/Model/Translatable/TranslationTrait.php @@ -4,8 +4,18 @@ namespace Knp\DoctrineBehaviors\Model\Translatable; +/** + * @template T of \Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface + */ trait TranslationTrait { + /** + * @template-use TranslationPropertiesTrait + */ use TranslationPropertiesTrait; + + /** + * @template-use TranslationMethodsTrait + */ use TranslationMethodsTrait; }