Skip to content

Commit a3a970b

Browse files
committed
Ensuring expected variable type
1 parent 205e138 commit a3a970b

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ parameters:
2020
level: 5
2121
inferPrivatePropertyTypeFromConstructor: true
2222
reportUnmatchedIgnoredErrors: false
23+
treatPhpDocTypesAsCertain: false
2324

2425
# Paths to be analyzed.
2526
paths:

src/Schema/Traits/Custom_Table_Query_Methods.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public static function get_total_items( array $args = [] ): int {
338338
* @since 3.0.0
339339
* @since 3.1.4 Enabled unfolding the value if is an array.
340340
*
341-
* @param array<mixed> $entries The entries to update.
341+
* @param array<array<string, null|int|string|float|bool|DateTimeInterface|string[]|int[]|float[]|DateTimeInterface[]>> $entries The entries to update.
342342
*
343343
* @return bool Whether the update was successful.
344344
*/
@@ -675,11 +675,11 @@ protected static function get_join_parts( string $join_table, string $join_condi
675675
* @since 3.1.1 Added the $order_by parameter.
676676
* @since 3.1.4 Enabled unfolding the value if is an array.
677677
*
678-
* @param string $column The column to get the models by.
679-
* @param mixed $value The value to get the models by.
680-
* @param string $operator The operator to use.
681-
* @param int $limit The limit of models to return.
682-
* @param string $order_by The order by clause to use.
678+
* @param string $column The column to get the models by.
679+
* @param null|int|string|float|bool|DateTimeInterface|string[]|int[]|float[]|DateTimeInterface[] $value The value to get the models by.
680+
* @param string $operator The operator to use.
681+
* @param int $limit The limit of models to return.
682+
* @param string $order_by The order by clause to use.
683683
*
684684
* @return mixed[] The models, or an empty array if no models are found.
685685
*
@@ -710,9 +710,9 @@ public static function get_all_by( string $column, $value, string $operator = '=
710710
* @since 3.1.4 Enabled unfolding the value if is an array.
711711
* @since 3.1.4 Added the $operator parameter.
712712
*
713-
* @param string $column The column to get the model by.
714-
* @param mixed $value The value to get the model by.
715-
* @param string $operator The operator to use.
713+
* @param string $column The column to get the model by.
714+
* @param null|int|string|float|bool|DateTimeInterface|string[]|int[]|float[]|DateTimeInterface[] $value The value to get the model by.
715+
* @param string $operator The operator to use.
716716
*
717717
* @return ?mixed The model, or `null` if no model is found.
718718
*
@@ -940,11 +940,21 @@ public static function cast_value_based_on_type( string $type, $value ) {
940940
*
941941
* @since 3.1.4
942942
*
943-
* @param mixed $value The value to ensure is an array.
943+
* @param null|string|int|float|bool|DateTimeInterface|string[]|int[]|float[]|DateTimeInterface[] $value The value to ensure is an array.
944944
*
945-
* @return array<mixed> The value as an array.
945+
* @return array<null|string|int|float|bool|DateTimeInterface> The value as an array.
946946
*/
947947
private static function ensure_array( $value ): array {
948+
if ( is_object( $value ) && ! $value instanceof DateTimeInterface ) {
949+
throw new InvalidArgumentException( 'Value should not be an object.' );
950+
}
951+
952+
if ( is_array( $value ) && $value ) {
953+
foreach ( $value as $v ) {
954+
self::ensure_array( $v );
955+
}
956+
}
957+
948958
return is_array( $value ) ? $value : [ $value ];
949959
}
950960
}

0 commit comments

Comments
 (0)