Skip to content

Commit b36b020

Browse files
author
fureev
committed
fix
1 parent 1af5f88 commit b36b020

File tree

1 file changed

+46
-60
lines changed

1 file changed

+46
-60
lines changed

src/Global/base.php

+46-60
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,10 @@
55
use Php\Support\Structures\Collections\ReadableCollection;
66

77
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
179
{
1810
return $value instanceof Closure || (is_object($value) && is_callable($value))
19-
? $value(...$args)
11+
? $value(...$params)
2012
: $value;
2113
}
2214
}
@@ -76,6 +68,14 @@ function dataGet(mixed $target, string|array|int|null $key, mixed $default = nul
7668

7769

7870
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+
*/
7979
function mapValue(callable $fn, iterable $collection, mixed ...$args): array
8080
{
8181
$result = [];
@@ -89,6 +89,14 @@ function mapValue(callable $fn, iterable $collection, mixed ...$args): array
8989
}
9090

9191
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+
*/
92100
function eachValue(callable $fn, iterable $collection, mixed ...$args): void
93101
{
94102
foreach ($collection as $key => $value) {
@@ -100,14 +108,8 @@ function eachValue(callable $fn, iterable $collection, mixed ...$args): void
100108
if (!function_exists('when')) {
101109
/**
102110
* 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
109111
*/
110-
function when($condition, $value, $default = null)
112+
function when(mixed $condition, mixed $value, mixed $default = null): mixed
111113
{
112114
if ($result = value($condition)) {
113115
return $value instanceof Closure ? $value($result) : $value;
@@ -118,12 +120,7 @@ function when($condition, $value, $default = null)
118120
}
119121

120122
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
127124
{
128125
if (is_object($class)) {
129126
$class = get_class($class);
@@ -136,13 +133,8 @@ function classNamespace($class): string
136133
if (!function_exists('isTrue')) {
137134
/**
138135
* Returns bool value of a value
139-
*
140-
* @param mixed $val
141-
* @param bool $return_null
142-
*
143-
* @return bool|null
144136
*/
145-
function isTrue($val, bool $return_null = false): ?bool
137+
function isTrue(mixed $val, bool $return_null = false): ?bool
146138
{
147139
if ($val === null && $return_null) {
148140
return null;
@@ -157,12 +149,13 @@ function isTrue($val, bool $return_null = false): ?bool
157149

158150
if (!function_exists('instance')) {
159151
/**
160-
* @param string|object $instance
152+
* @phpstan-param T|class-string<T>|null $instance
161153
* @param mixed ...$params
154+
* @phpstan-return T|null
162155
*
163-
* @return mixed
156+
* @template T as object
164157
*/
165-
function instance($instance, ...$params): mixed
158+
function instance(string|object|null $instance, mixed ...$params): ?object
166159
{
167160
if (is_object($instance)) {
168161
return $instance;
@@ -179,12 +172,8 @@ function instance($instance, ...$params): mixed
179172
if (!function_exists('class_basename')) {
180173
/**
181174
* Get the class "basename" of the given object / class.
182-
*
183-
* @param string|object $class
184-
*
185-
* @return string
186175
*/
187-
function class_basename($class)
176+
function class_basename(string|object $class): string
188177
{
189178
$class = is_object($class) ? get_class($class) : $class;
190179

@@ -196,17 +185,15 @@ function class_basename($class)
196185
/**
197186
* Returns all traits used by a trait and its traits.
198187
*
199-
* @param string $trait
200-
*
201-
* @return array
188+
* @return string[]
202189
*/
203190
function trait_uses_recursive(string $trait): array
204191
{
205192
if (!$traits = class_uses($trait)) {
206193
return [];
207194
}
208195

209-
foreach ((array)$traits as $trt) {
196+
foreach ($traits as $trt) {
210197
$traits += trait_uses_recursive($trt);
211198
}
212199

@@ -215,12 +202,6 @@ function trait_uses_recursive(string $trait): array
215202
}
216203

217204
if (!function_exists('does_trait_use')) {
218-
/**
219-
* @param string $class
220-
* @param string $trait
221-
*
222-
* @return bool
223-
*/
224205
function does_trait_use(string $class, string $trait): bool
225206
{
226207
return isset(trait_uses_recursive($class)[$trait]);
@@ -231,14 +212,12 @@ function does_trait_use(string $class, string $trait): bool
231212
/**
232213
* Returns all traits used by a class, its parent classes and trait of their traits.
233214
*
234-
* @param object|string $class
235-
*
236-
* @return array
215+
* @return string[]
237216
*/
238-
function class_uses_recursive($class): array
217+
function class_uses_recursive(object|string $class): array
239218
{
240219
if (is_object($class)) {
241-
$class = get_class($class);
220+
$class = $class::class;
242221
}
243222

244223
$results = [];
@@ -262,10 +241,7 @@ function remoteStaticCall(object|string|null $class, string $method, mixed ...$p
262241
return null;
263242
}
264243

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)) {
269245
return $class::$method(...$params);
270246
}
271247

@@ -283,10 +259,7 @@ function remoteStaticCallOrTrow(object|string|null $class, string $method, mixed
283259
throw new RuntimeException('Target Class is absent');
284260
}
285261

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)) {
290263
return $class::$method(...$params);
291264
}
292265

@@ -346,6 +319,19 @@ function findGetterMethod(object $instance, string $attribute): ?string
346319
}
347320
}
348321

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+
}
349335

350336
if (!function_exists('public_property_exists')) {
351337
/**

0 commit comments

Comments
 (0)