From 72fb2d62e5dbfd9c5e544c00059ceb4c69d61736 Mon Sep 17 00:00:00 2001 From: Rathes Sachchithananthan Date: Fri, 7 Sep 2018 21:20:18 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Change=20from=20doc=20?= =?UTF-8?q?block=20to=20type-hints=20---=20At=20least=20everywhere=20possi?= =?UTF-8?q?ble?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Translatable.php | 89 ++++++----------------------- src/TranslatableServiceProvider.php | 8 +-- src/Translation.php | 5 +- 3 files changed, 20 insertions(+), 82 deletions(-) diff --git a/src/Translatable.php b/src/Translatable.php index ea9b047..b053929 100644 --- a/src/Translatable.php +++ b/src/Translatable.php @@ -3,13 +3,11 @@ namespace Aheenam\Translatable; use Illuminate\Support\Facades\App; +use Illuminate\Support\Collection; trait Translatable { - /** - * @return \Illuminate\Support\Collection - */ - public function allTranslations() + public function allTranslations(): Collection { $translations = collect([]); @@ -35,7 +33,7 @@ public function allTranslations() } /** - * @param $key + * @param string $key * * @return mixed */ @@ -48,12 +46,7 @@ public function getAttributeValue($key) return $this->getTranslation($key, App::getLocale()); } - /** - * returns all attributes that are translatable. - * - * @return array - */ - public function getTranslatableAttributes() + public function getTranslatableAttributes(): array { /* @noinspection PhpUndefinedFieldInspection */ return (property_exists(static::class, 'translatable') && is_array($this->translatable)) @@ -62,11 +55,9 @@ public function getTranslatableAttributes() } /** - * @param $locale - * * @return Translatable */ - public function in($locale) + public function in(string $locale) { $translatedModel = new self(); @@ -85,21 +76,14 @@ public function in($locale) return $translatedModel; } - /** - * @param $locale - */ - public function removeTranslationIn($locale) + public function removeTranslationIn(string $locale) { $this->translations() ->where('locale', $locale) ->delete(); } - /** - * @param $locale - * @param $attribute - */ - public function removeTranslation($locale, $attribute) + public function removeTranslation(string $locale, string $attribute) { $this->translations() ->where('locale', $locale) @@ -118,12 +102,9 @@ public function translations() /** * returns the translation of a key for a given key/locale pair. * - * @param $key - * @param $locale - * * @return mixed */ - protected function getTranslation($key, $locale) + protected function getTranslation(string $key, string $locale) { return $this->translations() ->where('key', $key) @@ -131,13 +112,7 @@ protected function getTranslation($key, $locale) ->value('translation'); } - /** - * @param $locale - * @param $attribute - * - * @return bool - */ - protected function hasTranslation($locale, $attribute) + protected function hasTranslation(string $locale, string $attribute): bool { $translation = $this->translations() ->where('locale', $locale) @@ -147,26 +122,12 @@ protected function hasTranslation($locale, $attribute) return $translation !== null; } - /** - * returns if given key is translatable. - * - * @param $key - * - * @return bool - */ - protected function isTranslatableAttribute($key) + protected function isTranslatableAttribute(string $key): bool { return in_array($key, $this->getTranslatableAttributes()); } - /** - * @param $locale - * @param $attribute - * @param $translation - * - * @return void - */ - protected function setTranslation($locale, $attribute, $translation) + protected function setTranslation(string $locale, string $attribute, string $translation): void { $this->translations()->create([ 'key' => $attribute, @@ -175,13 +136,7 @@ protected function setTranslation($locale, $attribute, $translation) ]); } - /** - * @param $locale - * @param $translations - * - * @return void - */ - protected function setTranslationByArray($locale, $translations) + protected function setTranslationByArray(string $locale, array $translations): void { foreach ($translations as $attribute => $translation) { if ($this->isTranslatableAttribute($attribute)) { @@ -200,14 +155,9 @@ protected function setTranslationByArray($locale, $translations) } /** - * returns the translation of a key for a given key/locale pair. - * - * @param $key - * @param $locale - * * @return mixed */ - protected function translateAttribute($key, $locale) + protected function translateAttribute(string $key, string $locale) { if (! $this->isTranslatableAttribute($key) || config('app.fallback_locale') == $locale) { return parent::getAttributeValue($key); @@ -216,14 +166,7 @@ protected function translateAttribute($key, $locale) return $this->getTranslation($key, $locale); } - /** - * @param $locale - * @param $attribute - * @param $translation - * - * @return void - */ - protected function updateTranslation($locale, $attribute, $translation) + protected function updateTranslation(string $locale, string $attribute, string $translation): void { $this->translations() ->where('key', $attribute) @@ -234,8 +177,8 @@ protected function updateTranslation($locale, $attribute, $translation) } /** - * @param $method - * @param $arguments + * @param string $method + * @param array $parameters * * @return mixed */ diff --git a/src/TranslatableServiceProvider.php b/src/TranslatableServiceProvider.php index a2c58d4..331c53a 100644 --- a/src/TranslatableServiceProvider.php +++ b/src/TranslatableServiceProvider.php @@ -15,20 +15,16 @@ class TranslatableServiceProvider extends ServiceProvider /** * Bootstrap the application events. - * - * @return void */ - public function boot() + public function boot(): void { $this->loadMigrationsFrom(__DIR__.'/../database/migrations/'); } /** * Register the service provider. - * - * @return void */ - public function register() + public function register(): void { } } diff --git a/src/Translation.php b/src/Translation.php index 1bfdede..a774990 100644 --- a/src/Translation.php +++ b/src/Translation.php @@ -3,6 +3,7 @@ namespace Aheenam\Translatable; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\MorphTo; class Translation extends Model { @@ -15,10 +16,8 @@ class Translation extends Model /** * Get all of the owning translatable models. - * - * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ - public function translatable() + public function translatable(): MorphTo { return $this->morphTo(); } From 59c3acd7c2789d88312ec41a3d955f5850eac3e2 Mon Sep 17 00:00:00 2001 From: Rathes Sachchithananthan Date: Fri, 7 Sep 2018 21:27:31 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=9A=A8=20Apply=20StyleCI=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Translatable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Translatable.php b/src/Translatable.php index b053929..dda0deb 100644 --- a/src/Translatable.php +++ b/src/Translatable.php @@ -2,8 +2,8 @@ namespace Aheenam\Translatable; -use Illuminate\Support\Facades\App; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\App; trait Translatable {