5
5
use Php \Support \Structures \Collections \ReadableCollection ;
6
6
7
7
if (!function_exists ('value ' )) {
8
- /**
9
- * Return the default value of the given value.
10
- *
11
- * @param mixed $value
12
- * @param mixed ...$args
13
- *
14
- * @return mixed
15
- */
16
- function value (mixed $ value , mixed ...$ args ): mixed
8
+ function value (mixed $ value , mixed ...$ params ): mixed
17
9
{
18
10
return $ value instanceof Closure || (is_object ($ value ) && is_callable ($ value ))
19
- ? $ value (...$ args )
11
+ ? $ value (...$ params )
20
12
: $ value ;
21
13
}
22
14
}
@@ -76,6 +68,14 @@ function dataGet(mixed $target, string|array|int|null $key, mixed $default = nul
76
68
77
69
78
70
if (!function_exists ('mapValue ' )) {
71
+ /**
72
+ * @template TKey of array-key
73
+ * @template TValue
74
+ * @param callable $fn
75
+ * @param iterable<TKey, TValue> $collection
76
+ * @param mixed ...$args
77
+ * @return array<TKey, TValue>
78
+ */
79
79
function mapValue (callable $ fn , iterable $ collection , mixed ...$ args ): array
80
80
{
81
81
$ result = [];
@@ -89,6 +89,14 @@ function mapValue(callable $fn, iterable $collection, mixed ...$args): array
89
89
}
90
90
91
91
if (!function_exists ('eachValue ' )) {
92
+ /**
93
+ * @template TKey of array-key
94
+ * @template TValue
95
+ * @param callable $fn
96
+ * @param iterable<TKey, TValue> $collection
97
+ * @param mixed ...$args
98
+ * @return void
99
+ */
92
100
function eachValue (callable $ fn , iterable $ collection , mixed ...$ args ): void
93
101
{
94
102
foreach ($ collection as $ key => $ value ) {
@@ -100,14 +108,8 @@ function eachValue(callable $fn, iterable $collection, mixed ...$args): void
100
108
if (!function_exists ('when ' )) {
101
109
/**
102
110
* Returns a value when a condition is truthy.
103
- *
104
- * @param mixed|bool|\Closure $condition
105
- * @param mixed|\Closure $value
106
- * @param mixed|\Closure|null $default
107
- *
108
- * @return mixed
109
111
*/
110
- function when ($ condition , $ value , $ default = null )
112
+ function when (mixed $ condition , mixed $ value , mixed $ default = null ): mixed
111
113
{
112
114
if ($ result = value ($ condition )) {
113
115
return $ value instanceof Closure ? $ value ($ result ) : $ value ;
@@ -118,12 +120,7 @@ function when($condition, $value, $default = null)
118
120
}
119
121
120
122
if (!function_exists ('classNamespace ' )) {
121
- /**
122
- * @param object|string $class
123
- *
124
- * @return string
125
- */
126
- function classNamespace ($ class ): string
123
+ function classNamespace (object |string $ class ): string
127
124
{
128
125
if (is_object ($ class )) {
129
126
$ class = get_class ($ class );
@@ -136,13 +133,8 @@ function classNamespace($class): string
136
133
if (!function_exists ('isTrue ' )) {
137
134
/**
138
135
* Returns bool value of a value
139
- *
140
- * @param mixed $val
141
- * @param bool $return_null
142
- *
143
- * @return bool|null
144
136
*/
145
- function isTrue ($ val , bool $ return_null = false ): ?bool
137
+ function isTrue (mixed $ val , bool $ return_null = false ): ?bool
146
138
{
147
139
if ($ val === null && $ return_null ) {
148
140
return null ;
@@ -157,12 +149,13 @@ function isTrue($val, bool $return_null = false): ?bool
157
149
158
150
if (!function_exists ('instance ' )) {
159
151
/**
160
- * @param string|object $instance
152
+ * @phpstan- param T|class- string<T>|null $instance
161
153
* @param mixed ...$params
154
+ * @phpstan-return T|null
162
155
*
163
- * @return mixed
156
+ * @template T as object
164
157
*/
165
- function instance ($ instance , ...$ params ): mixed
158
+ function instance (string | object | null $ instance , mixed ...$ params ): ? object
166
159
{
167
160
if (is_object ($ instance )) {
168
161
return $ instance ;
@@ -179,12 +172,8 @@ function instance($instance, ...$params): mixed
179
172
if (!function_exists ('class_basename ' )) {
180
173
/**
181
174
* Get the class "basename" of the given object / class.
182
- *
183
- * @param string|object $class
184
- *
185
- * @return string
186
175
*/
187
- function class_basename ($ class )
176
+ function class_basename (string | object $ class ): string
188
177
{
189
178
$ class = is_object ($ class ) ? get_class ($ class ) : $ class ;
190
179
@@ -196,17 +185,15 @@ function class_basename($class)
196
185
/**
197
186
* Returns all traits used by a trait and its traits.
198
187
*
199
- * @param string $trait
200
- *
201
- * @return array
188
+ * @return string[]
202
189
*/
203
190
function trait_uses_recursive (string $ trait ): array
204
191
{
205
192
if (!$ traits = class_uses ($ trait )) {
206
193
return [];
207
194
}
208
195
209
- foreach (( array ) $ traits as $ trt ) {
196
+ foreach ($ traits as $ trt ) {
210
197
$ traits += trait_uses_recursive ($ trt );
211
198
}
212
199
@@ -215,12 +202,6 @@ function trait_uses_recursive(string $trait): array
215
202
}
216
203
217
204
if (!function_exists ('does_trait_use ' )) {
218
- /**
219
- * @param string $class
220
- * @param string $trait
221
- *
222
- * @return bool
223
- */
224
205
function does_trait_use (string $ class , string $ trait ): bool
225
206
{
226
207
return isset (trait_uses_recursive ($ class )[$ trait ]);
@@ -231,14 +212,12 @@ function does_trait_use(string $class, string $trait): bool
231
212
/**
232
213
* Returns all traits used by a class, its parent classes and trait of their traits.
233
214
*
234
- * @param object|string $class
235
- *
236
- * @return array
215
+ * @return string[]
237
216
*/
238
- function class_uses_recursive ($ class ): array
217
+ function class_uses_recursive (object | string $ class ): array
239
218
{
240
219
if (is_object ($ class )) {
241
- $ class = get_class ( $ class) ;
220
+ $ class = $ class::class ;
242
221
}
243
222
244
223
$ results = [];
@@ -262,10 +241,7 @@ function remoteStaticCall(object|string|null $class, string $method, mixed ...$p
262
241
return null ;
263
242
}
264
243
265
- if (
266
- (is_object ($ class ) || (is_string ($ class ) && class_exists ($ class ))) &&
267
- method_exists ($ class , $ method )
268
- ) {
244
+ if ((is_object ($ class ) || class_exists ($ class )) && method_exists ($ class , $ method )) {
269
245
return $ class ::$ method (...$ params );
270
246
}
271
247
@@ -283,10 +259,7 @@ function remoteStaticCallOrTrow(object|string|null $class, string $method, mixed
283
259
throw new RuntimeException ('Target Class is absent ' );
284
260
}
285
261
286
- if (
287
- (is_object ($ class ) || (is_string ($ class ) && class_exists ($ class ))) &&
288
- method_exists ($ class , $ method )
289
- ) {
262
+ if ((is_object ($ class ) || class_exists ($ class )) && method_exists ($ class , $ method )) {
290
263
return $ class ::$ method (...$ params );
291
264
}
292
265
@@ -346,6 +319,19 @@ function findGetterMethod(object $instance, string $attribute): ?string
346
319
}
347
320
}
348
321
322
+ if (!function_exists ('findSetterMethodByProp ' )) {
323
+ /**
324
+ * Returns getter-method's name or null by an attribute
325
+ */
326
+ function findSetterMethodByProp (object $ instance , string $ attribute ): ?string
327
+ {
328
+ if (method_exists ($ instance , $ method = attributeToSetterMethod ($ attribute ))) {
329
+ return $ method ;
330
+ }
331
+
332
+ return null ;
333
+ }
334
+ }
349
335
350
336
if (!function_exists ('public_property_exists ' )) {
351
337
/**
0 commit comments