Skip to content

Commit 4581aca

Browse files
authored
feat: Normalize Interfaces (#15)
* add size method * feat: Normalize Interfaces - Refined and standardized interface definitions across the project - Applied Single Responsibility Principle (SRP) to ensure each interface has a clear, singular purpose - Consistent naming conventions were implemented for better readability and maintainability - Added thorough documentation for each interface, including method descriptions and expected behaviors - Organized interfaces within appropriate namespaces to prevent naming collisions and maintain a logical structure - Ensured parameter names in interface methods are consistent with implementing classes - Avoided including constructors in interfaces to maintain flexibility These changes enhance the overall clarity, maintainability, and professional quality of the codebase.
1 parent 4f10f5a commit 4581aca

File tree

15 files changed

+181
-199
lines changed

15 files changed

+181
-199
lines changed

src/DataStructure/Behavioral/Countable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ interface Countable
2323
*
2424
* @return int The number of elements
2525
*/
26-
public function count(): int;
26+
public function size(): int;
2727
}

src/DataStructure/Behavioral/Indexable.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,19 @@
1919
interface Indexable
2020
{
2121
/**
22-
* Checks if an element exists at the given index.
22+
* Retrieves an element at the specified index.
2323
*
24-
* @param int $index The index to check
25-
*
26-
* @return bool True if the index exists, false otherwise
27-
*/
28-
public function contains(int $index): bool;
29-
30-
/**
31-
* Gets the element at the specified index.
32-
*
33-
* @param int $index The index of the element
24+
* @param int $index The index of the element to retrieve
3425
*
3526
* @return mixed The element at the specified index
3627
*/
3728
public function get(int $index): mixed;
3829

3930
/**
40-
* Sets the element at the specified index.
31+
* Sets an element at the specified index.
4132
*
42-
* @param int $index The index at which to set the element
33+
* @param int $index The index where the element should be set
4334
* @param mixed $element The element to set
4435
*/
4536
public function set(int $index, mixed $element): void;
46-
47-
/**
48-
* Removes the element at the specified index.
49-
*
50-
* @param int $index The index of the element to remove
51-
*
52-
* @return bool True if the element was removed, false otherwise
53-
*/
54-
public function remove(int $index): bool;
5537
}

src/DataStructure/Behavioral/IterableCollection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
*
1010
* Provides the ability to iterate over a collection.
1111
*
12-
* @author Walmir Silva <walmir.silva@kariricode.org>
12+
* @category Interfaces
13+
*
1314
* @author Walmir Silva <walmir.silva@kariricode.org>
1415
* @license MIT
1516
*
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\DataStructure\Behavioral;
6+
7+
/**
8+
* Interface Modifiable.
9+
*
10+
* Defines the contract for modifying elements in a collection.
11+
*
12+
* @category Interfaces
13+
*
14+
* @author Walmir Silva <walmir.silva@kariricode.org>
15+
* @license MIT
16+
*
17+
* @see https://kariricode.org/
18+
*/
19+
interface Modifiable
20+
{
21+
/**
22+
* Adds an element to the collection.
23+
*
24+
* @param mixed $element The element to add
25+
*/
26+
public function add(mixed $element): void;
27+
28+
/**
29+
* Removes an element from the collection.
30+
*
31+
* @param mixed $element The element to remove
32+
*
33+
* @return bool True if the element was removed, false otherwise
34+
*/
35+
public function remove(mixed $element): bool;
36+
37+
/**
38+
* Clears all elements from the collection.
39+
*/
40+
public function clear(): void;
41+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\DataStructure\Behavioral;
6+
7+
/**
8+
* Interface Searchable.
9+
*
10+
* Defines the contract for searching elements in a collection.
11+
*
12+
* @category Interfaces
13+
*
14+
* @author Walmir Silva <walmir.silva@kariricode.org>
15+
* @license MIT
16+
*
17+
* @see https://kariricode.org/
18+
*/
19+
interface Searchable
20+
{
21+
/**
22+
* Checks if the collection contains a specific element.
23+
*
24+
* @param mixed $element The element to check for
25+
*
26+
* @return bool True if the element is present, false otherwise
27+
*/
28+
public function contains(mixed $element): bool;
29+
30+
/**
31+
* Finds the index of a specific element in the collection.
32+
*
33+
* @param mixed $element The element to find
34+
*
35+
* @return int The index of the element
36+
*/
37+
public function find(mixed $element): mixed;
38+
}

src/DataStructure/Map.php

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

55
namespace KaririCode\Contract\DataStructure;
66

7+
use KaririCode\Contract\DataStructure\Behavioral\Countable;
8+
use KaririCode\Contract\DataStructure\Behavioral\IterableCollection;
9+
710
/**
811
* Interface Map.
912
*
@@ -16,37 +19,59 @@
1619
*
1720
* @see https://kariricode.org/
1821
*/
19-
interface Map
22+
interface Map extends Countable, IterableCollection
2023
{
2124
/**
22-
* Puts a key-value pair into the map.
25+
* Adds a key-value pair to the map.
2326
*
24-
* @param mixed $key the key
25-
* @param mixed $value the value
27+
* @param mixed $key The key to add
28+
* @param mixed $value The value to add
2629
*/
2730
public function put(mixed $key, mixed $value): void;
2831

2932
/**
30-
* Gets a value by its key.
33+
* Retrieves a value by its key.
3134
*
32-
* @param mixed $key the key
35+
* @param mixed $key The key to retrieve the value for
3336
*
34-
* @return mixed the value associated with the key
37+
* @return mixed The value associated with the key
3538
*/
3639
public function get(mixed $key): mixed;
3740

3841
/**
3942
* Removes a key-value pair from the map.
4043
*
41-
* @param mixed $key the key
44+
* @param mixed $key The key to remove
4245
*
4346
* @return bool true if the key-value pair was removed, false otherwise
4447
*/
4548
public function remove(mixed $key): bool;
49+
50+
/**
51+
* Checks if the map contains a specific key.
52+
*
53+
* @param mixed $key The key to check for
54+
*
55+
* @return bool true if the key is present, false otherwise
56+
*/
57+
public function containsKey(mixed $key): bool;
58+
4659
/**
47-
* Returns the number of key-value mappings in the map.
60+
* Returns all keys in the map.
4861
*
49-
* @return int the number of key-value mappings
62+
* @return array The keys in the map
63+
*/
64+
public function keys(): array;
65+
66+
/**
67+
* Returns all values in the map.
68+
*
69+
* @return array The values in the map
70+
*/
71+
public function values(): array;
72+
73+
/**
74+
* Clears all key-value pairs from the map.
5075
*/
51-
public function size(): int;
76+
public function clear(): void;
5277
}

src/DataStructure/Queue.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace KaririCode\Contract\DataStructure;
66

7+
use KaririCode\Contract\DataStructure\Behavioral\Modifiable;
8+
79
/**
810
* Interface Queue.
911
*
@@ -16,7 +18,7 @@
1618
*
1719
* @see https://kariricode.org/
1820
*/
19-
interface Queue
21+
interface Queue extends Modifiable
2022
{
2123
/**
2224
* Enqueues an element into the queue.

src/DataStructure/Set.php

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

55
namespace KaririCode\Contract\DataStructure;
66

7+
use KaririCode\Contract\DataStructure\Behavioral\Modifiable;
8+
use KaririCode\Contract\DataStructure\Behavioral\Searchable;
9+
710
/**
811
* Interface Set.
912
*
@@ -16,51 +19,32 @@
1619
*
1720
* @see https://kariricode.org/
1821
*/
19-
interface Set
22+
interface Set extends Modifiable, Searchable
2023
{
2124
/**
22-
* Adds an element to the set.
23-
*
24-
* @param mixed $element The element to add
25-
*
26-
* @return bool True if the element was added, false if it was already present
27-
*/
28-
public function add(mixed $element): bool;
29-
30-
/**
31-
* Removes an element from the set.
25+
* Performs a union of this set with another set.
3226
*
33-
* @param mixed $element The element to remove
27+
* @param Set $other The other set to union with
3428
*
35-
* @return bool True if the element was removed, false if it was not present
29+
* @return Set The resulting set after the union
3630
*/
37-
public function remove(mixed $element): bool;
31+
public function union(Set $other): Set;
3832

3933
/**
40-
* Checks if the set contains a specific element.
34+
* Performs an intersection of this set with another set.
4135
*
42-
* @param mixed $element The element to check for
36+
* @param Set $other The other set to intersect with
4337
*
44-
* @return bool True if the element is present, false otherwise
38+
* @return Set The resulting set after the intersection
4539
*/
46-
public function contains(mixed $element): bool;
40+
public function intersection(Set $other): Set;
4741

4842
/**
49-
* Removes all elements from the set.
50-
*/
51-
public function clear(): void;
52-
53-
/**
54-
* Returns the number of elements in the set.
43+
* Performs a difference of this set with another set.
5544
*
56-
* @return int The number of elements in the set
57-
*/
58-
public function size(): int;
59-
60-
/**
61-
* Checks if the set is empty.
45+
* @param Set $other The other set to differentiate with
6246
*
63-
* @return bool True if the set is empty, false otherwise
47+
* @return Set The resulting set after the difference
6448
*/
65-
public function isEmpty(): bool;
49+
public function difference(Set $other): Set;
6650
}

src/DataStructure/Structural/Collection.php

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use KaririCode\Contract\DataStructure\Behavioral\Countable;
88
use KaririCode\Contract\DataStructure\Behavioral\Indexable;
9+
use KaririCode\Contract\DataStructure\Behavioral\Modifiable;
10+
use KaririCode\Contract\DataStructure\Behavioral\Searchable;
911

1012
/**
1113
* Interface Collection.
@@ -19,56 +21,12 @@
1921
*
2022
* @see https://kariricode.org/
2123
*/
22-
interface Collection extends Countable, Indexable
24+
interface Collection extends Countable, Indexable, Modifiable, Searchable
2325
{
24-
/**
25-
* Adds an element to the collection.
26-
*
27-
* @param mixed $element The element to add
28-
*/
29-
public function add(mixed $element): void;
30-
3126
/**
3227
* Adds all elements from another collection to this collection.
3328
*
3429
* @param Collection $collection The collection whose elements are to be added
3530
*/
3631
public function addAll(Collection $collection): void;
37-
38-
/**
39-
* Removes the first occurrence of a specific element from the collection.
40-
*
41-
* @param mixed $element The element to remove
42-
*
43-
* @return bool True if the element was removed, false otherwise
44-
*/
45-
public function remove(mixed $element): bool;
46-
47-
/**
48-
* Checks if the collection contains a specific element.
49-
*
50-
* @param mixed $element The element to check for
51-
*
52-
* @return bool True if the element is present, false otherwise
53-
*/
54-
public function contains(mixed $element): bool;
55-
56-
/**
57-
* Removes all elements from the collection.
58-
*/
59-
public function clear(): void;
60-
61-
/**
62-
* Checks if the collection is empty.
63-
*
64-
* @return bool True if the collection is empty, false otherwise
65-
*/
66-
public function isEmpty(): bool;
67-
68-
/**
69-
* Returns all elements of the collection as an array.
70-
*
71-
* @return array The elements of the collection
72-
*/
73-
public function getItems(): array;
7432
}

0 commit comments

Comments
 (0)