Skip to content

Commit 6876a91

Browse files
committed
feat(default): introduce Default component
Signed-off-by: azjezz <azjezz@protonmail.com>
1 parent 5f0aeac commit 6876a91

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+693
-108
lines changed

docs/component/collection.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#### `Interfaces`
1414

1515
- [AccessibleCollectionInterface](./../../src/Psl/Collection/AccessibleCollectionInterface.php#L20)
16-
- [CollectionInterface](./../../src/Psl/Collection/CollectionInterface.php#L22)
16+
- [CollectionInterface](./../../src/Psl/Collection/CollectionInterface.php#L23)
1717
- [IndexAccessInterface](./../../src/Psl/Collection/IndexAccessInterface.php#L13)
1818
- [MapInterface](./../../src/Psl/Collection/MapInterface.php#L15)
1919
- [MutableAccessibleCollectionInterface](./../../src/Psl/Collection/MutableAccessibleCollectionInterface.php#L22)

docs/component/comparison.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828

2929
#### `Enums`
3030

31-
- [Order](./../../src/Psl/Comparison/Order.php#L7)
31+
- [Order](./../../src/Psl/Comparison/Order.php#L23)
3232

3333

docs/component/data-structure.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#### `Interfaces`
1414

1515
- [PriorityQueueInterface](./../../src/Psl/DataStructure/PriorityQueueInterface.php#L12)
16-
- [QueueInterface](./../../src/Psl/DataStructure/QueueInterface.php#L16)
16+
- [QueueInterface](./../../src/Psl/DataStructure/QueueInterface.php#L17)
1717
- [StackInterface](./../../src/Psl/DataStructure/StackInterface.php#L16)
1818

1919
#### `Classes`

docs/component/encoding-base64.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717

1818
#### `Enums`
1919

20-
- [Variant](./../../src/Psl/Encoding/Base64/Variant.php#L7)
20+
- [Variant](./../../src/Psl/Encoding/Base64/Variant.php#L12)
2121

2222

docs/component/html.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020

2121
#### `Enums`
2222

23-
- [Encoding](./../../src/Psl/Html/Encoding.php#L7)
23+
- [Encoding](./../../src/Psl/Html/Encoding.php#L20)
2424

2525

docs/component/network.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#### `Classes`
2121

2222
- [Address](./../../src/Psl/Network/Address.php#L7)
23-
- [SocketOptions](./../../src/Psl/Network/SocketOptions.php#L7)
23+
- [SocketOptions](./../../src/Psl/Network/SocketOptions.php#L14)
2424

2525
#### `Enums`
2626

docs/component/os.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818

1919
#### `Enums`
2020

21-
- [OperatingSystemFamily](./../../src/Psl/OS/OperatingSystemFamily.php#L7)
21+
- [OperatingSystemFamily](./../../src/Psl/OS/OperatingSystemFamily.php#L16)
2222

2323

docs/component/password.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919

2020
#### `Enums`
2121

22-
- [Algorithm](./../../src/Psl/Password/Algorithm.php#L11)
22+
- [Algorithm](./../../src/Psl/Password/Algorithm.php#L23)
2323

2424

docs/component/random-sequence.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818

1919
- [MersenneTwisterPHPVariantSequence](./../../src/Psl/RandomSequence/MersenneTwisterPHPVariantSequence.php#L10)
2020
- [MersenneTwisterSequence](./../../src/Psl/RandomSequence/MersenneTwisterSequence.php#L10)
21-
- [SecureSequence](./../../src/Psl/RandomSequence/SecureSequence.php#L12)
21+
- [SecureSequence](./../../src/Psl/RandomSequence/SecureSequence.php#L15)
2222

2323

docs/component/shell.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818

1919
#### `Enums`
2020

21-
- [ErrorOutputBehavior](./../../src/Psl/Shell/ErrorOutputBehavior.php#L7)
21+
- [ErrorOutputBehavior](./../../src/Psl/Shell/ErrorOutputBehavior.php#L17)
2222

2323

docs/component/str.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,6 @@
7979

8080
#### `Enums`
8181

82-
- [Encoding](./../../src/Psl/Str/Encoding.php#L7)
82+
- [Encoding](./../../src/Psl/Str/Encoding.php#L16)
8383

8484

docs/component/tcp.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
#### `Classes`
1818

19-
- [ConnectOptions](./../../src/Psl/TCP/ConnectOptions.php#L7)
19+
- [ConnectOptions](./../../src/Psl/TCP/ConnectOptions.php#L14)
2020
- [Server](./../../src/Psl/TCP/Server.php#L11)
21-
- [ServerOptions](./../../src/Psl/TCP/ServerOptions.php#L9)
21+
- [ServerOptions](./../../src/Psl/TCP/ServerOptions.php#L15)
2222

2323

examples/tcp/basic-http-server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</html>
2828
HTML;
2929

30-
$server = TCP\Server::create('localhost', 3030, TCP\ServerOptions::create(idleConnections: 1024));
30+
$server = TCP\Server::create('localhost', 3030, TCP\ServerOptions::create(idle_connections: 1024));
3131

3232
Async\Scheduler::onSignal(SIGINT, $server->close(...));
3333

src/Psl/Collection/CollectionInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Countable;
99
use IteratorAggregate;
1010
use JsonSerializable;
11+
use Psl\Default\DefaultInterface;
1112

1213
/**
1314
* The base interface implemented for a CollectionInterface type.
@@ -19,7 +20,7 @@
1920
*
2021
* @extends IteratorAggregate<Tk, Tv>
2122
*/
22-
interface CollectionInterface extends Countable, IteratorAggregate, JsonSerializable
23+
interface CollectionInterface extends Countable, DefaultInterface, IteratorAggregate, JsonSerializable
2324
{
2425
/**
2526
* Is the CollectionInterface empty?

src/Psl/Collection/Map.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ public function __construct(
3232
) {
3333
}
3434

35+
/**
36+
* Creates and returns a default instance of {@see Map}.
37+
*
38+
* @return static A default instance of {@see Map}.
39+
*
40+
* @pure
41+
*/
42+
public static function default(): static
43+
{
44+
return new self([]);
45+
}
46+
3547
/**
3648
* @template Tsk of array-key
3749
* @template Tsv

src/Psl/Collection/MutableMap.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ public function __construct(
3434
) {
3535
}
3636

37+
/**
38+
* Creates and returns a default instance of {@see MutableMap}.
39+
*
40+
* @return static A default instance of {@see MutableMap}.
41+
*
42+
* @pure
43+
*/
44+
public static function default(): static
45+
{
46+
return new self([]);
47+
}
48+
3749
/**
3850
* @template Tsk of array-key
3951
* @template Tsv

src/Psl/Collection/MutableVector.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ public function __construct(array $elements)
3939
}
4040
}
4141

42+
/**
43+
* Creates and returns a default instance of {@see MutableVector}.
44+
*
45+
* @return static A default instance of {@see MutableVector}.
46+
*
47+
* @pure
48+
*/
49+
public static function default(): static
50+
{
51+
return new self([]);
52+
}
53+
4254
/**
4355
* Create a vector from the given $elements array.
4456
*

src/Psl/Collection/Vector.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ public function __construct(array $elements)
3939
$this->elements = $list;
4040
}
4141

42+
/**
43+
* Creates and returns a default instance of {@see Vector}.
44+
*
45+
* @return static A default instance of {@see Vector}.
46+
*
47+
* @pure
48+
*/
49+
public static function default(): static
50+
{
51+
return new self([]);
52+
}
53+
4254
/**
4355
* Create a vector from the given $elements array.
4456
*
@@ -497,7 +509,7 @@ public function slice(int $start, ?int $length = null): Vector
497509
*
498510
* @param positive-int $size The size of each chunk.
499511
*
500-
* @return Vector<Vector<T>> A `Vector` containing the original `Vector` split
512+
* @return Vector<static<T>> A `Vector` containing the original `Vector` split
501513
* into chunks of the given size.
502514
*
503515
* @psalm-mutation-free
@@ -517,7 +529,7 @@ public function chunk(int $size): Vector
517529
*
518530
* @return Vector<T>
519531
*/
520-
static fn(array $chunk) => Vector::fromArray($chunk)
532+
static fn(array $chunk) => static::fromArray($chunk)
521533
));
522534
}
523535
}

src/Psl/Comparison/Order.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,41 @@
44

55
namespace Psl\Comparison;
66

7-
enum Order : int
7+
use Psl\Default\DefaultInterface;
8+
9+
/**
10+
* Enum the possible outcomes of a comparison operation.
11+
*
12+
* This enum provides a standardized way to represent the result of a comparison,
13+
* making it easier to understand and use comparison outcomes in a type-safe manner.
14+
* Implementing the DefaultInterface, it also provides a sensible default value.
15+
*
16+
* - `Less` indicates that the first value is less than the second.
17+
* - `Equal` suggests that the two values are equal.
18+
* - `Greater` means that the first value is greater than the second.
19+
*
20+
* Usage of this enum can help to avoid "magic numbers" in comparison logic and make
21+
* code more readable and maintainable.
22+
*/
23+
enum Order: int implements DefaultInterface
824
{
925
case Less = -1;
1026
case Equal = 0;
1127
case Greater = 1;
28+
29+
/**
30+
* Provides the default comparison outcome.
31+
*
32+
* This method returns the `Equal` case as the default state, indicating no difference
33+
* between the compared values. It's useful in contexts where a neutral or "no change"
34+
* state is needed as the starting point.
35+
*
36+
* @return static The default instance of the enum, which is `Order::Equal`.
37+
*
38+
* @pure
39+
*/
40+
public static function default(): static
41+
{
42+
return self::Equal;
43+
}
1244
}

src/Psl/DataStructure/PriorityQueue.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,24 @@ final class PriorityQueue implements PriorityQueueInterface
2222
*/
2323
private array $queue = [];
2424

25+
/**
26+
* Provides a default instance of the {@see PriorityQueue}.
27+
*
28+
* @return static A new instance of {@see PriorityQueue}, devoid of any nodes.
29+
*
30+
* @pure
31+
*/
32+
public static function default(): static
33+
{
34+
return new self();
35+
}
36+
2537
/**
2638
* Adds a node to the queue.
2739
*
2840
* @param T $node
41+
*
42+
* @external-mutation-free
2943
*/
3044
public function enqueue(mixed $node, int $priority = 0): void
3145
{
@@ -40,6 +54,8 @@ public function enqueue(mixed $node, int $priority = 0): void
4054
* or returns null if this queue is empty.
4155
*
4256
* @return null|T
57+
*
58+
* @mutation-free
4359
*/
4460
public function peek(): mixed
4561
{
@@ -64,6 +80,8 @@ public function peek(): mixed
6480
* or returns null if this queue is empty.
6581
*
6682
* @return null|T
83+
*
84+
* @external-mutation-free
6785
*/
6886
public function pull(): mixed
6987
{
@@ -80,6 +98,8 @@ public function pull(): mixed
8098
* @throws Exception\UnderflowException If the queue is empty.
8199
*
82100
* @return T
101+
*
102+
* @external-mutation-free
83103
*/
84104
public function dequeue(): mixed
85105
{
@@ -117,6 +137,8 @@ public function dequeue(): mixed
117137
* Count the nodes in the queue.
118138
*
119139
* @return int<0, max>
140+
*
141+
* @mutation-free
120142
*/
121143
public function count(): int
122144
{

src/Psl/DataStructure/Queue.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,24 @@ final class Queue implements QueueInterface
2121
*/
2222
private array $queue = [];
2323

24+
/**
25+
* Provides a default instance of the {@see Queue}.
26+
*
27+
* @return static A new instance of {@see Queue}, devoid of any nodes.
28+
*
29+
* @pure
30+
*/
31+
public static function default(): static
32+
{
33+
return new self();
34+
}
35+
2436
/**
2537
* Adds a node to the queue.
2638
*
2739
* @param T $node
40+
*
41+
* @external-mutation-free
2842
*/
2943
public function enqueue(mixed $node): void
3044
{
@@ -36,6 +50,8 @@ public function enqueue(mixed $node): void
3650
* or returns null if this queue is empty.
3751
*
3852
* @return null|T
53+
*
54+
* @mutation-free
3955
*/
4056
public function peek(): mixed
4157
{
@@ -47,6 +63,8 @@ public function peek(): mixed
4763
* or returns null if this queue is empty.
4864
*
4965
* @return null|T
66+
*
67+
* @external-mutation-free
5068
*/
5169
public function pull(): mixed
5270
{
@@ -59,6 +77,8 @@ public function pull(): mixed
5977
* @throws Exception\UnderflowException If the queue is empty.
6078
*
6179
* @return T
80+
*
81+
* @external-mutation-free
6282
*/
6383
public function dequeue(): mixed
6484
{
@@ -74,6 +94,8 @@ public function dequeue(): mixed
7494
* Count the nodes in the queue.
7595
*
7696
* @return int<0, max>
97+
*
98+
* @mutation-free
7799
*/
78100
public function count(): int
79101
{

0 commit comments

Comments
 (0)