-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathteds_interfaces.stub.php
141 lines (128 loc) · 5.54 KB
/
teds_interfaces.stub.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* @generate-class-entries
* @generate-legacy-arginfo 80000
*/
namespace Teds;
/**
* Collection is a common interface for an object with values that can be iterated over and counted,
* but not addressed with ArrayAccess (e.g. Sets, Heaps, objects that yield values in an order in their iterators, etc.)
*
* Note: JsonSerializable was not included here because it
* 1. Causes issues or inefficiencies with some ways of implementing data structures internally.
* 2. Forces the result to be represented as []
* 3. Forces polyfills to implement JsonSerializable as well, even when it would be less efficient
*
* @alias Teds\Values
* (This alias will be removed in a future major release. Do not use it.)
*/
interface Collection extends \Traversable, \Countable {
/** @psalm-return list<values> the list of values in the collection */
public function values(): array {}
/**
* Returns a list or associative array,
* typically attempting to cast keys to array key types (int/string) to insert elements of the collection into the array, or throwing.
*
* When this is impossible for the class in general,
* this throws.
*/
public function toArray(): array {}
// TODO: Provide a trait that provides default implementations for these methods?
/** Returns true if count() would be 0 */
public function isEmpty(): bool {}
/** Returns true if this contains a value identical to $value (for the definition of value equality used by the implementation, which may be stricter or weaker than `===`) */
public function contains(mixed $value): bool {}
/** Removes all elements from this Collection or throws. */
public function clear(): void {}
}
/**
* This represents a Collection that can be used like a list without gaps.
* E.g. get()/set() will work for is_int($offset) && 0 <= $offset and $offset < $list->count().
*
* @implementation-notes
* NOTE: List is a reserved keyword in php and cannot be used as an identifier, e.g. `list($x) = [1]`.
* List is also used as a non-standard type in phpdoc for arrays where array_is_list() is true.
*
* NOTE: Many methods of Sequence throw \OutOfBoundsException.
* https://www.php.net/manual/en/class.outofboundsexception.php is described as an exception that "represents errors that cannot be detected at compile time."
* Since the length of a Sequence (e.g. Vector, Deque) is often not known at compile time and the code in question may be accepting `Sequence $sequence`,
* throwing `OutOfBoundsException extends RuntimeException` seems more appropriate than `OutOfRangeException extends LogicException`
*/
interface Sequence extends Collection, \ArrayAccess {
/**
* Returns the value at $offset.
* Type safe alternative to offsetGet.
*
* @throws \OutOfBoundsException if $offset < 0 or $offset >= count
*/
public function get(int $offset): mixed {}
/**
* Sets the value at $offset to $value.
* Type safe alternative to offsetSet.
*
* @throws \OutOfBoundsException if $offset < 0 or $offset >= count
*/
public function set(int $offset, mixed $value): void {}
/**
* Inserts 0 or more values into $offset and move subsequent values out of the way.
*
* @throws \OutOfBoundsException if $offset < 0 or $offset > count
*/
public function insert(int $offset, mixed ...$values): void {}
/**
* Append 0 or more values to the end of the Sequence.
*/
public function push(mixed ...$values): void {}
/**
* Remove the last value and returns its value.
* @throws \UnderflowException if there are no more elements.
*/
public function pop(): mixed {}
/**
* Prepend 0 or more values to the start of the sequence.
*/
public function pushFront(mixed ...$values): void {}
/**
* Removes the first value and returns its value.
* @throws \UnderflowException if there are no more elements.
*/
public function popFront(): mixed {}
/**
* Returns the first value (at offset 0)
* @throws \UnderflowException if there are no elements.
*/
public function first(): mixed {}
/**
* Returns the first value (at offset count() - 1)
* @throws \UnderflowException if there are no elements.
*/
public function last(): mixed {}
}
/**
* A Map is a type of Collection mapping unique keys to values.
*
* Implementations should either coerce unsupported key types or throw TypeError when using keys.
*/
interface Map extends Collection, \ArrayAccess {
/**
* Returns true if there exists a key identical to $key according to the semantics of the implementing collection.
* Typically, this is close to the definition of `===`, but may be stricter or weaker in some implementations, e.g. for NAN, negative zero, etc.
*
* containsKey differs from offsetExists, where implementations of offsetExists usually return false if the key was found but the corresponding value was null.
* (The difference between containsKey and offsetExists is analogous to the difference between array_key_exists($key, $array) and isset($array[$key]))
*/
public function containsKey(mixed $value): bool {}
}
/**
* A Set is a type of Collection representing a set of unique values.
*/
interface Set extends Collection {
/**
* Returns true if $value was added to this Set and was not previously in this Set.
*/
public function add(mixed $value): bool {}
/**
* Returns true if $value was found in this Set before being removed from this Set.
*/
public function remove(mixed $value): bool {}
}