Skip to content

Commit 2951f78

Browse files
committed
feat!: make abstract value and trait readonly
1 parent de285f2 commit 2951f78

File tree

5 files changed

+7
-23
lines changed

5 files changed

+7
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file. This projec
99

1010
- Minimum PHP version is now 8.2.
1111
- Added generics to the value interface, abstract class and trait. This will be breaking if you are running PHPStan.
12+
- **BREAKING**: The abstract value and value trait are now both readonly. This is because value objects should always be immutable.
1213

1314
## [2.2.0] - 2022-03-04
1415

src/AbstractValue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @template TValue
1717
* @implements ValueInterface<TValue>
1818
*/
19-
abstract class AbstractValue implements ValueInterface
19+
abstract readonly class AbstractValue implements ValueInterface
2020
{
2121
/** @use ValueTrait<TValue> */
2222
use ValueTrait;

src/ValueTrait.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,15 @@ trait ValueTrait
2323
/**
2424
* @var TValue
2525
*/
26-
protected mixed $value;
26+
public readonly mixed $value;
2727

28-
/**
29-
* @return string
30-
*/
3128
public function __toString(): string
3229
{
3330
return $this->toString();
3431
}
3532

3633
/**
3734
* Fluent to string method.
38-
*
39-
* @return string
4035
*/
4136
public function toString(): string
4237
{
@@ -58,8 +53,7 @@ public function get(): mixed
5853
/**
5954
* Is the value any of the provided values?
6055
*
61-
* @param mixed ...$values
62-
* @return bool
56+
* @param mixed ...$values
6357
*/
6458
public function is(...$values): bool
6559
{
@@ -76,20 +70,14 @@ public function is(...$values): bool
7670
return false;
7771
}
7872

79-
/**
80-
* @return bool
81-
*/
8273
public function isEmpty(): bool
8374
{
8475
return empty($this->value);
8576
}
8677

87-
/**
88-
* @return bool
89-
*/
9078
public function isNotEmpty(): bool
9179
{
92-
return !$this->isEmpty();
80+
return ! $this->isEmpty();
9381
}
9482

9583
/**
@@ -102,9 +90,6 @@ public function jsonSerialize(): mixed
10290

10391
/**
10492
* Does the value match the provided value?
105-
*
106-
* @param mixed $value
107-
* @return bool
10893
*/
10994
protected function matches(mixed $value): bool
11095
{
@@ -121,8 +106,6 @@ protected function matches(mixed $value): bool
121106

122107
/**
123108
* Should strict comparison be used for comparing values?
124-
*
125-
* @return bool
126109
*/
127110
protected function useStrict(): bool
128111
{

tests/IntegerValue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* @extends AbstractValue<int>
1919
*/
20-
class IntegerValue extends AbstractValue
20+
final readonly class IntegerValue extends AbstractValue
2121
{
2222
protected function accept(mixed $value): bool
2323
{

tests/StringValue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* @extends AbstractValue<string>
1919
*/
20-
class StringValue extends AbstractValue
20+
final readonly class StringValue extends AbstractValue
2121
{
2222
protected function accept(mixed $value): bool
2323
{

0 commit comments

Comments
 (0)