Skip to content

Commit

Permalink
Fix and complete PHP7.1 type declarations (#1314)
Browse files Browse the repository at this point in the history
Fix wrong type in `qtranxf_adjust_config_files`.
Complete missing trivial types.
Improve robustness of `rest_request_after_callbacks` with type check.
  • Loading branch information
herrvigg committed Apr 8, 2023
1 parent f676269 commit 05239de
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/admin/activation_hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ function qtranxf_find_plugin_config_file( string $plugin ) {
return false;
}

function qtranxf_adjust_config_files( ?array $file_to_add, ?array $file_to_del ): void {
function qtranxf_adjust_config_files( ?string $file_to_add, ?string $file_to_del ): void {
$config_files = qtranxf_get_option_config_files();
if ( $file_to_add ) {
if ( in_array( $file_to_add, $config_files ) ) {
Expand Down
6 changes: 3 additions & 3 deletions src/admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function qtranxf_collect_translations( array &$qfields, &$request, string $edit_
/**
* @since 3.4.6.5
*/
function qtranxf_decode_json_name_value( $value ) {
function qtranxf_decode_json_name_value( $value ): ?array {
if ( strpos( $value, 'qtranslate-fields' ) === false ) {
return null;
}
Expand Down Expand Up @@ -132,7 +132,7 @@ function qtranxf_load_admin_page_config() {
* @return bool true when the current page is the configuration page of QT-XT.
* @since 3.4.7
*/
function qtranxf_admin_is_config_page() {
function qtranxf_admin_is_config_page(): bool {
global $q_config, $pagenow;

return ( $pagenow == 'options-general.php' )
Expand Down Expand Up @@ -529,7 +529,7 @@ function qtranxf_add_admin_highlight_css() {
*
* @return string
*/
function qtranxf_get_admin_highlight_css( $highlight_mode ) {
function qtranxf_get_admin_highlight_css( int $highlight_mode ): string {
$css = 'input.qtranxs-translatable, textarea.qtranxs-translatable, div.qtranxs-translatable, span.qtranxs-translatable {' . PHP_EOL;
switch ( $highlight_mode ) {
case QTX_HIGHLIGHT_MODE_BORDER_LEFT:
Expand Down
8 changes: 4 additions & 4 deletions src/admin/admin_utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ function qtranxf_admin_the_title( string $title ): string {

if ( ! function_exists( 'qtranxf_trim_words' ) ) {
// TODO clarify duplicate function name, defined in frontend!
function qtranxf_trim_words( string $text, int $num_words, string $more, string $original_text ) {
function qtranxf_trim_words( string $text, int $num_words, string $more, string $original_text ): string {
$blocks = qtranxf_get_language_blocks( $original_text );
if ( count( $blocks ) <= 1 ) {
return $text;
Expand Down Expand Up @@ -390,7 +390,7 @@ function qtranxf_add_meta_box_LSB( $post_type, $post = null ) {
* @return true if post type is listed in option 'Post Types'.
* @since 3.3
*/
function qtranxf_post_type_optional( $post_type ) {
function qtranxf_post_type_optional( string $post_type ): bool {
return ! in_array( $post_type, [ 'revision', 'nav_menu_item' ] );
}

Expand All @@ -404,7 +404,7 @@ function qtranxf_post_type_optional( $post_type ) {
* @since 3.4.5
* check the WP Nonce - OK if POST is empty
*/
function qtranxf_verify_nonce( $nonce_name, $nonce_field = '_wpnonce' ) {
function qtranxf_verify_nonce( string $nonce_name, string $nonce_field = '_wpnonce' ): bool {
return empty( $_POST ) || check_admin_referer( $nonce_name, $nonce_field );
}

Expand Down Expand Up @@ -479,7 +479,7 @@ function qtranxf_decode_name_value_pair( &$a, $name, $value ) {
* TODO this looks unnecessary, it might be possible to use json_decode directly with right options
* @since 3.4.6.5
*/
function qtranxf_decode_name_value( $name_values ) {
function qtranxf_decode_name_value( $name_values ): array {
$decoded = array();
foreach ( $name_values as $name_value ) {
qtranxf_decode_name_value_pair( $decoded, $name_value->name, wp_slash( $name_value->value ) );
Expand Down
7 changes: 4 additions & 3 deletions src/admin/block_editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ public function rest_request_before_callbacks( $response, array $handler, WP_RES
*
* @return mixed
*/
public function rest_request_after_callbacks( $response, $handler, $request ) {
if ( $request->get_param( 'context' ) !== 'edit' || $request->get_method() !== 'PUT' && $request->get_method() !== 'POST' ) {
public function rest_request_after_callbacks( $response, array $handler, WP_REST_Request $request ) {
if ( ! $response instanceof WP_HTTP_Response // This includes WP_REST_Response that derives from it.
|| $request->get_param( 'context' ) !== 'edit' || ( $request->get_method() !== 'PUT' && $request->get_method() !== 'POST' ) ) {
return $response;
}

Expand Down Expand Up @@ -194,7 +195,7 @@ public function enqueue_block_editor_assets(): void {
* @param WP_HTTP_Response|WP_REST_Response $response
* @param string $editor_lang
*
* @return mixed
* @return WP_HTTP_Response|WP_REST_Response
*/
private function select_raw_response_language( $response, string $editor_lang ) {
$response_data = $response->get_data();
Expand Down
4 changes: 2 additions & 2 deletions src/compatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
define( 'QTRANS_INIT', true );
}
if ( ! function_exists( 'qtrans_convertURL' ) ) {
function qtrans_convertURL( $url = '', $lang = '', $forceadmin = false, $showDefaultLanguage = false ) {
function qtrans_convertURL( $url = '', $lang = '', $forceadmin = false, $showDefaultLanguage = false ): string {
return qtranxf_convertURL( $url, $lang, $forceadmin, $showDefaultLanguage );
}
}
Expand All @@ -21,7 +21,7 @@ function qtrans_generateLanguageSelectCode( $style = '', $id = '' ) {
* Some 3rd-party plugins (for example "Google XML Sitemaps v3 for qTranslate") use this function and expect an array in return.
*/
if ( ! function_exists( 'qtrans_getAvailableLanguages' ) ) {
function qtrans_getAvailableLanguages( $text ) {
function qtrans_getAvailableLanguages( $text ): array {
$langs = qtranxf_getAvailableLanguages( $text );
if ( is_array( $langs ) ) {
return $langs;
Expand Down
6 changes: 3 additions & 3 deletions src/frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ function qtranxf_pre_get_posts( $query ) {//WP_Query
/**
* since 3.1-b3 new query to pass empty content and content without closing tags (sliders, galleries and other special kind of posts that never get translated)
*/
function qtranxf_where_clause_translated_posts( $lang, $table_posts ) {
function qtranxf_where_clause_translated_posts( $lang, $table_posts ): string {
$post_content = $table_posts . '.post_content';

return "($post_content='' OR $post_content LIKE '%![:$lang!]%' ESCAPE '!' OR $post_content LIKE '%<!--:$lang-->%' OR ($post_content NOT LIKE '%![:!]%' ESCAPE '!' AND $post_content NOT LIKE '%<!--:-->%'))";
Expand Down Expand Up @@ -563,7 +563,7 @@ function qtranxf_excludePages( $pages ) {
* $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms );
*
*/
function qtranxf_excludeUntranslatedAdjacentPosts( $where ) {
function qtranxf_excludeUntranslatedAdjacentPosts( $where ): string {
$lang = qtranxf_getLanguage();
$where .= ' AND ' . qtranxf_where_clause_translated_posts( $lang, 'p' );

Expand Down Expand Up @@ -636,7 +636,7 @@ function qtranxf_get_attachment_image_attributes( $attr, $attachment = null, $si
return $attr;
}

function qtranxf_home_url( $url, $path, $orig_scheme, $blog_id ) {
function qtranxf_home_url( $url, $path, $orig_scheme, $blog_id ): string {
global $q_config;
$lang = $q_config['language'];
$url = qtranxf_get_url_for_language( $url, $lang, ! $q_config['hide_default_language'] || $lang != $q_config['default_language'] );
Expand Down
6 changes: 3 additions & 3 deletions src/language_blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
/**
* Check if a string contains at least one language token
*
* @param string $str
* @param string|null $str
*
* @return bool
*/
function qtranxf_isMultilingual( $str ) {
function qtranxf_isMultilingual( ?string $str ): bool {
$lang_code = QTX_LANG_CODE_FORMAT;
return ! is_null( $str ) && preg_match( "/<!--:$lang_code-->|\[:$lang_code]|{:$lang_code}/im", $str );
}
Expand All @@ -21,7 +21,7 @@ function qtranxf_isMultilingual( $str ) {
* @return string[] array of string tokens, including the ML tags.
* @since 3.3.6 swirly bracket encoding added
*/
function qtranxf_get_language_blocks( $text ) {
function qtranxf_get_language_blocks( $text ): array {
$lang_code = QTX_LANG_CODE_FORMAT;
$split_regex = "#(<!--:$lang_code-->|<!--:-->|\[:$lang_code\]|\[:\]|\{:$lang_code\}|\{:\})#ism";

Expand Down
2 changes: 1 addition & 1 deletion src/modules/module_loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class QTX_Module_Loader {
*
* @param string $module_id
*
* @bool true if module active.
* @return bool true if module active.
*/
public static function is_module_active( string $module_id ): bool {
$modules_state = get_option( QTX_OPTIONS_MODULES_STATE, array() );
Expand Down
2 changes: 1 addition & 1 deletion src/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @since 3.4.6.9
*
*/
function qtranxf_term_set_i18n_config( ?WP_Term $term ) {
function qtranxf_term_set_i18n_config( WP_Term $term ) {
$term->i18n_config = array();
if ( isset( $term->name ) ) {
global $q_config;
Expand Down
42 changes: 21 additions & 21 deletions src/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Default domain translation for strings already translated by WordPress.
* Use of this function prevents xgettext, poedit and other translating parsers from including the string that does not need translation.
*/
function qtranxf_translate_wp( $string ) {
function qtranxf_translate_wp( $string ): ?string {
return __( $string );
}

Expand All @@ -22,7 +22,7 @@ function qtranxf_translate_wp( $string ) {
* @return string path to plugin folder relative to WP_CONTENT_DIR.
* @since 3.4.5
*/
function qtranxf_dir_from_wp_content( $plugin ) {
function qtranxf_dir_from_wp_content( string $plugin ): string {
global $wp_plugin_paths;
$plugin_realpath = wp_normalize_path( dirname( realpath( $plugin ) ) );
$plugin_dir = $plugin_realpath;
Expand Down Expand Up @@ -64,7 +64,7 @@ function qtranxf_dir_from_wp_content( $plugin ) {
* @since 3.4
* @since 3.4.5 modified for multisite.
*/
function qtranxf_plugin_dirname_from_wp_content() {
function qtranxf_plugin_dirname_from_wp_content(): string {
static $dirname;
if ( ! $dirname ) {
$dirname = qtranxf_dir_from_wp_content( QTRANSLATE_FILE );
Expand All @@ -73,7 +73,7 @@ function qtranxf_plugin_dirname_from_wp_content() {
return $dirname;
}

function qtranxf_get_address_info( $url ) {
function qtranxf_get_address_info( string $url ): array {
$info = qtranxf_parseURL( $url );
if ( ! isset( $info['path'] ) ) {
$info['path'] = '';
Expand All @@ -82,7 +82,7 @@ function qtranxf_get_address_info( $url ) {
return $info;
}

function qtranxf_get_home_info() {
function qtranxf_get_home_info(): array {
static $home_info;
if ( ! $home_info ) {
$url = get_option( 'home' );
Expand All @@ -92,7 +92,7 @@ function qtranxf_get_home_info() {
return $home_info;
}

function qtranxf_get_site_info() {
function qtranxf_get_site_info(): array {
static $site_info;
if ( ! $site_info ) {
$url = get_option( 'siteurl' );
Expand All @@ -106,7 +106,7 @@ function qtranxf_get_site_info() {
* Simplified version of WP's add_query_arg
* @since 3.2.8
*/
function qtranxf_add_query_arg( &$query, $key_value ) {
function qtranxf_add_query_arg( string &$query, string $key_value ): void {
if ( empty( $query ) ) {
$query = $key_value;
} else {
Expand All @@ -118,7 +118,7 @@ function qtranxf_add_query_arg( &$query, $key_value ) {
* Simplified version of WP's remove_query_arg
* @since 3.2.8
*/
function qtranxf_del_query_arg( &$query, $key ) {
function qtranxf_del_query_arg( string &$query, string $key ): void {
while ( preg_match( '/(&|&amp;|&#038;|^)(' . $key . '=[^&]+)(&|&amp;|&#038;|$)/i', $query, $matches ) ) {
$pos = strpos( $query, $matches[2] );
$count = strlen( $matches[2] );
Expand All @@ -135,7 +135,7 @@ function qtranxf_del_query_arg( &$query, $key ) {
}


function qtranxf_insertDropDownElement( $language, $url, $id ) {
function qtranxf_insertDropDownElement( string $language, string $url, $id ): string {
global $q_config;
$html = "
var sb = document.getElementById('qtranxs_select_" . $id . "');
Expand All @@ -157,7 +157,7 @@ function qtranxf_insertDropDownElement( $language, $url, $id ) {
/**
* @since 3.2.8 - change code to improve performance
*/
function qtranxf_startsWith( $string, $needle ) {
function qtranxf_startsWith( string $string, string $needle ): bool {
$len = strlen( $needle );
if ( $len > strlen( $string ) ) {
return false;
Expand All @@ -174,7 +174,7 @@ function qtranxf_startsWith( $string, $needle ) {
/**
* @since 3.2.8
*/
function qtranxf_endsWith( $string, $needle ) {
function qtranxf_endsWith( string $string, string $needle ): bool {
$len = strlen( $needle );
$base = strlen( $string ) - $len;
if ( $base < 0 ) {
Expand All @@ -197,7 +197,7 @@ function qtranxf_endsWith( $string, $needle ) {
* @see parse_request in wp_includes/class-wp.php for the final processing of REQUEST_URI
* @see rest_api_register_rewrites in wp_includes/rest-api.php for the REST rewrite rules using query_var = rest_route
*/
function qtranxf_is_rest_request_expected() {
function qtranxf_is_rest_request_expected(): bool {
return stripos( $_SERVER['REQUEST_URI'], '/' . rest_get_url_prefix() . '/' ) !== false;
}

Expand All @@ -207,7 +207,7 @@ function qtranxf_is_rest_request_expected() {
* @return bool
* @see is_graphql_http_request in https://github.com/wp-graphql/wp-graphql/blob/develop/src/Router.php
*/
function qtranxf_is_graphql_request_expected() {
function qtranxf_is_graphql_request_expected(): bool {
return function_exists( 'is_graphql_http_request' ) && is_graphql_http_request();
}

Expand All @@ -217,7 +217,7 @@ function qtranxf_is_graphql_request_expected() {
*
* @return bool
*/
function qtranxf_can_redirect() {
function qtranxf_can_redirect(): bool {
return ! is_admin() && ! wp_doing_ajax() && ! ( defined( 'WP_CLI' ) && WP_CLI ) && ! wp_doing_cron() && empty( $_POST )
&& ( ! qtranxf_is_rest_request_expected() )
&& ( ! qtranxf_is_graphql_request_expected() )
Expand Down Expand Up @@ -247,7 +247,7 @@ function qtranxf_post_type() {
* Test $cfg['pages'] against $url_path and $url_query ($_SERVER['QUERY_STRING'])
* @since 3.4
*/
function qtranxf_match_page( $cfg, $url_path, $url_query, $d ) {
function qtranxf_match_page( array $cfg, string $url_path, string $url_query, string $d ): bool {
if ( ! isset( $cfg['pages'] ) ) {
return true;
}
Expand All @@ -266,7 +266,7 @@ function qtranxf_match_page( $cfg, $url_path, $url_query, $d ) {
/**
* @since 3.4
*/
function qtranxf_match_post_type( $cfg_post_type, $post_type ) {
function qtranxf_match_post_type( $cfg_post_type, $post_type ): ?bool {

if ( is_string( $cfg_post_type ) ) {
return preg_match( $cfg_post_type, $post_type ) === 1;
Expand All @@ -284,7 +284,7 @@ function qtranxf_match_post_type( $cfg_post_type, $post_type ) {
/**
* @since 3.3.2
*/
function qtranxf_merge_config( $cfg_all, $cfg ) {
function qtranxf_merge_config( array $cfg_all, array $cfg ): array {
foreach ( $cfg as $k => $value ) {
if ( is_array( $value ) && isset( $cfg_all[ $k ] ) ) {
$cfg_all[ $k ] = qtranxf_merge_config( $cfg_all[ $k ], $value );
Expand All @@ -302,7 +302,7 @@ function qtranxf_merge_config( $cfg_all, $cfg ) {
*
* @return array of active configurations, per post type
*/
function qtranxf_parse_page_config( $config, $url_path, $url_query ) {
function qtranxf_parse_page_config( array $config, string $url_path, string $url_query ): array {
global $q_config;

if ( isset( $q_config['i18n-log-dir'] ) ) {
Expand Down Expand Up @@ -465,7 +465,7 @@ function qtranxf_parse_page_config( $config, $url_path, $url_query ) {
return $page_configs;
}

function qtranxf_write_config_log( $config, $suffix = '', $url_path = null, $url_query = null, $post_type = null ) {
function qtranxf_write_config_log( array $config, string $suffix = '', ?string $url_path = null, ?string $url_query = null, ?string $post_type = null ): void {
global $q_config;
if ( empty( $q_config['i18n-log-dir'] ) ) {
return;
Expand Down Expand Up @@ -530,7 +530,7 @@ function qtranxf_write_config_log( $config, $suffix = '', $url_path = null, $url
/**
* @since 3.4
*/
function qtranxf_add_filters( $filters ) {
function qtranxf_add_filters( array $filters ): void {
if ( ! empty( $filters['text'] ) ) {
foreach ( $filters['text'] as $name => $prio ) {
if ( $prio === '' ) {
Expand Down Expand Up @@ -560,7 +560,7 @@ function qtranxf_add_filters( $filters ) {
/**
* @since 3.4.6.9
*/
function qtranxf_remove_filters( $filters ) {
function qtranxf_remove_filters( array $filters ): void {
if ( ! empty( $filters['text'] ) ) {
foreach ( $filters['text'] as $name => $prio ) {
if ( $prio === '' ) {
Expand Down

0 comments on commit 05239de

Please sign in to comment.