diff --git a/inc/woocommerce.php b/inc/woocommerce.php index 7b30c37d..617e3608 100644 --- a/inc/woocommerce.php +++ b/inc/woocommerce.php @@ -81,11 +81,33 @@ if ( ! function_exists( 'understrap_wc_form_field_args' ) ) { * * @see https://woocommerce.github.io/code-reference/namespaces/default.html#function_woocommerce_form_field * - * @param array $args Form field arguments. - * @param string $key Value of the fields name attribute. - * @param string|null $value Value of option. + * @return array Filtered form field arguments. * - * @return array Form field arguments. + * @phpstan-template T of array{ + * 'type': string, + * 'label': string, + * 'description': string, + * 'placeholder': string, + * 'maxlength': false|int, + * 'required': bool, + * 'autocomplete': false|string, + * 'id': string, + * 'class': list, + * 'label_class': list, + * 'input_class': list, + * 'return': bool, + * 'options': array, + * 'custom_attributes': array, + * 'validate': list, + * 'default': string, + * 'autofocus': ?(string|bool), + * 'priority': ?string, + * } + * @phpstan-param T $args + * @phpstan-return T | array{'class': non-empty-list} */ function understrap_wc_form_field_args( $args, $key, $value ) { $bootstrap4 = 'bootstrap4' === get_theme_mod( 'understrap_bootstrap_version', 'bootstrap4' ); @@ -101,23 +123,25 @@ if ( ! function_exists( 'understrap_wc_form_field_args' ) ) { case 'country': /* * WooCommerce will populate a element. + * with the country names. */ + // Add class to the form field's html element wrapper. $args['class'][] = 'single-country'; break; case 'state': /* * WooCommerce will populate a element. + * with the state names. */ // Add custom data attributes to the form input itself. $args['custom_attributes']['data-plugin'] = 'select2'; $args['custom_attributes']['data-allow-clear'] = 'true'; $args['custom_attributes']['aria-hidden'] = 'true'; + + // If state is text input. + $args['input_class'][] = 'form-control'; break; case 'checkbox': /* @@ -129,7 +153,7 @@ if ( ! function_exists( 'understrap_wc_form_field_args' ) ) { // Get Bootstrap version specific CSS class base. $base = $bootstrap4 ? 'custom-control' : 'form-check'; - if ( isset( $args['label'] ) ) { + if ( '' !== $args['label'] || $bootstrap4 ) { // Wrap the label in tag. $args['label'] = "{$args['label']}"; } @@ -144,10 +168,7 @@ if ( ! function_exists( 'understrap_wc_form_field_args' ) ) { $args['input_class'][] = $base . '-input'; break; case 'select': - /* - * Targets all elements - * of type country or of type state. - */ + // Targets all . // Add a class to the form input itself. $args['input_class'][] = $bootstrap4 ? 'form-control' : 'form-select'; @@ -165,7 +186,6 @@ if ( ! function_exists( 'understrap_wc_form_field_args' ) ) { break; default: $args['input_class'][] = 'form-control'; - break; } // End of switch ( $args ). return $args; } @@ -188,10 +208,31 @@ if ( ! function_exists( 'understrap_wc_form_field_radio' ) ) { * @param string $key The wrapper element's id attribute value. * @param array $args An array of field arguments. * @param string|null $value The field's value. - * @return string + * @return string The field's filtered HTML. + * + * @phpstan-template T of array{ + * 'type': string, + * 'label': string, + * 'description': string, + * 'placeholder': string, + * 'maxlength': false|int, + * 'required': bool, + * 'autocomplete': false|string, + * 'id': string, + * 'class': list, + * 'label_class': list, + * 'input_class': list, + * 'return': bool, + * 'options': array, + * 'custom_attributes': array, + * 'validate': list, + * 'default': string, + * 'autofocus': ?(string|bool), + * 'priority': ?string, + * } + * @phpstan-param T $args */ function understrap_wc_form_field_radio( $field, $key, $args, $value ) { - // Set up Bootstrap version specific variables. if ( 'bootstrap4' === get_theme_mod( 'understrap_bootstrap_version', 'bootstrap4' ) ) { $wrapper_classes = 'custom-control custom-radio'; @@ -202,7 +243,7 @@ if ( ! function_exists( 'understrap_wc_form_field_radio' ) ) { } // Remove the first occurance of the label class if neccessary. - if ( isset( $args['label'] ) && isset( $args['label_class'] ) ) { + if ( '' !== $args['label'] && ! empty( $args['label_class'] ) ) { $strpos = strpos( $field, $label_class ); if ( false !== $strpos ) { $field = substr_replace( $field, '', $strpos, strlen( $label_class ) ); @@ -218,7 +259,7 @@ if ( ! function_exists( 'understrap_wc_form_field_radio' ) ) { // Wrap each radio in a . $field = str_replace( '', '', $field ); - if ( isset( $args['label'] ) ) { + if ( '' !== $args['label'] ) { // Remove the closing span tag from the first ' ) + strlen( '' ); $field = substr_replace( $field, '', $strpos, strlen( '' ) ); @@ -256,8 +297,8 @@ if ( ! function_exists( 'understrap_quantity_input_classes' ) ) { /** * Add Bootstrap class to quantity input field. * - * @param array $classes Array of quantity input classes. - * @return array + * @param array $classes Array of quantity input classes. + * @return array */ function understrap_quantity_input_classes( $classes ) { $classes[] = 'form-control'; diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index fceee8d7..d15567a6 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -114,38 +114,3 @@ parameters: message: "#^Function understrap_widget_classes\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 path: inc/widgets.php - - - - message: "#^Cannot access an offset on mixed\\.$#" - count: 10 - path: inc/woocommerce.php - - - - message: "#^Cannot access offset 'aria\\-hidden' on mixed\\.$#" - count: 1 - path: inc/woocommerce.php - - - - message: "#^Cannot access offset 'data\\-allow\\-clear' on mixed\\.$#" - count: 2 - path: inc/woocommerce.php - - - - message: "#^Cannot access offset 'data\\-plugin' on mixed\\.$#" - count: 2 - path: inc/woocommerce.php - - - - message: "#^Function understrap_quantity_input_classes\\(\\) has parameter \\$classes with no value type specified in iterable type array\\.$#" - count: 1 - path: inc/woocommerce.php - - - - message: "#^Function understrap_quantity_input_classes\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: inc/woocommerce.php - - - - message: "#^Part \\$args\\['label'\\] \\(mixed\\) of encapsed string cannot be cast to string\\.$#" - count: 1 - path: inc/woocommerce.php