Skip to content

Commit c810634

Browse files
committed
Implement User class with primitive types usages as a code smell showcase
1 parent aec01ee commit c810634

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

examples/php/php-user_modelling-01_base/src/User.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
final class User
88
{
9+
private const SPANISH_LANGUAGE = "es";
10+
private const UNDERAGE_UNTIL_AGE = 18;
11+
912
private string $locale;
1013
private int $age;
1114

@@ -15,13 +18,15 @@ public function __construct(string $locale, int $age)
1518
$this->age = $age;
1619
}
1720

18-
public function locale(): string
21+
public function understandSpanish(): bool
1922
{
20-
return $this->locale;
23+
$language = substr($this->locale, 0, 2);
24+
25+
return $language === self::SPANISH_LANGUAGE;
2126
}
2227

23-
public function age(): int
28+
public function isUnderage(): bool
2429
{
25-
return $this->age;
30+
return $this->age < self::UNDERAGE_UNTIL_AGE;
2631
}
2732
}

examples/php/php-user_modelling-01_base/tests/UserTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,34 @@
1010
final class UserTest extends TestCase
1111
{
1212
/** @test */
13-
public function shouldSayHelloWhenGreeting()
13+
public function should_detect_spanish_from_spain_as_spanish_language(): void
1414
{
1515
$user = new User("es_ES", 20);
1616

17-
self::assertEquals("es_ES", $user->locale());
17+
self::assertTrue($user->understandSpanish());
18+
}
19+
20+
/** @test */
21+
public function should_not_detect_english_from_great_britain_as_spanish_language(): void
22+
{
23+
$user = new User("en_GB", 20);
24+
25+
self::assertFalse($user->understandSpanish());
26+
}
27+
28+
/** @test */
29+
public function should_detect_underage_users_until_18_years_old(): void
30+
{
31+
$user = new User("en_GB", 17);
32+
33+
self::assertTrue($user->isUnderage());
34+
}
35+
36+
/** @test */
37+
public function should_not_detect_underage_users_starting_from_18_years_old(): void
38+
{
39+
$user = new User("en_GB", 18);
40+
41+
self::assertFalse($user->isUnderage());
1842
}
1943
}

0 commit comments

Comments
 (0)