Skip to content

Commit 8c5c2f0

Browse files
Fix types
1 parent dc12098 commit 8c5c2f0

File tree

12 files changed

+118
-95
lines changed

12 files changed

+118
-95
lines changed

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ parameters:
44
- src
55
ignoreErrors:
66
- '#Unsafe usage of new static\(\)#'
7+
#- '#Method .+ has parameter \$[a-zA-Z0-9_]+ with no value type specified in iterable type array#'
8+
#- '#Method .+ return type has no value type specified in iterable type array#'

src/Transformation/CommonTransformation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ public function addAction(mixed $action): static
9494
*
9595
* Appended transformation is nested.
9696
*
97-
* @param CommonTransformation|string|null $transformation The transformation to add.
97+
* @param mixed $transformation The transformation to add.
9898
*
9999
*/
100-
public function addTransformation(CommonTransformation|string|null $transformation): static
100+
public function addTransformation(mixed $transformation): static
101101
{
102102
$this->actions[] = ClassUtils::forceInstance($transformation, CommonTransformation::class);
103103

src/Transformation/CommonTransformationInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ interface CommonTransformationInterface extends ComponentInterface
2222
*
2323
* (Formerly known as fetch format).
2424
*
25-
* @param string|Format $format The format in which to deliver the asset.
25+
* @param Format|string|null $format The format in which to deliver the asset.
2626
*
2727
*
2828
* @see Format
2929
*/
30-
public function format(Format|string $format): static;
30+
public function format(Format|string|null $format): static;
3131

3232
/**
3333
* Controls compression quality.
3434
*
3535
* Reducing the quality is a trade-off between visual quality and file size.
3636
*
37-
* @param Quality|int|float|string $quality The quality value. (Range 1 to 100)
37+
* @param Quality|int|float|string|null $quality The quality value. (Range 1 to 100)
3838
*
3939
*/
40-
public function quality(Quality|int|float|string $quality): static;
40+
public function quality(Quality|int|float|string|null $quality): static;
4141

4242
/**
4343
* Applies a filter or an effect on an asset.

src/Transformation/Delivery/TransformationDeliveryTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function delivery($delivery): static
4040
*
4141
*
4242
*/
43-
public function format(Format|string $format): static
43+
public function format(Format|string|null $format): static
4444
{
4545
return $this->addAction(ClassUtils::verifyInstance($format, Format::class));
4646
}
@@ -53,7 +53,7 @@ public function format(Format|string $format): static
5353
*
5454
*
5555
*/
56-
public function quality(Quality|int|float|string $quality): static
56+
public function quality(Quality|int|float|string|null $quality): static
5757
{
5858
return $this->addAction(ClassUtils::verifyInstance($quality, Quality::class));
5959
}

src/Transformation/Qualifier/Dimensions/DimensionsQualifierTrait.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Cloudinary\Transformation;
1212

13+
use Cloudinary\ClassUtils;
1314
use Cloudinary\Transformation\Qualifier\Dimensions\Dpr;
1415
use Cloudinary\Transformation\Qualifier\Dimensions\Height;
1516
use Cloudinary\Transformation\Qualifier\Dimensions\Width;
@@ -70,8 +71,8 @@ public static function aspectRatio(...$aspectRatio): AspectRatio
7071
*
7172
* @see Dpr
7273
*/
73-
public static function dpr(float $dpr): Dpr
74+
public static function dpr(int|float|string|Dpr $dpr): Dpr
7475
{
75-
return new Dpr($dpr);
76+
return ClassUtils::verifyInstance($dpr, Dpr::class);
7677
}
7778
}

src/Transformation/Qualifier/QualifiersAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class QualifiersAction extends BaseAction
7575
'video_codec' => null,
7676
];
7777

78-
protected const QUALIFIERS = self::COMPLEX_QUALIFIERS + self::SIMPLE_QUALIFIERS;
78+
public const QUALIFIERS = self::COMPLEX_QUALIFIERS + self::SIMPLE_QUALIFIERS;
7979

8080
/**
8181
* Add qualifiers to the action.

src/Transformation/Video/Transcode/Fps.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* href=https://cloudinary.com/documentation/video_transformation_reference#video_settings
2222
* target="_blank">Video settings</a>
2323
*
24-
* @property MinMaxRange value
24+
* @property MinMaxRange $value
2525
*
2626
* @api
2727
*/

src/Utils/ArrayUtils.php

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ class ArrayUtils
2222
/**
2323
* Applies the callback to the elements of the given associative array
2424
*
25-
* @param callable $callback The callback function
26-
* @param array $array An array to run through the callback function.
25+
* @param callable $callback The callback function
26+
* @param ArrayObject<int|string, mixed>|array $array An array to run through the callback function.
2727
*
2828
* @return array Resulting array
2929
*/
30-
public static function mapAssoc(callable $callback, array $array): array
30+
public static function mapAssoc(callable $callback, ArrayObject|array $array): array
3131
{
3232
$r = [];
3333
foreach ($array as $key => $value) {
@@ -128,40 +128,46 @@ static function ($k, $v) use ($inner, $innerIsSafe) {
128128
* @see implode
129129
*/
130130
public static function implodeFiltered(
131-
string $glue,
132-
array $pieces,
133-
string $filterCallback = __NAMESPACE__ . '\ArrayUtils::safeFilterFunc',
131+
array|string $glue,
132+
array|null $pieces,
133+
callable|null $filterCallback = null,
134134
int $flag = 0
135135
): string {
136+
$filterCallback ??= fn($value) => ArrayUtils::safeFilterFunc($value);
137+
136138
return self::safeImplode($glue, self::safeFilter($pieces, $filterCallback, $flag));
137139
}
138140

139141
/**
140142
* Safe version of implode.
141143
*
142-
* In addition fixes serialisation of float values.
143-
*
144+
* In addition, fixes serialisation of float values.
144145
*
145146
*/
146-
public static function safeImplode($glue, array $pieces): string
147+
public static function safeImplode(array|string $glue, array|null $pieces): string
147148
{
148-
array_walk(
149-
$pieces,
150-
static function (&$value) {
151-
$value = TransformationUtils::floatToString($value);
152-
}
153-
);
149+
if (! is_null($pieces)) {
150+
array_walk(
151+
$pieces,
152+
static function (&$value) {
153+
$value = TransformationUtils::floatToString($value);
154+
}
155+
);
156+
}
154157

155158
return implode($glue, $pieces);
156159
}
157160

158161
/**
159162
* Implodes array values with escaping the glue character.
160163
*
161-
*
162164
*/
163-
public static function escapedImplode(string $glue, array $pieces): string
165+
public static function escapedImplode(string|array $glue, array|null $pieces): string
164166
{
167+
if (is_null($pieces)) {
168+
return '';
169+
}
170+
165171
return implode(
166172
$glue,
167173
array_map(
@@ -206,18 +212,17 @@ protected static function safeFilterFunc(mixed $value): bool|int
206212
*
207213
* Uses "strlen" filter function by default, which treats non-null values (e.g. 0, false, etc) as non-empty.
208214
*
209-
* @param callback|string $callback
210-
*
211-
*
212215
* @see array_filter
213216
* @see strlen
214217
*/
215218
public static function safeFilter(
216-
array $input,
217-
callable|string $callback = __NAMESPACE__ . '\ArrayUtils::safeFilterFunc',
219+
?array $input,
220+
callable|null $callback = null,
218221
int $flag = 0
219-
): array {
220-
return array_filter($input, $callback, $flag);
222+
): ?array {
223+
$callback ??= fn($value) => ArrayUtils::safeFilterFunc($value);
224+
225+
return is_null($input) ? $input : array_filter($input, $callback, $flag);
221226
}
222227

223228
/**
@@ -226,7 +231,7 @@ public static function safeFilter(
226231
* In case some of the missing/extra keys, the keys are sorted using alphabetic order. Ordered keys come first.
227232
*
228233
* @param ?array $array The associate array to order.
229-
* @param array $orderArray The desired order of the keys.
234+
* @param array $orderArray The desired order of the keys.
230235
*
231236
*/
232237
public static function sortByArray(?array $array, array $orderArray = []): ?array
@@ -256,15 +261,16 @@ public static function implodeUrl(array $urlParts): string
256261
/**
257262
* Commonly used util for building transformation URL
258263
*
259-
* @param array $qualifiers
260-
*
261264
* @return string The resulting string
262265
*
263266
* @internal
264267
*/
265-
public static function implodeActionQualifiers(...$qualifiers): string
268+
public static function implodeActionQualifiers(mixed ...$qualifiers): string
266269
{
267-
$serializedQualifiers = array_map('strval', $qualifiers);
270+
$serializedQualifiers = [];
271+
foreach ($qualifiers as $item) {
272+
$serializedQualifiers[] = (string)$item;
273+
}
268274

269275
sort($serializedQualifiers);
270276

@@ -288,13 +294,13 @@ public static function implodeQualifierValues(...$qualifierValues): string
288294
/**
289295
* Gets a key from an array if exists, otherwise returns default.
290296
*
291-
* @param ArrayObject|array $array The data array.
292-
* @param int|array|string $key The key. Can be a simple key(string|int), an index array that allows accessing
297+
* @param mixed $array The data array.
298+
* @param int|array|string $key The key. Can be a simple key(string|int), an index array that allows accessing
293299
* nested values
294-
* @param mixed|null $default The default value for the case when the key is not found.
300+
* @param mixed|null $default The default value for the case when the key is not found.
295301
*
296302
*/
297-
public static function get(ArrayObject|array $array, int|array|string $key, mixed $default = null): mixed
303+
public static function get(mixed $array, int|array|string $key, mixed $default = null): mixed
298304
{
299305
if (is_array($key)) {
300306
$currLevel = &$array;
@@ -323,13 +329,12 @@ public static function get(ArrayObject|array $array, int|array|string $key, mixe
323329
/**
324330
* Pops a key from an array if exists, otherwise returns default
325331
*
326-
* @param array $array Data array
327-
* @param int|array|string $key key can be a simple key(string|int) or an array that allows accessing nested
328-
* values
329-
* @param mixed|null $default Default value for the case when key is not found
332+
* @param array $array Data array
333+
* @param int|string $key A simple key(string|int)
334+
* @param mixed|null $default Default value for the case when key is not found
330335
*
331336
*/
332-
public static function pop(array &$array, int|array|string $key, mixed $default = null): mixed
337+
public static function pop(array &$array, int|string $key, mixed $default = null): mixed
333338
{
334339
$val = self::get($array, $key, $default);
335340
unset($array[$key]);
@@ -341,7 +346,7 @@ public static function pop(array &$array, int|array|string $key, mixed $default
341346
* Returns a subset of associative array whitelisted by an array of keys
342347
*
343348
* @param ?array $array Source array (associative or not)
344-
* @param array $keys Simple array of keys to keep
349+
* @param array $keys Simple array of keys to keep
345350
*
346351
* @return ?array Resulting array
347352
*/
@@ -363,7 +368,7 @@ public static function whitelist(?array $array, array $keys): ?array
363368
* Returns a subset of associative array with excluded keys specified by an array of keys
364369
*
365370
* @param ?array $array Source array (associative or not)
366-
* @param array $blacklistedKeys Simple array of keys to be excluded
371+
* @param array $blacklistedKeys Simple array of keys to be excluded
367372
*
368373
* @return ?array Resulting array
369374
*/
@@ -500,10 +505,11 @@ public static function addNonEmpty(array &$arr, int|string|null $key, mixed $val
500505
/**
501506
* Adds key-value pair to the associative array where value is taken from source array using the same key.
502507
*
503-
* @param array & $resultingArr The target array.
504-
* @param mixed $key The key to add
505-
* @param array $sourceArray The source array
506-
* @param mixed|null $defaultValue Fallback value, in case the key does not exist or is empty
508+
* @param array & $resultingArr The target array.
509+
* @param mixed $key The key to add
510+
* @param array|ArrayObject<int|string, mixed> $sourceArray The source array
511+
* @param mixed|null $defaultValue Fallback value, in case the key does not exist or is
512+
* empty
507513
*
508514
* @return array The resulting array
509515
*
@@ -512,7 +518,7 @@ public static function addNonEmpty(array &$arr, int|string|null $key, mixed $val
512518
public static function addNonEmptyFromOther(
513519
array &$resultingArr,
514520
mixed $key,
515-
array $sourceArray,
521+
array|ArrayObject $sourceArray,
516522
mixed $defaultValue = null
517523
): array {
518524
return self::addNonEmpty($resultingArr, $key, self::get($sourceArray, $key, $defaultValue));
@@ -533,7 +539,12 @@ public static function mergeNonEmpty(...$arrays): array
533539
$result = [];
534540

535541
foreach ($arrays as $array) {
536-
$result = array_merge($result, self::safeFilter($array));
542+
if (! empty($array)) {
543+
$filtered = self::safeFilter($array);
544+
if ($filtered) {
545+
$result = array_merge($result, $filtered);
546+
}
547+
}
537548
}
538549

539550
return $result;
@@ -621,11 +632,11 @@ public static function convertToAssoc(array $array): array
621632
/**
622633
* Helper function for making a recursive array copy while cloning objects on the way.
623634
*
624-
* @param array $array Source array
635+
* @param mixed $array Source array
625636
*
626-
* @return array Recursive copy of the source array
637+
* @return mixed Recursive copy of the source array
627638
*/
628-
public static function deepCopy(array $array): array
639+
public static function deepCopy(mixed $array): mixed
629640
{
630641
if (! is_array($array)) {
631642
return $array;
@@ -647,10 +658,8 @@ public static function deepCopy(array $array): array
647658

648659
/**
649660
* Indicates whether all parameters are non-empty
650-
*
651-
*
652661
*/
653-
public static function all(...$params): bool
662+
public static function all(array ...$params): bool
654663
{
655664
foreach ($params as $param) {
656665
if (empty($param)) {

src/Utils/ClassUtils.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public static function getBaseName(string $className): string
5151
/**
5252
* Gets class constants.
5353
*
54-
* @param object|string $instance The instance object.
55-
* @param array $exclusions The list of constants to exclude.
54+
* @param object|class-string $instance The instance object.
55+
* @param array $exclusions The list of constants to exclude.
5656
*
5757
* @return array of class constants
5858
*/

src/Utils/JsonUtils.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ class JsonUtils
2323
/**
2424
* Determines whether the input is a valid JSON string.
2525
*
26-
* @param string $string The input string.
26+
* @param mixed $string The input string.
2727
*
2828
*/
29-
public static function isJsonString(string $string): bool
29+
public static function isJsonString(mixed $string): bool
3030
{
3131
return is_string($string)
3232
&& is_array(json_decode($string, true)) //TODO: improve performance
@@ -52,7 +52,7 @@ public static function decode(mixed $json, bool $assoc = true, int $depth = 512,
5252
return $json;
5353
}
5454

55-
$result = json_decode($json, $assoc, $depth, $options);
55+
$result = json_decode($json, $assoc, max(1, $depth), $options);
5656

5757
if (json_last_error() !== JSON_ERROR_NONE) {
5858
throw new InvalidArgumentException('JsonException : ' . json_last_error_msg());
@@ -76,7 +76,7 @@ public static function decode(mixed $json, bool $assoc = true, int $depth = 512,
7676
*/
7777
public static function encode(mixed $value, int $options = 0, int $depth = 512): bool|string
7878
{
79-
$result = json_encode($value, $options, $depth);
79+
$result = json_encode($value, $options, max(1, $depth));
8080

8181
if (json_last_error() !== JSON_ERROR_NONE) {
8282
throw new InvalidArgumentException('JsonException : ' . json_last_error_msg());

0 commit comments

Comments
 (0)