From 31898cfe99cda7ba88d74fd2e2d7dbd43eeb5208 Mon Sep 17 00:00:00 2001 From: Vladimir Romanichev Date: Wed, 22 Sep 2021 14:22:43 +0300 Subject: [PATCH] change flow of scalar validations --- src/Language.php | 14 +++++++------- src/Word.php | 9 ++++----- tests/unit/TranslationTest.php | 6 ++---- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/Language.php b/src/Language.php index b9e1066..3c5e1e0 100755 --- a/src/Language.php +++ b/src/Language.php @@ -30,19 +30,19 @@ final class Language implements Stringify */ public function __construct(string $language) { - $this->language = $language; - } - - public function asString(): string - { - if (!in_array($this->language, $this->validLanguagesList)) { + if (!in_array($language, $this->validLanguagesList)) { throw new \InvalidArgumentException( - "Language {$this->language} is not exists in the list of valid languages " . join( + "Language {$language} is not exists in the list of valid languages " . implode( ',', $this->validLanguagesList ) ); } + $this->language = $language; + } + + public function asString(): string + { return $this->language; } } diff --git a/src/Word.php b/src/Word.php index a190c00..3bec5aa 100644 --- a/src/Word.php +++ b/src/Word.php @@ -15,16 +15,15 @@ final class Word implements Stringify */ public function __construct(string $word) { + if (strpos(trim($word), ' ') !== false) { + throw new \InvalidArgumentException("$word must be only one word"); + } $this->word = $word; } public function asString(): string { - $word = trim($this->word); - if (strpos(trim($word), ' ') !== false) { - throw new \InvalidArgumentException("$word must be only one word"); - } - return $word; + return trim($this->word); } } diff --git a/tests/unit/TranslationTest.php b/tests/unit/TranslationTest.php index a8f56c9..2815192 100644 --- a/tests/unit/TranslationTest.php +++ b/tests/unit/TranslationTest.php @@ -24,15 +24,13 @@ public function testAsArrayWithValidParametersRightSourceTextParameter(): void public function testWordWithPhraseInsteadAWord(): void { - $sut = new Word('this is a test phrase'); $this->expectException(\InvalidArgumentException::class); - $sut->asString(); + $sut = new Word('this is a test phrase'); } public function testLanguageWithInvalidValue(): void { - $sut = new Language('klingon'); $this->expectException(\InvalidArgumentException::class); - $sut->asString(); + $sut = new Language('klingon'); } }