Skip to content

Commit 4cae98f

Browse files
committed
Refactor User class and add HookedUser class with asymmetric visibility properties; update tests to validate property hooks
1 parent 68ef309 commit 4cae98f

3 files changed

Lines changed: 41 additions & 27 deletions

File tree

tests/Fixtures/Domain/User.php

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

77
class User
88
{
9-
/**
10-
* Asymmetric visibility property with @var constraint
11-
*
12-
* @var positive-int
13-
*/
14-
public private(set) int $id = 10;
15-
16-
/**
17-
* Property hook with asymmetric visibility and @var constraint
18-
*
19-
* @var non-empty-string
20-
*/
21-
public protected(set) string $username {
22-
get => $this->_username;
23-
set => $this->_username = trim($value);
24-
}
25-
26-
private string $_username = 'Alice';
27-
28-
public function updateProfile(int $newId, string $newUsername): void
29-
{
30-
$this->id = $newId; // Validated against @var positive-int!
31-
$this->username = $newUsername; // Validated in set hook against @var non-empty-string!
9+
public function __construct(
10+
public string $name = 'Alice',
11+
public int $id = 1
12+
) {
3213
}
33-
}
14+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypePHP\Tests\Fixtures\Types;
6+
7+
class HookedUser
8+
{
9+
/**
10+
* Asymmetric visibility property with @var constraint
11+
*
12+
* @var positive-int
13+
*/
14+
public private(set) int $id = 10;
15+
16+
/**
17+
* Property hook with asymmetric visibility and @var constraint
18+
*
19+
* @var non-empty-string
20+
*/
21+
public protected(set) string $username {
22+
get => $this->_username;
23+
set => $this->_username = trim($value);
24+
}
25+
26+
private string $_username = 'Alice';
27+
28+
public function updateProfile(int $newId, string $newUsername): void
29+
{
30+
$this->id = $newId;
31+
$this->username = $newUsername;
32+
}
33+
}

tests/TypeChecking/Boundaries/PropertyHooksTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
}
88

99
use TypePHP\Internal\Config;
10-
use TypePHP\Tests\Fixtures\Domain\User;
1110
use TypePHP\Tests\Fixtures\Types\HookedInterfaceImplementation;
11+
use TypePHP\Tests\Fixtures\Types\HookedUser;
1212
use TypePHP\Tests\Fixtures\Types\PropertyHooks;
1313

1414
beforeEach(function () {
@@ -76,8 +76,8 @@
7676
expect($fixture->unvalidatedHook)->toBe(-50);
7777
});
7878

79-
test('validates asymmetric visibility properties combined with property hooks', function () {
80-
$profile = new User();
79+
test('validates asymmetric visibility properties combined with property hooks', function () {
80+
$profile = new HookedUser();
8181

8282
$profile->updateProfile(100, 'Bob');
8383
expect($profile->id)->toBe(100);

0 commit comments

Comments
 (0)