Skip to content

Commit

Permalink
Fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
const-cloudinary committed Jan 14, 2025
1 parent dc12098 commit a4ebe1b
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 95 deletions.
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ parameters:
- src
ignoreErrors:
- '#Unsafe usage of new static\(\)#'
#- '#Method .+ has parameter \$[a-zA-Z0-9_]+ with no value type specified in iterable type array#'
#- '#Method .+ return type has no value type specified in iterable type array#'
4 changes: 2 additions & 2 deletions src/Transformation/CommonTransformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ public function addAction(mixed $action): static
*
* Appended transformation is nested.
*
* @param CommonTransformation|string|null $transformation The transformation to add.
* @param mixed $transformation The transformation to add.
*
*/
public function addTransformation(CommonTransformation|string|null $transformation): static
public function addTransformation(mixed $transformation): static
{
$this->actions[] = ClassUtils::forceInstance($transformation, CommonTransformation::class);

Expand Down
8 changes: 4 additions & 4 deletions src/Transformation/CommonTransformationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ interface CommonTransformationInterface extends ComponentInterface
*
* (Formerly known as fetch format).
*
* @param string|Format $format The format in which to deliver the asset.
* @param Format|string|null $format The format in which to deliver the asset.
*
*
* @see Format
*/
public function format(Format|string $format): static;
public function format(Format|string|null $format): static;

/**
* Controls compression quality.
*
* Reducing the quality is a trade-off between visual quality and file size.
*
* @param Quality|int|float|string $quality The quality value. (Range 1 to 100)
* @param Quality|int|float|string|null $quality The quality value. (Range 1 to 100)
*
*/
public function quality(Quality|int|float|string $quality): static;
public function quality(Quality|int|float|string|null $quality): static;

/**
* Applies a filter or an effect on an asset.
Expand Down
4 changes: 2 additions & 2 deletions src/Transformation/Delivery/TransformationDeliveryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function delivery($delivery): static
*
*
*/
public function format(Format|string $format): static
public function format(Format|string|null $format): static
{
return $this->addAction(ClassUtils::verifyInstance($format, Format::class));
}
Expand All @@ -53,7 +53,7 @@ public function format(Format|string $format): static
*
*
*/
public function quality(Quality|int|float|string $quality): static
public function quality(Quality|int|float|string|null $quality): static
{
return $this->addAction(ClassUtils::verifyInstance($quality, Quality::class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Cloudinary\Transformation;

use Cloudinary\ClassUtils;
use Cloudinary\Transformation\Qualifier\Dimensions\Dpr;
use Cloudinary\Transformation\Qualifier\Dimensions\Height;
use Cloudinary\Transformation\Qualifier\Dimensions\Width;
Expand Down Expand Up @@ -70,8 +71,8 @@ public static function aspectRatio(...$aspectRatio): AspectRatio
*
* @see Dpr
*/
public static function dpr(float $dpr): Dpr
public static function dpr(int|float|string|Dpr $dpr): Dpr
{
return new Dpr($dpr);
return ClassUtils::verifyInstance($dpr, Dpr::class);
}
}
2 changes: 1 addition & 1 deletion src/Transformation/Qualifier/QualifiersAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class QualifiersAction extends BaseAction
'video_codec' => null,
];

protected const QUALIFIERS = self::COMPLEX_QUALIFIERS + self::SIMPLE_QUALIFIERS;
public const QUALIFIERS = self::COMPLEX_QUALIFIERS + self::SIMPLE_QUALIFIERS;

/**
* Add qualifiers to the action.
Expand Down
2 changes: 1 addition & 1 deletion src/Transformation/Video/Transcode/Fps.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* href=https://cloudinary.com/documentation/video_transformation_reference#video_settings
* target="_blank">Video settings</a>
*
* @property MinMaxRange value
* @property MinMaxRange $value
*
* @api
*/
Expand Down
113 changes: 61 additions & 52 deletions src/Utils/ArrayUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ class ArrayUtils
/**
* Applies the callback to the elements of the given associative array
*
* @param callable $callback The callback function
* @param array $array An array to run through the callback function.
* @param callable $callback The callback function
* @param ArrayObject<int|string, mixed>|array $array An array to run through the callback function.
*
* @return array Resulting array
*/
public static function mapAssoc(callable $callback, array $array): array
public static function mapAssoc(callable $callback, ArrayObject|array $array): array
{
$r = [];
foreach ($array as $key => $value) {
Expand Down Expand Up @@ -128,40 +128,46 @@ static function ($k, $v) use ($inner, $innerIsSafe) {
* @see implode
*/
public static function implodeFiltered(
string $glue,
array $pieces,
string $filterCallback = __NAMESPACE__ . '\ArrayUtils::safeFilterFunc',
array|string $glue,
array|null $pieces,
callable|null $filterCallback = null,
int $flag = 0
): string {
$filterCallback ??= fn($value) => ArrayUtils::safeFilterFunc($value);

return self::safeImplode($glue, self::safeFilter($pieces, $filterCallback, $flag));
}

/**
* Safe version of implode.
*
* In addition fixes serialisation of float values.
*
* In addition, fixes serialisation of float values.
*
*/
public static function safeImplode($glue, array $pieces): string
public static function safeImplode(array|string $glue, array|null $pieces): string
{
array_walk(
$pieces,
static function (&$value) {
$value = TransformationUtils::floatToString($value);
}
);
if (! is_null($pieces)) {
array_walk(
$pieces,
static function (&$value) {
$value = TransformationUtils::floatToString($value);
}
);
}

return implode($glue, $pieces);
}

/**
* Implodes array values with escaping the glue character.
*
*
*/
public static function escapedImplode(string $glue, array $pieces): string
public static function escapedImplode(string|array $glue, array|null $pieces): string
{
if (is_null($pieces)) {
return '';
}

return implode(
$glue,
array_map(
Expand Down Expand Up @@ -206,18 +212,17 @@ protected static function safeFilterFunc(mixed $value): bool|int
*
* Uses "strlen" filter function by default, which treats non-null values (e.g. 0, false, etc) as non-empty.
*
* @param callback|string $callback
*
*
* @see array_filter
* @see strlen
*/
public static function safeFilter(
array $input,
callable|string $callback = __NAMESPACE__ . '\ArrayUtils::safeFilterFunc',
?array $input,
callable|null $callback = null,
int $flag = 0
): array {
return array_filter($input, $callback, $flag);
): ?array {
$callback ??= fn($value) => ArrayUtils::safeFilterFunc($value);

return is_null($input) ? $input : array_filter($input, $callback, $flag);
}

/**
Expand All @@ -226,7 +231,7 @@ public static function safeFilter(
* In case some of the missing/extra keys, the keys are sorted using alphabetic order. Ordered keys come first.
*
* @param ?array $array The associate array to order.
* @param array $orderArray The desired order of the keys.
* @param array $orderArray The desired order of the keys.
*
*/
public static function sortByArray(?array $array, array $orderArray = []): ?array
Expand Down Expand Up @@ -256,15 +261,16 @@ public static function implodeUrl(array $urlParts): string
/**
* Commonly used util for building transformation URL
*
* @param array $qualifiers
*
* @return string The resulting string
*
* @internal
*/
public static function implodeActionQualifiers(...$qualifiers): string
public static function implodeActionQualifiers(mixed ...$qualifiers): string
{
$serializedQualifiers = array_map('strval', $qualifiers);
$serializedQualifiers = [];
foreach ($qualifiers as $item) {
$serializedQualifiers[] = (string)$item;
}

sort($serializedQualifiers);

Expand All @@ -288,13 +294,13 @@ public static function implodeQualifierValues(...$qualifierValues): string
/**
* Gets a key from an array if exists, otherwise returns default.
*
* @param ArrayObject|array $array The data array.
* @param int|array|string $key The key. Can be a simple key(string|int), an index array that allows accessing
* @param mixed $array The data array.
* @param int|array|string $key The key. Can be a simple key(string|int), an index array that allows accessing
* nested values
* @param mixed|null $default The default value for the case when the key is not found.
* @param mixed|null $default The default value for the case when the key is not found.
*
*/
public static function get(ArrayObject|array $array, int|array|string $key, mixed $default = null): mixed
public static function get(mixed $array, int|array|string $key, mixed $default = null): mixed
{
if (is_array($key)) {
$currLevel = &$array;
Expand Down Expand Up @@ -323,13 +329,12 @@ public static function get(ArrayObject|array $array, int|array|string $key, mixe
/**
* Pops a key from an array if exists, otherwise returns default
*
* @param array $array Data array
* @param int|array|string $key key can be a simple key(string|int) or an array that allows accessing nested
* values
* @param mixed|null $default Default value for the case when key is not found
* @param array $array Data array
* @param int|string $key A simple key(string|int)
* @param mixed|null $default Default value for the case when key is not found
*
*/
public static function pop(array &$array, int|array|string $key, mixed $default = null): mixed
public static function pop(array &$array, int|string $key, mixed $default = null): mixed
{
$val = self::get($array, $key, $default);
unset($array[$key]);
Expand All @@ -341,7 +346,7 @@ public static function pop(array &$array, int|array|string $key, mixed $default
* Returns a subset of associative array whitelisted by an array of keys
*
* @param ?array $array Source array (associative or not)
* @param array $keys Simple array of keys to keep
* @param array $keys Simple array of keys to keep
*
* @return ?array Resulting array
*/
Expand All @@ -363,7 +368,7 @@ public static function whitelist(?array $array, array $keys): ?array
* Returns a subset of associative array with excluded keys specified by an array of keys
*
* @param ?array $array Source array (associative or not)
* @param array $blacklistedKeys Simple array of keys to be excluded
* @param array $blacklistedKeys Simple array of keys to be excluded
*
* @return ?array Resulting array
*/
Expand Down Expand Up @@ -500,10 +505,11 @@ public static function addNonEmpty(array &$arr, int|string|null $key, mixed $val
/**
* Adds key-value pair to the associative array where value is taken from source array using the same key.
*
* @param array & $resultingArr The target array.
* @param mixed $key The key to add
* @param array $sourceArray The source array
* @param mixed|null $defaultValue Fallback value, in case the key does not exist or is empty
* @param array & $resultingArr The target array.
* @param mixed $key The key to add
* @param array|ArrayObject<int|string, mixed> $sourceArray The source array
* @param mixed|null $defaultValue Fallback value, in case the key does not exist or is
* empty
*
* @return array The resulting array
*
Expand All @@ -512,7 +518,7 @@ public static function addNonEmpty(array &$arr, int|string|null $key, mixed $val
public static function addNonEmptyFromOther(
array &$resultingArr,
mixed $key,
array $sourceArray,
array|ArrayObject $sourceArray,
mixed $defaultValue = null
): array {
return self::addNonEmpty($resultingArr, $key, self::get($sourceArray, $key, $defaultValue));
Expand All @@ -533,7 +539,12 @@ public static function mergeNonEmpty(...$arrays): array
$result = [];

foreach ($arrays as $array) {
$result = array_merge($result, self::safeFilter($array));
if (! empty($array)) {
$filtered = self::safeFilter($array);
if ($filtered) {
$result = array_merge($result, $filtered);
}
}
}

return $result;
Expand Down Expand Up @@ -621,11 +632,11 @@ public static function convertToAssoc(array $array): array
/**
* Helper function for making a recursive array copy while cloning objects on the way.
*
* @param array $array Source array
* @param mixed $array Source array
*
* @return array Recursive copy of the source array
* @return mixed Recursive copy of the source array
*/
public static function deepCopy(array $array): array
public static function deepCopy(mixed $array): mixed
{
if (! is_array($array)) {
return $array;
Expand All @@ -647,10 +658,8 @@ public static function deepCopy(array $array): array

/**
* Indicates whether all parameters are non-empty
*
*
*/
public static function all(...$params): bool
public static function all(array ...$params): bool
{
foreach ($params as $param) {
if (empty($param)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/ClassUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public static function getBaseName(string $className): string
/**
* Gets class constants.
*
* @param object|string $instance The instance object.
* @param array $exclusions The list of constants to exclude.
* @param object|class-string $instance The instance object.
* @param array $exclusions The list of constants to exclude.
*
* @return array of class constants
*/
Expand Down
8 changes: 4 additions & 4 deletions src/Utils/JsonUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class JsonUtils
/**
* Determines whether the input is a valid JSON string.
*
* @param string $string The input string.
* @param mixed $string The input string.
*
*/
public static function isJsonString(string $string): bool
public static function isJsonString(mixed $string): bool
{
return is_string($string)
&& is_array(json_decode($string, true)) //TODO: improve performance
Expand All @@ -52,7 +52,7 @@ public static function decode(mixed $json, bool $assoc = true, int $depth = 512,
return $json;
}

$result = json_decode($json, $assoc, $depth, $options);
$result = json_decode($json, $assoc, max(1, $depth), $options);

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

if (json_last_error() !== JSON_ERROR_NONE) {
throw new InvalidArgumentException('JsonException : ' . json_last_error_msg());
Expand Down
Loading

0 comments on commit a4ebe1b

Please sign in to comment.