@@ -21,13 +21,13 @@ |
||
| 21 | 21 | $plugin_hooks_files = glob( $include_path . 'class-gravityview-plugin-hooks-*.php' ); |
| 22 | 22 | |
| 23 | 23 | // Load all plugin files automatically |
| 24 | -foreach ( (array) $plugin_hooks_files as $plugin_hooks_file ) { |
|
| 24 | +foreach ( (array)$plugin_hooks_files as $plugin_hooks_file ) { |
|
| 25 | 25 | include $plugin_hooks_file; |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | $theme_hooks_files = glob( $include_path . 'class-gravityview-theme-hooks-*.php' ); |
| 29 | 29 | |
| 30 | 30 | // Load all theme files automatically |
| 31 | -foreach ( (array) $theme_hooks_files as $theme_hooks_file ) { |
|
| 31 | +foreach ( (array)$theme_hooks_files as $theme_hooks_file ) { |
|
| 32 | 32 | include $theme_hooks_file; |
| 33 | 33 | } |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | |
| 65 | 65 | add_filter( 'icl_ls_languages', array( $this, 'wpml_ls_filter' ) ); |
| 66 | 66 | |
| 67 | - add_filter( 'gravityview_directory_link', array( $this, 'filter_gravityview_back_link') ); |
|
| 67 | + add_filter( 'gravityview_directory_link', array( $this, 'filter_gravityview_back_link' ) ); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /** |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | function filter_gravityview_back_link( $link ) { |
| 85 | 85 | global $wpml_url_filters; |
| 86 | 86 | |
| 87 | - if( $wpml_url_filters ) { |
|
| 87 | + if ( $wpml_url_filters ) { |
|
| 88 | 88 | $link = $wpml_url_filters->permalink_filter( $link, GravityView_frontend::getInstance()->getPostId() ); |
| 89 | 89 | } |
| 90 | 90 | |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | private function remove_url_hooks() { |
| 102 | 102 | global $wpml_url_filters; |
| 103 | 103 | |
| 104 | - if( ! $wpml_url_filters ) { |
|
| 104 | + if ( ! $wpml_url_filters ) { |
|
| 105 | 105 | return; |
| 106 | 106 | } |
| 107 | 107 | |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | private function add_url_hooks() { |
| 130 | 130 | global $wpml_url_filters; |
| 131 | 131 | |
| 132 | - if( ! $wpml_url_filters ) { |
|
| 132 | + if ( ! $wpml_url_filters ) { |
|
| 133 | 133 | return; |
| 134 | 134 | } |
| 135 | 135 | |
@@ -226,7 +226,7 @@ discard block |
||
| 226 | 226 | break; |
| 227 | 227 | } |
| 228 | 228 | |
| 229 | - $languages[ $lang_code ]['url'] = $entry_link; |
|
| 229 | + $languages[ $lang_code ][ 'url' ] = $entry_link; |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | $this->add_url_hooks(); |
@@ -140,7 +140,7 @@ |
||
| 140 | 140 | * |
| 141 | 141 | * @param array $items Styles to exclude from no-conflict |
| 142 | 142 | * |
| 143 | - * @return array |
|
| 143 | + * @return string[] |
|
| 144 | 144 | */ |
| 145 | 145 | public function register_no_conflict( $items ) { |
| 146 | 146 | |
@@ -421,7 +421,7 @@ |
||
| 421 | 421 | </header> |
| 422 | 422 | <div class="gform-settings-panel__content" style="padding: 0 1rem 1.25rem"> |
| 423 | 423 | |
| 424 | -HTML; |
|
| 424 | +html; |
|
| 425 | 425 | } else { |
| 426 | 426 | echo '<div class="gv-uninstall-form-wrapper" style="font-size: 110%; padding: 15px 0;">'; |
| 427 | 427 | } |
@@ -118,7 +118,7 @@ discard block |
||
| 118 | 118 | $setting_tabs = parent::get_app_settings_tabs(); |
| 119 | 119 | |
| 120 | 120 | foreach ( $setting_tabs as &$tab ) { |
| 121 | - if ( 'uninstall' !== $tab['name'] ) { |
|
| 121 | + if ( 'uninstall' !== $tab[ 'name' ] ) { |
|
| 122 | 122 | continue; |
| 123 | 123 | } |
| 124 | 124 | |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | // Add trash can icon to resemble the look and feel of the GF Settings page |
| 132 | - $tab['icon'] = 'dashicons-trash'; |
|
| 132 | + $tab[ 'icon' ] = 'dashicons-trash'; |
|
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | return array_filter( $setting_tabs ); |
@@ -144,8 +144,8 @@ discard block |
||
| 144 | 144 | */ |
| 145 | 145 | public function register_no_conflict( $items ) { |
| 146 | 146 | |
| 147 | - $items[] = 'gform_settings'; |
|
| 148 | - $items[] = 'gv-admin-edd-license'; |
|
| 147 | + $items[ ] = 'gform_settings'; |
|
| 148 | + $items[ ] = 'gv-admin-edd-license'; |
|
| 149 | 149 | |
| 150 | 150 | return $items; |
| 151 | 151 | } |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | |
| 197 | 197 | $href = add_query_arg( array( 'post_type' => 'gravityview', 'page' => 'gravityview_settings', 'view' => 'uninstall' ), admin_url( 'edit.php' ) ); |
| 198 | 198 | |
| 199 | - $uninstall_button = '<a href="' . esc_url( $href ) . '" class="' . gravityview_sanitize_html_class( $html_class ). '">' . esc_html__( 'Uninstall GravityView', 'gravityview' ) . '</a>'; |
|
| 199 | + $uninstall_button = '<a href="' . esc_url( $href ) . '" class="' . gravityview_sanitize_html_class( $html_class ) . '">' . esc_html__( 'Uninstall GravityView', 'gravityview' ) . '</a>'; |
|
| 200 | 200 | |
| 201 | 201 | $html .= $uninstall_button; |
| 202 | 202 | |
@@ -215,10 +215,10 @@ discard block |
||
| 215 | 215 | */ |
| 216 | 216 | public function settings_save( $field, $echo = true ) { |
| 217 | 217 | |
| 218 | - $field['type'] = 'submit'; |
|
| 219 | - $field['name'] = 'gform-settings-save'; |
|
| 220 | - $field['class'] = 'button button-primary primary button-hero'; |
|
| 221 | - $field['value'] = Utils::get( $field, 'value', __( 'Update Settings', 'gravityview' ) ); |
|
| 218 | + $field[ 'type' ] = 'submit'; |
|
| 219 | + $field[ 'name' ] = 'gform-settings-save'; |
|
| 220 | + $field[ 'class' ] = 'button button-primary primary button-hero'; |
|
| 221 | + $field[ 'value' ] = Utils::get( $field, 'value', __( 'Update Settings', 'gravityview' ) ); |
|
| 222 | 222 | |
| 223 | 223 | $html = $this->as_html( $field, false ); |
| 224 | 224 | |
@@ -240,8 +240,8 @@ discard block |
||
| 240 | 240 | */ |
| 241 | 241 | public function modify_app_settings_menu_title( $setting_tabs ) { |
| 242 | 242 | |
| 243 | - $setting_tabs[0]['label'] = __( 'GravityView Settings', 'gravityview' ); |
|
| 244 | - $setting_tabs[0]['icon'] = 'dashicons-admin-settings'; |
|
| 243 | + $setting_tabs[ 0 ][ 'label' ] = __( 'GravityView Settings', 'gravityview' ); |
|
| 244 | + $setting_tabs[ 0 ][ 'icon' ] = 'dashicons-admin-settings'; |
|
| 245 | 245 | |
| 246 | 246 | return $setting_tabs; |
| 247 | 247 | } |
@@ -489,7 +489,7 @@ discard block |
||
| 489 | 489 | $i = 0; |
| 490 | 490 | while ( $i < 11 ) { |
| 491 | 491 | echo '<li class="inline number-scale"><label><input name="likely_to_refer" id="likely_to_refer_' . $i . '" value="' . $i . '" type="radio"> ' . $i . '</label></li>'; |
| 492 | - $i ++; |
|
| 492 | + $i++; |
|
| 493 | 493 | } |
| 494 | 494 | ?> |
| 495 | 495 | </ul> |
@@ -693,14 +693,14 @@ discard block |
||
| 693 | 693 | */ |
| 694 | 694 | public function as_html( $field, $echo = true ) { |
| 695 | 695 | |
| 696 | - $field['type'] = ( isset( $field['type'] ) && in_array( $field['type'], array( 'submit', 'reset', 'button' ) ) ) ? $field['type'] : 'submit'; |
|
| 696 | + $field[ 'type' ] = ( isset( $field[ 'type' ] ) && in_array( $field[ 'type' ], array( 'submit', 'reset', 'button' ) ) ) ? $field[ 'type' ] : 'submit'; |
|
| 697 | 697 | |
| 698 | 698 | $attributes = $this->get_field_attributes( $field ); |
| 699 | 699 | $default_value = Utils::get( $field, 'value', Utils::get( $field, 'default_value' ) ); |
| 700 | - $value = $this->get( $field['name'], $default_value ); |
|
| 700 | + $value = $this->get( $field[ 'name' ], $default_value ); |
|
| 701 | 701 | |
| 702 | - $attributes['class'] = isset( $attributes['class'] ) ? esc_attr( $attributes['class'] ) : 'button-primary primary gfbutton'; |
|
| 703 | - $name = ( $field['name'] === 'gform-settings-save' ) ? $field['name'] : '_gaddon_setting_' . $field['name']; |
|
| 702 | + $attributes[ 'class' ] = isset( $attributes[ 'class' ] ) ? esc_attr( $attributes[ 'class' ] ) : 'button-primary primary gfbutton'; |
|
| 703 | + $name = ( $field[ 'name' ] === 'gform-settings-save' ) ? $field[ 'name' ] : '_gaddon_setting_' . $field[ 'name' ]; |
|
| 704 | 704 | |
| 705 | 705 | if ( empty( $value ) ) { |
| 706 | 706 | $value = __( 'Update Settings', 'gravityview' ); |
@@ -709,7 +709,7 @@ discard block |
||
| 709 | 709 | $attributes = $this->get_field_attributes( $field ); |
| 710 | 710 | |
| 711 | 711 | $html = '<input |
| 712 | - type="' . $field['type'] . '" |
|
| 712 | + type="' . $field[ 'type' ] . '" |
|
| 713 | 713 | name="' . esc_attr( $name ) . '" |
| 714 | 714 | value="' . $value . '" ' . |
| 715 | 715 | implode( ' ', $attributes ) . |
@@ -743,7 +743,7 @@ discard block |
||
| 743 | 743 | */ |
| 744 | 744 | public function is_save_postback() { |
| 745 | 745 | |
| 746 | - return isset( $_POST['gform-settings-save'] ) && isset( $_POST['_gravityview_save_settings_nonce'] ); |
|
| 746 | + return isset( $_POST[ 'gform-settings-save' ] ) && isset( $_POST[ '_gravityview_save_settings_nonce' ] ); |
|
| 747 | 747 | } |
| 748 | 748 | |
| 749 | 749 | /** |
@@ -855,7 +855,7 @@ discard block |
||
| 855 | 855 | |
| 856 | 856 | $scripts = parent::scripts(); |
| 857 | 857 | |
| 858 | - $scripts[] = array( |
|
| 858 | + $scripts[ ] = array( |
|
| 859 | 859 | 'handle' => 'gform_tooltip_init', |
| 860 | 860 | 'enqueue' => array( |
| 861 | 861 | array( |
@@ -876,7 +876,7 @@ discard block |
||
| 876 | 876 | |
| 877 | 877 | $styles = parent::styles(); |
| 878 | 878 | |
| 879 | - $styles[] = array( |
|
| 879 | + $styles[ ] = array( |
|
| 880 | 880 | 'handle' => 'gravityview_settings', |
| 881 | 881 | 'src' => plugins_url( 'assets/css/admin-settings.css', GRAVITYVIEW_FILE ), |
| 882 | 882 | 'version' => Plugin::$version, |
@@ -967,7 +967,7 @@ discard block |
||
| 967 | 967 | 'name' => 'support-email', |
| 968 | 968 | 'type' => 'text', |
| 969 | 969 | 'validate' => 'email', |
| 970 | - 'default_value' => $default_settings['support-email'], |
|
| 970 | + 'default_value' => $default_settings[ 'support-email' ], |
|
| 971 | 971 | 'label' => __( 'Support Email', 'gravityview' ), |
| 972 | 972 | 'description' => __( 'In order to provide responses to your support requests, please provide your email address.', 'gravityview' ), |
| 973 | 973 | 'class' => 'code regular-text', |
@@ -979,7 +979,7 @@ discard block |
||
| 979 | 979 | 'name' => 'support_port', |
| 980 | 980 | 'type' => 'radio', |
| 981 | 981 | 'label' => __( 'Show Support Port?', 'gravityview' ), |
| 982 | - 'default_value' => $default_settings['support_port'], |
|
| 982 | + 'default_value' => $default_settings[ 'support_port' ], |
|
| 983 | 983 | 'horizontal' => 1, |
| 984 | 984 | 'choices' => array( |
| 985 | 985 | array( |
@@ -998,7 +998,7 @@ discard block |
||
| 998 | 998 | 'name' => 'no-conflict-mode', |
| 999 | 999 | 'type' => 'radio', |
| 1000 | 1000 | 'label' => __( 'No-Conflict Mode', 'gravityview' ), |
| 1001 | - 'default_value' => $default_settings['no-conflict-mode'], |
|
| 1001 | + 'default_value' => $default_settings[ 'no-conflict-mode' ], |
|
| 1002 | 1002 | 'horizontal' => 1, |
| 1003 | 1003 | 'choices' => array( |
| 1004 | 1004 | array( |
@@ -1020,7 +1020,7 @@ discard block |
||
| 1020 | 1020 | 'name' => 'rest_api', |
| 1021 | 1021 | 'type' => 'radio', |
| 1022 | 1022 | 'label' => __( 'REST API', 'gravityview' ), |
| 1023 | - 'default_value' => $default_settings['rest_api'], |
|
| 1023 | + 'default_value' => $default_settings[ 'rest_api' ], |
|
| 1024 | 1024 | 'horizontal' => 1, |
| 1025 | 1025 | 'choices' => array( |
| 1026 | 1026 | array( |
@@ -1039,7 +1039,7 @@ discard block |
||
| 1039 | 1039 | 'name' => 'powered_by', |
| 1040 | 1040 | 'type' => 'checkbox', |
| 1041 | 1041 | 'label' => __( 'Display "Powered By" Link', 'gravityview' ), |
| 1042 | - 'default_value' => $default_settings['powered_by'], |
|
| 1042 | + 'default_value' => $default_settings[ 'powered_by' ], |
|
| 1043 | 1043 | 'choices' => array( |
| 1044 | 1044 | array( |
| 1045 | 1045 | 'label' => esc_html__( 'Display a "Powered by GravityView" link', 'gravityview' ), |
@@ -1064,7 +1064,7 @@ discard block |
||
| 1064 | 1064 | 'name' => 'beta', |
| 1065 | 1065 | 'type' => 'checkbox', |
| 1066 | 1066 | 'label' => __( 'Become a Beta Tester', 'gravityview' ), |
| 1067 | - 'default_value' => $default_settings['beta'], |
|
| 1067 | + 'default_value' => $default_settings[ 'beta' ], |
|
| 1068 | 1068 | 'horizontal' => 1, |
| 1069 | 1069 | 'choices' => array( |
| 1070 | 1070 | array( |
@@ -1098,17 +1098,17 @@ discard block |
||
| 1098 | 1098 | * @since 1.7.4 |
| 1099 | 1099 | */ |
| 1100 | 1100 | foreach ( $fields as &$field ) { |
| 1101 | - $field['name'] = isset( $field['name'] ) ? $field['name'] : Utils::get( $field, 'id' ); |
|
| 1102 | - $field['label'] = isset( $field['label'] ) ? $field['label'] : Utils::get( $field, 'title' ); |
|
| 1103 | - $field['default_value'] = isset( $field['default_value'] ) ? $field['default_value'] : Utils::get( $field, 'default' ); |
|
| 1104 | - $field['description'] = isset( $field['description'] ) ? $field['description'] : Utils::get( $field, 'subtitle' ); |
|
| 1101 | + $field[ 'name' ] = isset( $field[ 'name' ] ) ? $field[ 'name' ] : Utils::get( $field, 'id' ); |
|
| 1102 | + $field[ 'label' ] = isset( $field[ 'label' ] ) ? $field[ 'label' ] : Utils::get( $field, 'title' ); |
|
| 1103 | + $field[ 'default_value' ] = isset( $field[ 'default_value' ] ) ? $field[ 'default_value' ] : Utils::get( $field, 'default' ); |
|
| 1104 | + $field[ 'description' ] = isset( $field[ 'description' ] ) ? $field[ 'description' ] : Utils::get( $field, 'subtitle' ); |
|
| 1105 | 1105 | |
| 1106 | 1106 | if ( $disabled_attribute ) { |
| 1107 | - $field['disabled'] = $disabled_attribute; |
|
| 1107 | + $field[ 'disabled' ] = $disabled_attribute; |
|
| 1108 | 1108 | } |
| 1109 | 1109 | |
| 1110 | - if ( empty( $field['disabled'] ) ) { |
|
| 1111 | - unset( $field['disabled'] ); |
|
| 1110 | + if ( empty( $field[ 'disabled' ] ) ) { |
|
| 1111 | + unset( $field[ 'disabled' ] ); |
|
| 1112 | 1112 | } |
| 1113 | 1113 | } |
| 1114 | 1114 | |
@@ -1120,23 +1120,23 @@ discard block |
||
| 1120 | 1120 | 'description' => __( 'Enter the license key that was sent to you on purchase. This enables plugin updates & support.', 'gravityview' ), |
| 1121 | 1121 | 'type' => 'edd_license', |
| 1122 | 1122 | 'data-pending-text' => __( 'Verifying license…', 'gravityview' ), |
| 1123 | - 'default_value' => $default_settings['license_key'], |
|
| 1123 | + 'default_value' => $default_settings[ 'license_key' ], |
|
| 1124 | 1124 | 'class' => ( '' == $this->get( 'license_key' ) ) ? 'activate code regular-text edd-license-key' : 'deactivate code regular-text edd-license-key', |
| 1125 | 1125 | ), |
| 1126 | 1126 | array( |
| 1127 | 1127 | 'name' => 'license_key_response', |
| 1128 | - 'default_value' => $default_settings['license_key_response'], |
|
| 1128 | + 'default_value' => $default_settings[ 'license_key_response' ], |
|
| 1129 | 1129 | 'type' => 'hidden', |
| 1130 | 1130 | ), |
| 1131 | 1131 | array( |
| 1132 | 1132 | 'name' => 'license_key_status', |
| 1133 | - 'default_value' => $default_settings['license_key_status'], |
|
| 1133 | + 'default_value' => $default_settings[ 'license_key_status' ], |
|
| 1134 | 1134 | 'type' => 'hidden', |
| 1135 | 1135 | ), |
| 1136 | 1136 | ); |
| 1137 | 1137 | |
| 1138 | 1138 | if ( defined( 'GRAVITYVIEW_LICENSE_KEY' ) && GRAVITYVIEW_LICENSE_KEY ) { |
| 1139 | - $license_fields[0] = array_merge( $license_fields[0], array( |
|
| 1139 | + $license_fields[ 0 ] = array_merge( $license_fields[ 0 ], array( |
|
| 1140 | 1140 | 'disabled' => true, |
| 1141 | 1141 | 'title' => __( 'The license key is defined by your site\'s configuration file.', 'gravityview' ), |
| 1142 | 1142 | ) ); |
@@ -1147,7 +1147,7 @@ discard block |
||
| 1147 | 1147 | |
| 1148 | 1148 | if ( \gravityview()->plugin->is_GF_25() ) { |
| 1149 | 1149 | |
| 1150 | - $sections[] = array( |
|
| 1150 | + $sections[ ] = array( |
|
| 1151 | 1151 | 'title' => __( 'GravityView License', 'gravityview' ), |
| 1152 | 1152 | 'class' => 'gform-settings-panel--full gv-settings-panel--license', |
| 1153 | 1153 | 'description' => $version_info, |
@@ -1165,7 +1165,7 @@ discard block |
||
| 1165 | 1165 | ) ); |
| 1166 | 1166 | } |
| 1167 | 1167 | |
| 1168 | - $sections[] = array( |
|
| 1168 | + $sections[ ] = array( |
|
| 1169 | 1169 | 'title' => ( gravityview()->plugin->is_GF_25() ? __( 'GravityView Settings', 'gravityview' ) : null ), |
| 1170 | 1170 | 'class' => 'gform-settings-panel--full gv-settings-panel--core', |
| 1171 | 1171 | 'fields' => $fields, |
@@ -1189,8 +1189,8 @@ discard block |
||
| 1189 | 1189 | |
| 1190 | 1190 | if ( $disabled_attribute ) { |
| 1191 | 1191 | foreach ( $extension_sections as &$section ) { |
| 1192 | - foreach ( $section['fields'] as &$field ) { |
|
| 1193 | - $field['disabled'] = $disabled_attribute; |
|
| 1192 | + foreach ( $section[ 'fields' ] as &$field ) { |
|
| 1193 | + $field[ 'disabled' ] = $disabled_attribute; |
|
| 1194 | 1194 | } |
| 1195 | 1195 | } |
| 1196 | 1196 | } |
@@ -1256,7 +1256,7 @@ discard block |
||
| 1256 | 1256 | public function settings_edd_license( $field, $echo = true ) { |
| 1257 | 1257 | |
| 1258 | 1258 | if ( defined( 'GRAVITYVIEW_LICENSE_KEY' ) && GRAVITYVIEW_LICENSE_KEY ) { |
| 1259 | - $field['input_type'] = 'password'; |
|
| 1259 | + $field[ 'input_type' ] = 'password'; |
|
| 1260 | 1260 | } |
| 1261 | 1261 | |
| 1262 | 1262 | $text = $this->settings_text( $field, false ); |
@@ -1308,7 +1308,7 @@ discard block |
||
| 1308 | 1308 | |
| 1309 | 1309 | ?> |
| 1310 | 1310 | |
| 1311 | - <tr id="gaddon-setting-row-<?php echo esc_attr( $field['name'] ); ?>"> |
|
| 1311 | + <tr id="gaddon-setting-row-<?php echo esc_attr( $field[ 'name' ] ); ?>"> |
|
| 1312 | 1312 | <td colspan="2"> |
| 1313 | 1313 | <?php $this->single_setting( $field ); ?> |
| 1314 | 1314 | </td> |
@@ -1334,8 +1334,8 @@ discard block |
||
| 1334 | 1334 | */ |
| 1335 | 1335 | public function single_setting_row( $field ) { |
| 1336 | 1336 | |
| 1337 | - $field['gv_description'] = Utils::get( $field, 'description' ); |
|
| 1338 | - $field['description'] = Utils::get( $field, 'subtitle' ); |
|
| 1337 | + $field[ 'gv_description' ] = Utils::get( $field, 'description' ); |
|
| 1338 | + $field[ 'description' ] = Utils::get( $field, 'subtitle' ); |
|
| 1339 | 1339 | parent::single_setting_row( $field ); |
| 1340 | 1340 | } |
| 1341 | 1341 | |
@@ -1396,8 +1396,8 @@ discard block |
||
| 1396 | 1396 | // then we assume it's changed. If it's changed, unset the status and the previous response. |
| 1397 | 1397 | if ( ! $added_message && ( $local_key !== $response_key ) ) { |
| 1398 | 1398 | |
| 1399 | - unset( $posted_settings['license_key_response'] ); |
|
| 1400 | - unset( $posted_settings['license_key_status'] ); |
|
| 1399 | + unset( $posted_settings[ 'license_key_response' ] ); |
|
| 1400 | + unset( $posted_settings[ 'license_key_status' ] ); |
|
| 1401 | 1401 | |
| 1402 | 1402 | \GFCommon::add_error_message( __( 'The license key you entered has been saved, but not activated. Please activate the license.', 'gravityview' ) ); |
| 1403 | 1403 | |
@@ -65,7 +65,7 @@ |
||
| 65 | 65 | /** |
| 66 | 66 | * Returns an array of tokens this test wants to listen for. |
| 67 | 67 | * |
| 68 | - * @return array |
|
| 68 | + * @return integer[] |
|
| 69 | 69 | */ |
| 70 | 70 | public function register() |
| 71 | 71 | { |
@@ -24,157 +24,157 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | abstract class AbstractFunctionCallParameterSniff extends Sniff |
| 26 | 26 | { |
| 27 | - /** |
|
| 28 | - * Is the sniff looking for a function call or a method call ? |
|
| 29 | - * |
|
| 30 | - * Note: the child class may need to do additional checks to make sure that |
|
| 31 | - * the method called is of the right class/object. |
|
| 32 | - * Checking that is outside of the scope of this abstract sniff. |
|
| 33 | - * |
|
| 34 | - * @var bool False (default) if the sniff is looking for function calls. |
|
| 35 | - * True if the sniff is looking for method calls. |
|
| 36 | - */ |
|
| 37 | - protected $isMethod = false; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Functions the sniff is looking for. Should be defined in the child class. |
|
| 41 | - * |
|
| 42 | - * @var array The only requirement for this array is that the top level |
|
| 43 | - * array keys are the names of the functions you're looking for. |
|
| 44 | - * Other than that, the array can have arbitrary content |
|
| 45 | - * depending on your needs. |
|
| 46 | - */ |
|
| 47 | - protected $targetFunctions = array(); |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * List of tokens which when they preceed the $stackPtr indicate that this |
|
| 51 | - * is not a function call. |
|
| 52 | - * |
|
| 53 | - * @var array |
|
| 54 | - */ |
|
| 55 | - private $ignoreTokens = array( |
|
| 56 | - \T_DOUBLE_COLON => true, |
|
| 57 | - \T_OBJECT_OPERATOR => true, |
|
| 58 | - \T_FUNCTION => true, |
|
| 59 | - \T_NEW => true, |
|
| 60 | - \T_CONST => true, |
|
| 61 | - \T_USE => true, |
|
| 62 | - ); |
|
| 63 | - |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Returns an array of tokens this test wants to listen for. |
|
| 67 | - * |
|
| 68 | - * @return array |
|
| 69 | - */ |
|
| 70 | - public function register() |
|
| 71 | - { |
|
| 72 | - // Handle case-insensitivity of function names. |
|
| 73 | - $this->targetFunctions = $this->arrayKeysToLowercase($this->targetFunctions); |
|
| 74 | - |
|
| 75 | - return array(\T_STRING); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Processes this test, when one of its tokens is encountered. |
|
| 81 | - * |
|
| 82 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 83 | - * @param int $stackPtr The position of the current token in |
|
| 84 | - * the stack passed in $tokens. |
|
| 85 | - * |
|
| 86 | - * @return int|void Integer stack pointer to skip forward or void to continue |
|
| 87 | - * normal file processing. |
|
| 88 | - */ |
|
| 89 | - public function process(File $phpcsFile, $stackPtr) |
|
| 90 | - { |
|
| 91 | - if ($this->bowOutEarly() === true) { |
|
| 92 | - return; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - $tokens = $phpcsFile->getTokens(); |
|
| 96 | - $function = $tokens[$stackPtr]['content']; |
|
| 97 | - $functionLc = strtolower($function); |
|
| 98 | - |
|
| 99 | - if (isset($this->targetFunctions[$functionLc]) === false) { |
|
| 100 | - return; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
| 104 | - |
|
| 105 | - if ($this->isMethod === true) { |
|
| 106 | - if ($tokens[$prevNonEmpty]['code'] !== \T_DOUBLE_COLON |
|
| 107 | - && $tokens[$prevNonEmpty]['code'] !== \T_OBJECT_OPERATOR |
|
| 108 | - ) { |
|
| 109 | - // Not a call to a PHP method. |
|
| 110 | - return; |
|
| 111 | - } |
|
| 112 | - } else { |
|
| 113 | - if (isset($this->ignoreTokens[$tokens[$prevNonEmpty]['code']]) === true) { |
|
| 114 | - // Not a call to a PHP function. |
|
| 115 | - return; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - if ($tokens[$prevNonEmpty]['code'] === \T_NS_SEPARATOR |
|
| 119 | - && $tokens[$prevNonEmpty - 1]['code'] === \T_STRING |
|
| 120 | - ) { |
|
| 121 | - // Namespaced function. |
|
| 122 | - return; |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - $parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr); |
|
| 127 | - |
|
| 128 | - if (empty($parameters)) { |
|
| 129 | - return $this->processNoParameters($phpcsFile, $stackPtr, $function); |
|
| 130 | - } else { |
|
| 131 | - return $this->processParameters($phpcsFile, $stackPtr, $function, $parameters); |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Do a version check to determine if this sniff needs to run at all. |
|
| 138 | - * |
|
| 139 | - * If the check done in a child class is not specific to one PHP version, |
|
| 140 | - * this function should return `false`. |
|
| 141 | - * |
|
| 142 | - * @return bool |
|
| 143 | - */ |
|
| 144 | - abstract protected function bowOutEarly(); |
|
| 145 | - |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Process the parameters of a matched function. |
|
| 149 | - * |
|
| 150 | - * This method has to be made concrete in child classes. |
|
| 151 | - * |
|
| 152 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 153 | - * @param int $stackPtr The position of the current token in the stack. |
|
| 154 | - * @param string $functionName The token content (function name) which was matched. |
|
| 155 | - * @param array $parameters Array with information about the parameters. |
|
| 156 | - * |
|
| 157 | - * @return int|void Integer stack pointer to skip forward or void to continue |
|
| 158 | - * normal file processing. |
|
| 159 | - */ |
|
| 160 | - abstract public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters); |
|
| 161 | - |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Process the function if no parameters were found. |
|
| 165 | - * |
|
| 166 | - * Defaults to doing nothing. Can be overloaded in child classes to handle functions |
|
| 167 | - * were parameters are expected, but none found. |
|
| 168 | - * |
|
| 169 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 170 | - * @param int $stackPtr The position of the current token in the stack. |
|
| 171 | - * @param string $functionName The token content (function name) which was matched. |
|
| 172 | - * |
|
| 173 | - * @return int|void Integer stack pointer to skip forward or void to continue |
|
| 174 | - * normal file processing. |
|
| 175 | - */ |
|
| 176 | - public function processNoParameters(File $phpcsFile, $stackPtr, $functionName) |
|
| 177 | - { |
|
| 178 | - return; |
|
| 179 | - } |
|
| 27 | + /** |
|
| 28 | + * Is the sniff looking for a function call or a method call ? |
|
| 29 | + * |
|
| 30 | + * Note: the child class may need to do additional checks to make sure that |
|
| 31 | + * the method called is of the right class/object. |
|
| 32 | + * Checking that is outside of the scope of this abstract sniff. |
|
| 33 | + * |
|
| 34 | + * @var bool False (default) if the sniff is looking for function calls. |
|
| 35 | + * True if the sniff is looking for method calls. |
|
| 36 | + */ |
|
| 37 | + protected $isMethod = false; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Functions the sniff is looking for. Should be defined in the child class. |
|
| 41 | + * |
|
| 42 | + * @var array The only requirement for this array is that the top level |
|
| 43 | + * array keys are the names of the functions you're looking for. |
|
| 44 | + * Other than that, the array can have arbitrary content |
|
| 45 | + * depending on your needs. |
|
| 46 | + */ |
|
| 47 | + protected $targetFunctions = array(); |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * List of tokens which when they preceed the $stackPtr indicate that this |
|
| 51 | + * is not a function call. |
|
| 52 | + * |
|
| 53 | + * @var array |
|
| 54 | + */ |
|
| 55 | + private $ignoreTokens = array( |
|
| 56 | + \T_DOUBLE_COLON => true, |
|
| 57 | + \T_OBJECT_OPERATOR => true, |
|
| 58 | + \T_FUNCTION => true, |
|
| 59 | + \T_NEW => true, |
|
| 60 | + \T_CONST => true, |
|
| 61 | + \T_USE => true, |
|
| 62 | + ); |
|
| 63 | + |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Returns an array of tokens this test wants to listen for. |
|
| 67 | + * |
|
| 68 | + * @return array |
|
| 69 | + */ |
|
| 70 | + public function register() |
|
| 71 | + { |
|
| 72 | + // Handle case-insensitivity of function names. |
|
| 73 | + $this->targetFunctions = $this->arrayKeysToLowercase($this->targetFunctions); |
|
| 74 | + |
|
| 75 | + return array(\T_STRING); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Processes this test, when one of its tokens is encountered. |
|
| 81 | + * |
|
| 82 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 83 | + * @param int $stackPtr The position of the current token in |
|
| 84 | + * the stack passed in $tokens. |
|
| 85 | + * |
|
| 86 | + * @return int|void Integer stack pointer to skip forward or void to continue |
|
| 87 | + * normal file processing. |
|
| 88 | + */ |
|
| 89 | + public function process(File $phpcsFile, $stackPtr) |
|
| 90 | + { |
|
| 91 | + if ($this->bowOutEarly() === true) { |
|
| 92 | + return; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + $tokens = $phpcsFile->getTokens(); |
|
| 96 | + $function = $tokens[$stackPtr]['content']; |
|
| 97 | + $functionLc = strtolower($function); |
|
| 98 | + |
|
| 99 | + if (isset($this->targetFunctions[$functionLc]) === false) { |
|
| 100 | + return; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
| 104 | + |
|
| 105 | + if ($this->isMethod === true) { |
|
| 106 | + if ($tokens[$prevNonEmpty]['code'] !== \T_DOUBLE_COLON |
|
| 107 | + && $tokens[$prevNonEmpty]['code'] !== \T_OBJECT_OPERATOR |
|
| 108 | + ) { |
|
| 109 | + // Not a call to a PHP method. |
|
| 110 | + return; |
|
| 111 | + } |
|
| 112 | + } else { |
|
| 113 | + if (isset($this->ignoreTokens[$tokens[$prevNonEmpty]['code']]) === true) { |
|
| 114 | + // Not a call to a PHP function. |
|
| 115 | + return; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + if ($tokens[$prevNonEmpty]['code'] === \T_NS_SEPARATOR |
|
| 119 | + && $tokens[$prevNonEmpty - 1]['code'] === \T_STRING |
|
| 120 | + ) { |
|
| 121 | + // Namespaced function. |
|
| 122 | + return; |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + $parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr); |
|
| 127 | + |
|
| 128 | + if (empty($parameters)) { |
|
| 129 | + return $this->processNoParameters($phpcsFile, $stackPtr, $function); |
|
| 130 | + } else { |
|
| 131 | + return $this->processParameters($phpcsFile, $stackPtr, $function, $parameters); |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Do a version check to determine if this sniff needs to run at all. |
|
| 138 | + * |
|
| 139 | + * If the check done in a child class is not specific to one PHP version, |
|
| 140 | + * this function should return `false`. |
|
| 141 | + * |
|
| 142 | + * @return bool |
|
| 143 | + */ |
|
| 144 | + abstract protected function bowOutEarly(); |
|
| 145 | + |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Process the parameters of a matched function. |
|
| 149 | + * |
|
| 150 | + * This method has to be made concrete in child classes. |
|
| 151 | + * |
|
| 152 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 153 | + * @param int $stackPtr The position of the current token in the stack. |
|
| 154 | + * @param string $functionName The token content (function name) which was matched. |
|
| 155 | + * @param array $parameters Array with information about the parameters. |
|
| 156 | + * |
|
| 157 | + * @return int|void Integer stack pointer to skip forward or void to continue |
|
| 158 | + * normal file processing. |
|
| 159 | + */ |
|
| 160 | + abstract public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters); |
|
| 161 | + |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Process the function if no parameters were found. |
|
| 165 | + * |
|
| 166 | + * Defaults to doing nothing. Can be overloaded in child classes to handle functions |
|
| 167 | + * were parameters are expected, but none found. |
|
| 168 | + * |
|
| 169 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 170 | + * @param int $stackPtr The position of the current token in the stack. |
|
| 171 | + * @param string $functionName The token content (function name) which was matched. |
|
| 172 | + * |
|
| 173 | + * @return int|void Integer stack pointer to skip forward or void to continue |
|
| 174 | + * normal file processing. |
|
| 175 | + */ |
|
| 176 | + public function processNoParameters(File $phpcsFile, $stackPtr, $functionName) |
|
| 177 | + { |
|
| 178 | + return; |
|
| 179 | + } |
|
| 180 | 180 | } |
@@ -70,9 +70,9 @@ discard block |
||
| 70 | 70 | public function register() |
| 71 | 71 | { |
| 72 | 72 | // Handle case-insensitivity of function names. |
| 73 | - $this->targetFunctions = $this->arrayKeysToLowercase($this->targetFunctions); |
|
| 73 | + $this->targetFunctions = $this->arrayKeysToLowercase( $this->targetFunctions ); |
|
| 74 | 74 | |
| 75 | - return array(\T_STRING); |
|
| 75 | + return array( \T_STRING ); |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | |
@@ -86,49 +86,49 @@ discard block |
||
| 86 | 86 | * @return int|void Integer stack pointer to skip forward or void to continue |
| 87 | 87 | * normal file processing. |
| 88 | 88 | */ |
| 89 | - public function process(File $phpcsFile, $stackPtr) |
|
| 89 | + public function process( File $phpcsFile, $stackPtr ) |
|
| 90 | 90 | { |
| 91 | - if ($this->bowOutEarly() === true) { |
|
| 91 | + if ( $this->bowOutEarly() === true ) { |
|
| 92 | 92 | return; |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | $tokens = $phpcsFile->getTokens(); |
| 96 | - $function = $tokens[$stackPtr]['content']; |
|
| 97 | - $functionLc = strtolower($function); |
|
| 96 | + $function = $tokens[ $stackPtr ][ 'content' ]; |
|
| 97 | + $functionLc = strtolower( $function ); |
|
| 98 | 98 | |
| 99 | - if (isset($this->targetFunctions[$functionLc]) === false) { |
|
| 99 | + if ( isset( $this->targetFunctions[ $functionLc ] ) === false ) { |
|
| 100 | 100 | return; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | - $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
| 103 | + $prevNonEmpty = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true ); |
|
| 104 | 104 | |
| 105 | - if ($this->isMethod === true) { |
|
| 106 | - if ($tokens[$prevNonEmpty]['code'] !== \T_DOUBLE_COLON |
|
| 107 | - && $tokens[$prevNonEmpty]['code'] !== \T_OBJECT_OPERATOR |
|
| 105 | + if ( $this->isMethod === true ) { |
|
| 106 | + if ( $tokens[ $prevNonEmpty ][ 'code' ] !== \T_DOUBLE_COLON |
|
| 107 | + && $tokens[ $prevNonEmpty ][ 'code' ] !== \T_OBJECT_OPERATOR |
|
| 108 | 108 | ) { |
| 109 | 109 | // Not a call to a PHP method. |
| 110 | 110 | return; |
| 111 | 111 | } |
| 112 | 112 | } else { |
| 113 | - if (isset($this->ignoreTokens[$tokens[$prevNonEmpty]['code']]) === true) { |
|
| 113 | + if ( isset( $this->ignoreTokens[ $tokens[ $prevNonEmpty ][ 'code' ] ] ) === true ) { |
|
| 114 | 114 | // Not a call to a PHP function. |
| 115 | 115 | return; |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | - if ($tokens[$prevNonEmpty]['code'] === \T_NS_SEPARATOR |
|
| 119 | - && $tokens[$prevNonEmpty - 1]['code'] === \T_STRING |
|
| 118 | + if ( $tokens[ $prevNonEmpty ][ 'code' ] === \T_NS_SEPARATOR |
|
| 119 | + && $tokens[ $prevNonEmpty - 1 ][ 'code' ] === \T_STRING |
|
| 120 | 120 | ) { |
| 121 | 121 | // Namespaced function. |
| 122 | 122 | return; |
| 123 | 123 | } |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | - $parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr); |
|
| 126 | + $parameters = $this->getFunctionCallParameters( $phpcsFile, $stackPtr ); |
|
| 127 | 127 | |
| 128 | - if (empty($parameters)) { |
|
| 129 | - return $this->processNoParameters($phpcsFile, $stackPtr, $function); |
|
| 128 | + if ( empty( $parameters ) ) { |
|
| 129 | + return $this->processNoParameters( $phpcsFile, $stackPtr, $function ); |
|
| 130 | 130 | } else { |
| 131 | - return $this->processParameters($phpcsFile, $stackPtr, $function, $parameters); |
|
| 131 | + return $this->processParameters( $phpcsFile, $stackPtr, $function, $parameters ); |
|
| 132 | 132 | } |
| 133 | 133 | } |
| 134 | 134 | |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | * @return int|void Integer stack pointer to skip forward or void to continue |
| 158 | 158 | * normal file processing. |
| 159 | 159 | */ |
| 160 | - abstract public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters); |
|
| 160 | + abstract public function processParameters( File $phpcsFile, $stackPtr, $functionName, $parameters ); |
|
| 161 | 161 | |
| 162 | 162 | |
| 163 | 163 | /** |
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | * @return int|void Integer stack pointer to skip forward or void to continue |
| 174 | 174 | * normal file processing. |
| 175 | 175 | */ |
| 176 | - public function processNoParameters(File $phpcsFile, $stackPtr, $functionName) |
|
| 176 | + public function processNoParameters( File $phpcsFile, $stackPtr, $functionName ) |
|
| 177 | 177 | { |
| 178 | 178 | return; |
| 179 | 179 | } |
@@ -22,8 +22,7 @@ discard block |
||
| 22 | 22 | * @package PHPCompatibility |
| 23 | 23 | * @author Juliette Reinders Folmer <[email protected]> |
| 24 | 24 | */ |
| 25 | -abstract class AbstractFunctionCallParameterSniff extends Sniff |
|
| 26 | -{ |
|
| 25 | +abstract class AbstractFunctionCallParameterSniff extends Sniff { |
|
| 27 | 26 | /** |
| 28 | 27 | * Is the sniff looking for a function call or a method call ? |
| 29 | 28 | * |
@@ -67,8 +66,7 @@ discard block |
||
| 67 | 66 | * |
| 68 | 67 | * @return array |
| 69 | 68 | */ |
| 70 | - public function register() |
|
| 71 | - { |
|
| 69 | + public function register() { |
|
| 72 | 70 | // Handle case-insensitivity of function names. |
| 73 | 71 | $this->targetFunctions = $this->arrayKeysToLowercase($this->targetFunctions); |
| 74 | 72 | |
@@ -86,8 +84,7 @@ discard block |
||
| 86 | 84 | * @return int|void Integer stack pointer to skip forward or void to continue |
| 87 | 85 | * normal file processing. |
| 88 | 86 | */ |
| 89 | - public function process(File $phpcsFile, $stackPtr) |
|
| 90 | - { |
|
| 87 | + public function process(File $phpcsFile, $stackPtr) { |
|
| 91 | 88 | if ($this->bowOutEarly() === true) { |
| 92 | 89 | return; |
| 93 | 90 | } |
@@ -173,8 +170,7 @@ discard block |
||
| 173 | 170 | * @return int|void Integer stack pointer to skip forward or void to continue |
| 174 | 171 | * normal file processing. |
| 175 | 172 | */ |
| 176 | - public function processNoParameters(File $phpcsFile, $stackPtr, $functionName) |
|
| 177 | - { |
|
| 173 | + public function processNoParameters(File $phpcsFile, $stackPtr, $functionName) { |
|
| 178 | 174 | return; |
| 179 | 175 | } |
| 180 | 176 | } |
@@ -1016,7 +1016,7 @@ |
||
| 1016 | 1016 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
| 1017 | 1017 | * @param int $stackPtr The position of the return type token. |
| 1018 | 1018 | * |
| 1019 | - * @return string|false The name of the return type token. |
|
| 1019 | + * @return null|string The name of the return type token. |
|
| 1020 | 1020 | */ |
| 1021 | 1021 | public function getReturnTypeHintName(File $phpcsFile, $stackPtr) |
| 1022 | 1022 | { |
@@ -27,959 +27,959 @@ discard block |
||
| 27 | 27 | abstract class Sniff implements PHPCS_Sniff |
| 28 | 28 | { |
| 29 | 29 | |
| 30 | - const REGEX_COMPLEX_VARS = '`(?:(\{)?(?<!\\\\)\$)?(\{)?(?<!\\\\)\$(\{)?(?P<varname>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(?:->\$?(?P>varname)|\[[^\]]+\]|::\$?(?P>varname)|\([^\)]*\))*(?(3)\}|)(?(2)\}|)(?(1)\}|)`'; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * List of superglobals as an array of strings. |
|
| 34 | - * |
|
| 35 | - * Used by the ParameterShadowSuperGlobals and ForbiddenClosureUseVariableNames sniffs. |
|
| 36 | - * |
|
| 37 | - * @var array |
|
| 38 | - */ |
|
| 39 | - protected $superglobals = array( |
|
| 40 | - '$GLOBALS' => true, |
|
| 41 | - '$_SERVER' => true, |
|
| 42 | - '$_GET' => true, |
|
| 43 | - '$_POST' => true, |
|
| 44 | - '$_FILES' => true, |
|
| 45 | - '$_COOKIE' => true, |
|
| 46 | - '$_SESSION' => true, |
|
| 47 | - '$_REQUEST' => true, |
|
| 48 | - '$_ENV' => true, |
|
| 49 | - ); |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * List of functions using hash algorithm as parameter (always the first parameter). |
|
| 53 | - * |
|
| 54 | - * Used by the new/removed hash algorithm sniffs. |
|
| 55 | - * Key is the function name, value is the 1-based parameter position in the function call. |
|
| 56 | - * |
|
| 57 | - * @var array |
|
| 58 | - */ |
|
| 59 | - protected $hashAlgoFunctions = array( |
|
| 60 | - 'hash_file' => 1, |
|
| 61 | - 'hash_hmac_file' => 1, |
|
| 62 | - 'hash_hmac' => 1, |
|
| 63 | - 'hash_init' => 1, |
|
| 64 | - 'hash_pbkdf2' => 1, |
|
| 65 | - 'hash' => 1, |
|
| 66 | - ); |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * List of functions which take an ini directive as parameter (always the first parameter). |
|
| 71 | - * |
|
| 72 | - * Used by the new/removed ini directives sniffs. |
|
| 73 | - * Key is the function name, value is the 1-based parameter position in the function call. |
|
| 74 | - * |
|
| 75 | - * @var array |
|
| 76 | - */ |
|
| 77 | - protected $iniFunctions = array( |
|
| 78 | - 'ini_get' => 1, |
|
| 79 | - 'ini_set' => 1, |
|
| 80 | - ); |
|
| 81 | - |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Get the testVersion configuration variable. |
|
| 85 | - * |
|
| 86 | - * The testVersion configuration variable may be in any of the following formats: |
|
| 87 | - * 1) Omitted/empty, in which case no version is specified. This effectively |
|
| 88 | - * disables all the checks for new PHP features provided by this standard. |
|
| 89 | - * 2) A single PHP version number, e.g. "5.4" in which case the standard checks that |
|
| 90 | - * the code will run on that version of PHP (no deprecated features or newer |
|
| 91 | - * features being used). |
|
| 92 | - * 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run |
|
| 93 | - * on all PHP versions in that range, and that it doesn't use any features that |
|
| 94 | - * were deprecated by the final version in the list, or which were not available |
|
| 95 | - * for the first version in the list. |
|
| 96 | - * We accept ranges where one of the components is missing, e.g. "-5.6" means |
|
| 97 | - * all versions up to PHP 5.6, and "7.0-" means all versions above PHP 7.0. |
|
| 98 | - * PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2" |
|
| 99 | - * would be treated as invalid, and ignored. |
|
| 100 | - * |
|
| 101 | - * @return array $arrTestVersions will hold an array containing min/max version |
|
| 102 | - * of PHP that we are checking against (see above). If only a |
|
| 103 | - * single version number is specified, then this is used as |
|
| 104 | - * both the min and max. |
|
| 105 | - * |
|
| 106 | - * @throws \PHP_CodeSniffer_Exception If testVersion is invalid. |
|
| 107 | - */ |
|
| 108 | - private function getTestVersion() |
|
| 109 | - { |
|
| 110 | - static $arrTestVersions = array(); |
|
| 111 | - |
|
| 112 | - $default = array(null, null); |
|
| 113 | - $testVersion = trim(PHPCSHelper::getConfigData('testVersion')); |
|
| 114 | - |
|
| 115 | - if (empty($testVersion) === false && isset($arrTestVersions[$testVersion]) === false) { |
|
| 116 | - |
|
| 117 | - $arrTestVersions[$testVersion] = $default; |
|
| 118 | - |
|
| 119 | - if (preg_match('`^\d+\.\d+$`', $testVersion)) { |
|
| 120 | - $arrTestVersions[$testVersion] = array($testVersion, $testVersion); |
|
| 121 | - return $arrTestVersions[$testVersion]; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - if (preg_match('`^(\d+\.\d+)?\s*-\s*(\d+\.\d+)?$`', $testVersion, $matches)) { |
|
| 125 | - if (empty($matches[1]) === false || empty($matches[2]) === false) { |
|
| 126 | - // If no lower-limit is set, we set the min version to 4.0. |
|
| 127 | - // Whilst development focuses on PHP 5 and above, we also accept |
|
| 128 | - // sniffs for PHP 4, so we include that as the minimum. |
|
| 129 | - // (It makes no sense to support PHP 3 as this was effectively a |
|
| 130 | - // different language). |
|
| 131 | - $min = empty($matches[1]) ? '4.0' : $matches[1]; |
|
| 132 | - |
|
| 133 | - // If no upper-limit is set, we set the max version to 99.9. |
|
| 134 | - $max = empty($matches[2]) ? '99.9' : $matches[2]; |
|
| 135 | - |
|
| 136 | - if (version_compare($min, $max, '>')) { |
|
| 137 | - trigger_error( |
|
| 138 | - "Invalid range in testVersion setting: '" . $testVersion . "'", |
|
| 139 | - \E_USER_WARNING |
|
| 140 | - ); |
|
| 141 | - return $default; |
|
| 142 | - } else { |
|
| 143 | - $arrTestVersions[$testVersion] = array($min, $max); |
|
| 144 | - return $arrTestVersions[$testVersion]; |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - trigger_error( |
|
| 150 | - "Invalid testVersion setting: '" . $testVersion . "'", |
|
| 151 | - \E_USER_WARNING |
|
| 152 | - ); |
|
| 153 | - return $default; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - if (isset($arrTestVersions[$testVersion])) { |
|
| 157 | - return $arrTestVersions[$testVersion]; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - return $default; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Check whether a specific PHP version is equal to or higher than the maximum |
|
| 166 | - * supported PHP version as provided by the user in `testVersion`. |
|
| 167 | - * |
|
| 168 | - * Should be used when sniffing for *old* PHP features (deprecated/removed). |
|
| 169 | - * |
|
| 170 | - * @param string $phpVersion A PHP version number in 'major.minor' format. |
|
| 171 | - * |
|
| 172 | - * @return bool True if testVersion has not been provided or if the PHP version |
|
| 173 | - * is equal to or higher than the highest supported PHP version |
|
| 174 | - * in testVersion. False otherwise. |
|
| 175 | - */ |
|
| 176 | - public function supportsAbove($phpVersion) |
|
| 177 | - { |
|
| 178 | - $testVersion = $this->getTestVersion(); |
|
| 179 | - $testVersion = $testVersion[1]; |
|
| 180 | - |
|
| 181 | - if (\is_null($testVersion) |
|
| 182 | - || version_compare($testVersion, $phpVersion) >= 0 |
|
| 183 | - ) { |
|
| 184 | - return true; |
|
| 185 | - } else { |
|
| 186 | - return false; |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Check whether a specific PHP version is equal to or lower than the minimum |
|
| 193 | - * supported PHP version as provided by the user in `testVersion`. |
|
| 194 | - * |
|
| 195 | - * Should be used when sniffing for *new* PHP features. |
|
| 196 | - * |
|
| 197 | - * @param string $phpVersion A PHP version number in 'major.minor' format. |
|
| 198 | - * |
|
| 199 | - * @return bool True if the PHP version is equal to or lower than the lowest |
|
| 200 | - * supported PHP version in testVersion. |
|
| 201 | - * False otherwise or if no testVersion is provided. |
|
| 202 | - */ |
|
| 203 | - public function supportsBelow($phpVersion) |
|
| 204 | - { |
|
| 205 | - $testVersion = $this->getTestVersion(); |
|
| 206 | - $testVersion = $testVersion[0]; |
|
| 207 | - |
|
| 208 | - if (\is_null($testVersion) === false |
|
| 209 | - && version_compare($testVersion, $phpVersion) <= 0 |
|
| 210 | - ) { |
|
| 211 | - return true; |
|
| 212 | - } else { |
|
| 213 | - return false; |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Add a PHPCS message to the output stack as either a warning or an error. |
|
| 220 | - * |
|
| 221 | - * @param \PHP_CodeSniffer_File $phpcsFile The file the message applies to. |
|
| 222 | - * @param string $message The message. |
|
| 223 | - * @param int $stackPtr The position of the token |
|
| 224 | - * the message relates to. |
|
| 225 | - * @param bool $isError Whether to report the message as an |
|
| 226 | - * 'error' or 'warning'. |
|
| 227 | - * Defaults to true (error). |
|
| 228 | - * @param string $code The error code for the message. |
|
| 229 | - * Defaults to 'Found'. |
|
| 230 | - * @param array $data Optional input for the data replacements. |
|
| 231 | - * |
|
| 232 | - * @return void |
|
| 233 | - */ |
|
| 234 | - public function addMessage(File $phpcsFile, $message, $stackPtr, $isError, $code = 'Found', $data = array()) |
|
| 235 | - { |
|
| 236 | - if ($isError === true) { |
|
| 237 | - $phpcsFile->addError($message, $stackPtr, $code, $data); |
|
| 238 | - } else { |
|
| 239 | - $phpcsFile->addWarning($message, $stackPtr, $code, $data); |
|
| 240 | - } |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Convert an arbitrary string to an alphanumeric string with underscores. |
|
| 246 | - * |
|
| 247 | - * Pre-empt issues with arbitrary strings being used as error codes in XML and PHP. |
|
| 248 | - * |
|
| 249 | - * @param string $baseString Arbitrary string. |
|
| 250 | - * |
|
| 251 | - * @return string |
|
| 252 | - */ |
|
| 253 | - public function stringToErrorCode($baseString) |
|
| 254 | - { |
|
| 255 | - return preg_replace('`[^a-z0-9_]`i', '_', strtolower($baseString)); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * Strip quotes surrounding an arbitrary string. |
|
| 261 | - * |
|
| 262 | - * Intended for use with the contents of a T_CONSTANT_ENCAPSED_STRING / T_DOUBLE_QUOTED_STRING. |
|
| 263 | - * |
|
| 264 | - * @param string $string The raw string. |
|
| 265 | - * |
|
| 266 | - * @return string String without quotes around it. |
|
| 267 | - */ |
|
| 268 | - public function stripQuotes($string) |
|
| 269 | - { |
|
| 270 | - return preg_replace('`^([\'"])(.*)\1$`Ds', '$2', $string); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Strip variables from an arbitrary double quoted string. |
|
| 276 | - * |
|
| 277 | - * Intended for use with the contents of a T_DOUBLE_QUOTED_STRING. |
|
| 278 | - * |
|
| 279 | - * @param string $string The raw string. |
|
| 280 | - * |
|
| 281 | - * @return string String without variables in it. |
|
| 282 | - */ |
|
| 283 | - public function stripVariables($string) |
|
| 284 | - { |
|
| 285 | - if (strpos($string, '$') === false) { |
|
| 286 | - return $string; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - return preg_replace(self::REGEX_COMPLEX_VARS, '', $string); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * Make all top level array keys in an array lowercase. |
|
| 295 | - * |
|
| 296 | - * @param array $array Initial array. |
|
| 297 | - * |
|
| 298 | - * @return array Same array, but with all lowercase top level keys. |
|
| 299 | - */ |
|
| 300 | - public function arrayKeysToLowercase($array) |
|
| 301 | - { |
|
| 302 | - return array_change_key_case($array, \CASE_LOWER); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Checks if a function call has parameters. |
|
| 308 | - * |
|
| 309 | - * Expects to be passed the T_STRING or T_VARIABLE stack pointer for the function call. |
|
| 310 | - * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
|
| 311 | - * |
|
| 312 | - * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, it |
|
| 313 | - * will detect whether the array has values or is empty. |
|
| 314 | - * |
|
| 315 | - * @link https://github.com/PHPCompatibility/PHPCompatibility/issues/120 |
|
| 316 | - * @link https://github.com/PHPCompatibility/PHPCompatibility/issues/152 |
|
| 317 | - * |
|
| 318 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 319 | - * @param int $stackPtr The position of the function call token. |
|
| 320 | - * |
|
| 321 | - * @return bool |
|
| 322 | - */ |
|
| 323 | - public function doesFunctionCallHaveParameters(File $phpcsFile, $stackPtr) |
|
| 324 | - { |
|
| 325 | - $tokens = $phpcsFile->getTokens(); |
|
| 326 | - |
|
| 327 | - // Check for the existence of the token. |
|
| 328 | - if (isset($tokens[$stackPtr]) === false) { |
|
| 329 | - return false; |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - // Is this one of the tokens this function handles ? |
|
| 333 | - if (\in_array($tokens[$stackPtr]['code'], array(\T_STRING, \T_ARRAY, \T_OPEN_SHORT_ARRAY, \T_VARIABLE), true) === false) { |
|
| 334 | - return false; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
|
| 338 | - |
|
| 339 | - // Deal with short array syntax. |
|
| 340 | - if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY) { |
|
| 341 | - if (isset($tokens[$stackPtr]['bracket_closer']) === false) { |
|
| 342 | - return false; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - if ($nextNonEmpty === $tokens[$stackPtr]['bracket_closer']) { |
|
| 346 | - // No parameters. |
|
| 347 | - return false; |
|
| 348 | - } else { |
|
| 349 | - return true; |
|
| 350 | - } |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - // Deal with function calls & long arrays. |
|
| 354 | - // Next non-empty token should be the open parenthesis. |
|
| 355 | - if ($nextNonEmpty === false && $tokens[$nextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS) { |
|
| 356 | - return false; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - if (isset($tokens[$nextNonEmpty]['parenthesis_closer']) === false) { |
|
| 360 | - return false; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - $closeParenthesis = $tokens[$nextNonEmpty]['parenthesis_closer']; |
|
| 364 | - $nextNextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $nextNonEmpty + 1, $closeParenthesis + 1, true); |
|
| 365 | - |
|
| 366 | - if ($nextNextNonEmpty === $closeParenthesis) { |
|
| 367 | - // No parameters. |
|
| 368 | - return false; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - return true; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Count the number of parameters a function call has been passed. |
|
| 377 | - * |
|
| 378 | - * Expects to be passed the T_STRING or T_VARIABLE stack pointer for the function call. |
|
| 379 | - * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
|
| 380 | - * |
|
| 381 | - * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
|
| 382 | - * it will return the number of values in the array. |
|
| 383 | - * |
|
| 384 | - * @link https://github.com/PHPCompatibility/PHPCompatibility/issues/111 |
|
| 385 | - * @link https://github.com/PHPCompatibility/PHPCompatibility/issues/114 |
|
| 386 | - * @link https://github.com/PHPCompatibility/PHPCompatibility/issues/151 |
|
| 387 | - * |
|
| 388 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 389 | - * @param int $stackPtr The position of the function call token. |
|
| 390 | - * |
|
| 391 | - * @return int |
|
| 392 | - */ |
|
| 393 | - public function getFunctionCallParameterCount(File $phpcsFile, $stackPtr) |
|
| 394 | - { |
|
| 395 | - if ($this->doesFunctionCallHaveParameters($phpcsFile, $stackPtr) === false) { |
|
| 396 | - return 0; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - return \count($this->getFunctionCallParameters($phpcsFile, $stackPtr)); |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * Get information on all parameters passed to a function call. |
|
| 405 | - * |
|
| 406 | - * Expects to be passed the T_STRING or T_VARIABLE stack pointer for the function call. |
|
| 407 | - * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
|
| 408 | - * |
|
| 409 | - * Will return an multi-dimentional array with the start token pointer, end token |
|
| 410 | - * pointer and raw parameter value for all parameters. Index will be 1-based. |
|
| 411 | - * If no parameters are found, will return an empty array. |
|
| 412 | - * |
|
| 413 | - * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
|
| 414 | - * it will tokenize the values / key/value pairs contained in the array call. |
|
| 415 | - * |
|
| 416 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 417 | - * @param int $stackPtr The position of the function call token. |
|
| 418 | - * |
|
| 419 | - * @return array |
|
| 420 | - */ |
|
| 421 | - public function getFunctionCallParameters(File $phpcsFile, $stackPtr) |
|
| 422 | - { |
|
| 423 | - if ($this->doesFunctionCallHaveParameters($phpcsFile, $stackPtr) === false) { |
|
| 424 | - return array(); |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - // Ok, we know we have a T_STRING, T_VARIABLE, T_ARRAY or T_OPEN_SHORT_ARRAY with parameters |
|
| 428 | - // and valid open & close brackets/parenthesis. |
|
| 429 | - $tokens = $phpcsFile->getTokens(); |
|
| 430 | - |
|
| 431 | - // Mark the beginning and end tokens. |
|
| 432 | - if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY) { |
|
| 433 | - $opener = $stackPtr; |
|
| 434 | - $closer = $tokens[$stackPtr]['bracket_closer']; |
|
| 435 | - |
|
| 436 | - $nestedParenthesisCount = 0; |
|
| 437 | - |
|
| 438 | - } else { |
|
| 439 | - $opener = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
|
| 440 | - $closer = $tokens[$opener]['parenthesis_closer']; |
|
| 441 | - |
|
| 442 | - $nestedParenthesisCount = 1; |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - // Which nesting level is the one we are interested in ? |
|
| 446 | - if (isset($tokens[$opener]['nested_parenthesis'])) { |
|
| 447 | - $nestedParenthesisCount += \count($tokens[$opener]['nested_parenthesis']); |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - $parameters = array(); |
|
| 451 | - $nextComma = $opener; |
|
| 452 | - $paramStart = $opener + 1; |
|
| 453 | - $cnt = 1; |
|
| 454 | - while (($nextComma = $phpcsFile->findNext(array(\T_COMMA, $tokens[$closer]['code'], \T_OPEN_SHORT_ARRAY, \T_CLOSURE), $nextComma + 1, $closer + 1)) !== false) { |
|
| 455 | - // Ignore anything within short array definition brackets. |
|
| 456 | - if ($tokens[$nextComma]['type'] === 'T_OPEN_SHORT_ARRAY' |
|
| 457 | - && (isset($tokens[$nextComma]['bracket_opener']) |
|
| 458 | - && $tokens[$nextComma]['bracket_opener'] === $nextComma) |
|
| 459 | - && isset($tokens[$nextComma]['bracket_closer']) |
|
| 460 | - ) { |
|
| 461 | - // Skip forward to the end of the short array definition. |
|
| 462 | - $nextComma = $tokens[$nextComma]['bracket_closer']; |
|
| 463 | - continue; |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - // Skip past closures passed as function parameters. |
|
| 467 | - if ($tokens[$nextComma]['type'] === 'T_CLOSURE' |
|
| 468 | - && (isset($tokens[$nextComma]['scope_condition']) |
|
| 469 | - && $tokens[$nextComma]['scope_condition'] === $nextComma) |
|
| 470 | - && isset($tokens[$nextComma]['scope_closer']) |
|
| 471 | - ) { |
|
| 472 | - // Skip forward to the end of the closure declaration. |
|
| 473 | - $nextComma = $tokens[$nextComma]['scope_closer']; |
|
| 474 | - continue; |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - // Ignore comma's at a lower nesting level. |
|
| 478 | - if ($tokens[$nextComma]['type'] === 'T_COMMA' |
|
| 479 | - && isset($tokens[$nextComma]['nested_parenthesis']) |
|
| 480 | - && \count($tokens[$nextComma]['nested_parenthesis']) !== $nestedParenthesisCount |
|
| 481 | - ) { |
|
| 482 | - continue; |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - // Ignore closing parenthesis/bracket if not 'ours'. |
|
| 486 | - if ($tokens[$nextComma]['type'] === $tokens[$closer]['type'] && $nextComma !== $closer) { |
|
| 487 | - continue; |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - // Ok, we've reached the end of the parameter. |
|
| 491 | - $parameters[$cnt]['start'] = $paramStart; |
|
| 492 | - $parameters[$cnt]['end'] = $nextComma - 1; |
|
| 493 | - $parameters[$cnt]['raw'] = trim($phpcsFile->getTokensAsString($paramStart, ($nextComma - $paramStart))); |
|
| 494 | - |
|
| 495 | - // Check if there are more tokens before the closing parenthesis. |
|
| 496 | - // Prevents code like the following from setting a third parameter: |
|
| 497 | - // functionCall( $param1, $param2, ); |
|
| 498 | - $hasNextParam = $phpcsFile->findNext(Tokens::$emptyTokens, $nextComma + 1, $closer, true, null, true); |
|
| 499 | - if ($hasNextParam === false) { |
|
| 500 | - break; |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - // Prepare for the next parameter. |
|
| 504 | - $paramStart = $nextComma + 1; |
|
| 505 | - $cnt++; |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - return $parameters; |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - |
|
| 512 | - /** |
|
| 513 | - * Get information on a specific parameter passed to a function call. |
|
| 514 | - * |
|
| 515 | - * Expects to be passed the T_STRING or T_VARIABLE stack pointer for the function call. |
|
| 516 | - * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
|
| 517 | - * |
|
| 518 | - * Will return a array with the start token pointer, end token pointer and the raw value |
|
| 519 | - * of the parameter at a specific offset. |
|
| 520 | - * If the specified parameter is not found, will return false. |
|
| 521 | - * |
|
| 522 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 523 | - * @param int $stackPtr The position of the function call token. |
|
| 524 | - * @param int $paramOffset The 1-based index position of the parameter to retrieve. |
|
| 525 | - * |
|
| 526 | - * @return array|false |
|
| 527 | - */ |
|
| 528 | - public function getFunctionCallParameter(File $phpcsFile, $stackPtr, $paramOffset) |
|
| 529 | - { |
|
| 530 | - $parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr); |
|
| 531 | - |
|
| 532 | - if (isset($parameters[$paramOffset]) === false) { |
|
| 533 | - return false; |
|
| 534 | - } else { |
|
| 535 | - return $parameters[$paramOffset]; |
|
| 536 | - } |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - |
|
| 540 | - /** |
|
| 541 | - * Verify whether a token is within a scoped condition. |
|
| 542 | - * |
|
| 543 | - * If the optional $validScopes parameter has been passed, the function |
|
| 544 | - * will check that the token has at least one condition which is of a |
|
| 545 | - * type defined in $validScopes. |
|
| 546 | - * |
|
| 547 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 548 | - * @param int $stackPtr The position of the token. |
|
| 549 | - * @param array|int $validScopes Optional. Array of valid scopes |
|
| 550 | - * or int value of a valid scope. |
|
| 551 | - * Pass the T_.. constant(s) for the |
|
| 552 | - * desired scope to this parameter. |
|
| 553 | - * |
|
| 554 | - * @return bool Without the optional $scopeTypes: True if within a scope, false otherwise. |
|
| 555 | - * If the $scopeTypes are set: True if *one* of the conditions is a |
|
| 556 | - * valid scope, false otherwise. |
|
| 557 | - */ |
|
| 558 | - public function tokenHasScope(File $phpcsFile, $stackPtr, $validScopes = null) |
|
| 559 | - { |
|
| 560 | - $tokens = $phpcsFile->getTokens(); |
|
| 561 | - |
|
| 562 | - // Check for the existence of the token. |
|
| 563 | - if (isset($tokens[$stackPtr]) === false) { |
|
| 564 | - return false; |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - // No conditions = no scope. |
|
| 568 | - if (empty($tokens[$stackPtr]['conditions'])) { |
|
| 569 | - return false; |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - // Ok, there are conditions, do we have to check for specific ones ? |
|
| 573 | - if (isset($validScopes) === false) { |
|
| 574 | - return true; |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - return $phpcsFile->hasCondition($stackPtr, $validScopes); |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - |
|
| 581 | - /** |
|
| 582 | - * Verify whether a token is within a class scope. |
|
| 583 | - * |
|
| 584 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 585 | - * @param int $stackPtr The position of the token. |
|
| 586 | - * @param bool $strict Whether to strictly check for the T_CLASS |
|
| 587 | - * scope or also accept interfaces and traits |
|
| 588 | - * as scope. |
|
| 589 | - * |
|
| 590 | - * @return bool True if within class scope, false otherwise. |
|
| 591 | - */ |
|
| 592 | - public function inClassScope(File $phpcsFile, $stackPtr, $strict = true) |
|
| 593 | - { |
|
| 594 | - $validScopes = array(\T_CLASS); |
|
| 595 | - if (\defined('T_ANON_CLASS') === true) { |
|
| 596 | - $validScopes[] = \T_ANON_CLASS; |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - if ($strict === false) { |
|
| 600 | - $validScopes[] = \T_INTERFACE; |
|
| 601 | - $validScopes[] = \T_TRAIT; |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - return $phpcsFile->hasCondition($stackPtr, $validScopes); |
|
| 605 | - } |
|
| 606 | - |
|
| 607 | - |
|
| 608 | - /** |
|
| 609 | - * Returns the fully qualified class name for a new class instantiation. |
|
| 610 | - * |
|
| 611 | - * Returns an empty string if the class name could not be reliably inferred. |
|
| 612 | - * |
|
| 613 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 614 | - * @param int $stackPtr The position of a T_NEW token. |
|
| 615 | - * |
|
| 616 | - * @return string |
|
| 617 | - */ |
|
| 618 | - public function getFQClassNameFromNewToken(File $phpcsFile, $stackPtr) |
|
| 619 | - { |
|
| 620 | - $tokens = $phpcsFile->getTokens(); |
|
| 621 | - |
|
| 622 | - // Check for the existence of the token. |
|
| 623 | - if (isset($tokens[$stackPtr]) === false) { |
|
| 624 | - return ''; |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - if ($tokens[$stackPtr]['code'] !== \T_NEW) { |
|
| 628 | - return ''; |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - $start = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
|
| 632 | - if ($start === false) { |
|
| 633 | - return ''; |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - // Bow out if the next token is a variable as we don't know where it was defined. |
|
| 637 | - if ($tokens[$start]['code'] === \T_VARIABLE) { |
|
| 638 | - return ''; |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - // Bow out if the next token is the class keyword. |
|
| 642 | - if ($tokens[$start]['type'] === 'T_ANON_CLASS' || $tokens[$start]['code'] === \T_CLASS) { |
|
| 643 | - return ''; |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - $find = array( |
|
| 647 | - \T_NS_SEPARATOR, |
|
| 648 | - \T_STRING, |
|
| 649 | - \T_NAMESPACE, |
|
| 650 | - \T_WHITESPACE, |
|
| 651 | - ); |
|
| 652 | - |
|
| 653 | - $end = $phpcsFile->findNext($find, ($start + 1), null, true, null, true); |
|
| 654 | - $className = $phpcsFile->getTokensAsString($start, ($end - $start)); |
|
| 655 | - $className = trim($className); |
|
| 656 | - |
|
| 657 | - return $this->getFQName($phpcsFile, $stackPtr, $className); |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - |
|
| 661 | - /** |
|
| 662 | - * Returns the fully qualified name of the class that the specified class extends. |
|
| 663 | - * |
|
| 664 | - * Returns an empty string if the class does not extend another class or if |
|
| 665 | - * the class name could not be reliably inferred. |
|
| 666 | - * |
|
| 667 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 668 | - * @param int $stackPtr The position of a T_CLASS token. |
|
| 669 | - * |
|
| 670 | - * @return string |
|
| 671 | - */ |
|
| 672 | - public function getFQExtendedClassName(File $phpcsFile, $stackPtr) |
|
| 673 | - { |
|
| 674 | - $tokens = $phpcsFile->getTokens(); |
|
| 675 | - |
|
| 676 | - // Check for the existence of the token. |
|
| 677 | - if (isset($tokens[$stackPtr]) === false) { |
|
| 678 | - return ''; |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - if ($tokens[$stackPtr]['code'] !== \T_CLASS |
|
| 682 | - && $tokens[$stackPtr]['type'] !== 'T_ANON_CLASS' |
|
| 683 | - && $tokens[$stackPtr]['type'] !== 'T_INTERFACE' |
|
| 684 | - ) { |
|
| 685 | - return ''; |
|
| 686 | - } |
|
| 687 | - |
|
| 688 | - $extends = PHPCSHelper::findExtendedClassName($phpcsFile, $stackPtr); |
|
| 689 | - if (empty($extends) || \is_string($extends) === false) { |
|
| 690 | - return ''; |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - return $this->getFQName($phpcsFile, $stackPtr, $extends); |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - |
|
| 697 | - /** |
|
| 698 | - * Returns the class name for the static usage of a class. |
|
| 699 | - * This can be a call to a method, the use of a property or constant. |
|
| 700 | - * |
|
| 701 | - * Returns an empty string if the class name could not be reliably inferred. |
|
| 702 | - * |
|
| 703 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 704 | - * @param int $stackPtr The position of a T_NEW token. |
|
| 705 | - * |
|
| 706 | - * @return string |
|
| 707 | - */ |
|
| 708 | - public function getFQClassNameFromDoubleColonToken(File $phpcsFile, $stackPtr) |
|
| 709 | - { |
|
| 710 | - $tokens = $phpcsFile->getTokens(); |
|
| 711 | - |
|
| 712 | - // Check for the existence of the token. |
|
| 713 | - if (isset($tokens[$stackPtr]) === false) { |
|
| 714 | - return ''; |
|
| 715 | - } |
|
| 716 | - |
|
| 717 | - if ($tokens[$stackPtr]['code'] !== \T_DOUBLE_COLON) { |
|
| 718 | - return ''; |
|
| 719 | - } |
|
| 720 | - |
|
| 721 | - // Nothing to do if previous token is a variable as we don't know where it was defined. |
|
| 722 | - if ($tokens[$stackPtr - 1]['code'] === \T_VARIABLE) { |
|
| 723 | - return ''; |
|
| 724 | - } |
|
| 725 | - |
|
| 726 | - // Nothing to do if 'parent' or 'static' as we don't know how far the class tree extends. |
|
| 727 | - if (\in_array($tokens[$stackPtr - 1]['code'], array(\T_PARENT, \T_STATIC), true)) { |
|
| 728 | - return ''; |
|
| 729 | - } |
|
| 730 | - |
|
| 731 | - // Get the classname from the class declaration if self is used. |
|
| 732 | - if ($tokens[$stackPtr - 1]['code'] === \T_SELF) { |
|
| 733 | - $classDeclarationPtr = $phpcsFile->findPrevious(\T_CLASS, $stackPtr - 1); |
|
| 734 | - if ($classDeclarationPtr === false) { |
|
| 735 | - return ''; |
|
| 736 | - } |
|
| 737 | - $className = $phpcsFile->getDeclarationName($classDeclarationPtr); |
|
| 738 | - return $this->getFQName($phpcsFile, $classDeclarationPtr, $className); |
|
| 739 | - } |
|
| 740 | - |
|
| 741 | - $find = array( |
|
| 742 | - \T_NS_SEPARATOR, |
|
| 743 | - \T_STRING, |
|
| 744 | - \T_NAMESPACE, |
|
| 745 | - \T_WHITESPACE, |
|
| 746 | - ); |
|
| 747 | - |
|
| 748 | - $start = $phpcsFile->findPrevious($find, $stackPtr - 1, null, true, null, true); |
|
| 749 | - if ($start === false || isset($tokens[($start + 1)]) === false) { |
|
| 750 | - return ''; |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - $start = ($start + 1); |
|
| 754 | - $className = $phpcsFile->getTokensAsString($start, ($stackPtr - $start)); |
|
| 755 | - $className = trim($className); |
|
| 756 | - |
|
| 757 | - return $this->getFQName($phpcsFile, $stackPtr, $className); |
|
| 758 | - } |
|
| 759 | - |
|
| 760 | - |
|
| 761 | - /** |
|
| 762 | - * Get the Fully Qualified name for a class/function/constant etc. |
|
| 763 | - * |
|
| 764 | - * Checks if a class/function/constant name is already fully qualified and |
|
| 765 | - * if not, enrich it with the relevant namespace information. |
|
| 766 | - * |
|
| 767 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 768 | - * @param int $stackPtr The position of the token. |
|
| 769 | - * @param string $name The class / function / constant name. |
|
| 770 | - * |
|
| 771 | - * @return string |
|
| 772 | - */ |
|
| 773 | - public function getFQName(File $phpcsFile, $stackPtr, $name) |
|
| 774 | - { |
|
| 775 | - if (strpos($name, '\\') === 0) { |
|
| 776 | - // Already fully qualified. |
|
| 777 | - return $name; |
|
| 778 | - } |
|
| 779 | - |
|
| 780 | - // Remove the namespace keyword if used. |
|
| 781 | - if (strpos($name, 'namespace\\') === 0) { |
|
| 782 | - $name = substr($name, 10); |
|
| 783 | - } |
|
| 784 | - |
|
| 785 | - $namespace = $this->determineNamespace($phpcsFile, $stackPtr); |
|
| 786 | - |
|
| 787 | - if ($namespace === '') { |
|
| 788 | - return '\\' . $name; |
|
| 789 | - } else { |
|
| 790 | - return '\\' . $namespace . '\\' . $name; |
|
| 791 | - } |
|
| 792 | - } |
|
| 793 | - |
|
| 794 | - |
|
| 795 | - /** |
|
| 796 | - * Is the class/function/constant name namespaced or global ? |
|
| 797 | - * |
|
| 798 | - * @param string $FQName Fully Qualified name of a class, function etc. |
|
| 799 | - * I.e. should always start with a `\`. |
|
| 800 | - * |
|
| 801 | - * @return bool True if namespaced, false if global. |
|
| 802 | - */ |
|
| 803 | - public function isNamespaced($FQName) |
|
| 804 | - { |
|
| 805 | - if (strpos($FQName, '\\') !== 0) { |
|
| 806 | - throw new PHPCS_Exception('$FQName must be a fully qualified name'); |
|
| 807 | - } |
|
| 808 | - |
|
| 809 | - return (strpos(substr($FQName, 1), '\\') !== false); |
|
| 810 | - } |
|
| 811 | - |
|
| 812 | - |
|
| 813 | - /** |
|
| 814 | - * Determine the namespace name an arbitrary token lives in. |
|
| 815 | - * |
|
| 816 | - * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
|
| 817 | - * @param int $stackPtr The token position for which to determine the namespace. |
|
| 818 | - * |
|
| 819 | - * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
|
| 820 | - */ |
|
| 821 | - public function determineNamespace(File $phpcsFile, $stackPtr) |
|
| 822 | - { |
|
| 823 | - $tokens = $phpcsFile->getTokens(); |
|
| 824 | - |
|
| 825 | - // Check for the existence of the token. |
|
| 826 | - if (isset($tokens[$stackPtr]) === false) { |
|
| 827 | - return ''; |
|
| 828 | - } |
|
| 829 | - |
|
| 830 | - // Check for scoped namespace {}. |
|
| 831 | - if (empty($tokens[$stackPtr]['conditions']) === false) { |
|
| 832 | - $namespacePtr = $phpcsFile->getCondition($stackPtr, \T_NAMESPACE); |
|
| 833 | - if ($namespacePtr !== false) { |
|
| 834 | - $namespace = $this->getDeclaredNamespaceName($phpcsFile, $namespacePtr); |
|
| 835 | - if ($namespace !== false) { |
|
| 836 | - return $namespace; |
|
| 837 | - } |
|
| 838 | - |
|
| 839 | - // We are in a scoped namespace, but couldn't determine the name. Searching for a global namespace is futile. |
|
| 840 | - return ''; |
|
| 841 | - } |
|
| 842 | - } |
|
| 843 | - |
|
| 844 | - /* |
|
| 30 | + const REGEX_COMPLEX_VARS = '`(?:(\{)?(?<!\\\\)\$)?(\{)?(?<!\\\\)\$(\{)?(?P<varname>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(?:->\$?(?P>varname)|\[[^\]]+\]|::\$?(?P>varname)|\([^\)]*\))*(?(3)\}|)(?(2)\}|)(?(1)\}|)`'; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * List of superglobals as an array of strings. |
|
| 34 | + * |
|
| 35 | + * Used by the ParameterShadowSuperGlobals and ForbiddenClosureUseVariableNames sniffs. |
|
| 36 | + * |
|
| 37 | + * @var array |
|
| 38 | + */ |
|
| 39 | + protected $superglobals = array( |
|
| 40 | + '$GLOBALS' => true, |
|
| 41 | + '$_SERVER' => true, |
|
| 42 | + '$_GET' => true, |
|
| 43 | + '$_POST' => true, |
|
| 44 | + '$_FILES' => true, |
|
| 45 | + '$_COOKIE' => true, |
|
| 46 | + '$_SESSION' => true, |
|
| 47 | + '$_REQUEST' => true, |
|
| 48 | + '$_ENV' => true, |
|
| 49 | + ); |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * List of functions using hash algorithm as parameter (always the first parameter). |
|
| 53 | + * |
|
| 54 | + * Used by the new/removed hash algorithm sniffs. |
|
| 55 | + * Key is the function name, value is the 1-based parameter position in the function call. |
|
| 56 | + * |
|
| 57 | + * @var array |
|
| 58 | + */ |
|
| 59 | + protected $hashAlgoFunctions = array( |
|
| 60 | + 'hash_file' => 1, |
|
| 61 | + 'hash_hmac_file' => 1, |
|
| 62 | + 'hash_hmac' => 1, |
|
| 63 | + 'hash_init' => 1, |
|
| 64 | + 'hash_pbkdf2' => 1, |
|
| 65 | + 'hash' => 1, |
|
| 66 | + ); |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * List of functions which take an ini directive as parameter (always the first parameter). |
|
| 71 | + * |
|
| 72 | + * Used by the new/removed ini directives sniffs. |
|
| 73 | + * Key is the function name, value is the 1-based parameter position in the function call. |
|
| 74 | + * |
|
| 75 | + * @var array |
|
| 76 | + */ |
|
| 77 | + protected $iniFunctions = array( |
|
| 78 | + 'ini_get' => 1, |
|
| 79 | + 'ini_set' => 1, |
|
| 80 | + ); |
|
| 81 | + |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Get the testVersion configuration variable. |
|
| 85 | + * |
|
| 86 | + * The testVersion configuration variable may be in any of the following formats: |
|
| 87 | + * 1) Omitted/empty, in which case no version is specified. This effectively |
|
| 88 | + * disables all the checks for new PHP features provided by this standard. |
|
| 89 | + * 2) A single PHP version number, e.g. "5.4" in which case the standard checks that |
|
| 90 | + * the code will run on that version of PHP (no deprecated features or newer |
|
| 91 | + * features being used). |
|
| 92 | + * 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run |
|
| 93 | + * on all PHP versions in that range, and that it doesn't use any features that |
|
| 94 | + * were deprecated by the final version in the list, or which were not available |
|
| 95 | + * for the first version in the list. |
|
| 96 | + * We accept ranges where one of the components is missing, e.g. "-5.6" means |
|
| 97 | + * all versions up to PHP 5.6, and "7.0-" means all versions above PHP 7.0. |
|
| 98 | + * PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2" |
|
| 99 | + * would be treated as invalid, and ignored. |
|
| 100 | + * |
|
| 101 | + * @return array $arrTestVersions will hold an array containing min/max version |
|
| 102 | + * of PHP that we are checking against (see above). If only a |
|
| 103 | + * single version number is specified, then this is used as |
|
| 104 | + * both the min and max. |
|
| 105 | + * |
|
| 106 | + * @throws \PHP_CodeSniffer_Exception If testVersion is invalid. |
|
| 107 | + */ |
|
| 108 | + private function getTestVersion() |
|
| 109 | + { |
|
| 110 | + static $arrTestVersions = array(); |
|
| 111 | + |
|
| 112 | + $default = array(null, null); |
|
| 113 | + $testVersion = trim(PHPCSHelper::getConfigData('testVersion')); |
|
| 114 | + |
|
| 115 | + if (empty($testVersion) === false && isset($arrTestVersions[$testVersion]) === false) { |
|
| 116 | + |
|
| 117 | + $arrTestVersions[$testVersion] = $default; |
|
| 118 | + |
|
| 119 | + if (preg_match('`^\d+\.\d+$`', $testVersion)) { |
|
| 120 | + $arrTestVersions[$testVersion] = array($testVersion, $testVersion); |
|
| 121 | + return $arrTestVersions[$testVersion]; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + if (preg_match('`^(\d+\.\d+)?\s*-\s*(\d+\.\d+)?$`', $testVersion, $matches)) { |
|
| 125 | + if (empty($matches[1]) === false || empty($matches[2]) === false) { |
|
| 126 | + // If no lower-limit is set, we set the min version to 4.0. |
|
| 127 | + // Whilst development focuses on PHP 5 and above, we also accept |
|
| 128 | + // sniffs for PHP 4, so we include that as the minimum. |
|
| 129 | + // (It makes no sense to support PHP 3 as this was effectively a |
|
| 130 | + // different language). |
|
| 131 | + $min = empty($matches[1]) ? '4.0' : $matches[1]; |
|
| 132 | + |
|
| 133 | + // If no upper-limit is set, we set the max version to 99.9. |
|
| 134 | + $max = empty($matches[2]) ? '99.9' : $matches[2]; |
|
| 135 | + |
|
| 136 | + if (version_compare($min, $max, '>')) { |
|
| 137 | + trigger_error( |
|
| 138 | + "Invalid range in testVersion setting: '" . $testVersion . "'", |
|
| 139 | + \E_USER_WARNING |
|
| 140 | + ); |
|
| 141 | + return $default; |
|
| 142 | + } else { |
|
| 143 | + $arrTestVersions[$testVersion] = array($min, $max); |
|
| 144 | + return $arrTestVersions[$testVersion]; |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + trigger_error( |
|
| 150 | + "Invalid testVersion setting: '" . $testVersion . "'", |
|
| 151 | + \E_USER_WARNING |
|
| 152 | + ); |
|
| 153 | + return $default; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + if (isset($arrTestVersions[$testVersion])) { |
|
| 157 | + return $arrTestVersions[$testVersion]; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + return $default; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Check whether a specific PHP version is equal to or higher than the maximum |
|
| 166 | + * supported PHP version as provided by the user in `testVersion`. |
|
| 167 | + * |
|
| 168 | + * Should be used when sniffing for *old* PHP features (deprecated/removed). |
|
| 169 | + * |
|
| 170 | + * @param string $phpVersion A PHP version number in 'major.minor' format. |
|
| 171 | + * |
|
| 172 | + * @return bool True if testVersion has not been provided or if the PHP version |
|
| 173 | + * is equal to or higher than the highest supported PHP version |
|
| 174 | + * in testVersion. False otherwise. |
|
| 175 | + */ |
|
| 176 | + public function supportsAbove($phpVersion) |
|
| 177 | + { |
|
| 178 | + $testVersion = $this->getTestVersion(); |
|
| 179 | + $testVersion = $testVersion[1]; |
|
| 180 | + |
|
| 181 | + if (\is_null($testVersion) |
|
| 182 | + || version_compare($testVersion, $phpVersion) >= 0 |
|
| 183 | + ) { |
|
| 184 | + return true; |
|
| 185 | + } else { |
|
| 186 | + return false; |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Check whether a specific PHP version is equal to or lower than the minimum |
|
| 193 | + * supported PHP version as provided by the user in `testVersion`. |
|
| 194 | + * |
|
| 195 | + * Should be used when sniffing for *new* PHP features. |
|
| 196 | + * |
|
| 197 | + * @param string $phpVersion A PHP version number in 'major.minor' format. |
|
| 198 | + * |
|
| 199 | + * @return bool True if the PHP version is equal to or lower than the lowest |
|
| 200 | + * supported PHP version in testVersion. |
|
| 201 | + * False otherwise or if no testVersion is provided. |
|
| 202 | + */ |
|
| 203 | + public function supportsBelow($phpVersion) |
|
| 204 | + { |
|
| 205 | + $testVersion = $this->getTestVersion(); |
|
| 206 | + $testVersion = $testVersion[0]; |
|
| 207 | + |
|
| 208 | + if (\is_null($testVersion) === false |
|
| 209 | + && version_compare($testVersion, $phpVersion) <= 0 |
|
| 210 | + ) { |
|
| 211 | + return true; |
|
| 212 | + } else { |
|
| 213 | + return false; |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Add a PHPCS message to the output stack as either a warning or an error. |
|
| 220 | + * |
|
| 221 | + * @param \PHP_CodeSniffer_File $phpcsFile The file the message applies to. |
|
| 222 | + * @param string $message The message. |
|
| 223 | + * @param int $stackPtr The position of the token |
|
| 224 | + * the message relates to. |
|
| 225 | + * @param bool $isError Whether to report the message as an |
|
| 226 | + * 'error' or 'warning'. |
|
| 227 | + * Defaults to true (error). |
|
| 228 | + * @param string $code The error code for the message. |
|
| 229 | + * Defaults to 'Found'. |
|
| 230 | + * @param array $data Optional input for the data replacements. |
|
| 231 | + * |
|
| 232 | + * @return void |
|
| 233 | + */ |
|
| 234 | + public function addMessage(File $phpcsFile, $message, $stackPtr, $isError, $code = 'Found', $data = array()) |
|
| 235 | + { |
|
| 236 | + if ($isError === true) { |
|
| 237 | + $phpcsFile->addError($message, $stackPtr, $code, $data); |
|
| 238 | + } else { |
|
| 239 | + $phpcsFile->addWarning($message, $stackPtr, $code, $data); |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Convert an arbitrary string to an alphanumeric string with underscores. |
|
| 246 | + * |
|
| 247 | + * Pre-empt issues with arbitrary strings being used as error codes in XML and PHP. |
|
| 248 | + * |
|
| 249 | + * @param string $baseString Arbitrary string. |
|
| 250 | + * |
|
| 251 | + * @return string |
|
| 252 | + */ |
|
| 253 | + public function stringToErrorCode($baseString) |
|
| 254 | + { |
|
| 255 | + return preg_replace('`[^a-z0-9_]`i', '_', strtolower($baseString)); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * Strip quotes surrounding an arbitrary string. |
|
| 261 | + * |
|
| 262 | + * Intended for use with the contents of a T_CONSTANT_ENCAPSED_STRING / T_DOUBLE_QUOTED_STRING. |
|
| 263 | + * |
|
| 264 | + * @param string $string The raw string. |
|
| 265 | + * |
|
| 266 | + * @return string String without quotes around it. |
|
| 267 | + */ |
|
| 268 | + public function stripQuotes($string) |
|
| 269 | + { |
|
| 270 | + return preg_replace('`^([\'"])(.*)\1$`Ds', '$2', $string); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Strip variables from an arbitrary double quoted string. |
|
| 276 | + * |
|
| 277 | + * Intended for use with the contents of a T_DOUBLE_QUOTED_STRING. |
|
| 278 | + * |
|
| 279 | + * @param string $string The raw string. |
|
| 280 | + * |
|
| 281 | + * @return string String without variables in it. |
|
| 282 | + */ |
|
| 283 | + public function stripVariables($string) |
|
| 284 | + { |
|
| 285 | + if (strpos($string, '$') === false) { |
|
| 286 | + return $string; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + return preg_replace(self::REGEX_COMPLEX_VARS, '', $string); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * Make all top level array keys in an array lowercase. |
|
| 295 | + * |
|
| 296 | + * @param array $array Initial array. |
|
| 297 | + * |
|
| 298 | + * @return array Same array, but with all lowercase top level keys. |
|
| 299 | + */ |
|
| 300 | + public function arrayKeysToLowercase($array) |
|
| 301 | + { |
|
| 302 | + return array_change_key_case($array, \CASE_LOWER); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Checks if a function call has parameters. |
|
| 308 | + * |
|
| 309 | + * Expects to be passed the T_STRING or T_VARIABLE stack pointer for the function call. |
|
| 310 | + * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
|
| 311 | + * |
|
| 312 | + * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, it |
|
| 313 | + * will detect whether the array has values or is empty. |
|
| 314 | + * |
|
| 315 | + * @link https://github.com/PHPCompatibility/PHPCompatibility/issues/120 |
|
| 316 | + * @link https://github.com/PHPCompatibility/PHPCompatibility/issues/152 |
|
| 317 | + * |
|
| 318 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 319 | + * @param int $stackPtr The position of the function call token. |
|
| 320 | + * |
|
| 321 | + * @return bool |
|
| 322 | + */ |
|
| 323 | + public function doesFunctionCallHaveParameters(File $phpcsFile, $stackPtr) |
|
| 324 | + { |
|
| 325 | + $tokens = $phpcsFile->getTokens(); |
|
| 326 | + |
|
| 327 | + // Check for the existence of the token. |
|
| 328 | + if (isset($tokens[$stackPtr]) === false) { |
|
| 329 | + return false; |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + // Is this one of the tokens this function handles ? |
|
| 333 | + if (\in_array($tokens[$stackPtr]['code'], array(\T_STRING, \T_ARRAY, \T_OPEN_SHORT_ARRAY, \T_VARIABLE), true) === false) { |
|
| 334 | + return false; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
|
| 338 | + |
|
| 339 | + // Deal with short array syntax. |
|
| 340 | + if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY) { |
|
| 341 | + if (isset($tokens[$stackPtr]['bracket_closer']) === false) { |
|
| 342 | + return false; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + if ($nextNonEmpty === $tokens[$stackPtr]['bracket_closer']) { |
|
| 346 | + // No parameters. |
|
| 347 | + return false; |
|
| 348 | + } else { |
|
| 349 | + return true; |
|
| 350 | + } |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + // Deal with function calls & long arrays. |
|
| 354 | + // Next non-empty token should be the open parenthesis. |
|
| 355 | + if ($nextNonEmpty === false && $tokens[$nextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS) { |
|
| 356 | + return false; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + if (isset($tokens[$nextNonEmpty]['parenthesis_closer']) === false) { |
|
| 360 | + return false; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + $closeParenthesis = $tokens[$nextNonEmpty]['parenthesis_closer']; |
|
| 364 | + $nextNextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $nextNonEmpty + 1, $closeParenthesis + 1, true); |
|
| 365 | + |
|
| 366 | + if ($nextNextNonEmpty === $closeParenthesis) { |
|
| 367 | + // No parameters. |
|
| 368 | + return false; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + return true; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Count the number of parameters a function call has been passed. |
|
| 377 | + * |
|
| 378 | + * Expects to be passed the T_STRING or T_VARIABLE stack pointer for the function call. |
|
| 379 | + * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
|
| 380 | + * |
|
| 381 | + * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
|
| 382 | + * it will return the number of values in the array. |
|
| 383 | + * |
|
| 384 | + * @link https://github.com/PHPCompatibility/PHPCompatibility/issues/111 |
|
| 385 | + * @link https://github.com/PHPCompatibility/PHPCompatibility/issues/114 |
|
| 386 | + * @link https://github.com/PHPCompatibility/PHPCompatibility/issues/151 |
|
| 387 | + * |
|
| 388 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 389 | + * @param int $stackPtr The position of the function call token. |
|
| 390 | + * |
|
| 391 | + * @return int |
|
| 392 | + */ |
|
| 393 | + public function getFunctionCallParameterCount(File $phpcsFile, $stackPtr) |
|
| 394 | + { |
|
| 395 | + if ($this->doesFunctionCallHaveParameters($phpcsFile, $stackPtr) === false) { |
|
| 396 | + return 0; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + return \count($this->getFunctionCallParameters($phpcsFile, $stackPtr)); |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * Get information on all parameters passed to a function call. |
|
| 405 | + * |
|
| 406 | + * Expects to be passed the T_STRING or T_VARIABLE stack pointer for the function call. |
|
| 407 | + * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
|
| 408 | + * |
|
| 409 | + * Will return an multi-dimentional array with the start token pointer, end token |
|
| 410 | + * pointer and raw parameter value for all parameters. Index will be 1-based. |
|
| 411 | + * If no parameters are found, will return an empty array. |
|
| 412 | + * |
|
| 413 | + * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
|
| 414 | + * it will tokenize the values / key/value pairs contained in the array call. |
|
| 415 | + * |
|
| 416 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 417 | + * @param int $stackPtr The position of the function call token. |
|
| 418 | + * |
|
| 419 | + * @return array |
|
| 420 | + */ |
|
| 421 | + public function getFunctionCallParameters(File $phpcsFile, $stackPtr) |
|
| 422 | + { |
|
| 423 | + if ($this->doesFunctionCallHaveParameters($phpcsFile, $stackPtr) === false) { |
|
| 424 | + return array(); |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + // Ok, we know we have a T_STRING, T_VARIABLE, T_ARRAY or T_OPEN_SHORT_ARRAY with parameters |
|
| 428 | + // and valid open & close brackets/parenthesis. |
|
| 429 | + $tokens = $phpcsFile->getTokens(); |
|
| 430 | + |
|
| 431 | + // Mark the beginning and end tokens. |
|
| 432 | + if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY) { |
|
| 433 | + $opener = $stackPtr; |
|
| 434 | + $closer = $tokens[$stackPtr]['bracket_closer']; |
|
| 435 | + |
|
| 436 | + $nestedParenthesisCount = 0; |
|
| 437 | + |
|
| 438 | + } else { |
|
| 439 | + $opener = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
|
| 440 | + $closer = $tokens[$opener]['parenthesis_closer']; |
|
| 441 | + |
|
| 442 | + $nestedParenthesisCount = 1; |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + // Which nesting level is the one we are interested in ? |
|
| 446 | + if (isset($tokens[$opener]['nested_parenthesis'])) { |
|
| 447 | + $nestedParenthesisCount += \count($tokens[$opener]['nested_parenthesis']); |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + $parameters = array(); |
|
| 451 | + $nextComma = $opener; |
|
| 452 | + $paramStart = $opener + 1; |
|
| 453 | + $cnt = 1; |
|
| 454 | + while (($nextComma = $phpcsFile->findNext(array(\T_COMMA, $tokens[$closer]['code'], \T_OPEN_SHORT_ARRAY, \T_CLOSURE), $nextComma + 1, $closer + 1)) !== false) { |
|
| 455 | + // Ignore anything within short array definition brackets. |
|
| 456 | + if ($tokens[$nextComma]['type'] === 'T_OPEN_SHORT_ARRAY' |
|
| 457 | + && (isset($tokens[$nextComma]['bracket_opener']) |
|
| 458 | + && $tokens[$nextComma]['bracket_opener'] === $nextComma) |
|
| 459 | + && isset($tokens[$nextComma]['bracket_closer']) |
|
| 460 | + ) { |
|
| 461 | + // Skip forward to the end of the short array definition. |
|
| 462 | + $nextComma = $tokens[$nextComma]['bracket_closer']; |
|
| 463 | + continue; |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + // Skip past closures passed as function parameters. |
|
| 467 | + if ($tokens[$nextComma]['type'] === 'T_CLOSURE' |
|
| 468 | + && (isset($tokens[$nextComma]['scope_condition']) |
|
| 469 | + && $tokens[$nextComma]['scope_condition'] === $nextComma) |
|
| 470 | + && isset($tokens[$nextComma]['scope_closer']) |
|
| 471 | + ) { |
|
| 472 | + // Skip forward to the end of the closure declaration. |
|
| 473 | + $nextComma = $tokens[$nextComma]['scope_closer']; |
|
| 474 | + continue; |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + // Ignore comma's at a lower nesting level. |
|
| 478 | + if ($tokens[$nextComma]['type'] === 'T_COMMA' |
|
| 479 | + && isset($tokens[$nextComma]['nested_parenthesis']) |
|
| 480 | + && \count($tokens[$nextComma]['nested_parenthesis']) !== $nestedParenthesisCount |
|
| 481 | + ) { |
|
| 482 | + continue; |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + // Ignore closing parenthesis/bracket if not 'ours'. |
|
| 486 | + if ($tokens[$nextComma]['type'] === $tokens[$closer]['type'] && $nextComma !== $closer) { |
|
| 487 | + continue; |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + // Ok, we've reached the end of the parameter. |
|
| 491 | + $parameters[$cnt]['start'] = $paramStart; |
|
| 492 | + $parameters[$cnt]['end'] = $nextComma - 1; |
|
| 493 | + $parameters[$cnt]['raw'] = trim($phpcsFile->getTokensAsString($paramStart, ($nextComma - $paramStart))); |
|
| 494 | + |
|
| 495 | + // Check if there are more tokens before the closing parenthesis. |
|
| 496 | + // Prevents code like the following from setting a third parameter: |
|
| 497 | + // functionCall( $param1, $param2, ); |
|
| 498 | + $hasNextParam = $phpcsFile->findNext(Tokens::$emptyTokens, $nextComma + 1, $closer, true, null, true); |
|
| 499 | + if ($hasNextParam === false) { |
|
| 500 | + break; |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + // Prepare for the next parameter. |
|
| 504 | + $paramStart = $nextComma + 1; |
|
| 505 | + $cnt++; |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + return $parameters; |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + |
|
| 512 | + /** |
|
| 513 | + * Get information on a specific parameter passed to a function call. |
|
| 514 | + * |
|
| 515 | + * Expects to be passed the T_STRING or T_VARIABLE stack pointer for the function call. |
|
| 516 | + * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
|
| 517 | + * |
|
| 518 | + * Will return a array with the start token pointer, end token pointer and the raw value |
|
| 519 | + * of the parameter at a specific offset. |
|
| 520 | + * If the specified parameter is not found, will return false. |
|
| 521 | + * |
|
| 522 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 523 | + * @param int $stackPtr The position of the function call token. |
|
| 524 | + * @param int $paramOffset The 1-based index position of the parameter to retrieve. |
|
| 525 | + * |
|
| 526 | + * @return array|false |
|
| 527 | + */ |
|
| 528 | + public function getFunctionCallParameter(File $phpcsFile, $stackPtr, $paramOffset) |
|
| 529 | + { |
|
| 530 | + $parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr); |
|
| 531 | + |
|
| 532 | + if (isset($parameters[$paramOffset]) === false) { |
|
| 533 | + return false; |
|
| 534 | + } else { |
|
| 535 | + return $parameters[$paramOffset]; |
|
| 536 | + } |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + |
|
| 540 | + /** |
|
| 541 | + * Verify whether a token is within a scoped condition. |
|
| 542 | + * |
|
| 543 | + * If the optional $validScopes parameter has been passed, the function |
|
| 544 | + * will check that the token has at least one condition which is of a |
|
| 545 | + * type defined in $validScopes. |
|
| 546 | + * |
|
| 547 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 548 | + * @param int $stackPtr The position of the token. |
|
| 549 | + * @param array|int $validScopes Optional. Array of valid scopes |
|
| 550 | + * or int value of a valid scope. |
|
| 551 | + * Pass the T_.. constant(s) for the |
|
| 552 | + * desired scope to this parameter. |
|
| 553 | + * |
|
| 554 | + * @return bool Without the optional $scopeTypes: True if within a scope, false otherwise. |
|
| 555 | + * If the $scopeTypes are set: True if *one* of the conditions is a |
|
| 556 | + * valid scope, false otherwise. |
|
| 557 | + */ |
|
| 558 | + public function tokenHasScope(File $phpcsFile, $stackPtr, $validScopes = null) |
|
| 559 | + { |
|
| 560 | + $tokens = $phpcsFile->getTokens(); |
|
| 561 | + |
|
| 562 | + // Check for the existence of the token. |
|
| 563 | + if (isset($tokens[$stackPtr]) === false) { |
|
| 564 | + return false; |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + // No conditions = no scope. |
|
| 568 | + if (empty($tokens[$stackPtr]['conditions'])) { |
|
| 569 | + return false; |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + // Ok, there are conditions, do we have to check for specific ones ? |
|
| 573 | + if (isset($validScopes) === false) { |
|
| 574 | + return true; |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + return $phpcsFile->hasCondition($stackPtr, $validScopes); |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + |
|
| 581 | + /** |
|
| 582 | + * Verify whether a token is within a class scope. |
|
| 583 | + * |
|
| 584 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 585 | + * @param int $stackPtr The position of the token. |
|
| 586 | + * @param bool $strict Whether to strictly check for the T_CLASS |
|
| 587 | + * scope or also accept interfaces and traits |
|
| 588 | + * as scope. |
|
| 589 | + * |
|
| 590 | + * @return bool True if within class scope, false otherwise. |
|
| 591 | + */ |
|
| 592 | + public function inClassScope(File $phpcsFile, $stackPtr, $strict = true) |
|
| 593 | + { |
|
| 594 | + $validScopes = array(\T_CLASS); |
|
| 595 | + if (\defined('T_ANON_CLASS') === true) { |
|
| 596 | + $validScopes[] = \T_ANON_CLASS; |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + if ($strict === false) { |
|
| 600 | + $validScopes[] = \T_INTERFACE; |
|
| 601 | + $validScopes[] = \T_TRAIT; |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + return $phpcsFile->hasCondition($stackPtr, $validScopes); |
|
| 605 | + } |
|
| 606 | + |
|
| 607 | + |
|
| 608 | + /** |
|
| 609 | + * Returns the fully qualified class name for a new class instantiation. |
|
| 610 | + * |
|
| 611 | + * Returns an empty string if the class name could not be reliably inferred. |
|
| 612 | + * |
|
| 613 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 614 | + * @param int $stackPtr The position of a T_NEW token. |
|
| 615 | + * |
|
| 616 | + * @return string |
|
| 617 | + */ |
|
| 618 | + public function getFQClassNameFromNewToken(File $phpcsFile, $stackPtr) |
|
| 619 | + { |
|
| 620 | + $tokens = $phpcsFile->getTokens(); |
|
| 621 | + |
|
| 622 | + // Check for the existence of the token. |
|
| 623 | + if (isset($tokens[$stackPtr]) === false) { |
|
| 624 | + return ''; |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + if ($tokens[$stackPtr]['code'] !== \T_NEW) { |
|
| 628 | + return ''; |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + $start = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
|
| 632 | + if ($start === false) { |
|
| 633 | + return ''; |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + // Bow out if the next token is a variable as we don't know where it was defined. |
|
| 637 | + if ($tokens[$start]['code'] === \T_VARIABLE) { |
|
| 638 | + return ''; |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + // Bow out if the next token is the class keyword. |
|
| 642 | + if ($tokens[$start]['type'] === 'T_ANON_CLASS' || $tokens[$start]['code'] === \T_CLASS) { |
|
| 643 | + return ''; |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + $find = array( |
|
| 647 | + \T_NS_SEPARATOR, |
|
| 648 | + \T_STRING, |
|
| 649 | + \T_NAMESPACE, |
|
| 650 | + \T_WHITESPACE, |
|
| 651 | + ); |
|
| 652 | + |
|
| 653 | + $end = $phpcsFile->findNext($find, ($start + 1), null, true, null, true); |
|
| 654 | + $className = $phpcsFile->getTokensAsString($start, ($end - $start)); |
|
| 655 | + $className = trim($className); |
|
| 656 | + |
|
| 657 | + return $this->getFQName($phpcsFile, $stackPtr, $className); |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + |
|
| 661 | + /** |
|
| 662 | + * Returns the fully qualified name of the class that the specified class extends. |
|
| 663 | + * |
|
| 664 | + * Returns an empty string if the class does not extend another class or if |
|
| 665 | + * the class name could not be reliably inferred. |
|
| 666 | + * |
|
| 667 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 668 | + * @param int $stackPtr The position of a T_CLASS token. |
|
| 669 | + * |
|
| 670 | + * @return string |
|
| 671 | + */ |
|
| 672 | + public function getFQExtendedClassName(File $phpcsFile, $stackPtr) |
|
| 673 | + { |
|
| 674 | + $tokens = $phpcsFile->getTokens(); |
|
| 675 | + |
|
| 676 | + // Check for the existence of the token. |
|
| 677 | + if (isset($tokens[$stackPtr]) === false) { |
|
| 678 | + return ''; |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + if ($tokens[$stackPtr]['code'] !== \T_CLASS |
|
| 682 | + && $tokens[$stackPtr]['type'] !== 'T_ANON_CLASS' |
|
| 683 | + && $tokens[$stackPtr]['type'] !== 'T_INTERFACE' |
|
| 684 | + ) { |
|
| 685 | + return ''; |
|
| 686 | + } |
|
| 687 | + |
|
| 688 | + $extends = PHPCSHelper::findExtendedClassName($phpcsFile, $stackPtr); |
|
| 689 | + if (empty($extends) || \is_string($extends) === false) { |
|
| 690 | + return ''; |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + return $this->getFQName($phpcsFile, $stackPtr, $extends); |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + |
|
| 697 | + /** |
|
| 698 | + * Returns the class name for the static usage of a class. |
|
| 699 | + * This can be a call to a method, the use of a property or constant. |
|
| 700 | + * |
|
| 701 | + * Returns an empty string if the class name could not be reliably inferred. |
|
| 702 | + * |
|
| 703 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 704 | + * @param int $stackPtr The position of a T_NEW token. |
|
| 705 | + * |
|
| 706 | + * @return string |
|
| 707 | + */ |
|
| 708 | + public function getFQClassNameFromDoubleColonToken(File $phpcsFile, $stackPtr) |
|
| 709 | + { |
|
| 710 | + $tokens = $phpcsFile->getTokens(); |
|
| 711 | + |
|
| 712 | + // Check for the existence of the token. |
|
| 713 | + if (isset($tokens[$stackPtr]) === false) { |
|
| 714 | + return ''; |
|
| 715 | + } |
|
| 716 | + |
|
| 717 | + if ($tokens[$stackPtr]['code'] !== \T_DOUBLE_COLON) { |
|
| 718 | + return ''; |
|
| 719 | + } |
|
| 720 | + |
|
| 721 | + // Nothing to do if previous token is a variable as we don't know where it was defined. |
|
| 722 | + if ($tokens[$stackPtr - 1]['code'] === \T_VARIABLE) { |
|
| 723 | + return ''; |
|
| 724 | + } |
|
| 725 | + |
|
| 726 | + // Nothing to do if 'parent' or 'static' as we don't know how far the class tree extends. |
|
| 727 | + if (\in_array($tokens[$stackPtr - 1]['code'], array(\T_PARENT, \T_STATIC), true)) { |
|
| 728 | + return ''; |
|
| 729 | + } |
|
| 730 | + |
|
| 731 | + // Get the classname from the class declaration if self is used. |
|
| 732 | + if ($tokens[$stackPtr - 1]['code'] === \T_SELF) { |
|
| 733 | + $classDeclarationPtr = $phpcsFile->findPrevious(\T_CLASS, $stackPtr - 1); |
|
| 734 | + if ($classDeclarationPtr === false) { |
|
| 735 | + return ''; |
|
| 736 | + } |
|
| 737 | + $className = $phpcsFile->getDeclarationName($classDeclarationPtr); |
|
| 738 | + return $this->getFQName($phpcsFile, $classDeclarationPtr, $className); |
|
| 739 | + } |
|
| 740 | + |
|
| 741 | + $find = array( |
|
| 742 | + \T_NS_SEPARATOR, |
|
| 743 | + \T_STRING, |
|
| 744 | + \T_NAMESPACE, |
|
| 745 | + \T_WHITESPACE, |
|
| 746 | + ); |
|
| 747 | + |
|
| 748 | + $start = $phpcsFile->findPrevious($find, $stackPtr - 1, null, true, null, true); |
|
| 749 | + if ($start === false || isset($tokens[($start + 1)]) === false) { |
|
| 750 | + return ''; |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + $start = ($start + 1); |
|
| 754 | + $className = $phpcsFile->getTokensAsString($start, ($stackPtr - $start)); |
|
| 755 | + $className = trim($className); |
|
| 756 | + |
|
| 757 | + return $this->getFQName($phpcsFile, $stackPtr, $className); |
|
| 758 | + } |
|
| 759 | + |
|
| 760 | + |
|
| 761 | + /** |
|
| 762 | + * Get the Fully Qualified name for a class/function/constant etc. |
|
| 763 | + * |
|
| 764 | + * Checks if a class/function/constant name is already fully qualified and |
|
| 765 | + * if not, enrich it with the relevant namespace information. |
|
| 766 | + * |
|
| 767 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 768 | + * @param int $stackPtr The position of the token. |
|
| 769 | + * @param string $name The class / function / constant name. |
|
| 770 | + * |
|
| 771 | + * @return string |
|
| 772 | + */ |
|
| 773 | + public function getFQName(File $phpcsFile, $stackPtr, $name) |
|
| 774 | + { |
|
| 775 | + if (strpos($name, '\\') === 0) { |
|
| 776 | + // Already fully qualified. |
|
| 777 | + return $name; |
|
| 778 | + } |
|
| 779 | + |
|
| 780 | + // Remove the namespace keyword if used. |
|
| 781 | + if (strpos($name, 'namespace\\') === 0) { |
|
| 782 | + $name = substr($name, 10); |
|
| 783 | + } |
|
| 784 | + |
|
| 785 | + $namespace = $this->determineNamespace($phpcsFile, $stackPtr); |
|
| 786 | + |
|
| 787 | + if ($namespace === '') { |
|
| 788 | + return '\\' . $name; |
|
| 789 | + } else { |
|
| 790 | + return '\\' . $namespace . '\\' . $name; |
|
| 791 | + } |
|
| 792 | + } |
|
| 793 | + |
|
| 794 | + |
|
| 795 | + /** |
|
| 796 | + * Is the class/function/constant name namespaced or global ? |
|
| 797 | + * |
|
| 798 | + * @param string $FQName Fully Qualified name of a class, function etc. |
|
| 799 | + * I.e. should always start with a `\`. |
|
| 800 | + * |
|
| 801 | + * @return bool True if namespaced, false if global. |
|
| 802 | + */ |
|
| 803 | + public function isNamespaced($FQName) |
|
| 804 | + { |
|
| 805 | + if (strpos($FQName, '\\') !== 0) { |
|
| 806 | + throw new PHPCS_Exception('$FQName must be a fully qualified name'); |
|
| 807 | + } |
|
| 808 | + |
|
| 809 | + return (strpos(substr($FQName, 1), '\\') !== false); |
|
| 810 | + } |
|
| 811 | + |
|
| 812 | + |
|
| 813 | + /** |
|
| 814 | + * Determine the namespace name an arbitrary token lives in. |
|
| 815 | + * |
|
| 816 | + * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
|
| 817 | + * @param int $stackPtr The token position for which to determine the namespace. |
|
| 818 | + * |
|
| 819 | + * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
|
| 820 | + */ |
|
| 821 | + public function determineNamespace(File $phpcsFile, $stackPtr) |
|
| 822 | + { |
|
| 823 | + $tokens = $phpcsFile->getTokens(); |
|
| 824 | + |
|
| 825 | + // Check for the existence of the token. |
|
| 826 | + if (isset($tokens[$stackPtr]) === false) { |
|
| 827 | + return ''; |
|
| 828 | + } |
|
| 829 | + |
|
| 830 | + // Check for scoped namespace {}. |
|
| 831 | + if (empty($tokens[$stackPtr]['conditions']) === false) { |
|
| 832 | + $namespacePtr = $phpcsFile->getCondition($stackPtr, \T_NAMESPACE); |
|
| 833 | + if ($namespacePtr !== false) { |
|
| 834 | + $namespace = $this->getDeclaredNamespaceName($phpcsFile, $namespacePtr); |
|
| 835 | + if ($namespace !== false) { |
|
| 836 | + return $namespace; |
|
| 837 | + } |
|
| 838 | + |
|
| 839 | + // We are in a scoped namespace, but couldn't determine the name. Searching for a global namespace is futile. |
|
| 840 | + return ''; |
|
| 841 | + } |
|
| 842 | + } |
|
| 843 | + |
|
| 844 | + /* |
|
| 845 | 845 | * Not in a scoped namespace, so let's see if we can find a non-scoped namespace instead. |
| 846 | 846 | * Keeping in mind that: |
| 847 | 847 | * - there can be multiple non-scoped namespaces in a file (bad practice, but it happens). |
| 848 | 848 | * - the namespace keyword can also be used as part of a function/method call and such. |
| 849 | 849 | * - that a non-named namespace resolves to the global namespace. |
| 850 | 850 | */ |
| 851 | - $previousNSToken = $stackPtr; |
|
| 852 | - $namespace = false; |
|
| 853 | - do { |
|
| 854 | - $previousNSToken = $phpcsFile->findPrevious(\T_NAMESPACE, ($previousNSToken - 1)); |
|
| 855 | - |
|
| 856 | - // Stop if we encounter a scoped namespace declaration as we already know we're not in one. |
|
| 857 | - if (empty($tokens[$previousNSToken]['scope_condition']) === false && $tokens[$previousNSToken]['scope_condition'] === $previousNSToken) { |
|
| 858 | - break; |
|
| 859 | - } |
|
| 860 | - |
|
| 861 | - $namespace = $this->getDeclaredNamespaceName($phpcsFile, $previousNSToken); |
|
| 862 | - |
|
| 863 | - } while ($namespace === false && $previousNSToken !== false); |
|
| 864 | - |
|
| 865 | - // If we still haven't got a namespace, return an empty string. |
|
| 866 | - if ($namespace === false) { |
|
| 867 | - return ''; |
|
| 868 | - } else { |
|
| 869 | - return $namespace; |
|
| 870 | - } |
|
| 871 | - } |
|
| 872 | - |
|
| 873 | - /** |
|
| 874 | - * Get the complete namespace name for a namespace declaration. |
|
| 875 | - * |
|
| 876 | - * For hierarchical namespaces, the name will be composed of several tokens, |
|
| 877 | - * i.e. MyProject\Sub\Level which will be returned together as one string. |
|
| 878 | - * |
|
| 879 | - * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
|
| 880 | - * @param int|bool $stackPtr The position of a T_NAMESPACE token. |
|
| 881 | - * |
|
| 882 | - * @return string|false Namespace name or false if not a namespace declaration. |
|
| 883 | - * Namespace name can be an empty string for global namespace declaration. |
|
| 884 | - */ |
|
| 885 | - public function getDeclaredNamespaceName(File $phpcsFile, $stackPtr) |
|
| 886 | - { |
|
| 887 | - $tokens = $phpcsFile->getTokens(); |
|
| 888 | - |
|
| 889 | - // Check for the existence of the token. |
|
| 890 | - if ($stackPtr === false || isset($tokens[$stackPtr]) === false) { |
|
| 891 | - return false; |
|
| 892 | - } |
|
| 893 | - |
|
| 894 | - if ($tokens[$stackPtr]['code'] !== \T_NAMESPACE) { |
|
| 895 | - return false; |
|
| 896 | - } |
|
| 897 | - |
|
| 898 | - if ($tokens[($stackPtr + 1)]['code'] === \T_NS_SEPARATOR) { |
|
| 899 | - // Not a namespace declaration, but use of, i.e. namespace\someFunction(); |
|
| 900 | - return false; |
|
| 901 | - } |
|
| 902 | - |
|
| 903 | - $nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true); |
|
| 904 | - if ($tokens[$nextToken]['code'] === \T_OPEN_CURLY_BRACKET) { |
|
| 905 | - // Declaration for global namespace when using multiple namespaces in a file. |
|
| 906 | - // I.e.: namespace {} |
|
| 907 | - return ''; |
|
| 908 | - } |
|
| 909 | - |
|
| 910 | - // Ok, this should be a namespace declaration, so get all the parts together. |
|
| 911 | - $validTokens = array( |
|
| 912 | - \T_STRING => true, |
|
| 913 | - \T_NS_SEPARATOR => true, |
|
| 914 | - \T_WHITESPACE => true, |
|
| 915 | - ); |
|
| 916 | - |
|
| 917 | - $namespaceName = ''; |
|
| 918 | - while (isset($validTokens[$tokens[$nextToken]['code']]) === true) { |
|
| 919 | - $namespaceName .= trim($tokens[$nextToken]['content']); |
|
| 920 | - $nextToken++; |
|
| 921 | - } |
|
| 922 | - |
|
| 923 | - return $namespaceName; |
|
| 924 | - } |
|
| 925 | - |
|
| 926 | - |
|
| 927 | - /** |
|
| 928 | - * Get the stack pointer for a return type token for a given function. |
|
| 929 | - * |
|
| 930 | - * Compatible layer for older PHPCS versions which don't recognize |
|
| 931 | - * return type hints correctly. |
|
| 932 | - * |
|
| 933 | - * Expects to be passed T_RETURN_TYPE, T_FUNCTION or T_CLOSURE token. |
|
| 934 | - * |
|
| 935 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 936 | - * @param int $stackPtr The position of the token. |
|
| 937 | - * |
|
| 938 | - * @return int|false Stack pointer to the return type token or false if |
|
| 939 | - * no return type was found or the passed token was |
|
| 940 | - * not of the correct type. |
|
| 941 | - */ |
|
| 942 | - public function getReturnTypeHintToken(File $phpcsFile, $stackPtr) |
|
| 943 | - { |
|
| 944 | - $tokens = $phpcsFile->getTokens(); |
|
| 945 | - |
|
| 946 | - if (\defined('T_RETURN_TYPE') && $tokens[$stackPtr]['code'] === \T_RETURN_TYPE) { |
|
| 947 | - return $stackPtr; |
|
| 948 | - } |
|
| 949 | - |
|
| 950 | - if ($tokens[$stackPtr]['code'] !== \T_FUNCTION && $tokens[$stackPtr]['code'] !== \T_CLOSURE) { |
|
| 951 | - return false; |
|
| 952 | - } |
|
| 953 | - |
|
| 954 | - if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) { |
|
| 955 | - return false; |
|
| 956 | - } |
|
| 957 | - |
|
| 958 | - // Allow for interface and abstract method declarations. |
|
| 959 | - $endOfFunctionDeclaration = null; |
|
| 960 | - if (isset($tokens[$stackPtr]['scope_opener'])) { |
|
| 961 | - $endOfFunctionDeclaration = $tokens[$stackPtr]['scope_opener']; |
|
| 962 | - } else { |
|
| 963 | - $nextSemiColon = $phpcsFile->findNext(\T_SEMICOLON, ($tokens[$stackPtr]['parenthesis_closer'] + 1), null, false, null, true); |
|
| 964 | - if ($nextSemiColon !== false) { |
|
| 965 | - $endOfFunctionDeclaration = $nextSemiColon; |
|
| 966 | - } |
|
| 967 | - } |
|
| 968 | - |
|
| 969 | - if (isset($endOfFunctionDeclaration) === false) { |
|
| 970 | - return false; |
|
| 971 | - } |
|
| 972 | - |
|
| 973 | - $hasColon = $phpcsFile->findNext( |
|
| 974 | - array(\T_COLON, \T_INLINE_ELSE), |
|
| 975 | - ($tokens[$stackPtr]['parenthesis_closer'] + 1), |
|
| 976 | - $endOfFunctionDeclaration |
|
| 977 | - ); |
|
| 978 | - if ($hasColon === false) { |
|
| 979 | - return false; |
|
| 980 | - } |
|
| 981 | - |
|
| 982 | - /* |
|
| 851 | + $previousNSToken = $stackPtr; |
|
| 852 | + $namespace = false; |
|
| 853 | + do { |
|
| 854 | + $previousNSToken = $phpcsFile->findPrevious(\T_NAMESPACE, ($previousNSToken - 1)); |
|
| 855 | + |
|
| 856 | + // Stop if we encounter a scoped namespace declaration as we already know we're not in one. |
|
| 857 | + if (empty($tokens[$previousNSToken]['scope_condition']) === false && $tokens[$previousNSToken]['scope_condition'] === $previousNSToken) { |
|
| 858 | + break; |
|
| 859 | + } |
|
| 860 | + |
|
| 861 | + $namespace = $this->getDeclaredNamespaceName($phpcsFile, $previousNSToken); |
|
| 862 | + |
|
| 863 | + } while ($namespace === false && $previousNSToken !== false); |
|
| 864 | + |
|
| 865 | + // If we still haven't got a namespace, return an empty string. |
|
| 866 | + if ($namespace === false) { |
|
| 867 | + return ''; |
|
| 868 | + } else { |
|
| 869 | + return $namespace; |
|
| 870 | + } |
|
| 871 | + } |
|
| 872 | + |
|
| 873 | + /** |
|
| 874 | + * Get the complete namespace name for a namespace declaration. |
|
| 875 | + * |
|
| 876 | + * For hierarchical namespaces, the name will be composed of several tokens, |
|
| 877 | + * i.e. MyProject\Sub\Level which will be returned together as one string. |
|
| 878 | + * |
|
| 879 | + * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
|
| 880 | + * @param int|bool $stackPtr The position of a T_NAMESPACE token. |
|
| 881 | + * |
|
| 882 | + * @return string|false Namespace name or false if not a namespace declaration. |
|
| 883 | + * Namespace name can be an empty string for global namespace declaration. |
|
| 884 | + */ |
|
| 885 | + public function getDeclaredNamespaceName(File $phpcsFile, $stackPtr) |
|
| 886 | + { |
|
| 887 | + $tokens = $phpcsFile->getTokens(); |
|
| 888 | + |
|
| 889 | + // Check for the existence of the token. |
|
| 890 | + if ($stackPtr === false || isset($tokens[$stackPtr]) === false) { |
|
| 891 | + return false; |
|
| 892 | + } |
|
| 893 | + |
|
| 894 | + if ($tokens[$stackPtr]['code'] !== \T_NAMESPACE) { |
|
| 895 | + return false; |
|
| 896 | + } |
|
| 897 | + |
|
| 898 | + if ($tokens[($stackPtr + 1)]['code'] === \T_NS_SEPARATOR) { |
|
| 899 | + // Not a namespace declaration, but use of, i.e. namespace\someFunction(); |
|
| 900 | + return false; |
|
| 901 | + } |
|
| 902 | + |
|
| 903 | + $nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true); |
|
| 904 | + if ($tokens[$nextToken]['code'] === \T_OPEN_CURLY_BRACKET) { |
|
| 905 | + // Declaration for global namespace when using multiple namespaces in a file. |
|
| 906 | + // I.e.: namespace {} |
|
| 907 | + return ''; |
|
| 908 | + } |
|
| 909 | + |
|
| 910 | + // Ok, this should be a namespace declaration, so get all the parts together. |
|
| 911 | + $validTokens = array( |
|
| 912 | + \T_STRING => true, |
|
| 913 | + \T_NS_SEPARATOR => true, |
|
| 914 | + \T_WHITESPACE => true, |
|
| 915 | + ); |
|
| 916 | + |
|
| 917 | + $namespaceName = ''; |
|
| 918 | + while (isset($validTokens[$tokens[$nextToken]['code']]) === true) { |
|
| 919 | + $namespaceName .= trim($tokens[$nextToken]['content']); |
|
| 920 | + $nextToken++; |
|
| 921 | + } |
|
| 922 | + |
|
| 923 | + return $namespaceName; |
|
| 924 | + } |
|
| 925 | + |
|
| 926 | + |
|
| 927 | + /** |
|
| 928 | + * Get the stack pointer for a return type token for a given function. |
|
| 929 | + * |
|
| 930 | + * Compatible layer for older PHPCS versions which don't recognize |
|
| 931 | + * return type hints correctly. |
|
| 932 | + * |
|
| 933 | + * Expects to be passed T_RETURN_TYPE, T_FUNCTION or T_CLOSURE token. |
|
| 934 | + * |
|
| 935 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 936 | + * @param int $stackPtr The position of the token. |
|
| 937 | + * |
|
| 938 | + * @return int|false Stack pointer to the return type token or false if |
|
| 939 | + * no return type was found or the passed token was |
|
| 940 | + * not of the correct type. |
|
| 941 | + */ |
|
| 942 | + public function getReturnTypeHintToken(File $phpcsFile, $stackPtr) |
|
| 943 | + { |
|
| 944 | + $tokens = $phpcsFile->getTokens(); |
|
| 945 | + |
|
| 946 | + if (\defined('T_RETURN_TYPE') && $tokens[$stackPtr]['code'] === \T_RETURN_TYPE) { |
|
| 947 | + return $stackPtr; |
|
| 948 | + } |
|
| 949 | + |
|
| 950 | + if ($tokens[$stackPtr]['code'] !== \T_FUNCTION && $tokens[$stackPtr]['code'] !== \T_CLOSURE) { |
|
| 951 | + return false; |
|
| 952 | + } |
|
| 953 | + |
|
| 954 | + if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) { |
|
| 955 | + return false; |
|
| 956 | + } |
|
| 957 | + |
|
| 958 | + // Allow for interface and abstract method declarations. |
|
| 959 | + $endOfFunctionDeclaration = null; |
|
| 960 | + if (isset($tokens[$stackPtr]['scope_opener'])) { |
|
| 961 | + $endOfFunctionDeclaration = $tokens[$stackPtr]['scope_opener']; |
|
| 962 | + } else { |
|
| 963 | + $nextSemiColon = $phpcsFile->findNext(\T_SEMICOLON, ($tokens[$stackPtr]['parenthesis_closer'] + 1), null, false, null, true); |
|
| 964 | + if ($nextSemiColon !== false) { |
|
| 965 | + $endOfFunctionDeclaration = $nextSemiColon; |
|
| 966 | + } |
|
| 967 | + } |
|
| 968 | + |
|
| 969 | + if (isset($endOfFunctionDeclaration) === false) { |
|
| 970 | + return false; |
|
| 971 | + } |
|
| 972 | + |
|
| 973 | + $hasColon = $phpcsFile->findNext( |
|
| 974 | + array(\T_COLON, \T_INLINE_ELSE), |
|
| 975 | + ($tokens[$stackPtr]['parenthesis_closer'] + 1), |
|
| 976 | + $endOfFunctionDeclaration |
|
| 977 | + ); |
|
| 978 | + if ($hasColon === false) { |
|
| 979 | + return false; |
|
| 980 | + } |
|
| 981 | + |
|
| 982 | + /* |
|
| 983 | 983 | * - `self`, `parent` and `callable` are not being recognized as return types in PHPCS < 2.6.0. |
| 984 | 984 | * - Return types are not recognized at all in PHPCS < 2.4.0. |
| 985 | 985 | * - The T_RETURN_TYPE token is defined, but no longer in use since PHPCS 3.3.0+. |
@@ -988,1107 +988,1107 @@ discard block |
||
| 988 | 988 | * to prevent confusing sniffs looking for array declarations. |
| 989 | 989 | * As of PHPCS 3.3.0 `array` as a type declaration will be tokenized as `T_STRING`. |
| 990 | 990 | */ |
| 991 | - $unrecognizedTypes = array( |
|
| 992 | - \T_CALLABLE, |
|
| 993 | - \T_SELF, |
|
| 994 | - \T_PARENT, |
|
| 995 | - \T_ARRAY, // PHPCS < 2.4.0. |
|
| 996 | - \T_STRING, |
|
| 997 | - ); |
|
| 998 | - |
|
| 999 | - return $phpcsFile->findPrevious($unrecognizedTypes, ($endOfFunctionDeclaration - 1), $hasColon); |
|
| 1000 | - } |
|
| 1001 | - |
|
| 1002 | - |
|
| 1003 | - /** |
|
| 1004 | - * Get the complete return type declaration for a given function. |
|
| 1005 | - * |
|
| 1006 | - * Cross-version compatible way to retrieve the complete return type declaration. |
|
| 1007 | - * |
|
| 1008 | - * For a classname-based return type, PHPCS, as well as the Sniff::getReturnTypeHintToken() |
|
| 1009 | - * method will mark the classname as the return type token. |
|
| 1010 | - * This method will find preceeding namespaces and namespace separators and will return a |
|
| 1011 | - * string containing the qualified return type declaration. |
|
| 1012 | - * |
|
| 1013 | - * Expects to be passed a T_RETURN_TYPE token or the return value from a call to |
|
| 1014 | - * the Sniff::getReturnTypeHintToken() method. |
|
| 1015 | - * |
|
| 1016 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1017 | - * @param int $stackPtr The position of the return type token. |
|
| 1018 | - * |
|
| 1019 | - * @return string|false The name of the return type token. |
|
| 1020 | - */ |
|
| 1021 | - public function getReturnTypeHintName(File $phpcsFile, $stackPtr) |
|
| 1022 | - { |
|
| 1023 | - $tokens = $phpcsFile->getTokens(); |
|
| 1024 | - |
|
| 1025 | - // In older PHPCS versions, the nullable indicator will turn a return type colon into a T_INLINE_ELSE. |
|
| 1026 | - $colon = $phpcsFile->findPrevious(array(\T_COLON, \T_INLINE_ELSE, \T_FUNCTION, \T_CLOSE_PARENTHESIS), ($stackPtr - 1)); |
|
| 1027 | - if ($colon === false |
|
| 1028 | - || ($tokens[$colon]['code'] !== \T_COLON && $tokens[$colon]['code'] !== \T_INLINE_ELSE) |
|
| 1029 | - ) { |
|
| 1030 | - // Shouldn't happen, just in case. |
|
| 1031 | - return; |
|
| 1032 | - } |
|
| 1033 | - |
|
| 1034 | - $returnTypeHint = ''; |
|
| 1035 | - for ($i = ($colon + 1); $i <= $stackPtr; $i++) { |
|
| 1036 | - // As of PHPCS 3.3.0+, all tokens are tokenized as "normal", so T_CALLABLE, T_SELF etc are |
|
| 1037 | - // all possible, just exclude anything that's regarded as empty and the nullable indicator. |
|
| 1038 | - if (isset(Tokens::$emptyTokens[$tokens[$i]['code']])) { |
|
| 1039 | - continue; |
|
| 1040 | - } |
|
| 1041 | - |
|
| 1042 | - if ($tokens[$i]['type'] === 'T_NULLABLE') { |
|
| 1043 | - continue; |
|
| 1044 | - } |
|
| 1045 | - |
|
| 1046 | - if (\defined('T_NULLABLE') === false && $tokens[$i]['code'] === \T_INLINE_THEN) { |
|
| 1047 | - // Old PHPCS. |
|
| 1048 | - continue; |
|
| 1049 | - } |
|
| 1050 | - |
|
| 1051 | - $returnTypeHint .= $tokens[$i]['content']; |
|
| 1052 | - } |
|
| 1053 | - |
|
| 1054 | - return $returnTypeHint; |
|
| 1055 | - } |
|
| 1056 | - |
|
| 1057 | - |
|
| 1058 | - /** |
|
| 1059 | - * Check whether a T_VARIABLE token is a class property declaration. |
|
| 1060 | - * |
|
| 1061 | - * Compatibility layer for PHPCS cross-version compatibility |
|
| 1062 | - * as PHPCS 2.4.0 - 2.7.1 does not have good enough support for |
|
| 1063 | - * anonymous classes. Along the same lines, the`getMemberProperties()` |
|
| 1064 | - * method does not support the `var` prefix. |
|
| 1065 | - * |
|
| 1066 | - * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
|
| 1067 | - * @param int $stackPtr The position in the stack of the |
|
| 1068 | - * T_VARIABLE token to verify. |
|
| 1069 | - * |
|
| 1070 | - * @return bool |
|
| 1071 | - */ |
|
| 1072 | - public function isClassProperty(File $phpcsFile, $stackPtr) |
|
| 1073 | - { |
|
| 1074 | - $tokens = $phpcsFile->getTokens(); |
|
| 1075 | - |
|
| 1076 | - if (isset($tokens[$stackPtr]) === false || $tokens[$stackPtr]['code'] !== \T_VARIABLE) { |
|
| 1077 | - return false; |
|
| 1078 | - } |
|
| 1079 | - |
|
| 1080 | - // Note: interfaces can not declare properties. |
|
| 1081 | - $validScopes = array( |
|
| 1082 | - 'T_CLASS' => true, |
|
| 1083 | - 'T_ANON_CLASS' => true, |
|
| 1084 | - 'T_TRAIT' => true, |
|
| 1085 | - ); |
|
| 1086 | - |
|
| 1087 | - $scopePtr = $this->validDirectScope($phpcsFile, $stackPtr, $validScopes); |
|
| 1088 | - if ($scopePtr !== false) { |
|
| 1089 | - // Make sure it's not a method parameter. |
|
| 1090 | - if (empty($tokens[$stackPtr]['nested_parenthesis']) === true) { |
|
| 1091 | - return true; |
|
| 1092 | - } else { |
|
| 1093 | - $parenthesis = array_keys($tokens[$stackPtr]['nested_parenthesis']); |
|
| 1094 | - $deepestOpen = array_pop($parenthesis); |
|
| 1095 | - if ($deepestOpen < $scopePtr |
|
| 1096 | - || isset($tokens[$deepestOpen]['parenthesis_owner']) === false |
|
| 1097 | - || $tokens[$tokens[$deepestOpen]['parenthesis_owner']]['code'] !== \T_FUNCTION |
|
| 1098 | - ) { |
|
| 1099 | - return true; |
|
| 1100 | - } |
|
| 1101 | - } |
|
| 1102 | - } |
|
| 1103 | - |
|
| 1104 | - return false; |
|
| 1105 | - } |
|
| 1106 | - |
|
| 1107 | - |
|
| 1108 | - /** |
|
| 1109 | - * Check whether a T_CONST token is a class constant declaration. |
|
| 1110 | - * |
|
| 1111 | - * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
|
| 1112 | - * @param int $stackPtr The position in the stack of the |
|
| 1113 | - * T_CONST token to verify. |
|
| 1114 | - * |
|
| 1115 | - * @return bool |
|
| 1116 | - */ |
|
| 1117 | - public function isClassConstant(File $phpcsFile, $stackPtr) |
|
| 1118 | - { |
|
| 1119 | - $tokens = $phpcsFile->getTokens(); |
|
| 1120 | - |
|
| 1121 | - if (isset($tokens[$stackPtr]) === false || $tokens[$stackPtr]['code'] !== \T_CONST) { |
|
| 1122 | - return false; |
|
| 1123 | - } |
|
| 1124 | - |
|
| 1125 | - // Note: traits can not declare constants. |
|
| 1126 | - $validScopes = array( |
|
| 1127 | - 'T_CLASS' => true, |
|
| 1128 | - 'T_ANON_CLASS' => true, |
|
| 1129 | - 'T_INTERFACE' => true, |
|
| 1130 | - ); |
|
| 1131 | - if ($this->validDirectScope($phpcsFile, $stackPtr, $validScopes) !== false) { |
|
| 1132 | - return true; |
|
| 1133 | - } |
|
| 1134 | - |
|
| 1135 | - return false; |
|
| 1136 | - } |
|
| 1137 | - |
|
| 1138 | - |
|
| 1139 | - /** |
|
| 1140 | - * Check whether the direct wrapping scope of a token is within a limited set of |
|
| 1141 | - * acceptable tokens. |
|
| 1142 | - * |
|
| 1143 | - * Used to check, for instance, if a T_CONST is a class constant. |
|
| 1144 | - * |
|
| 1145 | - * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
|
| 1146 | - * @param int $stackPtr The position in the stack of the |
|
| 1147 | - * token to verify. |
|
| 1148 | - * @param array $validScopes Array of token types. |
|
| 1149 | - * Keys should be the token types in string |
|
| 1150 | - * format to allow for newer token types. |
|
| 1151 | - * Value is irrelevant. |
|
| 1152 | - * |
|
| 1153 | - * @return int|bool StackPtr to the scope if valid, false otherwise. |
|
| 1154 | - */ |
|
| 1155 | - protected function validDirectScope(File $phpcsFile, $stackPtr, $validScopes) |
|
| 1156 | - { |
|
| 1157 | - $tokens = $phpcsFile->getTokens(); |
|
| 1158 | - |
|
| 1159 | - if (empty($tokens[$stackPtr]['conditions']) === true) { |
|
| 1160 | - return false; |
|
| 1161 | - } |
|
| 1162 | - |
|
| 1163 | - /* |
|
| 991 | + $unrecognizedTypes = array( |
|
| 992 | + \T_CALLABLE, |
|
| 993 | + \T_SELF, |
|
| 994 | + \T_PARENT, |
|
| 995 | + \T_ARRAY, // PHPCS < 2.4.0. |
|
| 996 | + \T_STRING, |
|
| 997 | + ); |
|
| 998 | + |
|
| 999 | + return $phpcsFile->findPrevious($unrecognizedTypes, ($endOfFunctionDeclaration - 1), $hasColon); |
|
| 1000 | + } |
|
| 1001 | + |
|
| 1002 | + |
|
| 1003 | + /** |
|
| 1004 | + * Get the complete return type declaration for a given function. |
|
| 1005 | + * |
|
| 1006 | + * Cross-version compatible way to retrieve the complete return type declaration. |
|
| 1007 | + * |
|
| 1008 | + * For a classname-based return type, PHPCS, as well as the Sniff::getReturnTypeHintToken() |
|
| 1009 | + * method will mark the classname as the return type token. |
|
| 1010 | + * This method will find preceeding namespaces and namespace separators and will return a |
|
| 1011 | + * string containing the qualified return type declaration. |
|
| 1012 | + * |
|
| 1013 | + * Expects to be passed a T_RETURN_TYPE token or the return value from a call to |
|
| 1014 | + * the Sniff::getReturnTypeHintToken() method. |
|
| 1015 | + * |
|
| 1016 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1017 | + * @param int $stackPtr The position of the return type token. |
|
| 1018 | + * |
|
| 1019 | + * @return string|false The name of the return type token. |
|
| 1020 | + */ |
|
| 1021 | + public function getReturnTypeHintName(File $phpcsFile, $stackPtr) |
|
| 1022 | + { |
|
| 1023 | + $tokens = $phpcsFile->getTokens(); |
|
| 1024 | + |
|
| 1025 | + // In older PHPCS versions, the nullable indicator will turn a return type colon into a T_INLINE_ELSE. |
|
| 1026 | + $colon = $phpcsFile->findPrevious(array(\T_COLON, \T_INLINE_ELSE, \T_FUNCTION, \T_CLOSE_PARENTHESIS), ($stackPtr - 1)); |
|
| 1027 | + if ($colon === false |
|
| 1028 | + || ($tokens[$colon]['code'] !== \T_COLON && $tokens[$colon]['code'] !== \T_INLINE_ELSE) |
|
| 1029 | + ) { |
|
| 1030 | + // Shouldn't happen, just in case. |
|
| 1031 | + return; |
|
| 1032 | + } |
|
| 1033 | + |
|
| 1034 | + $returnTypeHint = ''; |
|
| 1035 | + for ($i = ($colon + 1); $i <= $stackPtr; $i++) { |
|
| 1036 | + // As of PHPCS 3.3.0+, all tokens are tokenized as "normal", so T_CALLABLE, T_SELF etc are |
|
| 1037 | + // all possible, just exclude anything that's regarded as empty and the nullable indicator. |
|
| 1038 | + if (isset(Tokens::$emptyTokens[$tokens[$i]['code']])) { |
|
| 1039 | + continue; |
|
| 1040 | + } |
|
| 1041 | + |
|
| 1042 | + if ($tokens[$i]['type'] === 'T_NULLABLE') { |
|
| 1043 | + continue; |
|
| 1044 | + } |
|
| 1045 | + |
|
| 1046 | + if (\defined('T_NULLABLE') === false && $tokens[$i]['code'] === \T_INLINE_THEN) { |
|
| 1047 | + // Old PHPCS. |
|
| 1048 | + continue; |
|
| 1049 | + } |
|
| 1050 | + |
|
| 1051 | + $returnTypeHint .= $tokens[$i]['content']; |
|
| 1052 | + } |
|
| 1053 | + |
|
| 1054 | + return $returnTypeHint; |
|
| 1055 | + } |
|
| 1056 | + |
|
| 1057 | + |
|
| 1058 | + /** |
|
| 1059 | + * Check whether a T_VARIABLE token is a class property declaration. |
|
| 1060 | + * |
|
| 1061 | + * Compatibility layer for PHPCS cross-version compatibility |
|
| 1062 | + * as PHPCS 2.4.0 - 2.7.1 does not have good enough support for |
|
| 1063 | + * anonymous classes. Along the same lines, the`getMemberProperties()` |
|
| 1064 | + * method does not support the `var` prefix. |
|
| 1065 | + * |
|
| 1066 | + * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
|
| 1067 | + * @param int $stackPtr The position in the stack of the |
|
| 1068 | + * T_VARIABLE token to verify. |
|
| 1069 | + * |
|
| 1070 | + * @return bool |
|
| 1071 | + */ |
|
| 1072 | + public function isClassProperty(File $phpcsFile, $stackPtr) |
|
| 1073 | + { |
|
| 1074 | + $tokens = $phpcsFile->getTokens(); |
|
| 1075 | + |
|
| 1076 | + if (isset($tokens[$stackPtr]) === false || $tokens[$stackPtr]['code'] !== \T_VARIABLE) { |
|
| 1077 | + return false; |
|
| 1078 | + } |
|
| 1079 | + |
|
| 1080 | + // Note: interfaces can not declare properties. |
|
| 1081 | + $validScopes = array( |
|
| 1082 | + 'T_CLASS' => true, |
|
| 1083 | + 'T_ANON_CLASS' => true, |
|
| 1084 | + 'T_TRAIT' => true, |
|
| 1085 | + ); |
|
| 1086 | + |
|
| 1087 | + $scopePtr = $this->validDirectScope($phpcsFile, $stackPtr, $validScopes); |
|
| 1088 | + if ($scopePtr !== false) { |
|
| 1089 | + // Make sure it's not a method parameter. |
|
| 1090 | + if (empty($tokens[$stackPtr]['nested_parenthesis']) === true) { |
|
| 1091 | + return true; |
|
| 1092 | + } else { |
|
| 1093 | + $parenthesis = array_keys($tokens[$stackPtr]['nested_parenthesis']); |
|
| 1094 | + $deepestOpen = array_pop($parenthesis); |
|
| 1095 | + if ($deepestOpen < $scopePtr |
|
| 1096 | + || isset($tokens[$deepestOpen]['parenthesis_owner']) === false |
|
| 1097 | + || $tokens[$tokens[$deepestOpen]['parenthesis_owner']]['code'] !== \T_FUNCTION |
|
| 1098 | + ) { |
|
| 1099 | + return true; |
|
| 1100 | + } |
|
| 1101 | + } |
|
| 1102 | + } |
|
| 1103 | + |
|
| 1104 | + return false; |
|
| 1105 | + } |
|
| 1106 | + |
|
| 1107 | + |
|
| 1108 | + /** |
|
| 1109 | + * Check whether a T_CONST token is a class constant declaration. |
|
| 1110 | + * |
|
| 1111 | + * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
|
| 1112 | + * @param int $stackPtr The position in the stack of the |
|
| 1113 | + * T_CONST token to verify. |
|
| 1114 | + * |
|
| 1115 | + * @return bool |
|
| 1116 | + */ |
|
| 1117 | + public function isClassConstant(File $phpcsFile, $stackPtr) |
|
| 1118 | + { |
|
| 1119 | + $tokens = $phpcsFile->getTokens(); |
|
| 1120 | + |
|
| 1121 | + if (isset($tokens[$stackPtr]) === false || $tokens[$stackPtr]['code'] !== \T_CONST) { |
|
| 1122 | + return false; |
|
| 1123 | + } |
|
| 1124 | + |
|
| 1125 | + // Note: traits can not declare constants. |
|
| 1126 | + $validScopes = array( |
|
| 1127 | + 'T_CLASS' => true, |
|
| 1128 | + 'T_ANON_CLASS' => true, |
|
| 1129 | + 'T_INTERFACE' => true, |
|
| 1130 | + ); |
|
| 1131 | + if ($this->validDirectScope($phpcsFile, $stackPtr, $validScopes) !== false) { |
|
| 1132 | + return true; |
|
| 1133 | + } |
|
| 1134 | + |
|
| 1135 | + return false; |
|
| 1136 | + } |
|
| 1137 | + |
|
| 1138 | + |
|
| 1139 | + /** |
|
| 1140 | + * Check whether the direct wrapping scope of a token is within a limited set of |
|
| 1141 | + * acceptable tokens. |
|
| 1142 | + * |
|
| 1143 | + * Used to check, for instance, if a T_CONST is a class constant. |
|
| 1144 | + * |
|
| 1145 | + * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
|
| 1146 | + * @param int $stackPtr The position in the stack of the |
|
| 1147 | + * token to verify. |
|
| 1148 | + * @param array $validScopes Array of token types. |
|
| 1149 | + * Keys should be the token types in string |
|
| 1150 | + * format to allow for newer token types. |
|
| 1151 | + * Value is irrelevant. |
|
| 1152 | + * |
|
| 1153 | + * @return int|bool StackPtr to the scope if valid, false otherwise. |
|
| 1154 | + */ |
|
| 1155 | + protected function validDirectScope(File $phpcsFile, $stackPtr, $validScopes) |
|
| 1156 | + { |
|
| 1157 | + $tokens = $phpcsFile->getTokens(); |
|
| 1158 | + |
|
| 1159 | + if (empty($tokens[$stackPtr]['conditions']) === true) { |
|
| 1160 | + return false; |
|
| 1161 | + } |
|
| 1162 | + |
|
| 1163 | + /* |
|
| 1164 | 1164 | * Check only the direct wrapping scope of the token. |
| 1165 | 1165 | */ |
| 1166 | - $conditions = array_keys($tokens[$stackPtr]['conditions']); |
|
| 1167 | - $ptr = array_pop($conditions); |
|
| 1168 | - |
|
| 1169 | - if (isset($tokens[$ptr]) === false) { |
|
| 1170 | - return false; |
|
| 1171 | - } |
|
| 1172 | - |
|
| 1173 | - if (isset($validScopes[$tokens[$ptr]['type']]) === true) { |
|
| 1174 | - return $ptr; |
|
| 1175 | - } |
|
| 1176 | - |
|
| 1177 | - return false; |
|
| 1178 | - } |
|
| 1179 | - |
|
| 1180 | - |
|
| 1181 | - /** |
|
| 1182 | - * Get an array of just the type hints from a function declaration. |
|
| 1183 | - * |
|
| 1184 | - * Expects to be passed T_FUNCTION or T_CLOSURE token. |
|
| 1185 | - * |
|
| 1186 | - * Strips potential nullable indicator and potential global namespace |
|
| 1187 | - * indicator from the type hints before returning them. |
|
| 1188 | - * |
|
| 1189 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1190 | - * @param int $stackPtr The position of the token. |
|
| 1191 | - * |
|
| 1192 | - * @return array Array with type hints or an empty array if |
|
| 1193 | - * - the function does not have any parameters |
|
| 1194 | - * - no type hints were found |
|
| 1195 | - * - or the passed token was not of the correct type. |
|
| 1196 | - */ |
|
| 1197 | - public function getTypeHintsFromFunctionDeclaration(File $phpcsFile, $stackPtr) |
|
| 1198 | - { |
|
| 1199 | - $tokens = $phpcsFile->getTokens(); |
|
| 1200 | - |
|
| 1201 | - if ($tokens[$stackPtr]['code'] !== \T_FUNCTION && $tokens[$stackPtr]['code'] !== \T_CLOSURE) { |
|
| 1202 | - return array(); |
|
| 1203 | - } |
|
| 1204 | - |
|
| 1205 | - $parameters = PHPCSHelper::getMethodParameters($phpcsFile, $stackPtr); |
|
| 1206 | - if (empty($parameters) || \is_array($parameters) === false) { |
|
| 1207 | - return array(); |
|
| 1208 | - } |
|
| 1209 | - |
|
| 1210 | - $typeHints = array(); |
|
| 1211 | - |
|
| 1212 | - foreach ($parameters as $param) { |
|
| 1213 | - if ($param['type_hint'] === '') { |
|
| 1214 | - continue; |
|
| 1215 | - } |
|
| 1216 | - |
|
| 1217 | - // Strip off potential nullable indication. |
|
| 1218 | - $typeHint = ltrim($param['type_hint'], '?'); |
|
| 1219 | - |
|
| 1220 | - // Strip off potential (global) namespace indication. |
|
| 1221 | - $typeHint = ltrim($typeHint, '\\'); |
|
| 1222 | - |
|
| 1223 | - if ($typeHint !== '') { |
|
| 1224 | - $typeHints[] = $typeHint; |
|
| 1225 | - } |
|
| 1226 | - } |
|
| 1227 | - |
|
| 1228 | - return $typeHints; |
|
| 1229 | - } |
|
| 1230 | - |
|
| 1231 | - |
|
| 1232 | - /** |
|
| 1233 | - * Get the hash algorithm name from the parameter in a hash function call. |
|
| 1234 | - * |
|
| 1235 | - * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
|
| 1236 | - * @param int $stackPtr The position of the T_STRING function token. |
|
| 1237 | - * |
|
| 1238 | - * @return string|false The algorithm name without quotes if this was a relevant hash |
|
| 1239 | - * function call or false if it was not. |
|
| 1240 | - */ |
|
| 1241 | - public function getHashAlgorithmParameter(File $phpcsFile, $stackPtr) |
|
| 1242 | - { |
|
| 1243 | - $tokens = $phpcsFile->getTokens(); |
|
| 1244 | - |
|
| 1245 | - // Check for the existence of the token. |
|
| 1246 | - if (isset($tokens[$stackPtr]) === false) { |
|
| 1247 | - return false; |
|
| 1248 | - } |
|
| 1249 | - |
|
| 1250 | - if ($tokens[$stackPtr]['code'] !== \T_STRING) { |
|
| 1251 | - return false; |
|
| 1252 | - } |
|
| 1253 | - |
|
| 1254 | - $functionName = $tokens[$stackPtr]['content']; |
|
| 1255 | - $functionNameLc = strtolower($functionName); |
|
| 1256 | - |
|
| 1257 | - // Bow out if not one of the functions we're targetting. |
|
| 1258 | - if (isset($this->hashAlgoFunctions[$functionNameLc]) === false) { |
|
| 1259 | - return false; |
|
| 1260 | - } |
|
| 1261 | - |
|
| 1262 | - // Get the parameter from the function call which should contain the algorithm name. |
|
| 1263 | - $algoParam = $this->getFunctionCallParameter($phpcsFile, $stackPtr, $this->hashAlgoFunctions[$functionNameLc]); |
|
| 1264 | - if ($algoParam === false) { |
|
| 1265 | - return false; |
|
| 1266 | - } |
|
| 1267 | - |
|
| 1268 | - // Algorithm is a text string, so we need to remove the quotes. |
|
| 1269 | - $algo = strtolower(trim($algoParam['raw'])); |
|
| 1270 | - $algo = $this->stripQuotes($algo); |
|
| 1271 | - |
|
| 1272 | - return $algo; |
|
| 1273 | - } |
|
| 1274 | - |
|
| 1275 | - |
|
| 1276 | - /** |
|
| 1277 | - * Determine whether an arbitrary T_STRING token is the use of a global constant. |
|
| 1278 | - * |
|
| 1279 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1280 | - * @param int $stackPtr The position of the function call token. |
|
| 1281 | - * |
|
| 1282 | - * @return bool |
|
| 1283 | - */ |
|
| 1284 | - public function isUseOfGlobalConstant(File $phpcsFile, $stackPtr) |
|
| 1285 | - { |
|
| 1286 | - static $isLowPHPCS, $isLowPHP; |
|
| 1287 | - |
|
| 1288 | - $tokens = $phpcsFile->getTokens(); |
|
| 1289 | - |
|
| 1290 | - // Check for the existence of the token. |
|
| 1291 | - if (isset($tokens[$stackPtr]) === false) { |
|
| 1292 | - return false; |
|
| 1293 | - } |
|
| 1294 | - |
|
| 1295 | - // Is this one of the tokens this function handles ? |
|
| 1296 | - if ($tokens[$stackPtr]['code'] !== \T_STRING) { |
|
| 1297 | - return false; |
|
| 1298 | - } |
|
| 1299 | - |
|
| 1300 | - // Check for older PHP, PHPCS version so we can compensate for misidentified tokens. |
|
| 1301 | - if (isset($isLowPHPCS, $isLowPHP) === false) { |
|
| 1302 | - $isLowPHP = false; |
|
| 1303 | - $isLowPHPCS = false; |
|
| 1304 | - if (version_compare(\PHP_VERSION_ID, '50400', '<')) { |
|
| 1305 | - $isLowPHP = true; |
|
| 1306 | - $isLowPHPCS = version_compare(PHPCSHelper::getVersion(), '2.4.0', '<'); |
|
| 1307 | - } |
|
| 1308 | - } |
|
| 1309 | - |
|
| 1310 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
| 1311 | - if ($next !== false |
|
| 1312 | - && ($tokens[$next]['code'] === \T_OPEN_PARENTHESIS |
|
| 1313 | - || $tokens[$next]['code'] === \T_DOUBLE_COLON) |
|
| 1314 | - ) { |
|
| 1315 | - // Function call or declaration. |
|
| 1316 | - return false; |
|
| 1317 | - } |
|
| 1318 | - |
|
| 1319 | - // Array of tokens which if found preceding the $stackPtr indicate that a T_STRING is not a global constant. |
|
| 1320 | - $tokensToIgnore = array( |
|
| 1321 | - 'T_NAMESPACE' => true, |
|
| 1322 | - 'T_USE' => true, |
|
| 1323 | - 'T_CLASS' => true, |
|
| 1324 | - 'T_TRAIT' => true, |
|
| 1325 | - 'T_INTERFACE' => true, |
|
| 1326 | - 'T_EXTENDS' => true, |
|
| 1327 | - 'T_IMPLEMENTS' => true, |
|
| 1328 | - 'T_NEW' => true, |
|
| 1329 | - 'T_FUNCTION' => true, |
|
| 1330 | - 'T_DOUBLE_COLON' => true, |
|
| 1331 | - 'T_OBJECT_OPERATOR' => true, |
|
| 1332 | - 'T_INSTANCEOF' => true, |
|
| 1333 | - 'T_INSTEADOF' => true, |
|
| 1334 | - 'T_GOTO' => true, |
|
| 1335 | - 'T_AS' => true, |
|
| 1336 | - 'T_PUBLIC' => true, |
|
| 1337 | - 'T_PROTECTED' => true, |
|
| 1338 | - 'T_PRIVATE' => true, |
|
| 1339 | - ); |
|
| 1340 | - |
|
| 1341 | - $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
| 1342 | - if ($prev !== false |
|
| 1343 | - && (isset($tokensToIgnore[$tokens[$prev]['type']]) === true |
|
| 1344 | - || ($tokens[$prev]['code'] === \T_STRING |
|
| 1345 | - && (($isLowPHPCS === true |
|
| 1346 | - && $tokens[$prev]['content'] === 'trait') |
|
| 1347 | - || ($isLowPHP === true |
|
| 1348 | - && $tokens[$prev]['content'] === 'insteadof')))) |
|
| 1349 | - ) { |
|
| 1350 | - // Not the use of a constant. |
|
| 1351 | - return false; |
|
| 1352 | - } |
|
| 1353 | - |
|
| 1354 | - if ($prev !== false |
|
| 1355 | - && $tokens[$prev]['code'] === \T_NS_SEPARATOR |
|
| 1356 | - && $tokens[($prev - 1)]['code'] === \T_STRING |
|
| 1357 | - ) { |
|
| 1358 | - // Namespaced constant of the same name. |
|
| 1359 | - return false; |
|
| 1360 | - } |
|
| 1361 | - |
|
| 1362 | - if ($prev !== false |
|
| 1363 | - && $tokens[$prev]['code'] === \T_CONST |
|
| 1364 | - && $this->isClassConstant($phpcsFile, $prev) === true |
|
| 1365 | - ) { |
|
| 1366 | - // Class constant declaration of the same name. |
|
| 1367 | - return false; |
|
| 1368 | - } |
|
| 1369 | - |
|
| 1370 | - /* |
|
| 1166 | + $conditions = array_keys($tokens[$stackPtr]['conditions']); |
|
| 1167 | + $ptr = array_pop($conditions); |
|
| 1168 | + |
|
| 1169 | + if (isset($tokens[$ptr]) === false) { |
|
| 1170 | + return false; |
|
| 1171 | + } |
|
| 1172 | + |
|
| 1173 | + if (isset($validScopes[$tokens[$ptr]['type']]) === true) { |
|
| 1174 | + return $ptr; |
|
| 1175 | + } |
|
| 1176 | + |
|
| 1177 | + return false; |
|
| 1178 | + } |
|
| 1179 | + |
|
| 1180 | + |
|
| 1181 | + /** |
|
| 1182 | + * Get an array of just the type hints from a function declaration. |
|
| 1183 | + * |
|
| 1184 | + * Expects to be passed T_FUNCTION or T_CLOSURE token. |
|
| 1185 | + * |
|
| 1186 | + * Strips potential nullable indicator and potential global namespace |
|
| 1187 | + * indicator from the type hints before returning them. |
|
| 1188 | + * |
|
| 1189 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1190 | + * @param int $stackPtr The position of the token. |
|
| 1191 | + * |
|
| 1192 | + * @return array Array with type hints or an empty array if |
|
| 1193 | + * - the function does not have any parameters |
|
| 1194 | + * - no type hints were found |
|
| 1195 | + * - or the passed token was not of the correct type. |
|
| 1196 | + */ |
|
| 1197 | + public function getTypeHintsFromFunctionDeclaration(File $phpcsFile, $stackPtr) |
|
| 1198 | + { |
|
| 1199 | + $tokens = $phpcsFile->getTokens(); |
|
| 1200 | + |
|
| 1201 | + if ($tokens[$stackPtr]['code'] !== \T_FUNCTION && $tokens[$stackPtr]['code'] !== \T_CLOSURE) { |
|
| 1202 | + return array(); |
|
| 1203 | + } |
|
| 1204 | + |
|
| 1205 | + $parameters = PHPCSHelper::getMethodParameters($phpcsFile, $stackPtr); |
|
| 1206 | + if (empty($parameters) || \is_array($parameters) === false) { |
|
| 1207 | + return array(); |
|
| 1208 | + } |
|
| 1209 | + |
|
| 1210 | + $typeHints = array(); |
|
| 1211 | + |
|
| 1212 | + foreach ($parameters as $param) { |
|
| 1213 | + if ($param['type_hint'] === '') { |
|
| 1214 | + continue; |
|
| 1215 | + } |
|
| 1216 | + |
|
| 1217 | + // Strip off potential nullable indication. |
|
| 1218 | + $typeHint = ltrim($param['type_hint'], '?'); |
|
| 1219 | + |
|
| 1220 | + // Strip off potential (global) namespace indication. |
|
| 1221 | + $typeHint = ltrim($typeHint, '\\'); |
|
| 1222 | + |
|
| 1223 | + if ($typeHint !== '') { |
|
| 1224 | + $typeHints[] = $typeHint; |
|
| 1225 | + } |
|
| 1226 | + } |
|
| 1227 | + |
|
| 1228 | + return $typeHints; |
|
| 1229 | + } |
|
| 1230 | + |
|
| 1231 | + |
|
| 1232 | + /** |
|
| 1233 | + * Get the hash algorithm name from the parameter in a hash function call. |
|
| 1234 | + * |
|
| 1235 | + * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
|
| 1236 | + * @param int $stackPtr The position of the T_STRING function token. |
|
| 1237 | + * |
|
| 1238 | + * @return string|false The algorithm name without quotes if this was a relevant hash |
|
| 1239 | + * function call or false if it was not. |
|
| 1240 | + */ |
|
| 1241 | + public function getHashAlgorithmParameter(File $phpcsFile, $stackPtr) |
|
| 1242 | + { |
|
| 1243 | + $tokens = $phpcsFile->getTokens(); |
|
| 1244 | + |
|
| 1245 | + // Check for the existence of the token. |
|
| 1246 | + if (isset($tokens[$stackPtr]) === false) { |
|
| 1247 | + return false; |
|
| 1248 | + } |
|
| 1249 | + |
|
| 1250 | + if ($tokens[$stackPtr]['code'] !== \T_STRING) { |
|
| 1251 | + return false; |
|
| 1252 | + } |
|
| 1253 | + |
|
| 1254 | + $functionName = $tokens[$stackPtr]['content']; |
|
| 1255 | + $functionNameLc = strtolower($functionName); |
|
| 1256 | + |
|
| 1257 | + // Bow out if not one of the functions we're targetting. |
|
| 1258 | + if (isset($this->hashAlgoFunctions[$functionNameLc]) === false) { |
|
| 1259 | + return false; |
|
| 1260 | + } |
|
| 1261 | + |
|
| 1262 | + // Get the parameter from the function call which should contain the algorithm name. |
|
| 1263 | + $algoParam = $this->getFunctionCallParameter($phpcsFile, $stackPtr, $this->hashAlgoFunctions[$functionNameLc]); |
|
| 1264 | + if ($algoParam === false) { |
|
| 1265 | + return false; |
|
| 1266 | + } |
|
| 1267 | + |
|
| 1268 | + // Algorithm is a text string, so we need to remove the quotes. |
|
| 1269 | + $algo = strtolower(trim($algoParam['raw'])); |
|
| 1270 | + $algo = $this->stripQuotes($algo); |
|
| 1271 | + |
|
| 1272 | + return $algo; |
|
| 1273 | + } |
|
| 1274 | + |
|
| 1275 | + |
|
| 1276 | + /** |
|
| 1277 | + * Determine whether an arbitrary T_STRING token is the use of a global constant. |
|
| 1278 | + * |
|
| 1279 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1280 | + * @param int $stackPtr The position of the function call token. |
|
| 1281 | + * |
|
| 1282 | + * @return bool |
|
| 1283 | + */ |
|
| 1284 | + public function isUseOfGlobalConstant(File $phpcsFile, $stackPtr) |
|
| 1285 | + { |
|
| 1286 | + static $isLowPHPCS, $isLowPHP; |
|
| 1287 | + |
|
| 1288 | + $tokens = $phpcsFile->getTokens(); |
|
| 1289 | + |
|
| 1290 | + // Check for the existence of the token. |
|
| 1291 | + if (isset($tokens[$stackPtr]) === false) { |
|
| 1292 | + return false; |
|
| 1293 | + } |
|
| 1294 | + |
|
| 1295 | + // Is this one of the tokens this function handles ? |
|
| 1296 | + if ($tokens[$stackPtr]['code'] !== \T_STRING) { |
|
| 1297 | + return false; |
|
| 1298 | + } |
|
| 1299 | + |
|
| 1300 | + // Check for older PHP, PHPCS version so we can compensate for misidentified tokens. |
|
| 1301 | + if (isset($isLowPHPCS, $isLowPHP) === false) { |
|
| 1302 | + $isLowPHP = false; |
|
| 1303 | + $isLowPHPCS = false; |
|
| 1304 | + if (version_compare(\PHP_VERSION_ID, '50400', '<')) { |
|
| 1305 | + $isLowPHP = true; |
|
| 1306 | + $isLowPHPCS = version_compare(PHPCSHelper::getVersion(), '2.4.0', '<'); |
|
| 1307 | + } |
|
| 1308 | + } |
|
| 1309 | + |
|
| 1310 | + $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
| 1311 | + if ($next !== false |
|
| 1312 | + && ($tokens[$next]['code'] === \T_OPEN_PARENTHESIS |
|
| 1313 | + || $tokens[$next]['code'] === \T_DOUBLE_COLON) |
|
| 1314 | + ) { |
|
| 1315 | + // Function call or declaration. |
|
| 1316 | + return false; |
|
| 1317 | + } |
|
| 1318 | + |
|
| 1319 | + // Array of tokens which if found preceding the $stackPtr indicate that a T_STRING is not a global constant. |
|
| 1320 | + $tokensToIgnore = array( |
|
| 1321 | + 'T_NAMESPACE' => true, |
|
| 1322 | + 'T_USE' => true, |
|
| 1323 | + 'T_CLASS' => true, |
|
| 1324 | + 'T_TRAIT' => true, |
|
| 1325 | + 'T_INTERFACE' => true, |
|
| 1326 | + 'T_EXTENDS' => true, |
|
| 1327 | + 'T_IMPLEMENTS' => true, |
|
| 1328 | + 'T_NEW' => true, |
|
| 1329 | + 'T_FUNCTION' => true, |
|
| 1330 | + 'T_DOUBLE_COLON' => true, |
|
| 1331 | + 'T_OBJECT_OPERATOR' => true, |
|
| 1332 | + 'T_INSTANCEOF' => true, |
|
| 1333 | + 'T_INSTEADOF' => true, |
|
| 1334 | + 'T_GOTO' => true, |
|
| 1335 | + 'T_AS' => true, |
|
| 1336 | + 'T_PUBLIC' => true, |
|
| 1337 | + 'T_PROTECTED' => true, |
|
| 1338 | + 'T_PRIVATE' => true, |
|
| 1339 | + ); |
|
| 1340 | + |
|
| 1341 | + $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
| 1342 | + if ($prev !== false |
|
| 1343 | + && (isset($tokensToIgnore[$tokens[$prev]['type']]) === true |
|
| 1344 | + || ($tokens[$prev]['code'] === \T_STRING |
|
| 1345 | + && (($isLowPHPCS === true |
|
| 1346 | + && $tokens[$prev]['content'] === 'trait') |
|
| 1347 | + || ($isLowPHP === true |
|
| 1348 | + && $tokens[$prev]['content'] === 'insteadof')))) |
|
| 1349 | + ) { |
|
| 1350 | + // Not the use of a constant. |
|
| 1351 | + return false; |
|
| 1352 | + } |
|
| 1353 | + |
|
| 1354 | + if ($prev !== false |
|
| 1355 | + && $tokens[$prev]['code'] === \T_NS_SEPARATOR |
|
| 1356 | + && $tokens[($prev - 1)]['code'] === \T_STRING |
|
| 1357 | + ) { |
|
| 1358 | + // Namespaced constant of the same name. |
|
| 1359 | + return false; |
|
| 1360 | + } |
|
| 1361 | + |
|
| 1362 | + if ($prev !== false |
|
| 1363 | + && $tokens[$prev]['code'] === \T_CONST |
|
| 1364 | + && $this->isClassConstant($phpcsFile, $prev) === true |
|
| 1365 | + ) { |
|
| 1366 | + // Class constant declaration of the same name. |
|
| 1367 | + return false; |
|
| 1368 | + } |
|
| 1369 | + |
|
| 1370 | + /* |
|
| 1371 | 1371 | * Deal with a number of variations of use statements. |
| 1372 | 1372 | */ |
| 1373 | - for ($i = $stackPtr; $i > 0; $i--) { |
|
| 1374 | - if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) { |
|
| 1375 | - break; |
|
| 1376 | - } |
|
| 1377 | - } |
|
| 1378 | - |
|
| 1379 | - $firstOnLine = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true); |
|
| 1380 | - if ($firstOnLine !== false && $tokens[$firstOnLine]['code'] === \T_USE) { |
|
| 1381 | - $nextOnLine = $phpcsFile->findNext(Tokens::$emptyTokens, ($firstOnLine + 1), null, true); |
|
| 1382 | - if ($nextOnLine !== false) { |
|
| 1383 | - if (($tokens[$nextOnLine]['code'] === \T_STRING && $tokens[$nextOnLine]['content'] === 'const') |
|
| 1384 | - || $tokens[$nextOnLine]['code'] === \T_CONST // Happens in some PHPCS versions. |
|
| 1385 | - ) { |
|
| 1386 | - $hasNsSep = $phpcsFile->findNext(\T_NS_SEPARATOR, ($nextOnLine + 1), $stackPtr); |
|
| 1387 | - if ($hasNsSep !== false) { |
|
| 1388 | - // Namespaced const (group) use statement. |
|
| 1389 | - return false; |
|
| 1390 | - } |
|
| 1391 | - } else { |
|
| 1392 | - // Not a const use statement. |
|
| 1393 | - return false; |
|
| 1394 | - } |
|
| 1395 | - } |
|
| 1396 | - } |
|
| 1397 | - |
|
| 1398 | - return true; |
|
| 1399 | - } |
|
| 1400 | - |
|
| 1401 | - |
|
| 1402 | - /** |
|
| 1403 | - * Determine whether the tokens between $start and $end together form a positive number |
|
| 1404 | - * as recognized by PHP. |
|
| 1405 | - * |
|
| 1406 | - * The outcome of this function is reliable for `true`, `false` should be regarded as |
|
| 1407 | - * "undetermined". |
|
| 1408 | - * |
|
| 1409 | - * Note: Zero is *not* regarded as a positive number. |
|
| 1410 | - * |
|
| 1411 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1412 | - * @param int $start Start of the snippet (inclusive), i.e. this |
|
| 1413 | - * token will be examined as part of the snippet. |
|
| 1414 | - * @param int $end End of the snippet (inclusive), i.e. this |
|
| 1415 | - * token will be examined as part of the snippet. |
|
| 1416 | - * @param bool $allowFloats Whether to only consider integers, or also floats. |
|
| 1417 | - * |
|
| 1418 | - * @return bool True if PHP would evaluate the snippet as a positive number. |
|
| 1419 | - * False if not or if it could not be reliably determined |
|
| 1420 | - * (variable or calculations and such). |
|
| 1421 | - */ |
|
| 1422 | - public function isPositiveNumber(File $phpcsFile, $start, $end, $allowFloats = false) |
|
| 1423 | - { |
|
| 1424 | - $number = $this->isNumber($phpcsFile, $start, $end, $allowFloats); |
|
| 1425 | - |
|
| 1426 | - if ($number === false) { |
|
| 1427 | - return false; |
|
| 1428 | - } |
|
| 1429 | - |
|
| 1430 | - return ($number > 0); |
|
| 1431 | - } |
|
| 1432 | - |
|
| 1433 | - |
|
| 1434 | - /** |
|
| 1435 | - * Determine whether the tokens between $start and $end together form a negative number |
|
| 1436 | - * as recognized by PHP. |
|
| 1437 | - * |
|
| 1438 | - * The outcome of this function is reliable for `true`, `false` should be regarded as |
|
| 1439 | - * "undetermined". |
|
| 1440 | - * |
|
| 1441 | - * Note: Zero is *not* regarded as a negative number. |
|
| 1442 | - * |
|
| 1443 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1444 | - * @param int $start Start of the snippet (inclusive), i.e. this |
|
| 1445 | - * token will be examined as part of the snippet. |
|
| 1446 | - * @param int $end End of the snippet (inclusive), i.e. this |
|
| 1447 | - * token will be examined as part of the snippet. |
|
| 1448 | - * @param bool $allowFloats Whether to only consider integers, or also floats. |
|
| 1449 | - * |
|
| 1450 | - * @return bool True if PHP would evaluate the snippet as a negative number. |
|
| 1451 | - * False if not or if it could not be reliably determined |
|
| 1452 | - * (variable or calculations and such). |
|
| 1453 | - */ |
|
| 1454 | - public function isNegativeNumber(File $phpcsFile, $start, $end, $allowFloats = false) |
|
| 1455 | - { |
|
| 1456 | - $number = $this->isNumber($phpcsFile, $start, $end, $allowFloats); |
|
| 1457 | - |
|
| 1458 | - if ($number === false) { |
|
| 1459 | - return false; |
|
| 1460 | - } |
|
| 1461 | - |
|
| 1462 | - return ($number < 0); |
|
| 1463 | - } |
|
| 1464 | - |
|
| 1465 | - /** |
|
| 1466 | - * Determine whether the tokens between $start and $end together form a number |
|
| 1467 | - * as recognized by PHP. |
|
| 1468 | - * |
|
| 1469 | - * The outcome of this function is reliable for "true-ish" values, `false` should |
|
| 1470 | - * be regarded as "undetermined". |
|
| 1471 | - * |
|
| 1472 | - * @link https://3v4l.org/npTeM |
|
| 1473 | - * |
|
| 1474 | - * Mainly intended for examining variable assignments, function call parameters, array values |
|
| 1475 | - * where the start and end of the snippet to examine is very clear. |
|
| 1476 | - * |
|
| 1477 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1478 | - * @param int $start Start of the snippet (inclusive), i.e. this |
|
| 1479 | - * token will be examined as part of the snippet. |
|
| 1480 | - * @param int $end End of the snippet (inclusive), i.e. this |
|
| 1481 | - * token will be examined as part of the snippet. |
|
| 1482 | - * @param bool $allowFloats Whether to only consider integers, or also floats. |
|
| 1483 | - * |
|
| 1484 | - * @return int|float|bool The number found if PHP would evaluate the snippet as a number. |
|
| 1485 | - * The return type will be int if $allowFloats is false, if |
|
| 1486 | - * $allowFloats is true, the return type will be float. |
|
| 1487 | - * False will be returned when the snippet does not evaluate to a |
|
| 1488 | - * number or if it could not be reliably determined |
|
| 1489 | - * (variable or calculations and such). |
|
| 1490 | - */ |
|
| 1491 | - protected function isNumber(File $phpcsFile, $start, $end, $allowFloats = false) |
|
| 1492 | - { |
|
| 1493 | - $stringTokens = Tokens::$heredocTokens + Tokens::$stringTokens; |
|
| 1494 | - |
|
| 1495 | - $validTokens = array(); |
|
| 1496 | - $validTokens[\T_LNUMBER] = true; |
|
| 1497 | - $validTokens[\T_TRUE] = true; // Evaluates to int 1. |
|
| 1498 | - $validTokens[\T_FALSE] = true; // Evaluates to int 0. |
|
| 1499 | - $validTokens[\T_NULL] = true; // Evaluates to int 0. |
|
| 1500 | - |
|
| 1501 | - if ($allowFloats === true) { |
|
| 1502 | - $validTokens[\T_DNUMBER] = true; |
|
| 1503 | - } |
|
| 1504 | - |
|
| 1505 | - $maybeValidTokens = $stringTokens + $validTokens; |
|
| 1506 | - |
|
| 1507 | - $tokens = $phpcsFile->getTokens(); |
|
| 1508 | - $searchEnd = ($end + 1); |
|
| 1509 | - $negativeNumber = false; |
|
| 1510 | - |
|
| 1511 | - if (isset($tokens[$start], $tokens[$searchEnd]) === false) { |
|
| 1512 | - return false; |
|
| 1513 | - } |
|
| 1514 | - |
|
| 1515 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $start, $searchEnd, true); |
|
| 1516 | - while ($nextNonEmpty !== false |
|
| 1517 | - && ($tokens[$nextNonEmpty]['code'] === \T_PLUS |
|
| 1518 | - || $tokens[$nextNonEmpty]['code'] === \T_MINUS) |
|
| 1519 | - ) { |
|
| 1520 | - |
|
| 1521 | - if ($tokens[$nextNonEmpty]['code'] === \T_MINUS) { |
|
| 1522 | - $negativeNumber = ($negativeNumber === false) ? true : false; |
|
| 1523 | - } |
|
| 1524 | - |
|
| 1525 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextNonEmpty + 1), $searchEnd, true); |
|
| 1526 | - } |
|
| 1527 | - |
|
| 1528 | - if ($nextNonEmpty === false || isset($maybeValidTokens[$tokens[$nextNonEmpty]['code']]) === false) { |
|
| 1529 | - return false; |
|
| 1530 | - } |
|
| 1531 | - |
|
| 1532 | - $content = false; |
|
| 1533 | - if ($tokens[$nextNonEmpty]['code'] === \T_LNUMBER |
|
| 1534 | - || $tokens[$nextNonEmpty]['code'] === \T_DNUMBER |
|
| 1535 | - ) { |
|
| 1536 | - $content = (float) $tokens[$nextNonEmpty]['content']; |
|
| 1537 | - } elseif ($tokens[$nextNonEmpty]['code'] === \T_TRUE) { |
|
| 1538 | - $content = 1.0; |
|
| 1539 | - } elseif ($tokens[$nextNonEmpty]['code'] === \T_FALSE |
|
| 1540 | - || $tokens[$nextNonEmpty]['code'] === \T_NULL |
|
| 1541 | - ) { |
|
| 1542 | - $content = 0.0; |
|
| 1543 | - } elseif (isset($stringTokens[$tokens[$nextNonEmpty]['code']]) === true) { |
|
| 1544 | - |
|
| 1545 | - if ($tokens[$nextNonEmpty]['code'] === \T_START_HEREDOC |
|
| 1546 | - || $tokens[$nextNonEmpty]['code'] === \T_START_NOWDOC |
|
| 1547 | - ) { |
|
| 1548 | - // Skip past heredoc/nowdoc opener to the first content. |
|
| 1549 | - $firstDocToken = $phpcsFile->findNext(array(\T_HEREDOC, \T_NOWDOC), ($nextNonEmpty + 1), $searchEnd); |
|
| 1550 | - if ($firstDocToken === false) { |
|
| 1551 | - // Live coding or parse error. |
|
| 1552 | - return false; |
|
| 1553 | - } |
|
| 1554 | - |
|
| 1555 | - $stringContent = $content = $tokens[$firstDocToken]['content']; |
|
| 1556 | - |
|
| 1557 | - // Skip forward to the end in preparation for the next part of the examination. |
|
| 1558 | - $nextNonEmpty = $phpcsFile->findNext(array(\T_END_HEREDOC, \T_END_NOWDOC), ($nextNonEmpty + 1), $searchEnd); |
|
| 1559 | - if ($nextNonEmpty === false) { |
|
| 1560 | - // Live coding or parse error. |
|
| 1561 | - return false; |
|
| 1562 | - } |
|
| 1563 | - } else { |
|
| 1564 | - // Gather subsequent lines for a multi-line string. |
|
| 1565 | - for ($i = $nextNonEmpty; $i < $searchEnd; $i++) { |
|
| 1566 | - if ($tokens[$i]['code'] !== $tokens[$nextNonEmpty]['code']) { |
|
| 1567 | - break; |
|
| 1568 | - } |
|
| 1569 | - $content .= $tokens[$i]['content']; |
|
| 1570 | - } |
|
| 1571 | - |
|
| 1572 | - $nextNonEmpty = --$i; |
|
| 1573 | - $content = $this->stripQuotes($content); |
|
| 1574 | - $stringContent = $content; |
|
| 1575 | - } |
|
| 1576 | - |
|
| 1577 | - /* |
|
| 1373 | + for ($i = $stackPtr; $i > 0; $i--) { |
|
| 1374 | + if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) { |
|
| 1375 | + break; |
|
| 1376 | + } |
|
| 1377 | + } |
|
| 1378 | + |
|
| 1379 | + $firstOnLine = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true); |
|
| 1380 | + if ($firstOnLine !== false && $tokens[$firstOnLine]['code'] === \T_USE) { |
|
| 1381 | + $nextOnLine = $phpcsFile->findNext(Tokens::$emptyTokens, ($firstOnLine + 1), null, true); |
|
| 1382 | + if ($nextOnLine !== false) { |
|
| 1383 | + if (($tokens[$nextOnLine]['code'] === \T_STRING && $tokens[$nextOnLine]['content'] === 'const') |
|
| 1384 | + || $tokens[$nextOnLine]['code'] === \T_CONST // Happens in some PHPCS versions. |
|
| 1385 | + ) { |
|
| 1386 | + $hasNsSep = $phpcsFile->findNext(\T_NS_SEPARATOR, ($nextOnLine + 1), $stackPtr); |
|
| 1387 | + if ($hasNsSep !== false) { |
|
| 1388 | + // Namespaced const (group) use statement. |
|
| 1389 | + return false; |
|
| 1390 | + } |
|
| 1391 | + } else { |
|
| 1392 | + // Not a const use statement. |
|
| 1393 | + return false; |
|
| 1394 | + } |
|
| 1395 | + } |
|
| 1396 | + } |
|
| 1397 | + |
|
| 1398 | + return true; |
|
| 1399 | + } |
|
| 1400 | + |
|
| 1401 | + |
|
| 1402 | + /** |
|
| 1403 | + * Determine whether the tokens between $start and $end together form a positive number |
|
| 1404 | + * as recognized by PHP. |
|
| 1405 | + * |
|
| 1406 | + * The outcome of this function is reliable for `true`, `false` should be regarded as |
|
| 1407 | + * "undetermined". |
|
| 1408 | + * |
|
| 1409 | + * Note: Zero is *not* regarded as a positive number. |
|
| 1410 | + * |
|
| 1411 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1412 | + * @param int $start Start of the snippet (inclusive), i.e. this |
|
| 1413 | + * token will be examined as part of the snippet. |
|
| 1414 | + * @param int $end End of the snippet (inclusive), i.e. this |
|
| 1415 | + * token will be examined as part of the snippet. |
|
| 1416 | + * @param bool $allowFloats Whether to only consider integers, or also floats. |
|
| 1417 | + * |
|
| 1418 | + * @return bool True if PHP would evaluate the snippet as a positive number. |
|
| 1419 | + * False if not or if it could not be reliably determined |
|
| 1420 | + * (variable or calculations and such). |
|
| 1421 | + */ |
|
| 1422 | + public function isPositiveNumber(File $phpcsFile, $start, $end, $allowFloats = false) |
|
| 1423 | + { |
|
| 1424 | + $number = $this->isNumber($phpcsFile, $start, $end, $allowFloats); |
|
| 1425 | + |
|
| 1426 | + if ($number === false) { |
|
| 1427 | + return false; |
|
| 1428 | + } |
|
| 1429 | + |
|
| 1430 | + return ($number > 0); |
|
| 1431 | + } |
|
| 1432 | + |
|
| 1433 | + |
|
| 1434 | + /** |
|
| 1435 | + * Determine whether the tokens between $start and $end together form a negative number |
|
| 1436 | + * as recognized by PHP. |
|
| 1437 | + * |
|
| 1438 | + * The outcome of this function is reliable for `true`, `false` should be regarded as |
|
| 1439 | + * "undetermined". |
|
| 1440 | + * |
|
| 1441 | + * Note: Zero is *not* regarded as a negative number. |
|
| 1442 | + * |
|
| 1443 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1444 | + * @param int $start Start of the snippet (inclusive), i.e. this |
|
| 1445 | + * token will be examined as part of the snippet. |
|
| 1446 | + * @param int $end End of the snippet (inclusive), i.e. this |
|
| 1447 | + * token will be examined as part of the snippet. |
|
| 1448 | + * @param bool $allowFloats Whether to only consider integers, or also floats. |
|
| 1449 | + * |
|
| 1450 | + * @return bool True if PHP would evaluate the snippet as a negative number. |
|
| 1451 | + * False if not or if it could not be reliably determined |
|
| 1452 | + * (variable or calculations and such). |
|
| 1453 | + */ |
|
| 1454 | + public function isNegativeNumber(File $phpcsFile, $start, $end, $allowFloats = false) |
|
| 1455 | + { |
|
| 1456 | + $number = $this->isNumber($phpcsFile, $start, $end, $allowFloats); |
|
| 1457 | + |
|
| 1458 | + if ($number === false) { |
|
| 1459 | + return false; |
|
| 1460 | + } |
|
| 1461 | + |
|
| 1462 | + return ($number < 0); |
|
| 1463 | + } |
|
| 1464 | + |
|
| 1465 | + /** |
|
| 1466 | + * Determine whether the tokens between $start and $end together form a number |
|
| 1467 | + * as recognized by PHP. |
|
| 1468 | + * |
|
| 1469 | + * The outcome of this function is reliable for "true-ish" values, `false` should |
|
| 1470 | + * be regarded as "undetermined". |
|
| 1471 | + * |
|
| 1472 | + * @link https://3v4l.org/npTeM |
|
| 1473 | + * |
|
| 1474 | + * Mainly intended for examining variable assignments, function call parameters, array values |
|
| 1475 | + * where the start and end of the snippet to examine is very clear. |
|
| 1476 | + * |
|
| 1477 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1478 | + * @param int $start Start of the snippet (inclusive), i.e. this |
|
| 1479 | + * token will be examined as part of the snippet. |
|
| 1480 | + * @param int $end End of the snippet (inclusive), i.e. this |
|
| 1481 | + * token will be examined as part of the snippet. |
|
| 1482 | + * @param bool $allowFloats Whether to only consider integers, or also floats. |
|
| 1483 | + * |
|
| 1484 | + * @return int|float|bool The number found if PHP would evaluate the snippet as a number. |
|
| 1485 | + * The return type will be int if $allowFloats is false, if |
|
| 1486 | + * $allowFloats is true, the return type will be float. |
|
| 1487 | + * False will be returned when the snippet does not evaluate to a |
|
| 1488 | + * number or if it could not be reliably determined |
|
| 1489 | + * (variable or calculations and such). |
|
| 1490 | + */ |
|
| 1491 | + protected function isNumber(File $phpcsFile, $start, $end, $allowFloats = false) |
|
| 1492 | + { |
|
| 1493 | + $stringTokens = Tokens::$heredocTokens + Tokens::$stringTokens; |
|
| 1494 | + |
|
| 1495 | + $validTokens = array(); |
|
| 1496 | + $validTokens[\T_LNUMBER] = true; |
|
| 1497 | + $validTokens[\T_TRUE] = true; // Evaluates to int 1. |
|
| 1498 | + $validTokens[\T_FALSE] = true; // Evaluates to int 0. |
|
| 1499 | + $validTokens[\T_NULL] = true; // Evaluates to int 0. |
|
| 1500 | + |
|
| 1501 | + if ($allowFloats === true) { |
|
| 1502 | + $validTokens[\T_DNUMBER] = true; |
|
| 1503 | + } |
|
| 1504 | + |
|
| 1505 | + $maybeValidTokens = $stringTokens + $validTokens; |
|
| 1506 | + |
|
| 1507 | + $tokens = $phpcsFile->getTokens(); |
|
| 1508 | + $searchEnd = ($end + 1); |
|
| 1509 | + $negativeNumber = false; |
|
| 1510 | + |
|
| 1511 | + if (isset($tokens[$start], $tokens[$searchEnd]) === false) { |
|
| 1512 | + return false; |
|
| 1513 | + } |
|
| 1514 | + |
|
| 1515 | + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $start, $searchEnd, true); |
|
| 1516 | + while ($nextNonEmpty !== false |
|
| 1517 | + && ($tokens[$nextNonEmpty]['code'] === \T_PLUS |
|
| 1518 | + || $tokens[$nextNonEmpty]['code'] === \T_MINUS) |
|
| 1519 | + ) { |
|
| 1520 | + |
|
| 1521 | + if ($tokens[$nextNonEmpty]['code'] === \T_MINUS) { |
|
| 1522 | + $negativeNumber = ($negativeNumber === false) ? true : false; |
|
| 1523 | + } |
|
| 1524 | + |
|
| 1525 | + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextNonEmpty + 1), $searchEnd, true); |
|
| 1526 | + } |
|
| 1527 | + |
|
| 1528 | + if ($nextNonEmpty === false || isset($maybeValidTokens[$tokens[$nextNonEmpty]['code']]) === false) { |
|
| 1529 | + return false; |
|
| 1530 | + } |
|
| 1531 | + |
|
| 1532 | + $content = false; |
|
| 1533 | + if ($tokens[$nextNonEmpty]['code'] === \T_LNUMBER |
|
| 1534 | + || $tokens[$nextNonEmpty]['code'] === \T_DNUMBER |
|
| 1535 | + ) { |
|
| 1536 | + $content = (float) $tokens[$nextNonEmpty]['content']; |
|
| 1537 | + } elseif ($tokens[$nextNonEmpty]['code'] === \T_TRUE) { |
|
| 1538 | + $content = 1.0; |
|
| 1539 | + } elseif ($tokens[$nextNonEmpty]['code'] === \T_FALSE |
|
| 1540 | + || $tokens[$nextNonEmpty]['code'] === \T_NULL |
|
| 1541 | + ) { |
|
| 1542 | + $content = 0.0; |
|
| 1543 | + } elseif (isset($stringTokens[$tokens[$nextNonEmpty]['code']]) === true) { |
|
| 1544 | + |
|
| 1545 | + if ($tokens[$nextNonEmpty]['code'] === \T_START_HEREDOC |
|
| 1546 | + || $tokens[$nextNonEmpty]['code'] === \T_START_NOWDOC |
|
| 1547 | + ) { |
|
| 1548 | + // Skip past heredoc/nowdoc opener to the first content. |
|
| 1549 | + $firstDocToken = $phpcsFile->findNext(array(\T_HEREDOC, \T_NOWDOC), ($nextNonEmpty + 1), $searchEnd); |
|
| 1550 | + if ($firstDocToken === false) { |
|
| 1551 | + // Live coding or parse error. |
|
| 1552 | + return false; |
|
| 1553 | + } |
|
| 1554 | + |
|
| 1555 | + $stringContent = $content = $tokens[$firstDocToken]['content']; |
|
| 1556 | + |
|
| 1557 | + // Skip forward to the end in preparation for the next part of the examination. |
|
| 1558 | + $nextNonEmpty = $phpcsFile->findNext(array(\T_END_HEREDOC, \T_END_NOWDOC), ($nextNonEmpty + 1), $searchEnd); |
|
| 1559 | + if ($nextNonEmpty === false) { |
|
| 1560 | + // Live coding or parse error. |
|
| 1561 | + return false; |
|
| 1562 | + } |
|
| 1563 | + } else { |
|
| 1564 | + // Gather subsequent lines for a multi-line string. |
|
| 1565 | + for ($i = $nextNonEmpty; $i < $searchEnd; $i++) { |
|
| 1566 | + if ($tokens[$i]['code'] !== $tokens[$nextNonEmpty]['code']) { |
|
| 1567 | + break; |
|
| 1568 | + } |
|
| 1569 | + $content .= $tokens[$i]['content']; |
|
| 1570 | + } |
|
| 1571 | + |
|
| 1572 | + $nextNonEmpty = --$i; |
|
| 1573 | + $content = $this->stripQuotes($content); |
|
| 1574 | + $stringContent = $content; |
|
| 1575 | + } |
|
| 1576 | + |
|
| 1577 | + /* |
|
| 1578 | 1578 | * Regexes based on the formats outlined in the manual, created by JRF. |
| 1579 | 1579 | * @link http://php.net/manual/en/language.types.float.php |
| 1580 | 1580 | */ |
| 1581 | - $regexInt = '`^\s*[0-9]+`'; |
|
| 1582 | - $regexFloat = '`^\s*(?:[+-]?(?:(?:(?P<LNUM>[0-9]+)|(?P<DNUM>([0-9]*\.(?P>LNUM)|(?P>LNUM)\.[0-9]*)))[eE][+-]?(?P>LNUM))|(?P>DNUM))`'; |
|
| 1583 | - |
|
| 1584 | - $intString = preg_match($regexInt, $content, $intMatch); |
|
| 1585 | - $floatString = preg_match($regexFloat, $content, $floatMatch); |
|
| 1586 | - |
|
| 1587 | - // Does the text string start with a number ? If so, PHP would juggle it and use it as a number. |
|
| 1588 | - if ($allowFloats === false) { |
|
| 1589 | - if ($intString !== 1 || $floatString === 1) { |
|
| 1590 | - if ($floatString === 1) { |
|
| 1591 | - // Found float. Only integers targetted. |
|
| 1592 | - return false; |
|
| 1593 | - } |
|
| 1594 | - |
|
| 1595 | - $content = 0.0; |
|
| 1596 | - } else { |
|
| 1597 | - $content = (float) trim($intMatch[0]); |
|
| 1598 | - } |
|
| 1599 | - } else { |
|
| 1600 | - if ($intString !== 1 && $floatString !== 1) { |
|
| 1601 | - $content = 0.0; |
|
| 1602 | - } else { |
|
| 1603 | - $content = ($floatString === 1) ? (float) trim($floatMatch[0]) : (float) trim($intMatch[0]); |
|
| 1604 | - } |
|
| 1605 | - } |
|
| 1606 | - |
|
| 1607 | - // Allow for different behaviour for hex numeric strings between PHP 5 vs PHP 7. |
|
| 1608 | - if ($intString === 1 && trim($intMatch[0]) === '0' |
|
| 1609 | - && preg_match('`^\s*(0x[A-Fa-f0-9]+)`', $stringContent, $hexNumberString) === 1 |
|
| 1610 | - && $this->supportsBelow('5.6') === true |
|
| 1611 | - ) { |
|
| 1612 | - // The filter extension still allows for hex numeric strings in PHP 7, so |
|
| 1613 | - // use that to get the numeric value if possible. |
|
| 1614 | - // If the filter extension is not available, the value will be zero, but so be it. |
|
| 1615 | - if (function_exists('filter_var')) { |
|
| 1616 | - $filtered = filter_var($hexNumberString[1], \FILTER_VALIDATE_INT, \FILTER_FLAG_ALLOW_HEX); |
|
| 1617 | - if ($filtered !== false) { |
|
| 1618 | - $content = $filtered; |
|
| 1619 | - } |
|
| 1620 | - } |
|
| 1621 | - } |
|
| 1622 | - } |
|
| 1623 | - |
|
| 1624 | - // OK, so we have a number, now is there still more code after it ? |
|
| 1625 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextNonEmpty + 1), $searchEnd, true); |
|
| 1626 | - if ($nextNonEmpty !== false) { |
|
| 1627 | - return false; |
|
| 1628 | - } |
|
| 1629 | - |
|
| 1630 | - if ($negativeNumber === true) { |
|
| 1631 | - $content = -$content; |
|
| 1632 | - } |
|
| 1633 | - |
|
| 1634 | - if ($allowFloats === false) { |
|
| 1635 | - return (int) $content; |
|
| 1636 | - } |
|
| 1637 | - |
|
| 1638 | - return $content; |
|
| 1639 | - } |
|
| 1640 | - |
|
| 1641 | - |
|
| 1642 | - /** |
|
| 1643 | - * Determine whether the tokens between $start and $end together form a numberic calculation |
|
| 1644 | - * as recognized by PHP. |
|
| 1645 | - * |
|
| 1646 | - * The outcome of this function is reliable for `true`, `false` should be regarded as "undetermined". |
|
| 1647 | - * |
|
| 1648 | - * Mainly intended for examining variable assignments, function call parameters, array values |
|
| 1649 | - * where the start and end of the snippet to examine is very clear. |
|
| 1650 | - * |
|
| 1651 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1652 | - * @param int $start Start of the snippet (inclusive), i.e. this |
|
| 1653 | - * token will be examined as part of the snippet. |
|
| 1654 | - * @param int $end End of the snippet (inclusive), i.e. this |
|
| 1655 | - * token will be examined as part of the snippet. |
|
| 1656 | - * |
|
| 1657 | - * @return bool |
|
| 1658 | - */ |
|
| 1659 | - protected function isNumericCalculation(File $phpcsFile, $start, $end) |
|
| 1660 | - { |
|
| 1661 | - $arithmeticTokens = Tokens::$arithmeticTokens; |
|
| 1662 | - |
|
| 1663 | - // phpcs:disable PHPCompatibility.Constants.NewConstants.t_powFound |
|
| 1664 | - if (\defined('T_POW') && isset($arithmeticTokens[\T_POW]) === false) { |
|
| 1665 | - // T_POW was not added to the arithmetic array until PHPCS 2.9.0. |
|
| 1666 | - $arithmeticTokens[\T_POW] = \T_POW; |
|
| 1667 | - } |
|
| 1668 | - // phpcs:enable |
|
| 1669 | - |
|
| 1670 | - $skipTokens = Tokens::$emptyTokens; |
|
| 1671 | - $skipTokens[] = \T_MINUS; |
|
| 1672 | - $skipTokens[] = \T_PLUS; |
|
| 1673 | - |
|
| 1674 | - // Find the first arithmetic operator, but skip past +/- signs before numbers. |
|
| 1675 | - $nextNonEmpty = ($start - 1); |
|
| 1676 | - do { |
|
| 1677 | - $nextNonEmpty = $phpcsFile->findNext($skipTokens, ($nextNonEmpty + 1), ($end + 1), true); |
|
| 1678 | - $arithmeticOperator = $phpcsFile->findNext($arithmeticTokens, ($nextNonEmpty + 1), ($end + 1)); |
|
| 1679 | - } while ($nextNonEmpty !== false && $arithmeticOperator !== false && $nextNonEmpty === $arithmeticOperator); |
|
| 1680 | - |
|
| 1681 | - if ($arithmeticOperator === false) { |
|
| 1682 | - return false; |
|
| 1683 | - } |
|
| 1684 | - |
|
| 1685 | - $tokens = $phpcsFile->getTokens(); |
|
| 1686 | - $subsetStart = $start; |
|
| 1687 | - $subsetEnd = ($arithmeticOperator - 1); |
|
| 1688 | - |
|
| 1689 | - while ($this->isNumber($phpcsFile, $subsetStart, $subsetEnd, true) !== false |
|
| 1690 | - && isset($tokens[($arithmeticOperator + 1)]) === true |
|
| 1691 | - ) { |
|
| 1692 | - // Recognize T_POW for PHPCS < 2.4.0 on low PHP versions. |
|
| 1693 | - if (\defined('T_POW') === false |
|
| 1694 | - && $tokens[$arithmeticOperator]['code'] === \T_MULTIPLY |
|
| 1695 | - && $tokens[($arithmeticOperator + 1)]['code'] === \T_MULTIPLY |
|
| 1696 | - && isset($tokens[$arithmeticOperator + 2]) === true |
|
| 1697 | - ) { |
|
| 1698 | - // Move operator one forward to the second * in T_POW. |
|
| 1699 | - ++$arithmeticOperator; |
|
| 1700 | - } |
|
| 1701 | - |
|
| 1702 | - $subsetStart = ($arithmeticOperator + 1); |
|
| 1703 | - $nextNonEmpty = $arithmeticOperator; |
|
| 1704 | - do { |
|
| 1705 | - $nextNonEmpty = $phpcsFile->findNext($skipTokens, ($nextNonEmpty + 1), ($end + 1), true); |
|
| 1706 | - $arithmeticOperator = $phpcsFile->findNext($arithmeticTokens, ($nextNonEmpty + 1), ($end + 1)); |
|
| 1707 | - } while ($nextNonEmpty !== false && $arithmeticOperator !== false && $nextNonEmpty === $arithmeticOperator); |
|
| 1708 | - |
|
| 1709 | - if ($arithmeticOperator === false) { |
|
| 1710 | - // Last calculation operator already reached. |
|
| 1711 | - if ($this->isNumber($phpcsFile, $subsetStart, $end, true) !== false) { |
|
| 1712 | - return true; |
|
| 1713 | - } |
|
| 1714 | - |
|
| 1715 | - return false; |
|
| 1716 | - } |
|
| 1717 | - |
|
| 1718 | - $subsetEnd = ($arithmeticOperator - 1); |
|
| 1719 | - } |
|
| 1720 | - |
|
| 1721 | - return false; |
|
| 1722 | - } |
|
| 1723 | - |
|
| 1724 | - |
|
| 1725 | - |
|
| 1726 | - /** |
|
| 1727 | - * Determine whether a ternary is a short ternary, i.e. without "middle". |
|
| 1728 | - * |
|
| 1729 | - * N.B.: This is a back-fill for a new method which is expected to go into |
|
| 1730 | - * PHP_CodeSniffer 3.5.0. |
|
| 1731 | - * Once that method has been merged into PHPCS, this one should be moved |
|
| 1732 | - * to the PHPCSHelper.php file. |
|
| 1733 | - * |
|
| 1734 | - * @since 9.2.0 |
|
| 1735 | - * |
|
| 1736 | - * @codeCoverageIgnore Method as pulled upstream is accompanied by unit tests. |
|
| 1737 | - * |
|
| 1738 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1739 | - * @param int $stackPtr The position of the ternary operator |
|
| 1740 | - * in the stack. |
|
| 1741 | - * |
|
| 1742 | - * @return bool True if short ternary, or false otherwise. |
|
| 1743 | - */ |
|
| 1744 | - public function isShortTernary(File $phpcsFile, $stackPtr) |
|
| 1745 | - { |
|
| 1746 | - $tokens = $phpcsFile->getTokens(); |
|
| 1747 | - if (isset($tokens[$stackPtr]) === false |
|
| 1748 | - || $tokens[$stackPtr]['code'] !== \T_INLINE_THEN |
|
| 1749 | - ) { |
|
| 1750 | - return false; |
|
| 1751 | - } |
|
| 1752 | - |
|
| 1753 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
| 1754 | - if ($nextNonEmpty === false) { |
|
| 1755 | - // Live coding or parse error. |
|
| 1756 | - return false; |
|
| 1757 | - } |
|
| 1758 | - |
|
| 1759 | - if ($tokens[$nextNonEmpty]['code'] === \T_INLINE_ELSE) { |
|
| 1760 | - return true; |
|
| 1761 | - } |
|
| 1762 | - |
|
| 1763 | - return false; |
|
| 1764 | - } |
|
| 1765 | - |
|
| 1766 | - |
|
| 1767 | - /** |
|
| 1768 | - * Determine whether a T_OPEN/CLOSE_SHORT_ARRAY token is a list() construct. |
|
| 1769 | - * |
|
| 1770 | - * Note: A variety of PHPCS versions have bugs in the tokenizing of short arrays. |
|
| 1771 | - * In that case, the tokens are identified as T_OPEN/CLOSE_SQUARE_BRACKET. |
|
| 1772 | - * |
|
| 1773 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1774 | - * @param int $stackPtr The position of the function call token. |
|
| 1775 | - * |
|
| 1776 | - * @return bool |
|
| 1777 | - */ |
|
| 1778 | - public function isShortList(File $phpcsFile, $stackPtr) |
|
| 1779 | - { |
|
| 1780 | - $tokens = $phpcsFile->getTokens(); |
|
| 1781 | - |
|
| 1782 | - // Check for the existence of the token. |
|
| 1783 | - if (isset($tokens[$stackPtr]) === false) { |
|
| 1784 | - return false; |
|
| 1785 | - } |
|
| 1786 | - |
|
| 1787 | - // Is this one of the tokens this function handles ? |
|
| 1788 | - if ($tokens[$stackPtr]['code'] !== \T_OPEN_SHORT_ARRAY |
|
| 1789 | - && $tokens[$stackPtr]['code'] !== \T_CLOSE_SHORT_ARRAY |
|
| 1790 | - ) { |
|
| 1791 | - return false; |
|
| 1792 | - } |
|
| 1793 | - |
|
| 1794 | - switch ($tokens[$stackPtr]['code']) { |
|
| 1795 | - case \T_OPEN_SHORT_ARRAY: |
|
| 1796 | - if (isset($tokens[$stackPtr]['bracket_closer']) === true) { |
|
| 1797 | - $opener = $stackPtr; |
|
| 1798 | - $closer = $tokens[$stackPtr]['bracket_closer']; |
|
| 1799 | - } |
|
| 1800 | - break; |
|
| 1801 | - |
|
| 1802 | - case \T_CLOSE_SHORT_ARRAY: |
|
| 1803 | - if (isset($tokens[$stackPtr]['bracket_opener']) === true) { |
|
| 1804 | - $opener = $tokens[$stackPtr]['bracket_opener']; |
|
| 1805 | - $closer = $stackPtr; |
|
| 1806 | - } |
|
| 1807 | - break; |
|
| 1808 | - } |
|
| 1809 | - |
|
| 1810 | - if (isset($opener, $closer) === false) { |
|
| 1811 | - // Parse error, live coding or real square bracket. |
|
| 1812 | - return false; |
|
| 1813 | - } |
|
| 1814 | - |
|
| 1815 | - /* |
|
| 1581 | + $regexInt = '`^\s*[0-9]+`'; |
|
| 1582 | + $regexFloat = '`^\s*(?:[+-]?(?:(?:(?P<LNUM>[0-9]+)|(?P<DNUM>([0-9]*\.(?P>LNUM)|(?P>LNUM)\.[0-9]*)))[eE][+-]?(?P>LNUM))|(?P>DNUM))`'; |
|
| 1583 | + |
|
| 1584 | + $intString = preg_match($regexInt, $content, $intMatch); |
|
| 1585 | + $floatString = preg_match($regexFloat, $content, $floatMatch); |
|
| 1586 | + |
|
| 1587 | + // Does the text string start with a number ? If so, PHP would juggle it and use it as a number. |
|
| 1588 | + if ($allowFloats === false) { |
|
| 1589 | + if ($intString !== 1 || $floatString === 1) { |
|
| 1590 | + if ($floatString === 1) { |
|
| 1591 | + // Found float. Only integers targetted. |
|
| 1592 | + return false; |
|
| 1593 | + } |
|
| 1594 | + |
|
| 1595 | + $content = 0.0; |
|
| 1596 | + } else { |
|
| 1597 | + $content = (float) trim($intMatch[0]); |
|
| 1598 | + } |
|
| 1599 | + } else { |
|
| 1600 | + if ($intString !== 1 && $floatString !== 1) { |
|
| 1601 | + $content = 0.0; |
|
| 1602 | + } else { |
|
| 1603 | + $content = ($floatString === 1) ? (float) trim($floatMatch[0]) : (float) trim($intMatch[0]); |
|
| 1604 | + } |
|
| 1605 | + } |
|
| 1606 | + |
|
| 1607 | + // Allow for different behaviour for hex numeric strings between PHP 5 vs PHP 7. |
|
| 1608 | + if ($intString === 1 && trim($intMatch[0]) === '0' |
|
| 1609 | + && preg_match('`^\s*(0x[A-Fa-f0-9]+)`', $stringContent, $hexNumberString) === 1 |
|
| 1610 | + && $this->supportsBelow('5.6') === true |
|
| 1611 | + ) { |
|
| 1612 | + // The filter extension still allows for hex numeric strings in PHP 7, so |
|
| 1613 | + // use that to get the numeric value if possible. |
|
| 1614 | + // If the filter extension is not available, the value will be zero, but so be it. |
|
| 1615 | + if (function_exists('filter_var')) { |
|
| 1616 | + $filtered = filter_var($hexNumberString[1], \FILTER_VALIDATE_INT, \FILTER_FLAG_ALLOW_HEX); |
|
| 1617 | + if ($filtered !== false) { |
|
| 1618 | + $content = $filtered; |
|
| 1619 | + } |
|
| 1620 | + } |
|
| 1621 | + } |
|
| 1622 | + } |
|
| 1623 | + |
|
| 1624 | + // OK, so we have a number, now is there still more code after it ? |
|
| 1625 | + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextNonEmpty + 1), $searchEnd, true); |
|
| 1626 | + if ($nextNonEmpty !== false) { |
|
| 1627 | + return false; |
|
| 1628 | + } |
|
| 1629 | + |
|
| 1630 | + if ($negativeNumber === true) { |
|
| 1631 | + $content = -$content; |
|
| 1632 | + } |
|
| 1633 | + |
|
| 1634 | + if ($allowFloats === false) { |
|
| 1635 | + return (int) $content; |
|
| 1636 | + } |
|
| 1637 | + |
|
| 1638 | + return $content; |
|
| 1639 | + } |
|
| 1640 | + |
|
| 1641 | + |
|
| 1642 | + /** |
|
| 1643 | + * Determine whether the tokens between $start and $end together form a numberic calculation |
|
| 1644 | + * as recognized by PHP. |
|
| 1645 | + * |
|
| 1646 | + * The outcome of this function is reliable for `true`, `false` should be regarded as "undetermined". |
|
| 1647 | + * |
|
| 1648 | + * Mainly intended for examining variable assignments, function call parameters, array values |
|
| 1649 | + * where the start and end of the snippet to examine is very clear. |
|
| 1650 | + * |
|
| 1651 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1652 | + * @param int $start Start of the snippet (inclusive), i.e. this |
|
| 1653 | + * token will be examined as part of the snippet. |
|
| 1654 | + * @param int $end End of the snippet (inclusive), i.e. this |
|
| 1655 | + * token will be examined as part of the snippet. |
|
| 1656 | + * |
|
| 1657 | + * @return bool |
|
| 1658 | + */ |
|
| 1659 | + protected function isNumericCalculation(File $phpcsFile, $start, $end) |
|
| 1660 | + { |
|
| 1661 | + $arithmeticTokens = Tokens::$arithmeticTokens; |
|
| 1662 | + |
|
| 1663 | + // phpcs:disable PHPCompatibility.Constants.NewConstants.t_powFound |
|
| 1664 | + if (\defined('T_POW') && isset($arithmeticTokens[\T_POW]) === false) { |
|
| 1665 | + // T_POW was not added to the arithmetic array until PHPCS 2.9.0. |
|
| 1666 | + $arithmeticTokens[\T_POW] = \T_POW; |
|
| 1667 | + } |
|
| 1668 | + // phpcs:enable |
|
| 1669 | + |
|
| 1670 | + $skipTokens = Tokens::$emptyTokens; |
|
| 1671 | + $skipTokens[] = \T_MINUS; |
|
| 1672 | + $skipTokens[] = \T_PLUS; |
|
| 1673 | + |
|
| 1674 | + // Find the first arithmetic operator, but skip past +/- signs before numbers. |
|
| 1675 | + $nextNonEmpty = ($start - 1); |
|
| 1676 | + do { |
|
| 1677 | + $nextNonEmpty = $phpcsFile->findNext($skipTokens, ($nextNonEmpty + 1), ($end + 1), true); |
|
| 1678 | + $arithmeticOperator = $phpcsFile->findNext($arithmeticTokens, ($nextNonEmpty + 1), ($end + 1)); |
|
| 1679 | + } while ($nextNonEmpty !== false && $arithmeticOperator !== false && $nextNonEmpty === $arithmeticOperator); |
|
| 1680 | + |
|
| 1681 | + if ($arithmeticOperator === false) { |
|
| 1682 | + return false; |
|
| 1683 | + } |
|
| 1684 | + |
|
| 1685 | + $tokens = $phpcsFile->getTokens(); |
|
| 1686 | + $subsetStart = $start; |
|
| 1687 | + $subsetEnd = ($arithmeticOperator - 1); |
|
| 1688 | + |
|
| 1689 | + while ($this->isNumber($phpcsFile, $subsetStart, $subsetEnd, true) !== false |
|
| 1690 | + && isset($tokens[($arithmeticOperator + 1)]) === true |
|
| 1691 | + ) { |
|
| 1692 | + // Recognize T_POW for PHPCS < 2.4.0 on low PHP versions. |
|
| 1693 | + if (\defined('T_POW') === false |
|
| 1694 | + && $tokens[$arithmeticOperator]['code'] === \T_MULTIPLY |
|
| 1695 | + && $tokens[($arithmeticOperator + 1)]['code'] === \T_MULTIPLY |
|
| 1696 | + && isset($tokens[$arithmeticOperator + 2]) === true |
|
| 1697 | + ) { |
|
| 1698 | + // Move operator one forward to the second * in T_POW. |
|
| 1699 | + ++$arithmeticOperator; |
|
| 1700 | + } |
|
| 1701 | + |
|
| 1702 | + $subsetStart = ($arithmeticOperator + 1); |
|
| 1703 | + $nextNonEmpty = $arithmeticOperator; |
|
| 1704 | + do { |
|
| 1705 | + $nextNonEmpty = $phpcsFile->findNext($skipTokens, ($nextNonEmpty + 1), ($end + 1), true); |
|
| 1706 | + $arithmeticOperator = $phpcsFile->findNext($arithmeticTokens, ($nextNonEmpty + 1), ($end + 1)); |
|
| 1707 | + } while ($nextNonEmpty !== false && $arithmeticOperator !== false && $nextNonEmpty === $arithmeticOperator); |
|
| 1708 | + |
|
| 1709 | + if ($arithmeticOperator === false) { |
|
| 1710 | + // Last calculation operator already reached. |
|
| 1711 | + if ($this->isNumber($phpcsFile, $subsetStart, $end, true) !== false) { |
|
| 1712 | + return true; |
|
| 1713 | + } |
|
| 1714 | + |
|
| 1715 | + return false; |
|
| 1716 | + } |
|
| 1717 | + |
|
| 1718 | + $subsetEnd = ($arithmeticOperator - 1); |
|
| 1719 | + } |
|
| 1720 | + |
|
| 1721 | + return false; |
|
| 1722 | + } |
|
| 1723 | + |
|
| 1724 | + |
|
| 1725 | + |
|
| 1726 | + /** |
|
| 1727 | + * Determine whether a ternary is a short ternary, i.e. without "middle". |
|
| 1728 | + * |
|
| 1729 | + * N.B.: This is a back-fill for a new method which is expected to go into |
|
| 1730 | + * PHP_CodeSniffer 3.5.0. |
|
| 1731 | + * Once that method has been merged into PHPCS, this one should be moved |
|
| 1732 | + * to the PHPCSHelper.php file. |
|
| 1733 | + * |
|
| 1734 | + * @since 9.2.0 |
|
| 1735 | + * |
|
| 1736 | + * @codeCoverageIgnore Method as pulled upstream is accompanied by unit tests. |
|
| 1737 | + * |
|
| 1738 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1739 | + * @param int $stackPtr The position of the ternary operator |
|
| 1740 | + * in the stack. |
|
| 1741 | + * |
|
| 1742 | + * @return bool True if short ternary, or false otherwise. |
|
| 1743 | + */ |
|
| 1744 | + public function isShortTernary(File $phpcsFile, $stackPtr) |
|
| 1745 | + { |
|
| 1746 | + $tokens = $phpcsFile->getTokens(); |
|
| 1747 | + if (isset($tokens[$stackPtr]) === false |
|
| 1748 | + || $tokens[$stackPtr]['code'] !== \T_INLINE_THEN |
|
| 1749 | + ) { |
|
| 1750 | + return false; |
|
| 1751 | + } |
|
| 1752 | + |
|
| 1753 | + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
| 1754 | + if ($nextNonEmpty === false) { |
|
| 1755 | + // Live coding or parse error. |
|
| 1756 | + return false; |
|
| 1757 | + } |
|
| 1758 | + |
|
| 1759 | + if ($tokens[$nextNonEmpty]['code'] === \T_INLINE_ELSE) { |
|
| 1760 | + return true; |
|
| 1761 | + } |
|
| 1762 | + |
|
| 1763 | + return false; |
|
| 1764 | + } |
|
| 1765 | + |
|
| 1766 | + |
|
| 1767 | + /** |
|
| 1768 | + * Determine whether a T_OPEN/CLOSE_SHORT_ARRAY token is a list() construct. |
|
| 1769 | + * |
|
| 1770 | + * Note: A variety of PHPCS versions have bugs in the tokenizing of short arrays. |
|
| 1771 | + * In that case, the tokens are identified as T_OPEN/CLOSE_SQUARE_BRACKET. |
|
| 1772 | + * |
|
| 1773 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1774 | + * @param int $stackPtr The position of the function call token. |
|
| 1775 | + * |
|
| 1776 | + * @return bool |
|
| 1777 | + */ |
|
| 1778 | + public function isShortList(File $phpcsFile, $stackPtr) |
|
| 1779 | + { |
|
| 1780 | + $tokens = $phpcsFile->getTokens(); |
|
| 1781 | + |
|
| 1782 | + // Check for the existence of the token. |
|
| 1783 | + if (isset($tokens[$stackPtr]) === false) { |
|
| 1784 | + return false; |
|
| 1785 | + } |
|
| 1786 | + |
|
| 1787 | + // Is this one of the tokens this function handles ? |
|
| 1788 | + if ($tokens[$stackPtr]['code'] !== \T_OPEN_SHORT_ARRAY |
|
| 1789 | + && $tokens[$stackPtr]['code'] !== \T_CLOSE_SHORT_ARRAY |
|
| 1790 | + ) { |
|
| 1791 | + return false; |
|
| 1792 | + } |
|
| 1793 | + |
|
| 1794 | + switch ($tokens[$stackPtr]['code']) { |
|
| 1795 | + case \T_OPEN_SHORT_ARRAY: |
|
| 1796 | + if (isset($tokens[$stackPtr]['bracket_closer']) === true) { |
|
| 1797 | + $opener = $stackPtr; |
|
| 1798 | + $closer = $tokens[$stackPtr]['bracket_closer']; |
|
| 1799 | + } |
|
| 1800 | + break; |
|
| 1801 | + |
|
| 1802 | + case \T_CLOSE_SHORT_ARRAY: |
|
| 1803 | + if (isset($tokens[$stackPtr]['bracket_opener']) === true) { |
|
| 1804 | + $opener = $tokens[$stackPtr]['bracket_opener']; |
|
| 1805 | + $closer = $stackPtr; |
|
| 1806 | + } |
|
| 1807 | + break; |
|
| 1808 | + } |
|
| 1809 | + |
|
| 1810 | + if (isset($opener, $closer) === false) { |
|
| 1811 | + // Parse error, live coding or real square bracket. |
|
| 1812 | + return false; |
|
| 1813 | + } |
|
| 1814 | + |
|
| 1815 | + /* |
|
| 1816 | 1816 | * PHPCS cross-version compatibility: work around for square brackets misidentified |
| 1817 | 1817 | * as short array when preceded by a variable variable in older PHPCS versions. |
| 1818 | 1818 | */ |
| 1819 | - $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($opener - 1), null, true, null, true); |
|
| 1820 | - |
|
| 1821 | - if ($prevNonEmpty !== false |
|
| 1822 | - && $tokens[$prevNonEmpty]['code'] === \T_CLOSE_CURLY_BRACKET |
|
| 1823 | - && isset($tokens[$prevNonEmpty]['bracket_opener']) === true |
|
| 1824 | - ) { |
|
| 1825 | - $maybeVariableVariable = $phpcsFile->findPrevious( |
|
| 1826 | - Tokens::$emptyTokens, |
|
| 1827 | - ($tokens[$prevNonEmpty]['bracket_opener'] - 1), |
|
| 1828 | - null, |
|
| 1829 | - true, |
|
| 1830 | - null, |
|
| 1831 | - true |
|
| 1832 | - ); |
|
| 1833 | - |
|
| 1834 | - if ($tokens[$maybeVariableVariable]['code'] === \T_VARIABLE |
|
| 1835 | - || $tokens[$maybeVariableVariable]['code'] === \T_DOLLAR |
|
| 1836 | - ) { |
|
| 1837 | - return false; |
|
| 1838 | - } |
|
| 1839 | - } |
|
| 1840 | - |
|
| 1841 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($closer + 1), null, true, null, true); |
|
| 1842 | - |
|
| 1843 | - if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === \T_EQUAL) { |
|
| 1844 | - return true; |
|
| 1845 | - } |
|
| 1846 | - |
|
| 1847 | - if ($prevNonEmpty !== false |
|
| 1848 | - && $tokens[$prevNonEmpty]['code'] === \T_AS |
|
| 1849 | - && isset($tokens[$prevNonEmpty]['nested_parenthesis']) === true |
|
| 1850 | - ) { |
|
| 1851 | - $parentheses = array_reverse($tokens[$prevNonEmpty]['nested_parenthesis'], true); |
|
| 1852 | - foreach ($parentheses as $open => $close) { |
|
| 1853 | - if (isset($tokens[$open]['parenthesis_owner']) |
|
| 1854 | - && $tokens[$tokens[$open]['parenthesis_owner']]['code'] === \T_FOREACH |
|
| 1855 | - ) { |
|
| 1856 | - return true; |
|
| 1857 | - } |
|
| 1858 | - } |
|
| 1859 | - } |
|
| 1860 | - |
|
| 1861 | - // Maybe this is a short list syntax nested inside another short list syntax ? |
|
| 1862 | - $parentOpener = $opener; |
|
| 1863 | - do { |
|
| 1864 | - $parentOpener = $phpcsFile->findPrevious( |
|
| 1865 | - array(\T_OPEN_SHORT_ARRAY, \T_OPEN_SQUARE_BRACKET), |
|
| 1866 | - ($parentOpener - 1), |
|
| 1867 | - null, |
|
| 1868 | - false, |
|
| 1869 | - null, |
|
| 1870 | - true |
|
| 1871 | - ); |
|
| 1872 | - |
|
| 1873 | - if ($parentOpener === false) { |
|
| 1874 | - return false; |
|
| 1875 | - } |
|
| 1876 | - |
|
| 1877 | - } while (isset($tokens[$parentOpener]['bracket_closer']) === true |
|
| 1878 | - && $tokens[$parentOpener]['bracket_closer'] < $opener |
|
| 1879 | - ); |
|
| 1880 | - |
|
| 1881 | - if (isset($tokens[$parentOpener]['bracket_closer']) === true |
|
| 1882 | - && $tokens[$parentOpener]['bracket_closer'] > $closer |
|
| 1883 | - ) { |
|
| 1884 | - // Work around tokenizer issue in PHPCS 2.0 - 2.7. |
|
| 1885 | - $phpcsVersion = PHPCSHelper::getVersion(); |
|
| 1886 | - if ((version_compare($phpcsVersion, '2.0', '>') === true |
|
| 1887 | - && version_compare($phpcsVersion, '2.8', '<') === true) |
|
| 1888 | - && $tokens[$parentOpener]['code'] === \T_OPEN_SQUARE_BRACKET |
|
| 1889 | - ) { |
|
| 1890 | - $nextNonEmpty = $phpcsFile->findNext( |
|
| 1891 | - Tokens::$emptyTokens, |
|
| 1892 | - ($tokens[$parentOpener]['bracket_closer'] + 1), |
|
| 1893 | - null, |
|
| 1894 | - true, |
|
| 1895 | - null, |
|
| 1896 | - true |
|
| 1897 | - ); |
|
| 1898 | - |
|
| 1899 | - if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === \T_EQUAL) { |
|
| 1900 | - return true; |
|
| 1901 | - } |
|
| 1902 | - |
|
| 1903 | - return false; |
|
| 1904 | - } |
|
| 1905 | - |
|
| 1906 | - return $this->isShortList($phpcsFile, $parentOpener); |
|
| 1907 | - } |
|
| 1908 | - |
|
| 1909 | - return false; |
|
| 1910 | - } |
|
| 1911 | - |
|
| 1912 | - |
|
| 1913 | - /** |
|
| 1914 | - * Determine whether the tokens between $start and $end could together represent a variable. |
|
| 1915 | - * |
|
| 1916 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1917 | - * @param int $start Starting point stack pointer. Inclusive. |
|
| 1918 | - * I.e. this token should be taken into account. |
|
| 1919 | - * @param int $end End point stack pointer. Exclusive. |
|
| 1920 | - * I.e. this token should not be taken into account. |
|
| 1921 | - * @param int $targetNestingLevel The nesting level the variable should be at. |
|
| 1922 | - * |
|
| 1923 | - * @return bool |
|
| 1924 | - */ |
|
| 1925 | - public function isVariable(File $phpcsFile, $start, $end, $targetNestingLevel) |
|
| 1926 | - { |
|
| 1927 | - static $tokenBlackList, $bracketTokens; |
|
| 1928 | - |
|
| 1929 | - // Create the token arrays only once. |
|
| 1930 | - if (isset($tokenBlackList, $bracketTokens) === false) { |
|
| 1931 | - |
|
| 1932 | - $tokenBlackList = array( |
|
| 1933 | - \T_OPEN_PARENTHESIS => \T_OPEN_PARENTHESIS, |
|
| 1934 | - \T_STRING_CONCAT => \T_STRING_CONCAT, |
|
| 1935 | - ); |
|
| 1936 | - $tokenBlackList += Tokens::$assignmentTokens; |
|
| 1937 | - $tokenBlackList += Tokens::$equalityTokens; |
|
| 1938 | - $tokenBlackList += Tokens::$comparisonTokens; |
|
| 1939 | - $tokenBlackList += Tokens::$operators; |
|
| 1940 | - $tokenBlackList += Tokens::$booleanOperators; |
|
| 1941 | - $tokenBlackList += Tokens::$castTokens; |
|
| 1942 | - |
|
| 1943 | - /* |
|
| 1819 | + $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($opener - 1), null, true, null, true); |
|
| 1820 | + |
|
| 1821 | + if ($prevNonEmpty !== false |
|
| 1822 | + && $tokens[$prevNonEmpty]['code'] === \T_CLOSE_CURLY_BRACKET |
|
| 1823 | + && isset($tokens[$prevNonEmpty]['bracket_opener']) === true |
|
| 1824 | + ) { |
|
| 1825 | + $maybeVariableVariable = $phpcsFile->findPrevious( |
|
| 1826 | + Tokens::$emptyTokens, |
|
| 1827 | + ($tokens[$prevNonEmpty]['bracket_opener'] - 1), |
|
| 1828 | + null, |
|
| 1829 | + true, |
|
| 1830 | + null, |
|
| 1831 | + true |
|
| 1832 | + ); |
|
| 1833 | + |
|
| 1834 | + if ($tokens[$maybeVariableVariable]['code'] === \T_VARIABLE |
|
| 1835 | + || $tokens[$maybeVariableVariable]['code'] === \T_DOLLAR |
|
| 1836 | + ) { |
|
| 1837 | + return false; |
|
| 1838 | + } |
|
| 1839 | + } |
|
| 1840 | + |
|
| 1841 | + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($closer + 1), null, true, null, true); |
|
| 1842 | + |
|
| 1843 | + if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === \T_EQUAL) { |
|
| 1844 | + return true; |
|
| 1845 | + } |
|
| 1846 | + |
|
| 1847 | + if ($prevNonEmpty !== false |
|
| 1848 | + && $tokens[$prevNonEmpty]['code'] === \T_AS |
|
| 1849 | + && isset($tokens[$prevNonEmpty]['nested_parenthesis']) === true |
|
| 1850 | + ) { |
|
| 1851 | + $parentheses = array_reverse($tokens[$prevNonEmpty]['nested_parenthesis'], true); |
|
| 1852 | + foreach ($parentheses as $open => $close) { |
|
| 1853 | + if (isset($tokens[$open]['parenthesis_owner']) |
|
| 1854 | + && $tokens[$tokens[$open]['parenthesis_owner']]['code'] === \T_FOREACH |
|
| 1855 | + ) { |
|
| 1856 | + return true; |
|
| 1857 | + } |
|
| 1858 | + } |
|
| 1859 | + } |
|
| 1860 | + |
|
| 1861 | + // Maybe this is a short list syntax nested inside another short list syntax ? |
|
| 1862 | + $parentOpener = $opener; |
|
| 1863 | + do { |
|
| 1864 | + $parentOpener = $phpcsFile->findPrevious( |
|
| 1865 | + array(\T_OPEN_SHORT_ARRAY, \T_OPEN_SQUARE_BRACKET), |
|
| 1866 | + ($parentOpener - 1), |
|
| 1867 | + null, |
|
| 1868 | + false, |
|
| 1869 | + null, |
|
| 1870 | + true |
|
| 1871 | + ); |
|
| 1872 | + |
|
| 1873 | + if ($parentOpener === false) { |
|
| 1874 | + return false; |
|
| 1875 | + } |
|
| 1876 | + |
|
| 1877 | + } while (isset($tokens[$parentOpener]['bracket_closer']) === true |
|
| 1878 | + && $tokens[$parentOpener]['bracket_closer'] < $opener |
|
| 1879 | + ); |
|
| 1880 | + |
|
| 1881 | + if (isset($tokens[$parentOpener]['bracket_closer']) === true |
|
| 1882 | + && $tokens[$parentOpener]['bracket_closer'] > $closer |
|
| 1883 | + ) { |
|
| 1884 | + // Work around tokenizer issue in PHPCS 2.0 - 2.7. |
|
| 1885 | + $phpcsVersion = PHPCSHelper::getVersion(); |
|
| 1886 | + if ((version_compare($phpcsVersion, '2.0', '>') === true |
|
| 1887 | + && version_compare($phpcsVersion, '2.8', '<') === true) |
|
| 1888 | + && $tokens[$parentOpener]['code'] === \T_OPEN_SQUARE_BRACKET |
|
| 1889 | + ) { |
|
| 1890 | + $nextNonEmpty = $phpcsFile->findNext( |
|
| 1891 | + Tokens::$emptyTokens, |
|
| 1892 | + ($tokens[$parentOpener]['bracket_closer'] + 1), |
|
| 1893 | + null, |
|
| 1894 | + true, |
|
| 1895 | + null, |
|
| 1896 | + true |
|
| 1897 | + ); |
|
| 1898 | + |
|
| 1899 | + if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === \T_EQUAL) { |
|
| 1900 | + return true; |
|
| 1901 | + } |
|
| 1902 | + |
|
| 1903 | + return false; |
|
| 1904 | + } |
|
| 1905 | + |
|
| 1906 | + return $this->isShortList($phpcsFile, $parentOpener); |
|
| 1907 | + } |
|
| 1908 | + |
|
| 1909 | + return false; |
|
| 1910 | + } |
|
| 1911 | + |
|
| 1912 | + |
|
| 1913 | + /** |
|
| 1914 | + * Determine whether the tokens between $start and $end could together represent a variable. |
|
| 1915 | + * |
|
| 1916 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 1917 | + * @param int $start Starting point stack pointer. Inclusive. |
|
| 1918 | + * I.e. this token should be taken into account. |
|
| 1919 | + * @param int $end End point stack pointer. Exclusive. |
|
| 1920 | + * I.e. this token should not be taken into account. |
|
| 1921 | + * @param int $targetNestingLevel The nesting level the variable should be at. |
|
| 1922 | + * |
|
| 1923 | + * @return bool |
|
| 1924 | + */ |
|
| 1925 | + public function isVariable(File $phpcsFile, $start, $end, $targetNestingLevel) |
|
| 1926 | + { |
|
| 1927 | + static $tokenBlackList, $bracketTokens; |
|
| 1928 | + |
|
| 1929 | + // Create the token arrays only once. |
|
| 1930 | + if (isset($tokenBlackList, $bracketTokens) === false) { |
|
| 1931 | + |
|
| 1932 | + $tokenBlackList = array( |
|
| 1933 | + \T_OPEN_PARENTHESIS => \T_OPEN_PARENTHESIS, |
|
| 1934 | + \T_STRING_CONCAT => \T_STRING_CONCAT, |
|
| 1935 | + ); |
|
| 1936 | + $tokenBlackList += Tokens::$assignmentTokens; |
|
| 1937 | + $tokenBlackList += Tokens::$equalityTokens; |
|
| 1938 | + $tokenBlackList += Tokens::$comparisonTokens; |
|
| 1939 | + $tokenBlackList += Tokens::$operators; |
|
| 1940 | + $tokenBlackList += Tokens::$booleanOperators; |
|
| 1941 | + $tokenBlackList += Tokens::$castTokens; |
|
| 1942 | + |
|
| 1943 | + /* |
|
| 1944 | 1944 | * List of brackets which can be part of a variable variable. |
| 1945 | 1945 | * |
| 1946 | 1946 | * Key is the open bracket token, value the close bracket token. |
| 1947 | 1947 | */ |
| 1948 | - $bracketTokens = array( |
|
| 1949 | - \T_OPEN_CURLY_BRACKET => \T_CLOSE_CURLY_BRACKET, |
|
| 1950 | - \T_OPEN_SQUARE_BRACKET => \T_CLOSE_SQUARE_BRACKET, |
|
| 1951 | - ); |
|
| 1952 | - } |
|
| 1953 | - |
|
| 1954 | - $tokens = $phpcsFile->getTokens(); |
|
| 1955 | - |
|
| 1956 | - // If no variable at all was found, then it's definitely a no-no. |
|
| 1957 | - $hasVariable = $phpcsFile->findNext(\T_VARIABLE, $start, $end); |
|
| 1958 | - if ($hasVariable === false) { |
|
| 1959 | - return false; |
|
| 1960 | - } |
|
| 1961 | - |
|
| 1962 | - // Check if the variable found is at the right level. Deeper levels are always an error. |
|
| 1963 | - if (isset($tokens[$hasVariable]['nested_parenthesis']) |
|
| 1964 | - && \count($tokens[$hasVariable]['nested_parenthesis']) !== $targetNestingLevel |
|
| 1965 | - ) { |
|
| 1966 | - return false; |
|
| 1967 | - } |
|
| 1968 | - |
|
| 1969 | - // Ok, so the first variable is at the right level, now are there any |
|
| 1970 | - // blacklisted tokens within the empty() ? |
|
| 1971 | - $hasBadToken = $phpcsFile->findNext($tokenBlackList, $start, $end); |
|
| 1972 | - if ($hasBadToken === false) { |
|
| 1973 | - return true; |
|
| 1974 | - } |
|
| 1975 | - |
|
| 1976 | - // If there are also bracket tokens, the blacklisted token might be part of a variable |
|
| 1977 | - // variable, but if there are no bracket tokens, we know we have an error. |
|
| 1978 | - $hasBrackets = $phpcsFile->findNext($bracketTokens, $start, $end); |
|
| 1979 | - if ($hasBrackets === false) { |
|
| 1980 | - return false; |
|
| 1981 | - } |
|
| 1982 | - |
|
| 1983 | - // Ok, we have both a blacklisted token as well as brackets, so we need to walk |
|
| 1984 | - // the tokens of the variable variable. |
|
| 1985 | - for ($i = $start; $i < $end; $i++) { |
|
| 1986 | - // If this is a bracket token, skip to the end of the bracketed expression. |
|
| 1987 | - if (isset($bracketTokens[$tokens[$i]['code']], $tokens[$i]['bracket_closer'])) { |
|
| 1988 | - $i = $tokens[$i]['bracket_closer']; |
|
| 1989 | - continue; |
|
| 1990 | - } |
|
| 1991 | - |
|
| 1992 | - // If it's a blacklisted token, not within brackets, we have an error. |
|
| 1993 | - if (isset($tokenBlackList[$tokens[$i]['code']])) { |
|
| 1994 | - return false; |
|
| 1995 | - } |
|
| 1996 | - } |
|
| 1997 | - |
|
| 1998 | - return true; |
|
| 1999 | - } |
|
| 2000 | - |
|
| 2001 | - /** |
|
| 2002 | - * Determine whether a T_MINUS/T_PLUS token is a unary operator. |
|
| 2003 | - * |
|
| 2004 | - * N.B.: This is a back-fill for a new method which is expected to go into |
|
| 2005 | - * PHP_CodeSniffer 3.5.0. |
|
| 2006 | - * Once that method has been merged into PHPCS, this one should be moved |
|
| 2007 | - * to the PHPCSHelper.php file. |
|
| 2008 | - * |
|
| 2009 | - * @since 9.2.0 |
|
| 2010 | - * |
|
| 2011 | - * @codeCoverageIgnore Method as pulled upstream is accompanied by unit tests. |
|
| 2012 | - * |
|
| 2013 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 2014 | - * @param int $stackPtr The position of the plus/minus token. |
|
| 2015 | - * |
|
| 2016 | - * @return bool True if the token passed is a unary operator. |
|
| 2017 | - * False otherwise or if the token is not a T_PLUS/T_MINUS token. |
|
| 2018 | - */ |
|
| 2019 | - public static function isUnaryPlusMinus(File $phpcsFile, $stackPtr) |
|
| 2020 | - { |
|
| 2021 | - $tokens = $phpcsFile->getTokens(); |
|
| 2022 | - |
|
| 2023 | - if (isset($tokens[$stackPtr]) === false |
|
| 2024 | - || ($tokens[$stackPtr]['code'] !== \T_PLUS |
|
| 2025 | - && $tokens[$stackPtr]['code'] !== \T_MINUS) |
|
| 2026 | - ) { |
|
| 2027 | - return false; |
|
| 2028 | - } |
|
| 2029 | - |
|
| 2030 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
| 2031 | - if ($next === false) { |
|
| 2032 | - // Live coding or parse error. |
|
| 2033 | - return false; |
|
| 2034 | - } |
|
| 2035 | - |
|
| 2036 | - if (isset(Tokens::$operators[$tokens[$next]['code']]) === true) { |
|
| 2037 | - // Next token is an operator, so this is not a unary. |
|
| 2038 | - return false; |
|
| 2039 | - } |
|
| 2040 | - |
|
| 2041 | - $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
| 2042 | - |
|
| 2043 | - if ($tokens[$prev]['code'] === \T_RETURN) { |
|
| 2044 | - // Just returning a positive/negative value; eg. (return -1). |
|
| 2045 | - return true; |
|
| 2046 | - } |
|
| 2047 | - |
|
| 2048 | - if (isset(Tokens::$operators[$tokens[$prev]['code']]) === true) { |
|
| 2049 | - // Just trying to operate on a positive/negative value; eg. ($var * -1). |
|
| 2050 | - return true; |
|
| 2051 | - } |
|
| 2052 | - |
|
| 2053 | - if (isset(Tokens::$comparisonTokens[$tokens[$prev]['code']]) === true) { |
|
| 2054 | - // Just trying to compare a positive/negative value; eg. ($var === -1). |
|
| 2055 | - return true; |
|
| 2056 | - } |
|
| 2057 | - |
|
| 2058 | - if (isset(Tokens::$booleanOperators[$tokens[$prev]['code']]) === true) { |
|
| 2059 | - // Just trying to compare a positive/negative value; eg. ($var || -1 === $b). |
|
| 2060 | - return true; |
|
| 2061 | - } |
|
| 2062 | - |
|
| 2063 | - if (isset(Tokens::$assignmentTokens[$tokens[$prev]['code']]) === true) { |
|
| 2064 | - // Just trying to assign a positive/negative value; eg. ($var = -1). |
|
| 2065 | - return true; |
|
| 2066 | - } |
|
| 2067 | - |
|
| 2068 | - if (isset(Tokens::$castTokens[$tokens[$prev]['code']]) === true) { |
|
| 2069 | - // Just casting a positive/negative value; eg. (string) -$var. |
|
| 2070 | - return true; |
|
| 2071 | - } |
|
| 2072 | - |
|
| 2073 | - // Other indicators that a plus/minus sign is a unary operator. |
|
| 2074 | - $invalidTokens = array( |
|
| 2075 | - \T_COMMA => true, |
|
| 2076 | - \T_OPEN_PARENTHESIS => true, |
|
| 2077 | - \T_OPEN_SQUARE_BRACKET => true, |
|
| 2078 | - \T_OPEN_SHORT_ARRAY => true, |
|
| 2079 | - \T_COLON => true, |
|
| 2080 | - \T_INLINE_THEN => true, |
|
| 2081 | - \T_INLINE_ELSE => true, |
|
| 2082 | - \T_CASE => true, |
|
| 2083 | - \T_OPEN_CURLY_BRACKET => true, |
|
| 2084 | - \T_STRING_CONCAT => true, |
|
| 2085 | - ); |
|
| 2086 | - |
|
| 2087 | - if (isset($invalidTokens[$tokens[$prev]['code']]) === true) { |
|
| 2088 | - // Just trying to use a positive/negative value; eg. myFunction($var, -2). |
|
| 2089 | - return true; |
|
| 2090 | - } |
|
| 2091 | - |
|
| 2092 | - return false; |
|
| 2093 | - } |
|
| 1948 | + $bracketTokens = array( |
|
| 1949 | + \T_OPEN_CURLY_BRACKET => \T_CLOSE_CURLY_BRACKET, |
|
| 1950 | + \T_OPEN_SQUARE_BRACKET => \T_CLOSE_SQUARE_BRACKET, |
|
| 1951 | + ); |
|
| 1952 | + } |
|
| 1953 | + |
|
| 1954 | + $tokens = $phpcsFile->getTokens(); |
|
| 1955 | + |
|
| 1956 | + // If no variable at all was found, then it's definitely a no-no. |
|
| 1957 | + $hasVariable = $phpcsFile->findNext(\T_VARIABLE, $start, $end); |
|
| 1958 | + if ($hasVariable === false) { |
|
| 1959 | + return false; |
|
| 1960 | + } |
|
| 1961 | + |
|
| 1962 | + // Check if the variable found is at the right level. Deeper levels are always an error. |
|
| 1963 | + if (isset($tokens[$hasVariable]['nested_parenthesis']) |
|
| 1964 | + && \count($tokens[$hasVariable]['nested_parenthesis']) !== $targetNestingLevel |
|
| 1965 | + ) { |
|
| 1966 | + return false; |
|
| 1967 | + } |
|
| 1968 | + |
|
| 1969 | + // Ok, so the first variable is at the right level, now are there any |
|
| 1970 | + // blacklisted tokens within the empty() ? |
|
| 1971 | + $hasBadToken = $phpcsFile->findNext($tokenBlackList, $start, $end); |
|
| 1972 | + if ($hasBadToken === false) { |
|
| 1973 | + return true; |
|
| 1974 | + } |
|
| 1975 | + |
|
| 1976 | + // If there are also bracket tokens, the blacklisted token might be part of a variable |
|
| 1977 | + // variable, but if there are no bracket tokens, we know we have an error. |
|
| 1978 | + $hasBrackets = $phpcsFile->findNext($bracketTokens, $start, $end); |
|
| 1979 | + if ($hasBrackets === false) { |
|
| 1980 | + return false; |
|
| 1981 | + } |
|
| 1982 | + |
|
| 1983 | + // Ok, we have both a blacklisted token as well as brackets, so we need to walk |
|
| 1984 | + // the tokens of the variable variable. |
|
| 1985 | + for ($i = $start; $i < $end; $i++) { |
|
| 1986 | + // If this is a bracket token, skip to the end of the bracketed expression. |
|
| 1987 | + if (isset($bracketTokens[$tokens[$i]['code']], $tokens[$i]['bracket_closer'])) { |
|
| 1988 | + $i = $tokens[$i]['bracket_closer']; |
|
| 1989 | + continue; |
|
| 1990 | + } |
|
| 1991 | + |
|
| 1992 | + // If it's a blacklisted token, not within brackets, we have an error. |
|
| 1993 | + if (isset($tokenBlackList[$tokens[$i]['code']])) { |
|
| 1994 | + return false; |
|
| 1995 | + } |
|
| 1996 | + } |
|
| 1997 | + |
|
| 1998 | + return true; |
|
| 1999 | + } |
|
| 2000 | + |
|
| 2001 | + /** |
|
| 2002 | + * Determine whether a T_MINUS/T_PLUS token is a unary operator. |
|
| 2003 | + * |
|
| 2004 | + * N.B.: This is a back-fill for a new method which is expected to go into |
|
| 2005 | + * PHP_CodeSniffer 3.5.0. |
|
| 2006 | + * Once that method has been merged into PHPCS, this one should be moved |
|
| 2007 | + * to the PHPCSHelper.php file. |
|
| 2008 | + * |
|
| 2009 | + * @since 9.2.0 |
|
| 2010 | + * |
|
| 2011 | + * @codeCoverageIgnore Method as pulled upstream is accompanied by unit tests. |
|
| 2012 | + * |
|
| 2013 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 2014 | + * @param int $stackPtr The position of the plus/minus token. |
|
| 2015 | + * |
|
| 2016 | + * @return bool True if the token passed is a unary operator. |
|
| 2017 | + * False otherwise or if the token is not a T_PLUS/T_MINUS token. |
|
| 2018 | + */ |
|
| 2019 | + public static function isUnaryPlusMinus(File $phpcsFile, $stackPtr) |
|
| 2020 | + { |
|
| 2021 | + $tokens = $phpcsFile->getTokens(); |
|
| 2022 | + |
|
| 2023 | + if (isset($tokens[$stackPtr]) === false |
|
| 2024 | + || ($tokens[$stackPtr]['code'] !== \T_PLUS |
|
| 2025 | + && $tokens[$stackPtr]['code'] !== \T_MINUS) |
|
| 2026 | + ) { |
|
| 2027 | + return false; |
|
| 2028 | + } |
|
| 2029 | + |
|
| 2030 | + $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
| 2031 | + if ($next === false) { |
|
| 2032 | + // Live coding or parse error. |
|
| 2033 | + return false; |
|
| 2034 | + } |
|
| 2035 | + |
|
| 2036 | + if (isset(Tokens::$operators[$tokens[$next]['code']]) === true) { |
|
| 2037 | + // Next token is an operator, so this is not a unary. |
|
| 2038 | + return false; |
|
| 2039 | + } |
|
| 2040 | + |
|
| 2041 | + $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
| 2042 | + |
|
| 2043 | + if ($tokens[$prev]['code'] === \T_RETURN) { |
|
| 2044 | + // Just returning a positive/negative value; eg. (return -1). |
|
| 2045 | + return true; |
|
| 2046 | + } |
|
| 2047 | + |
|
| 2048 | + if (isset(Tokens::$operators[$tokens[$prev]['code']]) === true) { |
|
| 2049 | + // Just trying to operate on a positive/negative value; eg. ($var * -1). |
|
| 2050 | + return true; |
|
| 2051 | + } |
|
| 2052 | + |
|
| 2053 | + if (isset(Tokens::$comparisonTokens[$tokens[$prev]['code']]) === true) { |
|
| 2054 | + // Just trying to compare a positive/negative value; eg. ($var === -1). |
|
| 2055 | + return true; |
|
| 2056 | + } |
|
| 2057 | + |
|
| 2058 | + if (isset(Tokens::$booleanOperators[$tokens[$prev]['code']]) === true) { |
|
| 2059 | + // Just trying to compare a positive/negative value; eg. ($var || -1 === $b). |
|
| 2060 | + return true; |
|
| 2061 | + } |
|
| 2062 | + |
|
| 2063 | + if (isset(Tokens::$assignmentTokens[$tokens[$prev]['code']]) === true) { |
|
| 2064 | + // Just trying to assign a positive/negative value; eg. ($var = -1). |
|
| 2065 | + return true; |
|
| 2066 | + } |
|
| 2067 | + |
|
| 2068 | + if (isset(Tokens::$castTokens[$tokens[$prev]['code']]) === true) { |
|
| 2069 | + // Just casting a positive/negative value; eg. (string) -$var. |
|
| 2070 | + return true; |
|
| 2071 | + } |
|
| 2072 | + |
|
| 2073 | + // Other indicators that a plus/minus sign is a unary operator. |
|
| 2074 | + $invalidTokens = array( |
|
| 2075 | + \T_COMMA => true, |
|
| 2076 | + \T_OPEN_PARENTHESIS => true, |
|
| 2077 | + \T_OPEN_SQUARE_BRACKET => true, |
|
| 2078 | + \T_OPEN_SHORT_ARRAY => true, |
|
| 2079 | + \T_COLON => true, |
|
| 2080 | + \T_INLINE_THEN => true, |
|
| 2081 | + \T_INLINE_ELSE => true, |
|
| 2082 | + \T_CASE => true, |
|
| 2083 | + \T_OPEN_CURLY_BRACKET => true, |
|
| 2084 | + \T_STRING_CONCAT => true, |
|
| 2085 | + ); |
|
| 2086 | + |
|
| 2087 | + if (isset($invalidTokens[$tokens[$prev]['code']]) === true) { |
|
| 2088 | + // Just trying to use a positive/negative value; eg. myFunction($var, -2). |
|
| 2089 | + return true; |
|
| 2090 | + } |
|
| 2091 | + |
|
| 2092 | + return false; |
|
| 2093 | + } |
|
| 2094 | 2094 | } |
@@ -109,39 +109,39 @@ discard block |
||
| 109 | 109 | { |
| 110 | 110 | static $arrTestVersions = array(); |
| 111 | 111 | |
| 112 | - $default = array(null, null); |
|
| 113 | - $testVersion = trim(PHPCSHelper::getConfigData('testVersion')); |
|
| 112 | + $default = array( null, null ); |
|
| 113 | + $testVersion = trim( PHPCSHelper::getConfigData( 'testVersion' ) ); |
|
| 114 | 114 | |
| 115 | - if (empty($testVersion) === false && isset($arrTestVersions[$testVersion]) === false) { |
|
| 115 | + if ( empty( $testVersion ) === false && isset( $arrTestVersions[ $testVersion ] ) === false ) { |
|
| 116 | 116 | |
| 117 | - $arrTestVersions[$testVersion] = $default; |
|
| 117 | + $arrTestVersions[ $testVersion ] = $default; |
|
| 118 | 118 | |
| 119 | - if (preg_match('`^\d+\.\d+$`', $testVersion)) { |
|
| 120 | - $arrTestVersions[$testVersion] = array($testVersion, $testVersion); |
|
| 121 | - return $arrTestVersions[$testVersion]; |
|
| 119 | + if ( preg_match( '`^\d+\.\d+$`', $testVersion ) ) { |
|
| 120 | + $arrTestVersions[ $testVersion ] = array( $testVersion, $testVersion ); |
|
| 121 | + return $arrTestVersions[ $testVersion ]; |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - if (preg_match('`^(\d+\.\d+)?\s*-\s*(\d+\.\d+)?$`', $testVersion, $matches)) { |
|
| 125 | - if (empty($matches[1]) === false || empty($matches[2]) === false) { |
|
| 124 | + if ( preg_match( '`^(\d+\.\d+)?\s*-\s*(\d+\.\d+)?$`', $testVersion, $matches ) ) { |
|
| 125 | + if ( empty( $matches[ 1 ] ) === false || empty( $matches[ 2 ] ) === false ) { |
|
| 126 | 126 | // If no lower-limit is set, we set the min version to 4.0. |
| 127 | 127 | // Whilst development focuses on PHP 5 and above, we also accept |
| 128 | 128 | // sniffs for PHP 4, so we include that as the minimum. |
| 129 | 129 | // (It makes no sense to support PHP 3 as this was effectively a |
| 130 | 130 | // different language). |
| 131 | - $min = empty($matches[1]) ? '4.0' : $matches[1]; |
|
| 131 | + $min = empty( $matches[ 1 ] ) ? '4.0' : $matches[ 1 ]; |
|
| 132 | 132 | |
| 133 | 133 | // If no upper-limit is set, we set the max version to 99.9. |
| 134 | - $max = empty($matches[2]) ? '99.9' : $matches[2]; |
|
| 134 | + $max = empty( $matches[ 2 ] ) ? '99.9' : $matches[ 2 ]; |
|
| 135 | 135 | |
| 136 | - if (version_compare($min, $max, '>')) { |
|
| 136 | + if ( version_compare( $min, $max, '>' ) ) { |
|
| 137 | 137 | trigger_error( |
| 138 | 138 | "Invalid range in testVersion setting: '" . $testVersion . "'", |
| 139 | 139 | \E_USER_WARNING |
| 140 | 140 | ); |
| 141 | 141 | return $default; |
| 142 | 142 | } else { |
| 143 | - $arrTestVersions[$testVersion] = array($min, $max); |
|
| 144 | - return $arrTestVersions[$testVersion]; |
|
| 143 | + $arrTestVersions[ $testVersion ] = array( $min, $max ); |
|
| 144 | + return $arrTestVersions[ $testVersion ]; |
|
| 145 | 145 | } |
| 146 | 146 | } |
| 147 | 147 | } |
@@ -153,8 +153,8 @@ discard block |
||
| 153 | 153 | return $default; |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | - if (isset($arrTestVersions[$testVersion])) { |
|
| 157 | - return $arrTestVersions[$testVersion]; |
|
| 156 | + if ( isset( $arrTestVersions[ $testVersion ] ) ) { |
|
| 157 | + return $arrTestVersions[ $testVersion ]; |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | return $default; |
@@ -173,13 +173,13 @@ discard block |
||
| 173 | 173 | * is equal to or higher than the highest supported PHP version |
| 174 | 174 | * in testVersion. False otherwise. |
| 175 | 175 | */ |
| 176 | - public function supportsAbove($phpVersion) |
|
| 176 | + public function supportsAbove( $phpVersion ) |
|
| 177 | 177 | { |
| 178 | 178 | $testVersion = $this->getTestVersion(); |
| 179 | - $testVersion = $testVersion[1]; |
|
| 179 | + $testVersion = $testVersion[ 1 ]; |
|
| 180 | 180 | |
| 181 | - if (\is_null($testVersion) |
|
| 182 | - || version_compare($testVersion, $phpVersion) >= 0 |
|
| 181 | + if ( \is_null( $testVersion ) |
|
| 182 | + || version_compare( $testVersion, $phpVersion ) >= 0 |
|
| 183 | 183 | ) { |
| 184 | 184 | return true; |
| 185 | 185 | } else { |
@@ -200,13 +200,13 @@ discard block |
||
| 200 | 200 | * supported PHP version in testVersion. |
| 201 | 201 | * False otherwise or if no testVersion is provided. |
| 202 | 202 | */ |
| 203 | - public function supportsBelow($phpVersion) |
|
| 203 | + public function supportsBelow( $phpVersion ) |
|
| 204 | 204 | { |
| 205 | 205 | $testVersion = $this->getTestVersion(); |
| 206 | - $testVersion = $testVersion[0]; |
|
| 206 | + $testVersion = $testVersion[ 0 ]; |
|
| 207 | 207 | |
| 208 | - if (\is_null($testVersion) === false |
|
| 209 | - && version_compare($testVersion, $phpVersion) <= 0 |
|
| 208 | + if ( \is_null( $testVersion ) === false |
|
| 209 | + && version_compare( $testVersion, $phpVersion ) <= 0 |
|
| 210 | 210 | ) { |
| 211 | 211 | return true; |
| 212 | 212 | } else { |
@@ -231,12 +231,12 @@ discard block |
||
| 231 | 231 | * |
| 232 | 232 | * @return void |
| 233 | 233 | */ |
| 234 | - public function addMessage(File $phpcsFile, $message, $stackPtr, $isError, $code = 'Found', $data = array()) |
|
| 234 | + public function addMessage( File $phpcsFile, $message, $stackPtr, $isError, $code = 'Found', $data = array() ) |
|
| 235 | 235 | { |
| 236 | - if ($isError === true) { |
|
| 237 | - $phpcsFile->addError($message, $stackPtr, $code, $data); |
|
| 236 | + if ( $isError === true ) { |
|
| 237 | + $phpcsFile->addError( $message, $stackPtr, $code, $data ); |
|
| 238 | 238 | } else { |
| 239 | - $phpcsFile->addWarning($message, $stackPtr, $code, $data); |
|
| 239 | + $phpcsFile->addWarning( $message, $stackPtr, $code, $data ); |
|
| 240 | 240 | } |
| 241 | 241 | } |
| 242 | 242 | |
@@ -250,9 +250,9 @@ discard block |
||
| 250 | 250 | * |
| 251 | 251 | * @return string |
| 252 | 252 | */ |
| 253 | - public function stringToErrorCode($baseString) |
|
| 253 | + public function stringToErrorCode( $baseString ) |
|
| 254 | 254 | { |
| 255 | - return preg_replace('`[^a-z0-9_]`i', '_', strtolower($baseString)); |
|
| 255 | + return preg_replace( '`[^a-z0-9_]`i', '_', strtolower( $baseString ) ); |
|
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | |
@@ -265,9 +265,9 @@ discard block |
||
| 265 | 265 | * |
| 266 | 266 | * @return string String without quotes around it. |
| 267 | 267 | */ |
| 268 | - public function stripQuotes($string) |
|
| 268 | + public function stripQuotes( $string ) |
|
| 269 | 269 | { |
| 270 | - return preg_replace('`^([\'"])(.*)\1$`Ds', '$2', $string); |
|
| 270 | + return preg_replace( '`^([\'"])(.*)\1$`Ds', '$2', $string ); |
|
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | |
@@ -280,13 +280,13 @@ discard block |
||
| 280 | 280 | * |
| 281 | 281 | * @return string String without variables in it. |
| 282 | 282 | */ |
| 283 | - public function stripVariables($string) |
|
| 283 | + public function stripVariables( $string ) |
|
| 284 | 284 | { |
| 285 | - if (strpos($string, '$') === false) { |
|
| 285 | + if ( strpos( $string, '$' ) === false ) { |
|
| 286 | 286 | return $string; |
| 287 | 287 | } |
| 288 | 288 | |
| 289 | - return preg_replace(self::REGEX_COMPLEX_VARS, '', $string); |
|
| 289 | + return preg_replace( self::REGEX_COMPLEX_VARS, '', $string ); |
|
| 290 | 290 | } |
| 291 | 291 | |
| 292 | 292 | |
@@ -297,9 +297,9 @@ discard block |
||
| 297 | 297 | * |
| 298 | 298 | * @return array Same array, but with all lowercase top level keys. |
| 299 | 299 | */ |
| 300 | - public function arrayKeysToLowercase($array) |
|
| 300 | + public function arrayKeysToLowercase( $array ) |
|
| 301 | 301 | { |
| 302 | - return array_change_key_case($array, \CASE_LOWER); |
|
| 302 | + return array_change_key_case( $array, \CASE_LOWER ); |
|
| 303 | 303 | } |
| 304 | 304 | |
| 305 | 305 | |
@@ -320,29 +320,29 @@ discard block |
||
| 320 | 320 | * |
| 321 | 321 | * @return bool |
| 322 | 322 | */ |
| 323 | - public function doesFunctionCallHaveParameters(File $phpcsFile, $stackPtr) |
|
| 323 | + public function doesFunctionCallHaveParameters( File $phpcsFile, $stackPtr ) |
|
| 324 | 324 | { |
| 325 | 325 | $tokens = $phpcsFile->getTokens(); |
| 326 | 326 | |
| 327 | 327 | // Check for the existence of the token. |
| 328 | - if (isset($tokens[$stackPtr]) === false) { |
|
| 328 | + if ( isset( $tokens[ $stackPtr ] ) === false ) { |
|
| 329 | 329 | return false; |
| 330 | 330 | } |
| 331 | 331 | |
| 332 | 332 | // Is this one of the tokens this function handles ? |
| 333 | - if (\in_array($tokens[$stackPtr]['code'], array(\T_STRING, \T_ARRAY, \T_OPEN_SHORT_ARRAY, \T_VARIABLE), true) === false) { |
|
| 333 | + if ( \in_array( $tokens[ $stackPtr ][ 'code' ], array( \T_STRING, \T_ARRAY, \T_OPEN_SHORT_ARRAY, \T_VARIABLE ), true ) === false ) { |
|
| 334 | 334 | return false; |
| 335 | 335 | } |
| 336 | 336 | |
| 337 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
|
| 337 | + $nextNonEmpty = $phpcsFile->findNext( Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true ); |
|
| 338 | 338 | |
| 339 | 339 | // Deal with short array syntax. |
| 340 | - if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY) { |
|
| 341 | - if (isset($tokens[$stackPtr]['bracket_closer']) === false) { |
|
| 340 | + if ( $tokens[ $stackPtr ][ 'code' ] === \T_OPEN_SHORT_ARRAY ) { |
|
| 341 | + if ( isset( $tokens[ $stackPtr ][ 'bracket_closer' ] ) === false ) { |
|
| 342 | 342 | return false; |
| 343 | 343 | } |
| 344 | 344 | |
| 345 | - if ($nextNonEmpty === $tokens[$stackPtr]['bracket_closer']) { |
|
| 345 | + if ( $nextNonEmpty === $tokens[ $stackPtr ][ 'bracket_closer' ] ) { |
|
| 346 | 346 | // No parameters. |
| 347 | 347 | return false; |
| 348 | 348 | } else { |
@@ -352,18 +352,18 @@ discard block |
||
| 352 | 352 | |
| 353 | 353 | // Deal with function calls & long arrays. |
| 354 | 354 | // Next non-empty token should be the open parenthesis. |
| 355 | - if ($nextNonEmpty === false && $tokens[$nextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS) { |
|
| 355 | + if ( $nextNonEmpty === false && $tokens[ $nextNonEmpty ][ 'code' ] !== \T_OPEN_PARENTHESIS ) { |
|
| 356 | 356 | return false; |
| 357 | 357 | } |
| 358 | 358 | |
| 359 | - if (isset($tokens[$nextNonEmpty]['parenthesis_closer']) === false) { |
|
| 359 | + if ( isset( $tokens[ $nextNonEmpty ][ 'parenthesis_closer' ] ) === false ) { |
|
| 360 | 360 | return false; |
| 361 | 361 | } |
| 362 | 362 | |
| 363 | - $closeParenthesis = $tokens[$nextNonEmpty]['parenthesis_closer']; |
|
| 364 | - $nextNextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $nextNonEmpty + 1, $closeParenthesis + 1, true); |
|
| 363 | + $closeParenthesis = $tokens[ $nextNonEmpty ][ 'parenthesis_closer' ]; |
|
| 364 | + $nextNextNonEmpty = $phpcsFile->findNext( Tokens::$emptyTokens, $nextNonEmpty + 1, $closeParenthesis + 1, true ); |
|
| 365 | 365 | |
| 366 | - if ($nextNextNonEmpty === $closeParenthesis) { |
|
| 366 | + if ( $nextNextNonEmpty === $closeParenthesis ) { |
|
| 367 | 367 | // No parameters. |
| 368 | 368 | return false; |
| 369 | 369 | } |
@@ -390,13 +390,13 @@ discard block |
||
| 390 | 390 | * |
| 391 | 391 | * @return int |
| 392 | 392 | */ |
| 393 | - public function getFunctionCallParameterCount(File $phpcsFile, $stackPtr) |
|
| 393 | + public function getFunctionCallParameterCount( File $phpcsFile, $stackPtr ) |
|
| 394 | 394 | { |
| 395 | - if ($this->doesFunctionCallHaveParameters($phpcsFile, $stackPtr) === false) { |
|
| 395 | + if ( $this->doesFunctionCallHaveParameters( $phpcsFile, $stackPtr ) === false ) { |
|
| 396 | 396 | return 0; |
| 397 | 397 | } |
| 398 | 398 | |
| 399 | - return \count($this->getFunctionCallParameters($phpcsFile, $stackPtr)); |
|
| 399 | + return \count( $this->getFunctionCallParameters( $phpcsFile, $stackPtr ) ); |
|
| 400 | 400 | } |
| 401 | 401 | |
| 402 | 402 | |
@@ -418,9 +418,9 @@ discard block |
||
| 418 | 418 | * |
| 419 | 419 | * @return array |
| 420 | 420 | */ |
| 421 | - public function getFunctionCallParameters(File $phpcsFile, $stackPtr) |
|
| 421 | + public function getFunctionCallParameters( File $phpcsFile, $stackPtr ) |
|
| 422 | 422 | { |
| 423 | - if ($this->doesFunctionCallHaveParameters($phpcsFile, $stackPtr) === false) { |
|
| 423 | + if ( $this->doesFunctionCallHaveParameters( $phpcsFile, $stackPtr ) === false ) { |
|
| 424 | 424 | return array(); |
| 425 | 425 | } |
| 426 | 426 | |
@@ -429,74 +429,74 @@ discard block |
||
| 429 | 429 | $tokens = $phpcsFile->getTokens(); |
| 430 | 430 | |
| 431 | 431 | // Mark the beginning and end tokens. |
| 432 | - if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY) { |
|
| 432 | + if ( $tokens[ $stackPtr ][ 'code' ] === \T_OPEN_SHORT_ARRAY ) { |
|
| 433 | 433 | $opener = $stackPtr; |
| 434 | - $closer = $tokens[$stackPtr]['bracket_closer']; |
|
| 434 | + $closer = $tokens[ $stackPtr ][ 'bracket_closer' ]; |
|
| 435 | 435 | |
| 436 | 436 | $nestedParenthesisCount = 0; |
| 437 | 437 | |
| 438 | 438 | } else { |
| 439 | - $opener = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
|
| 440 | - $closer = $tokens[$opener]['parenthesis_closer']; |
|
| 439 | + $opener = $phpcsFile->findNext( Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true ); |
|
| 440 | + $closer = $tokens[ $opener ][ 'parenthesis_closer' ]; |
|
| 441 | 441 | |
| 442 | 442 | $nestedParenthesisCount = 1; |
| 443 | 443 | } |
| 444 | 444 | |
| 445 | 445 | // Which nesting level is the one we are interested in ? |
| 446 | - if (isset($tokens[$opener]['nested_parenthesis'])) { |
|
| 447 | - $nestedParenthesisCount += \count($tokens[$opener]['nested_parenthesis']); |
|
| 446 | + if ( isset( $tokens[ $opener ][ 'nested_parenthesis' ] ) ) { |
|
| 447 | + $nestedParenthesisCount += \count( $tokens[ $opener ][ 'nested_parenthesis' ] ); |
|
| 448 | 448 | } |
| 449 | 449 | |
| 450 | 450 | $parameters = array(); |
| 451 | 451 | $nextComma = $opener; |
| 452 | 452 | $paramStart = $opener + 1; |
| 453 | 453 | $cnt = 1; |
| 454 | - while (($nextComma = $phpcsFile->findNext(array(\T_COMMA, $tokens[$closer]['code'], \T_OPEN_SHORT_ARRAY, \T_CLOSURE), $nextComma + 1, $closer + 1)) !== false) { |
|
| 454 | + while ( ( $nextComma = $phpcsFile->findNext( array( \T_COMMA, $tokens[ $closer ][ 'code' ], \T_OPEN_SHORT_ARRAY, \T_CLOSURE ), $nextComma + 1, $closer + 1 ) ) !== false ) { |
|
| 455 | 455 | // Ignore anything within short array definition brackets. |
| 456 | - if ($tokens[$nextComma]['type'] === 'T_OPEN_SHORT_ARRAY' |
|
| 457 | - && (isset($tokens[$nextComma]['bracket_opener']) |
|
| 458 | - && $tokens[$nextComma]['bracket_opener'] === $nextComma) |
|
| 459 | - && isset($tokens[$nextComma]['bracket_closer']) |
|
| 456 | + if ( $tokens[ $nextComma ][ 'type' ] === 'T_OPEN_SHORT_ARRAY' |
|
| 457 | + && ( isset( $tokens[ $nextComma ][ 'bracket_opener' ] ) |
|
| 458 | + && $tokens[ $nextComma ][ 'bracket_opener' ] === $nextComma ) |
|
| 459 | + && isset( $tokens[ $nextComma ][ 'bracket_closer' ] ) |
|
| 460 | 460 | ) { |
| 461 | 461 | // Skip forward to the end of the short array definition. |
| 462 | - $nextComma = $tokens[$nextComma]['bracket_closer']; |
|
| 462 | + $nextComma = $tokens[ $nextComma ][ 'bracket_closer' ]; |
|
| 463 | 463 | continue; |
| 464 | 464 | } |
| 465 | 465 | |
| 466 | 466 | // Skip past closures passed as function parameters. |
| 467 | - if ($tokens[$nextComma]['type'] === 'T_CLOSURE' |
|
| 468 | - && (isset($tokens[$nextComma]['scope_condition']) |
|
| 469 | - && $tokens[$nextComma]['scope_condition'] === $nextComma) |
|
| 470 | - && isset($tokens[$nextComma]['scope_closer']) |
|
| 467 | + if ( $tokens[ $nextComma ][ 'type' ] === 'T_CLOSURE' |
|
| 468 | + && ( isset( $tokens[ $nextComma ][ 'scope_condition' ] ) |
|
| 469 | + && $tokens[ $nextComma ][ 'scope_condition' ] === $nextComma ) |
|
| 470 | + && isset( $tokens[ $nextComma ][ 'scope_closer' ] ) |
|
| 471 | 471 | ) { |
| 472 | 472 | // Skip forward to the end of the closure declaration. |
| 473 | - $nextComma = $tokens[$nextComma]['scope_closer']; |
|
| 473 | + $nextComma = $tokens[ $nextComma ][ 'scope_closer' ]; |
|
| 474 | 474 | continue; |
| 475 | 475 | } |
| 476 | 476 | |
| 477 | 477 | // Ignore comma's at a lower nesting level. |
| 478 | - if ($tokens[$nextComma]['type'] === 'T_COMMA' |
|
| 479 | - && isset($tokens[$nextComma]['nested_parenthesis']) |
|
| 480 | - && \count($tokens[$nextComma]['nested_parenthesis']) !== $nestedParenthesisCount |
|
| 478 | + if ( $tokens[ $nextComma ][ 'type' ] === 'T_COMMA' |
|
| 479 | + && isset( $tokens[ $nextComma ][ 'nested_parenthesis' ] ) |
|
| 480 | + && \count( $tokens[ $nextComma ][ 'nested_parenthesis' ] ) !== $nestedParenthesisCount |
|
| 481 | 481 | ) { |
| 482 | 482 | continue; |
| 483 | 483 | } |
| 484 | 484 | |
| 485 | 485 | // Ignore closing parenthesis/bracket if not 'ours'. |
| 486 | - if ($tokens[$nextComma]['type'] === $tokens[$closer]['type'] && $nextComma !== $closer) { |
|
| 486 | + if ( $tokens[ $nextComma ][ 'type' ] === $tokens[ $closer ][ 'type' ] && $nextComma !== $closer ) { |
|
| 487 | 487 | continue; |
| 488 | 488 | } |
| 489 | 489 | |
| 490 | 490 | // Ok, we've reached the end of the parameter. |
| 491 | - $parameters[$cnt]['start'] = $paramStart; |
|
| 492 | - $parameters[$cnt]['end'] = $nextComma - 1; |
|
| 493 | - $parameters[$cnt]['raw'] = trim($phpcsFile->getTokensAsString($paramStart, ($nextComma - $paramStart))); |
|
| 491 | + $parameters[ $cnt ][ 'start' ] = $paramStart; |
|
| 492 | + $parameters[ $cnt ][ 'end' ] = $nextComma - 1; |
|
| 493 | + $parameters[ $cnt ][ 'raw' ] = trim( $phpcsFile->getTokensAsString( $paramStart, ( $nextComma - $paramStart ) ) ); |
|
| 494 | 494 | |
| 495 | 495 | // Check if there are more tokens before the closing parenthesis. |
| 496 | 496 | // Prevents code like the following from setting a third parameter: |
| 497 | 497 | // functionCall( $param1, $param2, ); |
| 498 | - $hasNextParam = $phpcsFile->findNext(Tokens::$emptyTokens, $nextComma + 1, $closer, true, null, true); |
|
| 499 | - if ($hasNextParam === false) { |
|
| 498 | + $hasNextParam = $phpcsFile->findNext( Tokens::$emptyTokens, $nextComma + 1, $closer, true, null, true ); |
|
| 499 | + if ( $hasNextParam === false ) { |
|
| 500 | 500 | break; |
| 501 | 501 | } |
| 502 | 502 | |
@@ -525,14 +525,14 @@ discard block |
||
| 525 | 525 | * |
| 526 | 526 | * @return array|false |
| 527 | 527 | */ |
| 528 | - public function getFunctionCallParameter(File $phpcsFile, $stackPtr, $paramOffset) |
|
| 528 | + public function getFunctionCallParameter( File $phpcsFile, $stackPtr, $paramOffset ) |
|
| 529 | 529 | { |
| 530 | - $parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr); |
|
| 530 | + $parameters = $this->getFunctionCallParameters( $phpcsFile, $stackPtr ); |
|
| 531 | 531 | |
| 532 | - if (isset($parameters[$paramOffset]) === false) { |
|
| 532 | + if ( isset( $parameters[ $paramOffset ] ) === false ) { |
|
| 533 | 533 | return false; |
| 534 | 534 | } else { |
| 535 | - return $parameters[$paramOffset]; |
|
| 535 | + return $parameters[ $paramOffset ]; |
|
| 536 | 536 | } |
| 537 | 537 | } |
| 538 | 538 | |
@@ -555,26 +555,26 @@ discard block |
||
| 555 | 555 | * If the $scopeTypes are set: True if *one* of the conditions is a |
| 556 | 556 | * valid scope, false otherwise. |
| 557 | 557 | */ |
| 558 | - public function tokenHasScope(File $phpcsFile, $stackPtr, $validScopes = null) |
|
| 558 | + public function tokenHasScope( File $phpcsFile, $stackPtr, $validScopes = null ) |
|
| 559 | 559 | { |
| 560 | 560 | $tokens = $phpcsFile->getTokens(); |
| 561 | 561 | |
| 562 | 562 | // Check for the existence of the token. |
| 563 | - if (isset($tokens[$stackPtr]) === false) { |
|
| 563 | + if ( isset( $tokens[ $stackPtr ] ) === false ) { |
|
| 564 | 564 | return false; |
| 565 | 565 | } |
| 566 | 566 | |
| 567 | 567 | // No conditions = no scope. |
| 568 | - if (empty($tokens[$stackPtr]['conditions'])) { |
|
| 568 | + if ( empty( $tokens[ $stackPtr ][ 'conditions' ] ) ) { |
|
| 569 | 569 | return false; |
| 570 | 570 | } |
| 571 | 571 | |
| 572 | 572 | // Ok, there are conditions, do we have to check for specific ones ? |
| 573 | - if (isset($validScopes) === false) { |
|
| 573 | + if ( isset( $validScopes ) === false ) { |
|
| 574 | 574 | return true; |
| 575 | 575 | } |
| 576 | 576 | |
| 577 | - return $phpcsFile->hasCondition($stackPtr, $validScopes); |
|
| 577 | + return $phpcsFile->hasCondition( $stackPtr, $validScopes ); |
|
| 578 | 578 | } |
| 579 | 579 | |
| 580 | 580 | |
@@ -589,19 +589,19 @@ discard block |
||
| 589 | 589 | * |
| 590 | 590 | * @return bool True if within class scope, false otherwise. |
| 591 | 591 | */ |
| 592 | - public function inClassScope(File $phpcsFile, $stackPtr, $strict = true) |
|
| 592 | + public function inClassScope( File $phpcsFile, $stackPtr, $strict = true ) |
|
| 593 | 593 | { |
| 594 | - $validScopes = array(\T_CLASS); |
|
| 595 | - if (\defined('T_ANON_CLASS') === true) { |
|
| 596 | - $validScopes[] = \T_ANON_CLASS; |
|
| 594 | + $validScopes = array( \T_CLASS ); |
|
| 595 | + if ( \defined( 'T_ANON_CLASS' ) === true ) { |
|
| 596 | + $validScopes[ ] = \T_ANON_CLASS; |
|
| 597 | 597 | } |
| 598 | 598 | |
| 599 | - if ($strict === false) { |
|
| 600 | - $validScopes[] = \T_INTERFACE; |
|
| 601 | - $validScopes[] = \T_TRAIT; |
|
| 599 | + if ( $strict === false ) { |
|
| 600 | + $validScopes[ ] = \T_INTERFACE; |
|
| 601 | + $validScopes[ ] = \T_TRAIT; |
|
| 602 | 602 | } |
| 603 | 603 | |
| 604 | - return $phpcsFile->hasCondition($stackPtr, $validScopes); |
|
| 604 | + return $phpcsFile->hasCondition( $stackPtr, $validScopes ); |
|
| 605 | 605 | } |
| 606 | 606 | |
| 607 | 607 | |
@@ -615,31 +615,31 @@ discard block |
||
| 615 | 615 | * |
| 616 | 616 | * @return string |
| 617 | 617 | */ |
| 618 | - public function getFQClassNameFromNewToken(File $phpcsFile, $stackPtr) |
|
| 618 | + public function getFQClassNameFromNewToken( File $phpcsFile, $stackPtr ) |
|
| 619 | 619 | { |
| 620 | 620 | $tokens = $phpcsFile->getTokens(); |
| 621 | 621 | |
| 622 | 622 | // Check for the existence of the token. |
| 623 | - if (isset($tokens[$stackPtr]) === false) { |
|
| 623 | + if ( isset( $tokens[ $stackPtr ] ) === false ) { |
|
| 624 | 624 | return ''; |
| 625 | 625 | } |
| 626 | 626 | |
| 627 | - if ($tokens[$stackPtr]['code'] !== \T_NEW) { |
|
| 627 | + if ( $tokens[ $stackPtr ][ 'code' ] !== \T_NEW ) { |
|
| 628 | 628 | return ''; |
| 629 | 629 | } |
| 630 | 630 | |
| 631 | - $start = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
|
| 632 | - if ($start === false) { |
|
| 631 | + $start = $phpcsFile->findNext( Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true ); |
|
| 632 | + if ( $start === false ) { |
|
| 633 | 633 | return ''; |
| 634 | 634 | } |
| 635 | 635 | |
| 636 | 636 | // Bow out if the next token is a variable as we don't know where it was defined. |
| 637 | - if ($tokens[$start]['code'] === \T_VARIABLE) { |
|
| 637 | + if ( $tokens[ $start ][ 'code' ] === \T_VARIABLE ) { |
|
| 638 | 638 | return ''; |
| 639 | 639 | } |
| 640 | 640 | |
| 641 | 641 | // Bow out if the next token is the class keyword. |
| 642 | - if ($tokens[$start]['type'] === 'T_ANON_CLASS' || $tokens[$start]['code'] === \T_CLASS) { |
|
| 642 | + if ( $tokens[ $start ][ 'type' ] === 'T_ANON_CLASS' || $tokens[ $start ][ 'code' ] === \T_CLASS ) { |
|
| 643 | 643 | return ''; |
| 644 | 644 | } |
| 645 | 645 | |
@@ -650,11 +650,11 @@ discard block |
||
| 650 | 650 | \T_WHITESPACE, |
| 651 | 651 | ); |
| 652 | 652 | |
| 653 | - $end = $phpcsFile->findNext($find, ($start + 1), null, true, null, true); |
|
| 654 | - $className = $phpcsFile->getTokensAsString($start, ($end - $start)); |
|
| 655 | - $className = trim($className); |
|
| 653 | + $end = $phpcsFile->findNext( $find, ( $start + 1 ), null, true, null, true ); |
|
| 654 | + $className = $phpcsFile->getTokensAsString( $start, ( $end - $start ) ); |
|
| 655 | + $className = trim( $className ); |
|
| 656 | 656 | |
| 657 | - return $this->getFQName($phpcsFile, $stackPtr, $className); |
|
| 657 | + return $this->getFQName( $phpcsFile, $stackPtr, $className ); |
|
| 658 | 658 | } |
| 659 | 659 | |
| 660 | 660 | |
@@ -669,28 +669,28 @@ discard block |
||
| 669 | 669 | * |
| 670 | 670 | * @return string |
| 671 | 671 | */ |
| 672 | - public function getFQExtendedClassName(File $phpcsFile, $stackPtr) |
|
| 672 | + public function getFQExtendedClassName( File $phpcsFile, $stackPtr ) |
|
| 673 | 673 | { |
| 674 | 674 | $tokens = $phpcsFile->getTokens(); |
| 675 | 675 | |
| 676 | 676 | // Check for the existence of the token. |
| 677 | - if (isset($tokens[$stackPtr]) === false) { |
|
| 677 | + if ( isset( $tokens[ $stackPtr ] ) === false ) { |
|
| 678 | 678 | return ''; |
| 679 | 679 | } |
| 680 | 680 | |
| 681 | - if ($tokens[$stackPtr]['code'] !== \T_CLASS |
|
| 682 | - && $tokens[$stackPtr]['type'] !== 'T_ANON_CLASS' |
|
| 683 | - && $tokens[$stackPtr]['type'] !== 'T_INTERFACE' |
|
| 681 | + if ( $tokens[ $stackPtr ][ 'code' ] !== \T_CLASS |
|
| 682 | + && $tokens[ $stackPtr ][ 'type' ] !== 'T_ANON_CLASS' |
|
| 683 | + && $tokens[ $stackPtr ][ 'type' ] !== 'T_INTERFACE' |
|
| 684 | 684 | ) { |
| 685 | 685 | return ''; |
| 686 | 686 | } |
| 687 | 687 | |
| 688 | - $extends = PHPCSHelper::findExtendedClassName($phpcsFile, $stackPtr); |
|
| 689 | - if (empty($extends) || \is_string($extends) === false) { |
|
| 688 | + $extends = PHPCSHelper::findExtendedClassName( $phpcsFile, $stackPtr ); |
|
| 689 | + if ( empty( $extends ) || \is_string( $extends ) === false ) { |
|
| 690 | 690 | return ''; |
| 691 | 691 | } |
| 692 | 692 | |
| 693 | - return $this->getFQName($phpcsFile, $stackPtr, $extends); |
|
| 693 | + return $this->getFQName( $phpcsFile, $stackPtr, $extends ); |
|
| 694 | 694 | } |
| 695 | 695 | |
| 696 | 696 | |
@@ -705,37 +705,37 @@ discard block |
||
| 705 | 705 | * |
| 706 | 706 | * @return string |
| 707 | 707 | */ |
| 708 | - public function getFQClassNameFromDoubleColonToken(File $phpcsFile, $stackPtr) |
|
| 708 | + public function getFQClassNameFromDoubleColonToken( File $phpcsFile, $stackPtr ) |
|
| 709 | 709 | { |
| 710 | 710 | $tokens = $phpcsFile->getTokens(); |
| 711 | 711 | |
| 712 | 712 | // Check for the existence of the token. |
| 713 | - if (isset($tokens[$stackPtr]) === false) { |
|
| 713 | + if ( isset( $tokens[ $stackPtr ] ) === false ) { |
|
| 714 | 714 | return ''; |
| 715 | 715 | } |
| 716 | 716 | |
| 717 | - if ($tokens[$stackPtr]['code'] !== \T_DOUBLE_COLON) { |
|
| 717 | + if ( $tokens[ $stackPtr ][ 'code' ] !== \T_DOUBLE_COLON ) { |
|
| 718 | 718 | return ''; |
| 719 | 719 | } |
| 720 | 720 | |
| 721 | 721 | // Nothing to do if previous token is a variable as we don't know where it was defined. |
| 722 | - if ($tokens[$stackPtr - 1]['code'] === \T_VARIABLE) { |
|
| 722 | + if ( $tokens[ $stackPtr - 1 ][ 'code' ] === \T_VARIABLE ) { |
|
| 723 | 723 | return ''; |
| 724 | 724 | } |
| 725 | 725 | |
| 726 | 726 | // Nothing to do if 'parent' or 'static' as we don't know how far the class tree extends. |
| 727 | - if (\in_array($tokens[$stackPtr - 1]['code'], array(\T_PARENT, \T_STATIC), true)) { |
|
| 727 | + if ( \in_array( $tokens[ $stackPtr - 1 ][ 'code' ], array( \T_PARENT, \T_STATIC ), true ) ) { |
|
| 728 | 728 | return ''; |
| 729 | 729 | } |
| 730 | 730 | |
| 731 | 731 | // Get the classname from the class declaration if self is used. |
| 732 | - if ($tokens[$stackPtr - 1]['code'] === \T_SELF) { |
|
| 733 | - $classDeclarationPtr = $phpcsFile->findPrevious(\T_CLASS, $stackPtr - 1); |
|
| 734 | - if ($classDeclarationPtr === false) { |
|
| 732 | + if ( $tokens[ $stackPtr - 1 ][ 'code' ] === \T_SELF ) { |
|
| 733 | + $classDeclarationPtr = $phpcsFile->findPrevious( \T_CLASS, $stackPtr - 1 ); |
|
| 734 | + if ( $classDeclarationPtr === false ) { |
|
| 735 | 735 | return ''; |
| 736 | 736 | } |
| 737 | - $className = $phpcsFile->getDeclarationName($classDeclarationPtr); |
|
| 738 | - return $this->getFQName($phpcsFile, $classDeclarationPtr, $className); |
|
| 737 | + $className = $phpcsFile->getDeclarationName( $classDeclarationPtr ); |
|
| 738 | + return $this->getFQName( $phpcsFile, $classDeclarationPtr, $className ); |
|
| 739 | 739 | } |
| 740 | 740 | |
| 741 | 741 | $find = array( |
@@ -745,16 +745,16 @@ discard block |
||
| 745 | 745 | \T_WHITESPACE, |
| 746 | 746 | ); |
| 747 | 747 | |
| 748 | - $start = $phpcsFile->findPrevious($find, $stackPtr - 1, null, true, null, true); |
|
| 749 | - if ($start === false || isset($tokens[($start + 1)]) === false) { |
|
| 748 | + $start = $phpcsFile->findPrevious( $find, $stackPtr - 1, null, true, null, true ); |
|
| 749 | + if ( $start === false || isset( $tokens[ ( $start + 1 ) ] ) === false ) { |
|
| 750 | 750 | return ''; |
| 751 | 751 | } |
| 752 | 752 | |
| 753 | - $start = ($start + 1); |
|
| 754 | - $className = $phpcsFile->getTokensAsString($start, ($stackPtr - $start)); |
|
| 755 | - $className = trim($className); |
|
| 753 | + $start = ( $start + 1 ); |
|
| 754 | + $className = $phpcsFile->getTokensAsString( $start, ( $stackPtr - $start ) ); |
|
| 755 | + $className = trim( $className ); |
|
| 756 | 756 | |
| 757 | - return $this->getFQName($phpcsFile, $stackPtr, $className); |
|
| 757 | + return $this->getFQName( $phpcsFile, $stackPtr, $className ); |
|
| 758 | 758 | } |
| 759 | 759 | |
| 760 | 760 | |
@@ -770,21 +770,21 @@ discard block |
||
| 770 | 770 | * |
| 771 | 771 | * @return string |
| 772 | 772 | */ |
| 773 | - public function getFQName(File $phpcsFile, $stackPtr, $name) |
|
| 773 | + public function getFQName( File $phpcsFile, $stackPtr, $name ) |
|
| 774 | 774 | { |
| 775 | - if (strpos($name, '\\') === 0) { |
|
| 775 | + if ( strpos( $name, '\\' ) === 0 ) { |
|
| 776 | 776 | // Already fully qualified. |
| 777 | 777 | return $name; |
| 778 | 778 | } |
| 779 | 779 | |
| 780 | 780 | // Remove the namespace keyword if used. |
| 781 | - if (strpos($name, 'namespace\\') === 0) { |
|
| 782 | - $name = substr($name, 10); |
|
| 781 | + if ( strpos( $name, 'namespace\\' ) === 0 ) { |
|
| 782 | + $name = substr( $name, 10 ); |
|
| 783 | 783 | } |
| 784 | 784 | |
| 785 | - $namespace = $this->determineNamespace($phpcsFile, $stackPtr); |
|
| 785 | + $namespace = $this->determineNamespace( $phpcsFile, $stackPtr ); |
|
| 786 | 786 | |
| 787 | - if ($namespace === '') { |
|
| 787 | + if ( $namespace === '' ) { |
|
| 788 | 788 | return '\\' . $name; |
| 789 | 789 | } else { |
| 790 | 790 | return '\\' . $namespace . '\\' . $name; |
@@ -800,13 +800,13 @@ discard block |
||
| 800 | 800 | * |
| 801 | 801 | * @return bool True if namespaced, false if global. |
| 802 | 802 | */ |
| 803 | - public function isNamespaced($FQName) |
|
| 803 | + public function isNamespaced( $FQName ) |
|
| 804 | 804 | { |
| 805 | - if (strpos($FQName, '\\') !== 0) { |
|
| 806 | - throw new PHPCS_Exception('$FQName must be a fully qualified name'); |
|
| 805 | + if ( strpos( $FQName, '\\' ) !== 0 ) { |
|
| 806 | + throw new PHPCS_Exception( '$FQName must be a fully qualified name' ); |
|
| 807 | 807 | } |
| 808 | 808 | |
| 809 | - return (strpos(substr($FQName, 1), '\\') !== false); |
|
| 809 | + return ( strpos( substr( $FQName, 1 ), '\\' ) !== false ); |
|
| 810 | 810 | } |
| 811 | 811 | |
| 812 | 812 | |
@@ -818,21 +818,21 @@ discard block |
||
| 818 | 818 | * |
| 819 | 819 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
| 820 | 820 | */ |
| 821 | - public function determineNamespace(File $phpcsFile, $stackPtr) |
|
| 821 | + public function determineNamespace( File $phpcsFile, $stackPtr ) |
|
| 822 | 822 | { |
| 823 | 823 | $tokens = $phpcsFile->getTokens(); |
| 824 | 824 | |
| 825 | 825 | // Check for the existence of the token. |
| 826 | - if (isset($tokens[$stackPtr]) === false) { |
|
| 826 | + if ( isset( $tokens[ $stackPtr ] ) === false ) { |
|
| 827 | 827 | return ''; |
| 828 | 828 | } |
| 829 | 829 | |
| 830 | 830 | // Check for scoped namespace {}. |
| 831 | - if (empty($tokens[$stackPtr]['conditions']) === false) { |
|
| 832 | - $namespacePtr = $phpcsFile->getCondition($stackPtr, \T_NAMESPACE); |
|
| 833 | - if ($namespacePtr !== false) { |
|
| 834 | - $namespace = $this->getDeclaredNamespaceName($phpcsFile, $namespacePtr); |
|
| 835 | - if ($namespace !== false) { |
|
| 831 | + if ( empty( $tokens[ $stackPtr ][ 'conditions' ] ) === false ) { |
|
| 832 | + $namespacePtr = $phpcsFile->getCondition( $stackPtr, \T_NAMESPACE ); |
|
| 833 | + if ( $namespacePtr !== false ) { |
|
| 834 | + $namespace = $this->getDeclaredNamespaceName( $phpcsFile, $namespacePtr ); |
|
| 835 | + if ( $namespace !== false ) { |
|
| 836 | 836 | return $namespace; |
| 837 | 837 | } |
| 838 | 838 | |
@@ -851,19 +851,19 @@ discard block |
||
| 851 | 851 | $previousNSToken = $stackPtr; |
| 852 | 852 | $namespace = false; |
| 853 | 853 | do { |
| 854 | - $previousNSToken = $phpcsFile->findPrevious(\T_NAMESPACE, ($previousNSToken - 1)); |
|
| 854 | + $previousNSToken = $phpcsFile->findPrevious( \T_NAMESPACE, ( $previousNSToken - 1 ) ); |
|
| 855 | 855 | |
| 856 | 856 | // Stop if we encounter a scoped namespace declaration as we already know we're not in one. |
| 857 | - if (empty($tokens[$previousNSToken]['scope_condition']) === false && $tokens[$previousNSToken]['scope_condition'] === $previousNSToken) { |
|
| 857 | + if ( empty( $tokens[ $previousNSToken ][ 'scope_condition' ] ) === false && $tokens[ $previousNSToken ][ 'scope_condition' ] === $previousNSToken ) { |
|
| 858 | 858 | break; |
| 859 | 859 | } |
| 860 | 860 | |
| 861 | - $namespace = $this->getDeclaredNamespaceName($phpcsFile, $previousNSToken); |
|
| 861 | + $namespace = $this->getDeclaredNamespaceName( $phpcsFile, $previousNSToken ); |
|
| 862 | 862 | |
| 863 | - } while ($namespace === false && $previousNSToken !== false); |
|
| 863 | + } while ( $namespace === false && $previousNSToken !== false ); |
|
| 864 | 864 | |
| 865 | 865 | // If we still haven't got a namespace, return an empty string. |
| 866 | - if ($namespace === false) { |
|
| 866 | + if ( $namespace === false ) { |
|
| 867 | 867 | return ''; |
| 868 | 868 | } else { |
| 869 | 869 | return $namespace; |
@@ -882,26 +882,26 @@ discard block |
||
| 882 | 882 | * @return string|false Namespace name or false if not a namespace declaration. |
| 883 | 883 | * Namespace name can be an empty string for global namespace declaration. |
| 884 | 884 | */ |
| 885 | - public function getDeclaredNamespaceName(File $phpcsFile, $stackPtr) |
|
| 885 | + public function getDeclaredNamespaceName( File $phpcsFile, $stackPtr ) |
|
| 886 | 886 | { |
| 887 | 887 | $tokens = $phpcsFile->getTokens(); |
| 888 | 888 | |
| 889 | 889 | // Check for the existence of the token. |
| 890 | - if ($stackPtr === false || isset($tokens[$stackPtr]) === false) { |
|
| 890 | + if ( $stackPtr === false || isset( $tokens[ $stackPtr ] ) === false ) { |
|
| 891 | 891 | return false; |
| 892 | 892 | } |
| 893 | 893 | |
| 894 | - if ($tokens[$stackPtr]['code'] !== \T_NAMESPACE) { |
|
| 894 | + if ( $tokens[ $stackPtr ][ 'code' ] !== \T_NAMESPACE ) { |
|
| 895 | 895 | return false; |
| 896 | 896 | } |
| 897 | 897 | |
| 898 | - if ($tokens[($stackPtr + 1)]['code'] === \T_NS_SEPARATOR) { |
|
| 898 | + if ( $tokens[ ( $stackPtr + 1 ) ][ 'code' ] === \T_NS_SEPARATOR ) { |
|
| 899 | 899 | // Not a namespace declaration, but use of, i.e. namespace\someFunction(); |
| 900 | 900 | return false; |
| 901 | 901 | } |
| 902 | 902 | |
| 903 | - $nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true); |
|
| 904 | - if ($tokens[$nextToken]['code'] === \T_OPEN_CURLY_BRACKET) { |
|
| 903 | + $nextToken = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true ); |
|
| 904 | + if ( $tokens[ $nextToken ][ 'code' ] === \T_OPEN_CURLY_BRACKET ) { |
|
| 905 | 905 | // Declaration for global namespace when using multiple namespaces in a file. |
| 906 | 906 | // I.e.: namespace {} |
| 907 | 907 | return ''; |
@@ -915,8 +915,8 @@ discard block |
||
| 915 | 915 | ); |
| 916 | 916 | |
| 917 | 917 | $namespaceName = ''; |
| 918 | - while (isset($validTokens[$tokens[$nextToken]['code']]) === true) { |
|
| 919 | - $namespaceName .= trim($tokens[$nextToken]['content']); |
|
| 918 | + while ( isset( $validTokens[ $tokens[ $nextToken ][ 'code' ] ] ) === true ) { |
|
| 919 | + $namespaceName .= trim( $tokens[ $nextToken ][ 'content' ] ); |
|
| 920 | 920 | $nextToken++; |
| 921 | 921 | } |
| 922 | 922 | |
@@ -939,43 +939,43 @@ discard block |
||
| 939 | 939 | * no return type was found or the passed token was |
| 940 | 940 | * not of the correct type. |
| 941 | 941 | */ |
| 942 | - public function getReturnTypeHintToken(File $phpcsFile, $stackPtr) |
|
| 942 | + public function getReturnTypeHintToken( File $phpcsFile, $stackPtr ) |
|
| 943 | 943 | { |
| 944 | 944 | $tokens = $phpcsFile->getTokens(); |
| 945 | 945 | |
| 946 | - if (\defined('T_RETURN_TYPE') && $tokens[$stackPtr]['code'] === \T_RETURN_TYPE) { |
|
| 946 | + if ( \defined( 'T_RETURN_TYPE' ) && $tokens[ $stackPtr ][ 'code' ] === \T_RETURN_TYPE ) { |
|
| 947 | 947 | return $stackPtr; |
| 948 | 948 | } |
| 949 | 949 | |
| 950 | - if ($tokens[$stackPtr]['code'] !== \T_FUNCTION && $tokens[$stackPtr]['code'] !== \T_CLOSURE) { |
|
| 950 | + if ( $tokens[ $stackPtr ][ 'code' ] !== \T_FUNCTION && $tokens[ $stackPtr ][ 'code' ] !== \T_CLOSURE ) { |
|
| 951 | 951 | return false; |
| 952 | 952 | } |
| 953 | 953 | |
| 954 | - if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) { |
|
| 954 | + if ( isset( $tokens[ $stackPtr ][ 'parenthesis_closer' ] ) === false ) { |
|
| 955 | 955 | return false; |
| 956 | 956 | } |
| 957 | 957 | |
| 958 | 958 | // Allow for interface and abstract method declarations. |
| 959 | 959 | $endOfFunctionDeclaration = null; |
| 960 | - if (isset($tokens[$stackPtr]['scope_opener'])) { |
|
| 961 | - $endOfFunctionDeclaration = $tokens[$stackPtr]['scope_opener']; |
|
| 960 | + if ( isset( $tokens[ $stackPtr ][ 'scope_opener' ] ) ) { |
|
| 961 | + $endOfFunctionDeclaration = $tokens[ $stackPtr ][ 'scope_opener' ]; |
|
| 962 | 962 | } else { |
| 963 | - $nextSemiColon = $phpcsFile->findNext(\T_SEMICOLON, ($tokens[$stackPtr]['parenthesis_closer'] + 1), null, false, null, true); |
|
| 964 | - if ($nextSemiColon !== false) { |
|
| 963 | + $nextSemiColon = $phpcsFile->findNext( \T_SEMICOLON, ( $tokens[ $stackPtr ][ 'parenthesis_closer' ] + 1 ), null, false, null, true ); |
|
| 964 | + if ( $nextSemiColon !== false ) { |
|
| 965 | 965 | $endOfFunctionDeclaration = $nextSemiColon; |
| 966 | 966 | } |
| 967 | 967 | } |
| 968 | 968 | |
| 969 | - if (isset($endOfFunctionDeclaration) === false) { |
|
| 969 | + if ( isset( $endOfFunctionDeclaration ) === false ) { |
|
| 970 | 970 | return false; |
| 971 | 971 | } |
| 972 | 972 | |
| 973 | 973 | $hasColon = $phpcsFile->findNext( |
| 974 | - array(\T_COLON, \T_INLINE_ELSE), |
|
| 975 | - ($tokens[$stackPtr]['parenthesis_closer'] + 1), |
|
| 974 | + array( \T_COLON, \T_INLINE_ELSE ), |
|
| 975 | + ( $tokens[ $stackPtr ][ 'parenthesis_closer' ] + 1 ), |
|
| 976 | 976 | $endOfFunctionDeclaration |
| 977 | 977 | ); |
| 978 | - if ($hasColon === false) { |
|
| 978 | + if ( $hasColon === false ) { |
|
| 979 | 979 | return false; |
| 980 | 980 | } |
| 981 | 981 | |
@@ -996,7 +996,7 @@ discard block |
||
| 996 | 996 | \T_STRING, |
| 997 | 997 | ); |
| 998 | 998 | |
| 999 | - return $phpcsFile->findPrevious($unrecognizedTypes, ($endOfFunctionDeclaration - 1), $hasColon); |
|
| 999 | + return $phpcsFile->findPrevious( $unrecognizedTypes, ( $endOfFunctionDeclaration - 1 ), $hasColon ); |
|
| 1000 | 1000 | } |
| 1001 | 1001 | |
| 1002 | 1002 | |
@@ -1018,37 +1018,37 @@ discard block |
||
| 1018 | 1018 | * |
| 1019 | 1019 | * @return string|false The name of the return type token. |
| 1020 | 1020 | */ |
| 1021 | - public function getReturnTypeHintName(File $phpcsFile, $stackPtr) |
|
| 1021 | + public function getReturnTypeHintName( File $phpcsFile, $stackPtr ) |
|
| 1022 | 1022 | { |
| 1023 | 1023 | $tokens = $phpcsFile->getTokens(); |
| 1024 | 1024 | |
| 1025 | 1025 | // In older PHPCS versions, the nullable indicator will turn a return type colon into a T_INLINE_ELSE. |
| 1026 | - $colon = $phpcsFile->findPrevious(array(\T_COLON, \T_INLINE_ELSE, \T_FUNCTION, \T_CLOSE_PARENTHESIS), ($stackPtr - 1)); |
|
| 1027 | - if ($colon === false |
|
| 1028 | - || ($tokens[$colon]['code'] !== \T_COLON && $tokens[$colon]['code'] !== \T_INLINE_ELSE) |
|
| 1026 | + $colon = $phpcsFile->findPrevious( array( \T_COLON, \T_INLINE_ELSE, \T_FUNCTION, \T_CLOSE_PARENTHESIS ), ( $stackPtr - 1 ) ); |
|
| 1027 | + if ( $colon === false |
|
| 1028 | + || ( $tokens[ $colon ][ 'code' ] !== \T_COLON && $tokens[ $colon ][ 'code' ] !== \T_INLINE_ELSE ) |
|
| 1029 | 1029 | ) { |
| 1030 | 1030 | // Shouldn't happen, just in case. |
| 1031 | 1031 | return; |
| 1032 | 1032 | } |
| 1033 | 1033 | |
| 1034 | 1034 | $returnTypeHint = ''; |
| 1035 | - for ($i = ($colon + 1); $i <= $stackPtr; $i++) { |
|
| 1035 | + for ( $i = ( $colon + 1 ); $i <= $stackPtr; $i++ ) { |
|
| 1036 | 1036 | // As of PHPCS 3.3.0+, all tokens are tokenized as "normal", so T_CALLABLE, T_SELF etc are |
| 1037 | 1037 | // all possible, just exclude anything that's regarded as empty and the nullable indicator. |
| 1038 | - if (isset(Tokens::$emptyTokens[$tokens[$i]['code']])) { |
|
| 1038 | + if ( isset( Tokens::$emptyTokens[ $tokens[ $i ][ 'code' ] ] ) ) { |
|
| 1039 | 1039 | continue; |
| 1040 | 1040 | } |
| 1041 | 1041 | |
| 1042 | - if ($tokens[$i]['type'] === 'T_NULLABLE') { |
|
| 1042 | + if ( $tokens[ $i ][ 'type' ] === 'T_NULLABLE' ) { |
|
| 1043 | 1043 | continue; |
| 1044 | 1044 | } |
| 1045 | 1045 | |
| 1046 | - if (\defined('T_NULLABLE') === false && $tokens[$i]['code'] === \T_INLINE_THEN) { |
|
| 1046 | + if ( \defined( 'T_NULLABLE' ) === false && $tokens[ $i ][ 'code' ] === \T_INLINE_THEN ) { |
|
| 1047 | 1047 | // Old PHPCS. |
| 1048 | 1048 | continue; |
| 1049 | 1049 | } |
| 1050 | 1050 | |
| 1051 | - $returnTypeHint .= $tokens[$i]['content']; |
|
| 1051 | + $returnTypeHint .= $tokens[ $i ][ 'content' ]; |
|
| 1052 | 1052 | } |
| 1053 | 1053 | |
| 1054 | 1054 | return $returnTypeHint; |
@@ -1069,11 +1069,11 @@ discard block |
||
| 1069 | 1069 | * |
| 1070 | 1070 | * @return bool |
| 1071 | 1071 | */ |
| 1072 | - public function isClassProperty(File $phpcsFile, $stackPtr) |
|
| 1072 | + public function isClassProperty( File $phpcsFile, $stackPtr ) |
|
| 1073 | 1073 | { |
| 1074 | 1074 | $tokens = $phpcsFile->getTokens(); |
| 1075 | 1075 | |
| 1076 | - if (isset($tokens[$stackPtr]) === false || $tokens[$stackPtr]['code'] !== \T_VARIABLE) { |
|
| 1076 | + if ( isset( $tokens[ $stackPtr ] ) === false || $tokens[ $stackPtr ][ 'code' ] !== \T_VARIABLE ) { |
|
| 1077 | 1077 | return false; |
| 1078 | 1078 | } |
| 1079 | 1079 | |
@@ -1084,17 +1084,17 @@ discard block |
||
| 1084 | 1084 | 'T_TRAIT' => true, |
| 1085 | 1085 | ); |
| 1086 | 1086 | |
| 1087 | - $scopePtr = $this->validDirectScope($phpcsFile, $stackPtr, $validScopes); |
|
| 1088 | - if ($scopePtr !== false) { |
|
| 1087 | + $scopePtr = $this->validDirectScope( $phpcsFile, $stackPtr, $validScopes ); |
|
| 1088 | + if ( $scopePtr !== false ) { |
|
| 1089 | 1089 | // Make sure it's not a method parameter. |
| 1090 | - if (empty($tokens[$stackPtr]['nested_parenthesis']) === true) { |
|
| 1090 | + if ( empty( $tokens[ $stackPtr ][ 'nested_parenthesis' ] ) === true ) { |
|
| 1091 | 1091 | return true; |
| 1092 | 1092 | } else { |
| 1093 | - $parenthesis = array_keys($tokens[$stackPtr]['nested_parenthesis']); |
|
| 1094 | - $deepestOpen = array_pop($parenthesis); |
|
| 1095 | - if ($deepestOpen < $scopePtr |
|
| 1096 | - || isset($tokens[$deepestOpen]['parenthesis_owner']) === false |
|
| 1097 | - || $tokens[$tokens[$deepestOpen]['parenthesis_owner']]['code'] !== \T_FUNCTION |
|
| 1093 | + $parenthesis = array_keys( $tokens[ $stackPtr ][ 'nested_parenthesis' ] ); |
|
| 1094 | + $deepestOpen = array_pop( $parenthesis ); |
|
| 1095 | + if ( $deepestOpen < $scopePtr |
|
| 1096 | + || isset( $tokens[ $deepestOpen ][ 'parenthesis_owner' ] ) === false |
|
| 1097 | + || $tokens[ $tokens[ $deepestOpen ][ 'parenthesis_owner' ] ][ 'code' ] !== \T_FUNCTION |
|
| 1098 | 1098 | ) { |
| 1099 | 1099 | return true; |
| 1100 | 1100 | } |
@@ -1114,11 +1114,11 @@ discard block |
||
| 1114 | 1114 | * |
| 1115 | 1115 | * @return bool |
| 1116 | 1116 | */ |
| 1117 | - public function isClassConstant(File $phpcsFile, $stackPtr) |
|
| 1117 | + public function isClassConstant( File $phpcsFile, $stackPtr ) |
|
| 1118 | 1118 | { |
| 1119 | 1119 | $tokens = $phpcsFile->getTokens(); |
| 1120 | 1120 | |
| 1121 | - if (isset($tokens[$stackPtr]) === false || $tokens[$stackPtr]['code'] !== \T_CONST) { |
|
| 1121 | + if ( isset( $tokens[ $stackPtr ] ) === false || $tokens[ $stackPtr ][ 'code' ] !== \T_CONST ) { |
|
| 1122 | 1122 | return false; |
| 1123 | 1123 | } |
| 1124 | 1124 | |
@@ -1128,7 +1128,7 @@ discard block |
||
| 1128 | 1128 | 'T_ANON_CLASS' => true, |
| 1129 | 1129 | 'T_INTERFACE' => true, |
| 1130 | 1130 | ); |
| 1131 | - if ($this->validDirectScope($phpcsFile, $stackPtr, $validScopes) !== false) { |
|
| 1131 | + if ( $this->validDirectScope( $phpcsFile, $stackPtr, $validScopes ) !== false ) { |
|
| 1132 | 1132 | return true; |
| 1133 | 1133 | } |
| 1134 | 1134 | |
@@ -1152,25 +1152,25 @@ discard block |
||
| 1152 | 1152 | * |
| 1153 | 1153 | * @return int|bool StackPtr to the scope if valid, false otherwise. |
| 1154 | 1154 | */ |
| 1155 | - protected function validDirectScope(File $phpcsFile, $stackPtr, $validScopes) |
|
| 1155 | + protected function validDirectScope( File $phpcsFile, $stackPtr, $validScopes ) |
|
| 1156 | 1156 | { |
| 1157 | 1157 | $tokens = $phpcsFile->getTokens(); |
| 1158 | 1158 | |
| 1159 | - if (empty($tokens[$stackPtr]['conditions']) === true) { |
|
| 1159 | + if ( empty( $tokens[ $stackPtr ][ 'conditions' ] ) === true ) { |
|
| 1160 | 1160 | return false; |
| 1161 | 1161 | } |
| 1162 | 1162 | |
| 1163 | 1163 | /* |
| 1164 | 1164 | * Check only the direct wrapping scope of the token. |
| 1165 | 1165 | */ |
| 1166 | - $conditions = array_keys($tokens[$stackPtr]['conditions']); |
|
| 1167 | - $ptr = array_pop($conditions); |
|
| 1166 | + $conditions = array_keys( $tokens[ $stackPtr ][ 'conditions' ] ); |
|
| 1167 | + $ptr = array_pop( $conditions ); |
|
| 1168 | 1168 | |
| 1169 | - if (isset($tokens[$ptr]) === false) { |
|
| 1169 | + if ( isset( $tokens[ $ptr ] ) === false ) { |
|
| 1170 | 1170 | return false; |
| 1171 | 1171 | } |
| 1172 | 1172 | |
| 1173 | - if (isset($validScopes[$tokens[$ptr]['type']]) === true) { |
|
| 1173 | + if ( isset( $validScopes[ $tokens[ $ptr ][ 'type' ] ] ) === true ) { |
|
| 1174 | 1174 | return $ptr; |
| 1175 | 1175 | } |
| 1176 | 1176 | |
@@ -1194,34 +1194,34 @@ discard block |
||
| 1194 | 1194 | * - no type hints were found |
| 1195 | 1195 | * - or the passed token was not of the correct type. |
| 1196 | 1196 | */ |
| 1197 | - public function getTypeHintsFromFunctionDeclaration(File $phpcsFile, $stackPtr) |
|
| 1197 | + public function getTypeHintsFromFunctionDeclaration( File $phpcsFile, $stackPtr ) |
|
| 1198 | 1198 | { |
| 1199 | 1199 | $tokens = $phpcsFile->getTokens(); |
| 1200 | 1200 | |
| 1201 | - if ($tokens[$stackPtr]['code'] !== \T_FUNCTION && $tokens[$stackPtr]['code'] !== \T_CLOSURE) { |
|
| 1201 | + if ( $tokens[ $stackPtr ][ 'code' ] !== \T_FUNCTION && $tokens[ $stackPtr ][ 'code' ] !== \T_CLOSURE ) { |
|
| 1202 | 1202 | return array(); |
| 1203 | 1203 | } |
| 1204 | 1204 | |
| 1205 | - $parameters = PHPCSHelper::getMethodParameters($phpcsFile, $stackPtr); |
|
| 1206 | - if (empty($parameters) || \is_array($parameters) === false) { |
|
| 1205 | + $parameters = PHPCSHelper::getMethodParameters( $phpcsFile, $stackPtr ); |
|
| 1206 | + if ( empty( $parameters ) || \is_array( $parameters ) === false ) { |
|
| 1207 | 1207 | return array(); |
| 1208 | 1208 | } |
| 1209 | 1209 | |
| 1210 | 1210 | $typeHints = array(); |
| 1211 | 1211 | |
| 1212 | - foreach ($parameters as $param) { |
|
| 1213 | - if ($param['type_hint'] === '') { |
|
| 1212 | + foreach ( $parameters as $param ) { |
|
| 1213 | + if ( $param[ 'type_hint' ] === '' ) { |
|
| 1214 | 1214 | continue; |
| 1215 | 1215 | } |
| 1216 | 1216 | |
| 1217 | 1217 | // Strip off potential nullable indication. |
| 1218 | - $typeHint = ltrim($param['type_hint'], '?'); |
|
| 1218 | + $typeHint = ltrim( $param[ 'type_hint' ], '?' ); |
|
| 1219 | 1219 | |
| 1220 | 1220 | // Strip off potential (global) namespace indication. |
| 1221 | - $typeHint = ltrim($typeHint, '\\'); |
|
| 1221 | + $typeHint = ltrim( $typeHint, '\\' ); |
|
| 1222 | 1222 | |
| 1223 | - if ($typeHint !== '') { |
|
| 1224 | - $typeHints[] = $typeHint; |
|
| 1223 | + if ( $typeHint !== '' ) { |
|
| 1224 | + $typeHints[ ] = $typeHint; |
|
| 1225 | 1225 | } |
| 1226 | 1226 | } |
| 1227 | 1227 | |
@@ -1238,36 +1238,36 @@ discard block |
||
| 1238 | 1238 | * @return string|false The algorithm name without quotes if this was a relevant hash |
| 1239 | 1239 | * function call or false if it was not. |
| 1240 | 1240 | */ |
| 1241 | - public function getHashAlgorithmParameter(File $phpcsFile, $stackPtr) |
|
| 1241 | + public function getHashAlgorithmParameter( File $phpcsFile, $stackPtr ) |
|
| 1242 | 1242 | { |
| 1243 | 1243 | $tokens = $phpcsFile->getTokens(); |
| 1244 | 1244 | |
| 1245 | 1245 | // Check for the existence of the token. |
| 1246 | - if (isset($tokens[$stackPtr]) === false) { |
|
| 1246 | + if ( isset( $tokens[ $stackPtr ] ) === false ) { |
|
| 1247 | 1247 | return false; |
| 1248 | 1248 | } |
| 1249 | 1249 | |
| 1250 | - if ($tokens[$stackPtr]['code'] !== \T_STRING) { |
|
| 1250 | + if ( $tokens[ $stackPtr ][ 'code' ] !== \T_STRING ) { |
|
| 1251 | 1251 | return false; |
| 1252 | 1252 | } |
| 1253 | 1253 | |
| 1254 | - $functionName = $tokens[$stackPtr]['content']; |
|
| 1255 | - $functionNameLc = strtolower($functionName); |
|
| 1254 | + $functionName = $tokens[ $stackPtr ][ 'content' ]; |
|
| 1255 | + $functionNameLc = strtolower( $functionName ); |
|
| 1256 | 1256 | |
| 1257 | 1257 | // Bow out if not one of the functions we're targetting. |
| 1258 | - if (isset($this->hashAlgoFunctions[$functionNameLc]) === false) { |
|
| 1258 | + if ( isset( $this->hashAlgoFunctions[ $functionNameLc ] ) === false ) { |
|
| 1259 | 1259 | return false; |
| 1260 | 1260 | } |
| 1261 | 1261 | |
| 1262 | 1262 | // Get the parameter from the function call which should contain the algorithm name. |
| 1263 | - $algoParam = $this->getFunctionCallParameter($phpcsFile, $stackPtr, $this->hashAlgoFunctions[$functionNameLc]); |
|
| 1264 | - if ($algoParam === false) { |
|
| 1263 | + $algoParam = $this->getFunctionCallParameter( $phpcsFile, $stackPtr, $this->hashAlgoFunctions[ $functionNameLc ] ); |
|
| 1264 | + if ( $algoParam === false ) { |
|
| 1265 | 1265 | return false; |
| 1266 | 1266 | } |
| 1267 | 1267 | |
| 1268 | 1268 | // Algorithm is a text string, so we need to remove the quotes. |
| 1269 | - $algo = strtolower(trim($algoParam['raw'])); |
|
| 1270 | - $algo = $this->stripQuotes($algo); |
|
| 1269 | + $algo = strtolower( trim( $algoParam[ 'raw' ] ) ); |
|
| 1270 | + $algo = $this->stripQuotes( $algo ); |
|
| 1271 | 1271 | |
| 1272 | 1272 | return $algo; |
| 1273 | 1273 | } |
@@ -1281,36 +1281,36 @@ discard block |
||
| 1281 | 1281 | * |
| 1282 | 1282 | * @return bool |
| 1283 | 1283 | */ |
| 1284 | - public function isUseOfGlobalConstant(File $phpcsFile, $stackPtr) |
|
| 1284 | + public function isUseOfGlobalConstant( File $phpcsFile, $stackPtr ) |
|
| 1285 | 1285 | { |
| 1286 | 1286 | static $isLowPHPCS, $isLowPHP; |
| 1287 | 1287 | |
| 1288 | 1288 | $tokens = $phpcsFile->getTokens(); |
| 1289 | 1289 | |
| 1290 | 1290 | // Check for the existence of the token. |
| 1291 | - if (isset($tokens[$stackPtr]) === false) { |
|
| 1291 | + if ( isset( $tokens[ $stackPtr ] ) === false ) { |
|
| 1292 | 1292 | return false; |
| 1293 | 1293 | } |
| 1294 | 1294 | |
| 1295 | 1295 | // Is this one of the tokens this function handles ? |
| 1296 | - if ($tokens[$stackPtr]['code'] !== \T_STRING) { |
|
| 1296 | + if ( $tokens[ $stackPtr ][ 'code' ] !== \T_STRING ) { |
|
| 1297 | 1297 | return false; |
| 1298 | 1298 | } |
| 1299 | 1299 | |
| 1300 | 1300 | // Check for older PHP, PHPCS version so we can compensate for misidentified tokens. |
| 1301 | - if (isset($isLowPHPCS, $isLowPHP) === false) { |
|
| 1301 | + if ( isset( $isLowPHPCS, $isLowPHP ) === false ) { |
|
| 1302 | 1302 | $isLowPHP = false; |
| 1303 | 1303 | $isLowPHPCS = false; |
| 1304 | - if (version_compare(\PHP_VERSION_ID, '50400', '<')) { |
|
| 1304 | + if ( version_compare( \PHP_VERSION_ID, '50400', '<' ) ) { |
|
| 1305 | 1305 | $isLowPHP = true; |
| 1306 | - $isLowPHPCS = version_compare(PHPCSHelper::getVersion(), '2.4.0', '<'); |
|
| 1306 | + $isLowPHPCS = version_compare( PHPCSHelper::getVersion(), '2.4.0', '<' ); |
|
| 1307 | 1307 | } |
| 1308 | 1308 | } |
| 1309 | 1309 | |
| 1310 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
| 1311 | - if ($next !== false |
|
| 1312 | - && ($tokens[$next]['code'] === \T_OPEN_PARENTHESIS |
|
| 1313 | - || $tokens[$next]['code'] === \T_DOUBLE_COLON) |
|
| 1310 | + $next = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); |
|
| 1311 | + if ( $next !== false |
|
| 1312 | + && ( $tokens[ $next ][ 'code' ] === \T_OPEN_PARENTHESIS |
|
| 1313 | + || $tokens[ $next ][ 'code' ] === \T_DOUBLE_COLON ) |
|
| 1314 | 1314 | ) { |
| 1315 | 1315 | // Function call or declaration. |
| 1316 | 1316 | return false; |
@@ -1338,30 +1338,30 @@ discard block |
||
| 1338 | 1338 | 'T_PRIVATE' => true, |
| 1339 | 1339 | ); |
| 1340 | 1340 | |
| 1341 | - $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
| 1342 | - if ($prev !== false |
|
| 1343 | - && (isset($tokensToIgnore[$tokens[$prev]['type']]) === true |
|
| 1344 | - || ($tokens[$prev]['code'] === \T_STRING |
|
| 1345 | - && (($isLowPHPCS === true |
|
| 1346 | - && $tokens[$prev]['content'] === 'trait') |
|
| 1347 | - || ($isLowPHP === true |
|
| 1348 | - && $tokens[$prev]['content'] === 'insteadof')))) |
|
| 1341 | + $prev = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true ); |
|
| 1342 | + if ( $prev !== false |
|
| 1343 | + && ( isset( $tokensToIgnore[ $tokens[ $prev ][ 'type' ] ] ) === true |
|
| 1344 | + || ( $tokens[ $prev ][ 'code' ] === \T_STRING |
|
| 1345 | + && ( ( $isLowPHPCS === true |
|
| 1346 | + && $tokens[ $prev ][ 'content' ] === 'trait' ) |
|
| 1347 | + || ( $isLowPHP === true |
|
| 1348 | + && $tokens[ $prev ][ 'content' ] === 'insteadof' ) ) ) ) |
|
| 1349 | 1349 | ) { |
| 1350 | 1350 | // Not the use of a constant. |
| 1351 | 1351 | return false; |
| 1352 | 1352 | } |
| 1353 | 1353 | |
| 1354 | - if ($prev !== false |
|
| 1355 | - && $tokens[$prev]['code'] === \T_NS_SEPARATOR |
|
| 1356 | - && $tokens[($prev - 1)]['code'] === \T_STRING |
|
| 1354 | + if ( $prev !== false |
|
| 1355 | + && $tokens[ $prev ][ 'code' ] === \T_NS_SEPARATOR |
|
| 1356 | + && $tokens[ ( $prev - 1 ) ][ 'code' ] === \T_STRING |
|
| 1357 | 1357 | ) { |
| 1358 | 1358 | // Namespaced constant of the same name. |
| 1359 | 1359 | return false; |
| 1360 | 1360 | } |
| 1361 | 1361 | |
| 1362 | - if ($prev !== false |
|
| 1363 | - && $tokens[$prev]['code'] === \T_CONST |
|
| 1364 | - && $this->isClassConstant($phpcsFile, $prev) === true |
|
| 1362 | + if ( $prev !== false |
|
| 1363 | + && $tokens[ $prev ][ 'code' ] === \T_CONST |
|
| 1364 | + && $this->isClassConstant( $phpcsFile, $prev ) === true |
|
| 1365 | 1365 | ) { |
| 1366 | 1366 | // Class constant declaration of the same name. |
| 1367 | 1367 | return false; |
@@ -1370,21 +1370,21 @@ discard block |
||
| 1370 | 1370 | /* |
| 1371 | 1371 | * Deal with a number of variations of use statements. |
| 1372 | 1372 | */ |
| 1373 | - for ($i = $stackPtr; $i > 0; $i--) { |
|
| 1374 | - if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) { |
|
| 1373 | + for ( $i = $stackPtr; $i > 0; $i-- ) { |
|
| 1374 | + if ( $tokens[ $i ][ 'line' ] !== $tokens[ $stackPtr ][ 'line' ] ) { |
|
| 1375 | 1375 | break; |
| 1376 | 1376 | } |
| 1377 | 1377 | } |
| 1378 | 1378 | |
| 1379 | - $firstOnLine = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true); |
|
| 1380 | - if ($firstOnLine !== false && $tokens[$firstOnLine]['code'] === \T_USE) { |
|
| 1381 | - $nextOnLine = $phpcsFile->findNext(Tokens::$emptyTokens, ($firstOnLine + 1), null, true); |
|
| 1382 | - if ($nextOnLine !== false) { |
|
| 1383 | - if (($tokens[$nextOnLine]['code'] === \T_STRING && $tokens[$nextOnLine]['content'] === 'const') |
|
| 1384 | - || $tokens[$nextOnLine]['code'] === \T_CONST // Happens in some PHPCS versions. |
|
| 1379 | + $firstOnLine = $phpcsFile->findNext( Tokens::$emptyTokens, ( $i + 1 ), null, true ); |
|
| 1380 | + if ( $firstOnLine !== false && $tokens[ $firstOnLine ][ 'code' ] === \T_USE ) { |
|
| 1381 | + $nextOnLine = $phpcsFile->findNext( Tokens::$emptyTokens, ( $firstOnLine + 1 ), null, true ); |
|
| 1382 | + if ( $nextOnLine !== false ) { |
|
| 1383 | + if ( ( $tokens[ $nextOnLine ][ 'code' ] === \T_STRING && $tokens[ $nextOnLine ][ 'content' ] === 'const' ) |
|
| 1384 | + || $tokens[ $nextOnLine ][ 'code' ] === \T_CONST // Happens in some PHPCS versions. |
|
| 1385 | 1385 | ) { |
| 1386 | - $hasNsSep = $phpcsFile->findNext(\T_NS_SEPARATOR, ($nextOnLine + 1), $stackPtr); |
|
| 1387 | - if ($hasNsSep !== false) { |
|
| 1386 | + $hasNsSep = $phpcsFile->findNext( \T_NS_SEPARATOR, ( $nextOnLine + 1 ), $stackPtr ); |
|
| 1387 | + if ( $hasNsSep !== false ) { |
|
| 1388 | 1388 | // Namespaced const (group) use statement. |
| 1389 | 1389 | return false; |
| 1390 | 1390 | } |
@@ -1419,15 +1419,15 @@ discard block |
||
| 1419 | 1419 | * False if not or if it could not be reliably determined |
| 1420 | 1420 | * (variable or calculations and such). |
| 1421 | 1421 | */ |
| 1422 | - public function isPositiveNumber(File $phpcsFile, $start, $end, $allowFloats = false) |
|
| 1422 | + public function isPositiveNumber( File $phpcsFile, $start, $end, $allowFloats = false ) |
|
| 1423 | 1423 | { |
| 1424 | - $number = $this->isNumber($phpcsFile, $start, $end, $allowFloats); |
|
| 1424 | + $number = $this->isNumber( $phpcsFile, $start, $end, $allowFloats ); |
|
| 1425 | 1425 | |
| 1426 | - if ($number === false) { |
|
| 1426 | + if ( $number === false ) { |
|
| 1427 | 1427 | return false; |
| 1428 | 1428 | } |
| 1429 | 1429 | |
| 1430 | - return ($number > 0); |
|
| 1430 | + return ( $number > 0 ); |
|
| 1431 | 1431 | } |
| 1432 | 1432 | |
| 1433 | 1433 | |
@@ -1451,15 +1451,15 @@ discard block |
||
| 1451 | 1451 | * False if not or if it could not be reliably determined |
| 1452 | 1452 | * (variable or calculations and such). |
| 1453 | 1453 | */ |
| 1454 | - public function isNegativeNumber(File $phpcsFile, $start, $end, $allowFloats = false) |
|
| 1454 | + public function isNegativeNumber( File $phpcsFile, $start, $end, $allowFloats = false ) |
|
| 1455 | 1455 | { |
| 1456 | - $number = $this->isNumber($phpcsFile, $start, $end, $allowFloats); |
|
| 1456 | + $number = $this->isNumber( $phpcsFile, $start, $end, $allowFloats ); |
|
| 1457 | 1457 | |
| 1458 | - if ($number === false) { |
|
| 1458 | + if ( $number === false ) { |
|
| 1459 | 1459 | return false; |
| 1460 | 1460 | } |
| 1461 | 1461 | |
| 1462 | - return ($number < 0); |
|
| 1462 | + return ( $number < 0 ); |
|
| 1463 | 1463 | } |
| 1464 | 1464 | |
| 1465 | 1465 | /** |
@@ -1488,89 +1488,89 @@ discard block |
||
| 1488 | 1488 | * number or if it could not be reliably determined |
| 1489 | 1489 | * (variable or calculations and such). |
| 1490 | 1490 | */ |
| 1491 | - protected function isNumber(File $phpcsFile, $start, $end, $allowFloats = false) |
|
| 1491 | + protected function isNumber( File $phpcsFile, $start, $end, $allowFloats = false ) |
|
| 1492 | 1492 | { |
| 1493 | 1493 | $stringTokens = Tokens::$heredocTokens + Tokens::$stringTokens; |
| 1494 | 1494 | |
| 1495 | 1495 | $validTokens = array(); |
| 1496 | - $validTokens[\T_LNUMBER] = true; |
|
| 1497 | - $validTokens[\T_TRUE] = true; // Evaluates to int 1. |
|
| 1498 | - $validTokens[\T_FALSE] = true; // Evaluates to int 0. |
|
| 1499 | - $validTokens[\T_NULL] = true; // Evaluates to int 0. |
|
| 1496 | + $validTokens[ \T_LNUMBER ] = true; |
|
| 1497 | + $validTokens[ \T_TRUE ] = true; // Evaluates to int 1. |
|
| 1498 | + $validTokens[ \T_FALSE ] = true; // Evaluates to int 0. |
|
| 1499 | + $validTokens[ \T_NULL ] = true; // Evaluates to int 0. |
|
| 1500 | 1500 | |
| 1501 | - if ($allowFloats === true) { |
|
| 1502 | - $validTokens[\T_DNUMBER] = true; |
|
| 1501 | + if ( $allowFloats === true ) { |
|
| 1502 | + $validTokens[ \T_DNUMBER ] = true; |
|
| 1503 | 1503 | } |
| 1504 | 1504 | |
| 1505 | 1505 | $maybeValidTokens = $stringTokens + $validTokens; |
| 1506 | 1506 | |
| 1507 | 1507 | $tokens = $phpcsFile->getTokens(); |
| 1508 | - $searchEnd = ($end + 1); |
|
| 1508 | + $searchEnd = ( $end + 1 ); |
|
| 1509 | 1509 | $negativeNumber = false; |
| 1510 | 1510 | |
| 1511 | - if (isset($tokens[$start], $tokens[$searchEnd]) === false) { |
|
| 1511 | + if ( isset( $tokens[ $start ], $tokens[ $searchEnd ] ) === false ) { |
|
| 1512 | 1512 | return false; |
| 1513 | 1513 | } |
| 1514 | 1514 | |
| 1515 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $start, $searchEnd, true); |
|
| 1516 | - while ($nextNonEmpty !== false |
|
| 1517 | - && ($tokens[$nextNonEmpty]['code'] === \T_PLUS |
|
| 1518 | - || $tokens[$nextNonEmpty]['code'] === \T_MINUS) |
|
| 1515 | + $nextNonEmpty = $phpcsFile->findNext( Tokens::$emptyTokens, $start, $searchEnd, true ); |
|
| 1516 | + while ( $nextNonEmpty !== false |
|
| 1517 | + && ( $tokens[ $nextNonEmpty ][ 'code' ] === \T_PLUS |
|
| 1518 | + || $tokens[ $nextNonEmpty ][ 'code' ] === \T_MINUS ) |
|
| 1519 | 1519 | ) { |
| 1520 | 1520 | |
| 1521 | - if ($tokens[$nextNonEmpty]['code'] === \T_MINUS) { |
|
| 1522 | - $negativeNumber = ($negativeNumber === false) ? true : false; |
|
| 1521 | + if ( $tokens[ $nextNonEmpty ][ 'code' ] === \T_MINUS ) { |
|
| 1522 | + $negativeNumber = ( $negativeNumber === false ) ? true : false; |
|
| 1523 | 1523 | } |
| 1524 | 1524 | |
| 1525 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextNonEmpty + 1), $searchEnd, true); |
|
| 1525 | + $nextNonEmpty = $phpcsFile->findNext( Tokens::$emptyTokens, ( $nextNonEmpty + 1 ), $searchEnd, true ); |
|
| 1526 | 1526 | } |
| 1527 | 1527 | |
| 1528 | - if ($nextNonEmpty === false || isset($maybeValidTokens[$tokens[$nextNonEmpty]['code']]) === false) { |
|
| 1528 | + if ( $nextNonEmpty === false || isset( $maybeValidTokens[ $tokens[ $nextNonEmpty ][ 'code' ] ] ) === false ) { |
|
| 1529 | 1529 | return false; |
| 1530 | 1530 | } |
| 1531 | 1531 | |
| 1532 | 1532 | $content = false; |
| 1533 | - if ($tokens[$nextNonEmpty]['code'] === \T_LNUMBER |
|
| 1534 | - || $tokens[$nextNonEmpty]['code'] === \T_DNUMBER |
|
| 1533 | + if ( $tokens[ $nextNonEmpty ][ 'code' ] === \T_LNUMBER |
|
| 1534 | + || $tokens[ $nextNonEmpty ][ 'code' ] === \T_DNUMBER |
|
| 1535 | 1535 | ) { |
| 1536 | - $content = (float) $tokens[$nextNonEmpty]['content']; |
|
| 1537 | - } elseif ($tokens[$nextNonEmpty]['code'] === \T_TRUE) { |
|
| 1536 | + $content = (float)$tokens[ $nextNonEmpty ][ 'content' ]; |
|
| 1537 | + } elseif ( $tokens[ $nextNonEmpty ][ 'code' ] === \T_TRUE ) { |
|
| 1538 | 1538 | $content = 1.0; |
| 1539 | - } elseif ($tokens[$nextNonEmpty]['code'] === \T_FALSE |
|
| 1540 | - || $tokens[$nextNonEmpty]['code'] === \T_NULL |
|
| 1539 | + } elseif ( $tokens[ $nextNonEmpty ][ 'code' ] === \T_FALSE |
|
| 1540 | + || $tokens[ $nextNonEmpty ][ 'code' ] === \T_NULL |
|
| 1541 | 1541 | ) { |
| 1542 | 1542 | $content = 0.0; |
| 1543 | - } elseif (isset($stringTokens[$tokens[$nextNonEmpty]['code']]) === true) { |
|
| 1543 | + } elseif ( isset( $stringTokens[ $tokens[ $nextNonEmpty ][ 'code' ] ] ) === true ) { |
|
| 1544 | 1544 | |
| 1545 | - if ($tokens[$nextNonEmpty]['code'] === \T_START_HEREDOC |
|
| 1546 | - || $tokens[$nextNonEmpty]['code'] === \T_START_NOWDOC |
|
| 1545 | + if ( $tokens[ $nextNonEmpty ][ 'code' ] === \T_START_HEREDOC |
|
| 1546 | + || $tokens[ $nextNonEmpty ][ 'code' ] === \T_START_NOWDOC |
|
| 1547 | 1547 | ) { |
| 1548 | 1548 | // Skip past heredoc/nowdoc opener to the first content. |
| 1549 | - $firstDocToken = $phpcsFile->findNext(array(\T_HEREDOC, \T_NOWDOC), ($nextNonEmpty + 1), $searchEnd); |
|
| 1550 | - if ($firstDocToken === false) { |
|
| 1549 | + $firstDocToken = $phpcsFile->findNext( array( \T_HEREDOC, \T_NOWDOC ), ( $nextNonEmpty + 1 ), $searchEnd ); |
|
| 1550 | + if ( $firstDocToken === false ) { |
|
| 1551 | 1551 | // Live coding or parse error. |
| 1552 | 1552 | return false; |
| 1553 | 1553 | } |
| 1554 | 1554 | |
| 1555 | - $stringContent = $content = $tokens[$firstDocToken]['content']; |
|
| 1555 | + $stringContent = $content = $tokens[ $firstDocToken ][ 'content' ]; |
|
| 1556 | 1556 | |
| 1557 | 1557 | // Skip forward to the end in preparation for the next part of the examination. |
| 1558 | - $nextNonEmpty = $phpcsFile->findNext(array(\T_END_HEREDOC, \T_END_NOWDOC), ($nextNonEmpty + 1), $searchEnd); |
|
| 1559 | - if ($nextNonEmpty === false) { |
|
| 1558 | + $nextNonEmpty = $phpcsFile->findNext( array( \T_END_HEREDOC, \T_END_NOWDOC ), ( $nextNonEmpty + 1 ), $searchEnd ); |
|
| 1559 | + if ( $nextNonEmpty === false ) { |
|
| 1560 | 1560 | // Live coding or parse error. |
| 1561 | 1561 | return false; |
| 1562 | 1562 | } |
| 1563 | 1563 | } else { |
| 1564 | 1564 | // Gather subsequent lines for a multi-line string. |
| 1565 | - for ($i = $nextNonEmpty; $i < $searchEnd; $i++) { |
|
| 1566 | - if ($tokens[$i]['code'] !== $tokens[$nextNonEmpty]['code']) { |
|
| 1565 | + for ( $i = $nextNonEmpty; $i < $searchEnd; $i++ ) { |
|
| 1566 | + if ( $tokens[ $i ][ 'code' ] !== $tokens[ $nextNonEmpty ][ 'code' ] ) { |
|
| 1567 | 1567 | break; |
| 1568 | 1568 | } |
| 1569 | - $content .= $tokens[$i]['content']; |
|
| 1569 | + $content .= $tokens[ $i ][ 'content' ]; |
|
| 1570 | 1570 | } |
| 1571 | 1571 | |
| 1572 | 1572 | $nextNonEmpty = --$i; |
| 1573 | - $content = $this->stripQuotes($content); |
|
| 1573 | + $content = $this->stripQuotes( $content ); |
|
| 1574 | 1574 | $stringContent = $content; |
| 1575 | 1575 | } |
| 1576 | 1576 | |
@@ -1581,40 +1581,40 @@ discard block |
||
| 1581 | 1581 | $regexInt = '`^\s*[0-9]+`'; |
| 1582 | 1582 | $regexFloat = '`^\s*(?:[+-]?(?:(?:(?P<LNUM>[0-9]+)|(?P<DNUM>([0-9]*\.(?P>LNUM)|(?P>LNUM)\.[0-9]*)))[eE][+-]?(?P>LNUM))|(?P>DNUM))`'; |
| 1583 | 1583 | |
| 1584 | - $intString = preg_match($regexInt, $content, $intMatch); |
|
| 1585 | - $floatString = preg_match($regexFloat, $content, $floatMatch); |
|
| 1584 | + $intString = preg_match( $regexInt, $content, $intMatch ); |
|
| 1585 | + $floatString = preg_match( $regexFloat, $content, $floatMatch ); |
|
| 1586 | 1586 | |
| 1587 | 1587 | // Does the text string start with a number ? If so, PHP would juggle it and use it as a number. |
| 1588 | - if ($allowFloats === false) { |
|
| 1589 | - if ($intString !== 1 || $floatString === 1) { |
|
| 1590 | - if ($floatString === 1) { |
|
| 1588 | + if ( $allowFloats === false ) { |
|
| 1589 | + if ( $intString !== 1 || $floatString === 1 ) { |
|
| 1590 | + if ( $floatString === 1 ) { |
|
| 1591 | 1591 | // Found float. Only integers targetted. |
| 1592 | 1592 | return false; |
| 1593 | 1593 | } |
| 1594 | 1594 | |
| 1595 | 1595 | $content = 0.0; |
| 1596 | 1596 | } else { |
| 1597 | - $content = (float) trim($intMatch[0]); |
|
| 1597 | + $content = (float)trim( $intMatch[ 0 ] ); |
|
| 1598 | 1598 | } |
| 1599 | 1599 | } else { |
| 1600 | - if ($intString !== 1 && $floatString !== 1) { |
|
| 1600 | + if ( $intString !== 1 && $floatString !== 1 ) { |
|
| 1601 | 1601 | $content = 0.0; |
| 1602 | 1602 | } else { |
| 1603 | - $content = ($floatString === 1) ? (float) trim($floatMatch[0]) : (float) trim($intMatch[0]); |
|
| 1603 | + $content = ( $floatString === 1 ) ? (float)trim( $floatMatch[ 0 ] ) : (float)trim( $intMatch[ 0 ] ); |
|
| 1604 | 1604 | } |
| 1605 | 1605 | } |
| 1606 | 1606 | |
| 1607 | 1607 | // Allow for different behaviour for hex numeric strings between PHP 5 vs PHP 7. |
| 1608 | - if ($intString === 1 && trim($intMatch[0]) === '0' |
|
| 1609 | - && preg_match('`^\s*(0x[A-Fa-f0-9]+)`', $stringContent, $hexNumberString) === 1 |
|
| 1610 | - && $this->supportsBelow('5.6') === true |
|
| 1608 | + if ( $intString === 1 && trim( $intMatch[ 0 ] ) === '0' |
|
| 1609 | + && preg_match( '`^\s*(0x[A-Fa-f0-9]+)`', $stringContent, $hexNumberString ) === 1 |
|
| 1610 | + && $this->supportsBelow( '5.6' ) === true |
|
| 1611 | 1611 | ) { |
| 1612 | 1612 | // The filter extension still allows for hex numeric strings in PHP 7, so |
| 1613 | 1613 | // use that to get the numeric value if possible. |
| 1614 | 1614 | // If the filter extension is not available, the value will be zero, but so be it. |
| 1615 | - if (function_exists('filter_var')) { |
|
| 1616 | - $filtered = filter_var($hexNumberString[1], \FILTER_VALIDATE_INT, \FILTER_FLAG_ALLOW_HEX); |
|
| 1617 | - if ($filtered !== false) { |
|
| 1615 | + if ( function_exists( 'filter_var' ) ) { |
|
| 1616 | + $filtered = filter_var( $hexNumberString[ 1 ], \FILTER_VALIDATE_INT, \FILTER_FLAG_ALLOW_HEX ); |
|
| 1617 | + if ( $filtered !== false ) { |
|
| 1618 | 1618 | $content = $filtered; |
| 1619 | 1619 | } |
| 1620 | 1620 | } |
@@ -1622,17 +1622,17 @@ discard block |
||
| 1622 | 1622 | } |
| 1623 | 1623 | |
| 1624 | 1624 | // OK, so we have a number, now is there still more code after it ? |
| 1625 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextNonEmpty + 1), $searchEnd, true); |
|
| 1626 | - if ($nextNonEmpty !== false) { |
|
| 1625 | + $nextNonEmpty = $phpcsFile->findNext( Tokens::$emptyTokens, ( $nextNonEmpty + 1 ), $searchEnd, true ); |
|
| 1626 | + if ( $nextNonEmpty !== false ) { |
|
| 1627 | 1627 | return false; |
| 1628 | 1628 | } |
| 1629 | 1629 | |
| 1630 | - if ($negativeNumber === true) { |
|
| 1630 | + if ( $negativeNumber === true ) { |
|
| 1631 | 1631 | $content = -$content; |
| 1632 | 1632 | } |
| 1633 | 1633 | |
| 1634 | - if ($allowFloats === false) { |
|
| 1635 | - return (int) $content; |
|
| 1634 | + if ( $allowFloats === false ) { |
|
| 1635 | + return (int)$content; |
|
| 1636 | 1636 | } |
| 1637 | 1637 | |
| 1638 | 1638 | return $content; |
@@ -1656,66 +1656,66 @@ discard block |
||
| 1656 | 1656 | * |
| 1657 | 1657 | * @return bool |
| 1658 | 1658 | */ |
| 1659 | - protected function isNumericCalculation(File $phpcsFile, $start, $end) |
|
| 1659 | + protected function isNumericCalculation( File $phpcsFile, $start, $end ) |
|
| 1660 | 1660 | { |
| 1661 | 1661 | $arithmeticTokens = Tokens::$arithmeticTokens; |
| 1662 | 1662 | |
| 1663 | 1663 | // phpcs:disable PHPCompatibility.Constants.NewConstants.t_powFound |
| 1664 | - if (\defined('T_POW') && isset($arithmeticTokens[\T_POW]) === false) { |
|
| 1664 | + if ( \defined( 'T_POW' ) && isset( $arithmeticTokens[ \T_POW ] ) === false ) { |
|
| 1665 | 1665 | // T_POW was not added to the arithmetic array until PHPCS 2.9.0. |
| 1666 | - $arithmeticTokens[\T_POW] = \T_POW; |
|
| 1666 | + $arithmeticTokens[ \T_POW ] = \T_POW; |
|
| 1667 | 1667 | } |
| 1668 | 1668 | // phpcs:enable |
| 1669 | 1669 | |
| 1670 | 1670 | $skipTokens = Tokens::$emptyTokens; |
| 1671 | - $skipTokens[] = \T_MINUS; |
|
| 1672 | - $skipTokens[] = \T_PLUS; |
|
| 1671 | + $skipTokens[ ] = \T_MINUS; |
|
| 1672 | + $skipTokens[ ] = \T_PLUS; |
|
| 1673 | 1673 | |
| 1674 | 1674 | // Find the first arithmetic operator, but skip past +/- signs before numbers. |
| 1675 | - $nextNonEmpty = ($start - 1); |
|
| 1675 | + $nextNonEmpty = ( $start - 1 ); |
|
| 1676 | 1676 | do { |
| 1677 | - $nextNonEmpty = $phpcsFile->findNext($skipTokens, ($nextNonEmpty + 1), ($end + 1), true); |
|
| 1678 | - $arithmeticOperator = $phpcsFile->findNext($arithmeticTokens, ($nextNonEmpty + 1), ($end + 1)); |
|
| 1679 | - } while ($nextNonEmpty !== false && $arithmeticOperator !== false && $nextNonEmpty === $arithmeticOperator); |
|
| 1677 | + $nextNonEmpty = $phpcsFile->findNext( $skipTokens, ( $nextNonEmpty + 1 ), ( $end + 1 ), true ); |
|
| 1678 | + $arithmeticOperator = $phpcsFile->findNext( $arithmeticTokens, ( $nextNonEmpty + 1 ), ( $end + 1 ) ); |
|
| 1679 | + } while ( $nextNonEmpty !== false && $arithmeticOperator !== false && $nextNonEmpty === $arithmeticOperator ); |
|
| 1680 | 1680 | |
| 1681 | - if ($arithmeticOperator === false) { |
|
| 1681 | + if ( $arithmeticOperator === false ) { |
|
| 1682 | 1682 | return false; |
| 1683 | 1683 | } |
| 1684 | 1684 | |
| 1685 | 1685 | $tokens = $phpcsFile->getTokens(); |
| 1686 | 1686 | $subsetStart = $start; |
| 1687 | - $subsetEnd = ($arithmeticOperator - 1); |
|
| 1687 | + $subsetEnd = ( $arithmeticOperator - 1 ); |
|
| 1688 | 1688 | |
| 1689 | - while ($this->isNumber($phpcsFile, $subsetStart, $subsetEnd, true) !== false |
|
| 1690 | - && isset($tokens[($arithmeticOperator + 1)]) === true |
|
| 1689 | + while ( $this->isNumber( $phpcsFile, $subsetStart, $subsetEnd, true ) !== false |
|
| 1690 | + && isset( $tokens[ ( $arithmeticOperator + 1 ) ] ) === true |
|
| 1691 | 1691 | ) { |
| 1692 | 1692 | // Recognize T_POW for PHPCS < 2.4.0 on low PHP versions. |
| 1693 | - if (\defined('T_POW') === false |
|
| 1694 | - && $tokens[$arithmeticOperator]['code'] === \T_MULTIPLY |
|
| 1695 | - && $tokens[($arithmeticOperator + 1)]['code'] === \T_MULTIPLY |
|
| 1696 | - && isset($tokens[$arithmeticOperator + 2]) === true |
|
| 1693 | + if ( \defined( 'T_POW' ) === false |
|
| 1694 | + && $tokens[ $arithmeticOperator ][ 'code' ] === \T_MULTIPLY |
|
| 1695 | + && $tokens[ ( $arithmeticOperator + 1 ) ][ 'code' ] === \T_MULTIPLY |
|
| 1696 | + && isset( $tokens[ $arithmeticOperator + 2 ] ) === true |
|
| 1697 | 1697 | ) { |
| 1698 | 1698 | // Move operator one forward to the second * in T_POW. |
| 1699 | 1699 | ++$arithmeticOperator; |
| 1700 | 1700 | } |
| 1701 | 1701 | |
| 1702 | - $subsetStart = ($arithmeticOperator + 1); |
|
| 1702 | + $subsetStart = ( $arithmeticOperator + 1 ); |
|
| 1703 | 1703 | $nextNonEmpty = $arithmeticOperator; |
| 1704 | 1704 | do { |
| 1705 | - $nextNonEmpty = $phpcsFile->findNext($skipTokens, ($nextNonEmpty + 1), ($end + 1), true); |
|
| 1706 | - $arithmeticOperator = $phpcsFile->findNext($arithmeticTokens, ($nextNonEmpty + 1), ($end + 1)); |
|
| 1707 | - } while ($nextNonEmpty !== false && $arithmeticOperator !== false && $nextNonEmpty === $arithmeticOperator); |
|
| 1705 | + $nextNonEmpty = $phpcsFile->findNext( $skipTokens, ( $nextNonEmpty + 1 ), ( $end + 1 ), true ); |
|
| 1706 | + $arithmeticOperator = $phpcsFile->findNext( $arithmeticTokens, ( $nextNonEmpty + 1 ), ( $end + 1 ) ); |
|
| 1707 | + } while ( $nextNonEmpty !== false && $arithmeticOperator !== false && $nextNonEmpty === $arithmeticOperator ); |
|
| 1708 | 1708 | |
| 1709 | - if ($arithmeticOperator === false) { |
|
| 1709 | + if ( $arithmeticOperator === false ) { |
|
| 1710 | 1710 | // Last calculation operator already reached. |
| 1711 | - if ($this->isNumber($phpcsFile, $subsetStart, $end, true) !== false) { |
|
| 1711 | + if ( $this->isNumber( $phpcsFile, $subsetStart, $end, true ) !== false ) { |
|
| 1712 | 1712 | return true; |
| 1713 | 1713 | } |
| 1714 | 1714 | |
| 1715 | 1715 | return false; |
| 1716 | 1716 | } |
| 1717 | 1717 | |
| 1718 | - $subsetEnd = ($arithmeticOperator - 1); |
|
| 1718 | + $subsetEnd = ( $arithmeticOperator - 1 ); |
|
| 1719 | 1719 | } |
| 1720 | 1720 | |
| 1721 | 1721 | return false; |
@@ -1741,22 +1741,22 @@ discard block |
||
| 1741 | 1741 | * |
| 1742 | 1742 | * @return bool True if short ternary, or false otherwise. |
| 1743 | 1743 | */ |
| 1744 | - public function isShortTernary(File $phpcsFile, $stackPtr) |
|
| 1744 | + public function isShortTernary( File $phpcsFile, $stackPtr ) |
|
| 1745 | 1745 | { |
| 1746 | 1746 | $tokens = $phpcsFile->getTokens(); |
| 1747 | - if (isset($tokens[$stackPtr]) === false |
|
| 1748 | - || $tokens[$stackPtr]['code'] !== \T_INLINE_THEN |
|
| 1747 | + if ( isset( $tokens[ $stackPtr ] ) === false |
|
| 1748 | + || $tokens[ $stackPtr ][ 'code' ] !== \T_INLINE_THEN |
|
| 1749 | 1749 | ) { |
| 1750 | 1750 | return false; |
| 1751 | 1751 | } |
| 1752 | 1752 | |
| 1753 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
| 1754 | - if ($nextNonEmpty === false) { |
|
| 1753 | + $nextNonEmpty = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); |
|
| 1754 | + if ( $nextNonEmpty === false ) { |
|
| 1755 | 1755 | // Live coding or parse error. |
| 1756 | 1756 | return false; |
| 1757 | 1757 | } |
| 1758 | 1758 | |
| 1759 | - if ($tokens[$nextNonEmpty]['code'] === \T_INLINE_ELSE) { |
|
| 1759 | + if ( $tokens[ $nextNonEmpty ][ 'code' ] === \T_INLINE_ELSE ) { |
|
| 1760 | 1760 | return true; |
| 1761 | 1761 | } |
| 1762 | 1762 | |
@@ -1775,39 +1775,39 @@ discard block |
||
| 1775 | 1775 | * |
| 1776 | 1776 | * @return bool |
| 1777 | 1777 | */ |
| 1778 | - public function isShortList(File $phpcsFile, $stackPtr) |
|
| 1778 | + public function isShortList( File $phpcsFile, $stackPtr ) |
|
| 1779 | 1779 | { |
| 1780 | 1780 | $tokens = $phpcsFile->getTokens(); |
| 1781 | 1781 | |
| 1782 | 1782 | // Check for the existence of the token. |
| 1783 | - if (isset($tokens[$stackPtr]) === false) { |
|
| 1783 | + if ( isset( $tokens[ $stackPtr ] ) === false ) { |
|
| 1784 | 1784 | return false; |
| 1785 | 1785 | } |
| 1786 | 1786 | |
| 1787 | 1787 | // Is this one of the tokens this function handles ? |
| 1788 | - if ($tokens[$stackPtr]['code'] !== \T_OPEN_SHORT_ARRAY |
|
| 1789 | - && $tokens[$stackPtr]['code'] !== \T_CLOSE_SHORT_ARRAY |
|
| 1788 | + if ( $tokens[ $stackPtr ][ 'code' ] !== \T_OPEN_SHORT_ARRAY |
|
| 1789 | + && $tokens[ $stackPtr ][ 'code' ] !== \T_CLOSE_SHORT_ARRAY |
|
| 1790 | 1790 | ) { |
| 1791 | 1791 | return false; |
| 1792 | 1792 | } |
| 1793 | 1793 | |
| 1794 | - switch ($tokens[$stackPtr]['code']) { |
|
| 1794 | + switch ( $tokens[ $stackPtr ][ 'code' ] ) { |
|
| 1795 | 1795 | case \T_OPEN_SHORT_ARRAY: |
| 1796 | - if (isset($tokens[$stackPtr]['bracket_closer']) === true) { |
|
| 1796 | + if ( isset( $tokens[ $stackPtr ][ 'bracket_closer' ] ) === true ) { |
|
| 1797 | 1797 | $opener = $stackPtr; |
| 1798 | - $closer = $tokens[$stackPtr]['bracket_closer']; |
|
| 1798 | + $closer = $tokens[ $stackPtr ][ 'bracket_closer' ]; |
|
| 1799 | 1799 | } |
| 1800 | 1800 | break; |
| 1801 | 1801 | |
| 1802 | 1802 | case \T_CLOSE_SHORT_ARRAY: |
| 1803 | - if (isset($tokens[$stackPtr]['bracket_opener']) === true) { |
|
| 1804 | - $opener = $tokens[$stackPtr]['bracket_opener']; |
|
| 1803 | + if ( isset( $tokens[ $stackPtr ][ 'bracket_opener' ] ) === true ) { |
|
| 1804 | + $opener = $tokens[ $stackPtr ][ 'bracket_opener' ]; |
|
| 1805 | 1805 | $closer = $stackPtr; |
| 1806 | 1806 | } |
| 1807 | 1807 | break; |
| 1808 | 1808 | } |
| 1809 | 1809 | |
| 1810 | - if (isset($opener, $closer) === false) { |
|
| 1810 | + if ( isset( $opener, $closer ) === false ) { |
|
| 1811 | 1811 | // Parse error, live coding or real square bracket. |
| 1812 | 1812 | return false; |
| 1813 | 1813 | } |
@@ -1816,42 +1816,42 @@ discard block |
||
| 1816 | 1816 | * PHPCS cross-version compatibility: work around for square brackets misidentified |
| 1817 | 1817 | * as short array when preceded by a variable variable in older PHPCS versions. |
| 1818 | 1818 | */ |
| 1819 | - $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($opener - 1), null, true, null, true); |
|
| 1819 | + $prevNonEmpty = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $opener - 1 ), null, true, null, true ); |
|
| 1820 | 1820 | |
| 1821 | - if ($prevNonEmpty !== false |
|
| 1822 | - && $tokens[$prevNonEmpty]['code'] === \T_CLOSE_CURLY_BRACKET |
|
| 1823 | - && isset($tokens[$prevNonEmpty]['bracket_opener']) === true |
|
| 1821 | + if ( $prevNonEmpty !== false |
|
| 1822 | + && $tokens[ $prevNonEmpty ][ 'code' ] === \T_CLOSE_CURLY_BRACKET |
|
| 1823 | + && isset( $tokens[ $prevNonEmpty ][ 'bracket_opener' ] ) === true |
|
| 1824 | 1824 | ) { |
| 1825 | 1825 | $maybeVariableVariable = $phpcsFile->findPrevious( |
| 1826 | 1826 | Tokens::$emptyTokens, |
| 1827 | - ($tokens[$prevNonEmpty]['bracket_opener'] - 1), |
|
| 1827 | + ( $tokens[ $prevNonEmpty ][ 'bracket_opener' ] - 1 ), |
|
| 1828 | 1828 | null, |
| 1829 | 1829 | true, |
| 1830 | 1830 | null, |
| 1831 | 1831 | true |
| 1832 | 1832 | ); |
| 1833 | 1833 | |
| 1834 | - if ($tokens[$maybeVariableVariable]['code'] === \T_VARIABLE |
|
| 1835 | - || $tokens[$maybeVariableVariable]['code'] === \T_DOLLAR |
|
| 1834 | + if ( $tokens[ $maybeVariableVariable ][ 'code' ] === \T_VARIABLE |
|
| 1835 | + || $tokens[ $maybeVariableVariable ][ 'code' ] === \T_DOLLAR |
|
| 1836 | 1836 | ) { |
| 1837 | 1837 | return false; |
| 1838 | 1838 | } |
| 1839 | 1839 | } |
| 1840 | 1840 | |
| 1841 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($closer + 1), null, true, null, true); |
|
| 1841 | + $nextNonEmpty = $phpcsFile->findNext( Tokens::$emptyTokens, ( $closer + 1 ), null, true, null, true ); |
|
| 1842 | 1842 | |
| 1843 | - if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === \T_EQUAL) { |
|
| 1843 | + if ( $nextNonEmpty !== false && $tokens[ $nextNonEmpty ][ 'code' ] === \T_EQUAL ) { |
|
| 1844 | 1844 | return true; |
| 1845 | 1845 | } |
| 1846 | 1846 | |
| 1847 | - if ($prevNonEmpty !== false |
|
| 1848 | - && $tokens[$prevNonEmpty]['code'] === \T_AS |
|
| 1849 | - && isset($tokens[$prevNonEmpty]['nested_parenthesis']) === true |
|
| 1847 | + if ( $prevNonEmpty !== false |
|
| 1848 | + && $tokens[ $prevNonEmpty ][ 'code' ] === \T_AS |
|
| 1849 | + && isset( $tokens[ $prevNonEmpty ][ 'nested_parenthesis' ] ) === true |
|
| 1850 | 1850 | ) { |
| 1851 | - $parentheses = array_reverse($tokens[$prevNonEmpty]['nested_parenthesis'], true); |
|
| 1852 | - foreach ($parentheses as $open => $close) { |
|
| 1853 | - if (isset($tokens[$open]['parenthesis_owner']) |
|
| 1854 | - && $tokens[$tokens[$open]['parenthesis_owner']]['code'] === \T_FOREACH |
|
| 1851 | + $parentheses = array_reverse( $tokens[ $prevNonEmpty ][ 'nested_parenthesis' ], true ); |
|
| 1852 | + foreach ( $parentheses as $open => $close ) { |
|
| 1853 | + if ( isset( $tokens[ $open ][ 'parenthesis_owner' ] ) |
|
| 1854 | + && $tokens[ $tokens[ $open ][ 'parenthesis_owner' ] ][ 'code' ] === \T_FOREACH |
|
| 1855 | 1855 | ) { |
| 1856 | 1856 | return true; |
| 1857 | 1857 | } |
@@ -1862,48 +1862,48 @@ discard block |
||
| 1862 | 1862 | $parentOpener = $opener; |
| 1863 | 1863 | do { |
| 1864 | 1864 | $parentOpener = $phpcsFile->findPrevious( |
| 1865 | - array(\T_OPEN_SHORT_ARRAY, \T_OPEN_SQUARE_BRACKET), |
|
| 1866 | - ($parentOpener - 1), |
|
| 1865 | + array( \T_OPEN_SHORT_ARRAY, \T_OPEN_SQUARE_BRACKET ), |
|
| 1866 | + ( $parentOpener - 1 ), |
|
| 1867 | 1867 | null, |
| 1868 | 1868 | false, |
| 1869 | 1869 | null, |
| 1870 | 1870 | true |
| 1871 | 1871 | ); |
| 1872 | 1872 | |
| 1873 | - if ($parentOpener === false) { |
|
| 1873 | + if ( $parentOpener === false ) { |
|
| 1874 | 1874 | return false; |
| 1875 | 1875 | } |
| 1876 | 1876 | |
| 1877 | - } while (isset($tokens[$parentOpener]['bracket_closer']) === true |
|
| 1878 | - && $tokens[$parentOpener]['bracket_closer'] < $opener |
|
| 1877 | + } while ( isset( $tokens[ $parentOpener ][ 'bracket_closer' ] ) === true |
|
| 1878 | + && $tokens[ $parentOpener ][ 'bracket_closer' ] < $opener |
|
| 1879 | 1879 | ); |
| 1880 | 1880 | |
| 1881 | - if (isset($tokens[$parentOpener]['bracket_closer']) === true |
|
| 1882 | - && $tokens[$parentOpener]['bracket_closer'] > $closer |
|
| 1881 | + if ( isset( $tokens[ $parentOpener ][ 'bracket_closer' ] ) === true |
|
| 1882 | + && $tokens[ $parentOpener ][ 'bracket_closer' ] > $closer |
|
| 1883 | 1883 | ) { |
| 1884 | 1884 | // Work around tokenizer issue in PHPCS 2.0 - 2.7. |
| 1885 | 1885 | $phpcsVersion = PHPCSHelper::getVersion(); |
| 1886 | - if ((version_compare($phpcsVersion, '2.0', '>') === true |
|
| 1887 | - && version_compare($phpcsVersion, '2.8', '<') === true) |
|
| 1888 | - && $tokens[$parentOpener]['code'] === \T_OPEN_SQUARE_BRACKET |
|
| 1886 | + if ( ( version_compare( $phpcsVersion, '2.0', '>' ) === true |
|
| 1887 | + && version_compare( $phpcsVersion, '2.8', '<' ) === true ) |
|
| 1888 | + && $tokens[ $parentOpener ][ 'code' ] === \T_OPEN_SQUARE_BRACKET |
|
| 1889 | 1889 | ) { |
| 1890 | 1890 | $nextNonEmpty = $phpcsFile->findNext( |
| 1891 | 1891 | Tokens::$emptyTokens, |
| 1892 | - ($tokens[$parentOpener]['bracket_closer'] + 1), |
|
| 1892 | + ( $tokens[ $parentOpener ][ 'bracket_closer' ] + 1 ), |
|
| 1893 | 1893 | null, |
| 1894 | 1894 | true, |
| 1895 | 1895 | null, |
| 1896 | 1896 | true |
| 1897 | 1897 | ); |
| 1898 | 1898 | |
| 1899 | - if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === \T_EQUAL) { |
|
| 1899 | + if ( $nextNonEmpty !== false && $tokens[ $nextNonEmpty ][ 'code' ] === \T_EQUAL ) { |
|
| 1900 | 1900 | return true; |
| 1901 | 1901 | } |
| 1902 | 1902 | |
| 1903 | 1903 | return false; |
| 1904 | 1904 | } |
| 1905 | 1905 | |
| 1906 | - return $this->isShortList($phpcsFile, $parentOpener); |
|
| 1906 | + return $this->isShortList( $phpcsFile, $parentOpener ); |
|
| 1907 | 1907 | } |
| 1908 | 1908 | |
| 1909 | 1909 | return false; |
@@ -1922,14 +1922,14 @@ discard block |
||
| 1922 | 1922 | * |
| 1923 | 1923 | * @return bool |
| 1924 | 1924 | */ |
| 1925 | - public function isVariable(File $phpcsFile, $start, $end, $targetNestingLevel) |
|
| 1925 | + public function isVariable( File $phpcsFile, $start, $end, $targetNestingLevel ) |
|
| 1926 | 1926 | { |
| 1927 | 1927 | static $tokenBlackList, $bracketTokens; |
| 1928 | 1928 | |
| 1929 | 1929 | // Create the token arrays only once. |
| 1930 | - if (isset($tokenBlackList, $bracketTokens) === false) { |
|
| 1930 | + if ( isset( $tokenBlackList, $bracketTokens ) === false ) { |
|
| 1931 | 1931 | |
| 1932 | - $tokenBlackList = array( |
|
| 1932 | + $tokenBlackList = array( |
|
| 1933 | 1933 | \T_OPEN_PARENTHESIS => \T_OPEN_PARENTHESIS, |
| 1934 | 1934 | \T_STRING_CONCAT => \T_STRING_CONCAT, |
| 1935 | 1935 | ); |
@@ -1954,43 +1954,43 @@ discard block |
||
| 1954 | 1954 | $tokens = $phpcsFile->getTokens(); |
| 1955 | 1955 | |
| 1956 | 1956 | // If no variable at all was found, then it's definitely a no-no. |
| 1957 | - $hasVariable = $phpcsFile->findNext(\T_VARIABLE, $start, $end); |
|
| 1958 | - if ($hasVariable === false) { |
|
| 1957 | + $hasVariable = $phpcsFile->findNext( \T_VARIABLE, $start, $end ); |
|
| 1958 | + if ( $hasVariable === false ) { |
|
| 1959 | 1959 | return false; |
| 1960 | 1960 | } |
| 1961 | 1961 | |
| 1962 | 1962 | // Check if the variable found is at the right level. Deeper levels are always an error. |
| 1963 | - if (isset($tokens[$hasVariable]['nested_parenthesis']) |
|
| 1964 | - && \count($tokens[$hasVariable]['nested_parenthesis']) !== $targetNestingLevel |
|
| 1963 | + if ( isset( $tokens[ $hasVariable ][ 'nested_parenthesis' ] ) |
|
| 1964 | + && \count( $tokens[ $hasVariable ][ 'nested_parenthesis' ] ) !== $targetNestingLevel |
|
| 1965 | 1965 | ) { |
| 1966 | 1966 | return false; |
| 1967 | 1967 | } |
| 1968 | 1968 | |
| 1969 | 1969 | // Ok, so the first variable is at the right level, now are there any |
| 1970 | 1970 | // blacklisted tokens within the empty() ? |
| 1971 | - $hasBadToken = $phpcsFile->findNext($tokenBlackList, $start, $end); |
|
| 1972 | - if ($hasBadToken === false) { |
|
| 1971 | + $hasBadToken = $phpcsFile->findNext( $tokenBlackList, $start, $end ); |
|
| 1972 | + if ( $hasBadToken === false ) { |
|
| 1973 | 1973 | return true; |
| 1974 | 1974 | } |
| 1975 | 1975 | |
| 1976 | 1976 | // If there are also bracket tokens, the blacklisted token might be part of a variable |
| 1977 | 1977 | // variable, but if there are no bracket tokens, we know we have an error. |
| 1978 | - $hasBrackets = $phpcsFile->findNext($bracketTokens, $start, $end); |
|
| 1979 | - if ($hasBrackets === false) { |
|
| 1978 | + $hasBrackets = $phpcsFile->findNext( $bracketTokens, $start, $end ); |
|
| 1979 | + if ( $hasBrackets === false ) { |
|
| 1980 | 1980 | return false; |
| 1981 | 1981 | } |
| 1982 | 1982 | |
| 1983 | 1983 | // Ok, we have both a blacklisted token as well as brackets, so we need to walk |
| 1984 | 1984 | // the tokens of the variable variable. |
| 1985 | - for ($i = $start; $i < $end; $i++) { |
|
| 1985 | + for ( $i = $start; $i < $end; $i++ ) { |
|
| 1986 | 1986 | // If this is a bracket token, skip to the end of the bracketed expression. |
| 1987 | - if (isset($bracketTokens[$tokens[$i]['code']], $tokens[$i]['bracket_closer'])) { |
|
| 1988 | - $i = $tokens[$i]['bracket_closer']; |
|
| 1987 | + if ( isset( $bracketTokens[ $tokens[ $i ][ 'code' ] ], $tokens[ $i ][ 'bracket_closer' ] ) ) { |
|
| 1988 | + $i = $tokens[ $i ][ 'bracket_closer' ]; |
|
| 1989 | 1989 | continue; |
| 1990 | 1990 | } |
| 1991 | 1991 | |
| 1992 | 1992 | // If it's a blacklisted token, not within brackets, we have an error. |
| 1993 | - if (isset($tokenBlackList[$tokens[$i]['code']])) { |
|
| 1993 | + if ( isset( $tokenBlackList[ $tokens[ $i ][ 'code' ] ] ) ) { |
|
| 1994 | 1994 | return false; |
| 1995 | 1995 | } |
| 1996 | 1996 | } |
@@ -2016,56 +2016,56 @@ discard block |
||
| 2016 | 2016 | * @return bool True if the token passed is a unary operator. |
| 2017 | 2017 | * False otherwise or if the token is not a T_PLUS/T_MINUS token. |
| 2018 | 2018 | */ |
| 2019 | - public static function isUnaryPlusMinus(File $phpcsFile, $stackPtr) |
|
| 2019 | + public static function isUnaryPlusMinus( File $phpcsFile, $stackPtr ) |
|
| 2020 | 2020 | { |
| 2021 | 2021 | $tokens = $phpcsFile->getTokens(); |
| 2022 | 2022 | |
| 2023 | - if (isset($tokens[$stackPtr]) === false |
|
| 2024 | - || ($tokens[$stackPtr]['code'] !== \T_PLUS |
|
| 2025 | - && $tokens[$stackPtr]['code'] !== \T_MINUS) |
|
| 2023 | + if ( isset( $tokens[ $stackPtr ] ) === false |
|
| 2024 | + || ( $tokens[ $stackPtr ][ 'code' ] !== \T_PLUS |
|
| 2025 | + && $tokens[ $stackPtr ][ 'code' ] !== \T_MINUS ) |
|
| 2026 | 2026 | ) { |
| 2027 | 2027 | return false; |
| 2028 | 2028 | } |
| 2029 | 2029 | |
| 2030 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
| 2031 | - if ($next === false) { |
|
| 2030 | + $next = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); |
|
| 2031 | + if ( $next === false ) { |
|
| 2032 | 2032 | // Live coding or parse error. |
| 2033 | 2033 | return false; |
| 2034 | 2034 | } |
| 2035 | 2035 | |
| 2036 | - if (isset(Tokens::$operators[$tokens[$next]['code']]) === true) { |
|
| 2036 | + if ( isset( Tokens::$operators[ $tokens[ $next ][ 'code' ] ] ) === true ) { |
|
| 2037 | 2037 | // Next token is an operator, so this is not a unary. |
| 2038 | 2038 | return false; |
| 2039 | 2039 | } |
| 2040 | 2040 | |
| 2041 | - $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
| 2041 | + $prev = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true ); |
|
| 2042 | 2042 | |
| 2043 | - if ($tokens[$prev]['code'] === \T_RETURN) { |
|
| 2043 | + if ( $tokens[ $prev ][ 'code' ] === \T_RETURN ) { |
|
| 2044 | 2044 | // Just returning a positive/negative value; eg. (return -1). |
| 2045 | 2045 | return true; |
| 2046 | 2046 | } |
| 2047 | 2047 | |
| 2048 | - if (isset(Tokens::$operators[$tokens[$prev]['code']]) === true) { |
|
| 2048 | + if ( isset( Tokens::$operators[ $tokens[ $prev ][ 'code' ] ] ) === true ) { |
|
| 2049 | 2049 | // Just trying to operate on a positive/negative value; eg. ($var * -1). |
| 2050 | 2050 | return true; |
| 2051 | 2051 | } |
| 2052 | 2052 | |
| 2053 | - if (isset(Tokens::$comparisonTokens[$tokens[$prev]['code']]) === true) { |
|
| 2053 | + if ( isset( Tokens::$comparisonTokens[ $tokens[ $prev ][ 'code' ] ] ) === true ) { |
|
| 2054 | 2054 | // Just trying to compare a positive/negative value; eg. ($var === -1). |
| 2055 | 2055 | return true; |
| 2056 | 2056 | } |
| 2057 | 2057 | |
| 2058 | - if (isset(Tokens::$booleanOperators[$tokens[$prev]['code']]) === true) { |
|
| 2058 | + if ( isset( Tokens::$booleanOperators[ $tokens[ $prev ][ 'code' ] ] ) === true ) { |
|
| 2059 | 2059 | // Just trying to compare a positive/negative value; eg. ($var || -1 === $b). |
| 2060 | 2060 | return true; |
| 2061 | 2061 | } |
| 2062 | 2062 | |
| 2063 | - if (isset(Tokens::$assignmentTokens[$tokens[$prev]['code']]) === true) { |
|
| 2063 | + if ( isset( Tokens::$assignmentTokens[ $tokens[ $prev ][ 'code' ] ] ) === true ) { |
|
| 2064 | 2064 | // Just trying to assign a positive/negative value; eg. ($var = -1). |
| 2065 | 2065 | return true; |
| 2066 | 2066 | } |
| 2067 | 2067 | |
| 2068 | - if (isset(Tokens::$castTokens[$tokens[$prev]['code']]) === true) { |
|
| 2068 | + if ( isset( Tokens::$castTokens[ $tokens[ $prev ][ 'code' ] ] ) === true ) { |
|
| 2069 | 2069 | // Just casting a positive/negative value; eg. (string) -$var. |
| 2070 | 2070 | return true; |
| 2071 | 2071 | } |
@@ -2084,7 +2084,7 @@ discard block |
||
| 2084 | 2084 | \T_STRING_CONCAT => true, |
| 2085 | 2085 | ); |
| 2086 | 2086 | |
| 2087 | - if (isset($invalidTokens[$tokens[$prev]['code']]) === true) { |
|
| 2087 | + if ( isset( $invalidTokens[ $tokens[ $prev ][ 'code' ] ] ) === true ) { |
|
| 2088 | 2088 | // Just trying to use a positive/negative value; eg. myFunction($var, -2). |
| 2089 | 2089 | return true; |
| 2090 | 2090 | } |
@@ -24,8 +24,7 @@ discard block |
||
| 24 | 24 | * @author Wim Godden <[email protected]> |
| 25 | 25 | * @copyright 2014 Cu.be Solutions bvba |
| 26 | 26 | */ |
| 27 | -abstract class Sniff implements PHPCS_Sniff |
|
| 28 | -{ |
|
| 27 | +abstract class Sniff implements PHPCS_Sniff { |
|
| 29 | 28 | |
| 30 | 29 | const REGEX_COMPLEX_VARS = '`(?:(\{)?(?<!\\\\)\$)?(\{)?(?<!\\\\)\$(\{)?(?P<varname>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(?:->\$?(?P>varname)|\[[^\]]+\]|::\$?(?P>varname)|\([^\)]*\))*(?(3)\}|)(?(2)\}|)(?(1)\}|)`'; |
| 31 | 30 | |
@@ -105,8 +104,7 @@ discard block |
||
| 105 | 104 | * |
| 106 | 105 | * @throws \PHP_CodeSniffer_Exception If testVersion is invalid. |
| 107 | 106 | */ |
| 108 | - private function getTestVersion() |
|
| 109 | - { |
|
| 107 | + private function getTestVersion() { |
|
| 110 | 108 | static $arrTestVersions = array(); |
| 111 | 109 | |
| 112 | 110 | $default = array(null, null); |
@@ -173,8 +171,7 @@ discard block |
||
| 173 | 171 | * is equal to or higher than the highest supported PHP version |
| 174 | 172 | * in testVersion. False otherwise. |
| 175 | 173 | */ |
| 176 | - public function supportsAbove($phpVersion) |
|
| 177 | - { |
|
| 174 | + public function supportsAbove($phpVersion) { |
|
| 178 | 175 | $testVersion = $this->getTestVersion(); |
| 179 | 176 | $testVersion = $testVersion[1]; |
| 180 | 177 | |
@@ -200,8 +197,7 @@ discard block |
||
| 200 | 197 | * supported PHP version in testVersion. |
| 201 | 198 | * False otherwise or if no testVersion is provided. |
| 202 | 199 | */ |
| 203 | - public function supportsBelow($phpVersion) |
|
| 204 | - { |
|
| 200 | + public function supportsBelow($phpVersion) { |
|
| 205 | 201 | $testVersion = $this->getTestVersion(); |
| 206 | 202 | $testVersion = $testVersion[0]; |
| 207 | 203 | |
@@ -231,8 +227,7 @@ discard block |
||
| 231 | 227 | * |
| 232 | 228 | * @return void |
| 233 | 229 | */ |
| 234 | - public function addMessage(File $phpcsFile, $message, $stackPtr, $isError, $code = 'Found', $data = array()) |
|
| 235 | - { |
|
| 230 | + public function addMessage(File $phpcsFile, $message, $stackPtr, $isError, $code = 'Found', $data = array()) { |
|
| 236 | 231 | if ($isError === true) { |
| 237 | 232 | $phpcsFile->addError($message, $stackPtr, $code, $data); |
| 238 | 233 | } else { |
@@ -250,8 +245,7 @@ discard block |
||
| 250 | 245 | * |
| 251 | 246 | * @return string |
| 252 | 247 | */ |
| 253 | - public function stringToErrorCode($baseString) |
|
| 254 | - { |
|
| 248 | + public function stringToErrorCode($baseString) { |
|
| 255 | 249 | return preg_replace('`[^a-z0-9_]`i', '_', strtolower($baseString)); |
| 256 | 250 | } |
| 257 | 251 | |
@@ -265,8 +259,7 @@ discard block |
||
| 265 | 259 | * |
| 266 | 260 | * @return string String without quotes around it. |
| 267 | 261 | */ |
| 268 | - public function stripQuotes($string) |
|
| 269 | - { |
|
| 262 | + public function stripQuotes($string) { |
|
| 270 | 263 | return preg_replace('`^([\'"])(.*)\1$`Ds', '$2', $string); |
| 271 | 264 | } |
| 272 | 265 | |
@@ -280,8 +273,7 @@ discard block |
||
| 280 | 273 | * |
| 281 | 274 | * @return string String without variables in it. |
| 282 | 275 | */ |
| 283 | - public function stripVariables($string) |
|
| 284 | - { |
|
| 276 | + public function stripVariables($string) { |
|
| 285 | 277 | if (strpos($string, '$') === false) { |
| 286 | 278 | return $string; |
| 287 | 279 | } |
@@ -297,8 +289,7 @@ discard block |
||
| 297 | 289 | * |
| 298 | 290 | * @return array Same array, but with all lowercase top level keys. |
| 299 | 291 | */ |
| 300 | - public function arrayKeysToLowercase($array) |
|
| 301 | - { |
|
| 292 | + public function arrayKeysToLowercase($array) { |
|
| 302 | 293 | return array_change_key_case($array, \CASE_LOWER); |
| 303 | 294 | } |
| 304 | 295 | |
@@ -320,8 +311,7 @@ discard block |
||
| 320 | 311 | * |
| 321 | 312 | * @return bool |
| 322 | 313 | */ |
| 323 | - public function doesFunctionCallHaveParameters(File $phpcsFile, $stackPtr) |
|
| 324 | - { |
|
| 314 | + public function doesFunctionCallHaveParameters(File $phpcsFile, $stackPtr) { |
|
| 325 | 315 | $tokens = $phpcsFile->getTokens(); |
| 326 | 316 | |
| 327 | 317 | // Check for the existence of the token. |
@@ -390,8 +380,7 @@ discard block |
||
| 390 | 380 | * |
| 391 | 381 | * @return int |
| 392 | 382 | */ |
| 393 | - public function getFunctionCallParameterCount(File $phpcsFile, $stackPtr) |
|
| 394 | - { |
|
| 383 | + public function getFunctionCallParameterCount(File $phpcsFile, $stackPtr) { |
|
| 395 | 384 | if ($this->doesFunctionCallHaveParameters($phpcsFile, $stackPtr) === false) { |
| 396 | 385 | return 0; |
| 397 | 386 | } |
@@ -418,8 +407,7 @@ discard block |
||
| 418 | 407 | * |
| 419 | 408 | * @return array |
| 420 | 409 | */ |
| 421 | - public function getFunctionCallParameters(File $phpcsFile, $stackPtr) |
|
| 422 | - { |
|
| 410 | + public function getFunctionCallParameters(File $phpcsFile, $stackPtr) { |
|
| 423 | 411 | if ($this->doesFunctionCallHaveParameters($phpcsFile, $stackPtr) === false) { |
| 424 | 412 | return array(); |
| 425 | 413 | } |
@@ -525,8 +513,7 @@ discard block |
||
| 525 | 513 | * |
| 526 | 514 | * @return array|false |
| 527 | 515 | */ |
| 528 | - public function getFunctionCallParameter(File $phpcsFile, $stackPtr, $paramOffset) |
|
| 529 | - { |
|
| 516 | + public function getFunctionCallParameter(File $phpcsFile, $stackPtr, $paramOffset) { |
|
| 530 | 517 | $parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr); |
| 531 | 518 | |
| 532 | 519 | if (isset($parameters[$paramOffset]) === false) { |
@@ -555,8 +542,7 @@ discard block |
||
| 555 | 542 | * If the $scopeTypes are set: True if *one* of the conditions is a |
| 556 | 543 | * valid scope, false otherwise. |
| 557 | 544 | */ |
| 558 | - public function tokenHasScope(File $phpcsFile, $stackPtr, $validScopes = null) |
|
| 559 | - { |
|
| 545 | + public function tokenHasScope(File $phpcsFile, $stackPtr, $validScopes = null) { |
|
| 560 | 546 | $tokens = $phpcsFile->getTokens(); |
| 561 | 547 | |
| 562 | 548 | // Check for the existence of the token. |
@@ -589,8 +575,7 @@ discard block |
||
| 589 | 575 | * |
| 590 | 576 | * @return bool True if within class scope, false otherwise. |
| 591 | 577 | */ |
| 592 | - public function inClassScope(File $phpcsFile, $stackPtr, $strict = true) |
|
| 593 | - { |
|
| 578 | + public function inClassScope(File $phpcsFile, $stackPtr, $strict = true) { |
|
| 594 | 579 | $validScopes = array(\T_CLASS); |
| 595 | 580 | if (\defined('T_ANON_CLASS') === true) { |
| 596 | 581 | $validScopes[] = \T_ANON_CLASS; |
@@ -615,8 +600,7 @@ discard block |
||
| 615 | 600 | * |
| 616 | 601 | * @return string |
| 617 | 602 | */ |
| 618 | - public function getFQClassNameFromNewToken(File $phpcsFile, $stackPtr) |
|
| 619 | - { |
|
| 603 | + public function getFQClassNameFromNewToken(File $phpcsFile, $stackPtr) { |
|
| 620 | 604 | $tokens = $phpcsFile->getTokens(); |
| 621 | 605 | |
| 622 | 606 | // Check for the existence of the token. |
@@ -669,8 +653,7 @@ discard block |
||
| 669 | 653 | * |
| 670 | 654 | * @return string |
| 671 | 655 | */ |
| 672 | - public function getFQExtendedClassName(File $phpcsFile, $stackPtr) |
|
| 673 | - { |
|
| 656 | + public function getFQExtendedClassName(File $phpcsFile, $stackPtr) { |
|
| 674 | 657 | $tokens = $phpcsFile->getTokens(); |
| 675 | 658 | |
| 676 | 659 | // Check for the existence of the token. |
@@ -705,8 +688,7 @@ discard block |
||
| 705 | 688 | * |
| 706 | 689 | * @return string |
| 707 | 690 | */ |
| 708 | - public function getFQClassNameFromDoubleColonToken(File $phpcsFile, $stackPtr) |
|
| 709 | - { |
|
| 691 | + public function getFQClassNameFromDoubleColonToken(File $phpcsFile, $stackPtr) { |
|
| 710 | 692 | $tokens = $phpcsFile->getTokens(); |
| 711 | 693 | |
| 712 | 694 | // Check for the existence of the token. |
@@ -770,8 +752,7 @@ discard block |
||
| 770 | 752 | * |
| 771 | 753 | * @return string |
| 772 | 754 | */ |
| 773 | - public function getFQName(File $phpcsFile, $stackPtr, $name) |
|
| 774 | - { |
|
| 755 | + public function getFQName(File $phpcsFile, $stackPtr, $name) { |
|
| 775 | 756 | if (strpos($name, '\\') === 0) { |
| 776 | 757 | // Already fully qualified. |
| 777 | 758 | return $name; |
@@ -800,8 +781,7 @@ discard block |
||
| 800 | 781 | * |
| 801 | 782 | * @return bool True if namespaced, false if global. |
| 802 | 783 | */ |
| 803 | - public function isNamespaced($FQName) |
|
| 804 | - { |
|
| 784 | + public function isNamespaced($FQName) { |
|
| 805 | 785 | if (strpos($FQName, '\\') !== 0) { |
| 806 | 786 | throw new PHPCS_Exception('$FQName must be a fully qualified name'); |
| 807 | 787 | } |
@@ -818,8 +798,7 @@ discard block |
||
| 818 | 798 | * |
| 819 | 799 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
| 820 | 800 | */ |
| 821 | - public function determineNamespace(File $phpcsFile, $stackPtr) |
|
| 822 | - { |
|
| 801 | + public function determineNamespace(File $phpcsFile, $stackPtr) { |
|
| 823 | 802 | $tokens = $phpcsFile->getTokens(); |
| 824 | 803 | |
| 825 | 804 | // Check for the existence of the token. |
@@ -882,8 +861,7 @@ discard block |
||
| 882 | 861 | * @return string|false Namespace name or false if not a namespace declaration. |
| 883 | 862 | * Namespace name can be an empty string for global namespace declaration. |
| 884 | 863 | */ |
| 885 | - public function getDeclaredNamespaceName(File $phpcsFile, $stackPtr) |
|
| 886 | - { |
|
| 864 | + public function getDeclaredNamespaceName(File $phpcsFile, $stackPtr) { |
|
| 887 | 865 | $tokens = $phpcsFile->getTokens(); |
| 888 | 866 | |
| 889 | 867 | // Check for the existence of the token. |
@@ -939,8 +917,7 @@ discard block |
||
| 939 | 917 | * no return type was found or the passed token was |
| 940 | 918 | * not of the correct type. |
| 941 | 919 | */ |
| 942 | - public function getReturnTypeHintToken(File $phpcsFile, $stackPtr) |
|
| 943 | - { |
|
| 920 | + public function getReturnTypeHintToken(File $phpcsFile, $stackPtr) { |
|
| 944 | 921 | $tokens = $phpcsFile->getTokens(); |
| 945 | 922 | |
| 946 | 923 | if (\defined('T_RETURN_TYPE') && $tokens[$stackPtr]['code'] === \T_RETURN_TYPE) { |
@@ -1018,8 +995,7 @@ discard block |
||
| 1018 | 995 | * |
| 1019 | 996 | * @return string|false The name of the return type token. |
| 1020 | 997 | */ |
| 1021 | - public function getReturnTypeHintName(File $phpcsFile, $stackPtr) |
|
| 1022 | - { |
|
| 998 | + public function getReturnTypeHintName(File $phpcsFile, $stackPtr) { |
|
| 1023 | 999 | $tokens = $phpcsFile->getTokens(); |
| 1024 | 1000 | |
| 1025 | 1001 | // In older PHPCS versions, the nullable indicator will turn a return type colon into a T_INLINE_ELSE. |
@@ -1069,8 +1045,7 @@ discard block |
||
| 1069 | 1045 | * |
| 1070 | 1046 | * @return bool |
| 1071 | 1047 | */ |
| 1072 | - public function isClassProperty(File $phpcsFile, $stackPtr) |
|
| 1073 | - { |
|
| 1048 | + public function isClassProperty(File $phpcsFile, $stackPtr) { |
|
| 1074 | 1049 | $tokens = $phpcsFile->getTokens(); |
| 1075 | 1050 | |
| 1076 | 1051 | if (isset($tokens[$stackPtr]) === false || $tokens[$stackPtr]['code'] !== \T_VARIABLE) { |
@@ -1114,8 +1089,7 @@ discard block |
||
| 1114 | 1089 | * |
| 1115 | 1090 | * @return bool |
| 1116 | 1091 | */ |
| 1117 | - public function isClassConstant(File $phpcsFile, $stackPtr) |
|
| 1118 | - { |
|
| 1092 | + public function isClassConstant(File $phpcsFile, $stackPtr) { |
|
| 1119 | 1093 | $tokens = $phpcsFile->getTokens(); |
| 1120 | 1094 | |
| 1121 | 1095 | if (isset($tokens[$stackPtr]) === false || $tokens[$stackPtr]['code'] !== \T_CONST) { |
@@ -1152,8 +1126,7 @@ discard block |
||
| 1152 | 1126 | * |
| 1153 | 1127 | * @return int|bool StackPtr to the scope if valid, false otherwise. |
| 1154 | 1128 | */ |
| 1155 | - protected function validDirectScope(File $phpcsFile, $stackPtr, $validScopes) |
|
| 1156 | - { |
|
| 1129 | + protected function validDirectScope(File $phpcsFile, $stackPtr, $validScopes) { |
|
| 1157 | 1130 | $tokens = $phpcsFile->getTokens(); |
| 1158 | 1131 | |
| 1159 | 1132 | if (empty($tokens[$stackPtr]['conditions']) === true) { |
@@ -1194,8 +1167,7 @@ discard block |
||
| 1194 | 1167 | * - no type hints were found |
| 1195 | 1168 | * - or the passed token was not of the correct type. |
| 1196 | 1169 | */ |
| 1197 | - public function getTypeHintsFromFunctionDeclaration(File $phpcsFile, $stackPtr) |
|
| 1198 | - { |
|
| 1170 | + public function getTypeHintsFromFunctionDeclaration(File $phpcsFile, $stackPtr) { |
|
| 1199 | 1171 | $tokens = $phpcsFile->getTokens(); |
| 1200 | 1172 | |
| 1201 | 1173 | if ($tokens[$stackPtr]['code'] !== \T_FUNCTION && $tokens[$stackPtr]['code'] !== \T_CLOSURE) { |
@@ -1238,8 +1210,7 @@ discard block |
||
| 1238 | 1210 | * @return string|false The algorithm name without quotes if this was a relevant hash |
| 1239 | 1211 | * function call or false if it was not. |
| 1240 | 1212 | */ |
| 1241 | - public function getHashAlgorithmParameter(File $phpcsFile, $stackPtr) |
|
| 1242 | - { |
|
| 1213 | + public function getHashAlgorithmParameter(File $phpcsFile, $stackPtr) { |
|
| 1243 | 1214 | $tokens = $phpcsFile->getTokens(); |
| 1244 | 1215 | |
| 1245 | 1216 | // Check for the existence of the token. |
@@ -1281,8 +1252,7 @@ discard block |
||
| 1281 | 1252 | * |
| 1282 | 1253 | * @return bool |
| 1283 | 1254 | */ |
| 1284 | - public function isUseOfGlobalConstant(File $phpcsFile, $stackPtr) |
|
| 1285 | - { |
|
| 1255 | + public function isUseOfGlobalConstant(File $phpcsFile, $stackPtr) { |
|
| 1286 | 1256 | static $isLowPHPCS, $isLowPHP; |
| 1287 | 1257 | |
| 1288 | 1258 | $tokens = $phpcsFile->getTokens(); |
@@ -1419,8 +1389,7 @@ discard block |
||
| 1419 | 1389 | * False if not or if it could not be reliably determined |
| 1420 | 1390 | * (variable or calculations and such). |
| 1421 | 1391 | */ |
| 1422 | - public function isPositiveNumber(File $phpcsFile, $start, $end, $allowFloats = false) |
|
| 1423 | - { |
|
| 1392 | + public function isPositiveNumber(File $phpcsFile, $start, $end, $allowFloats = false) { |
|
| 1424 | 1393 | $number = $this->isNumber($phpcsFile, $start, $end, $allowFloats); |
| 1425 | 1394 | |
| 1426 | 1395 | if ($number === false) { |
@@ -1451,8 +1420,7 @@ discard block |
||
| 1451 | 1420 | * False if not or if it could not be reliably determined |
| 1452 | 1421 | * (variable or calculations and such). |
| 1453 | 1422 | */ |
| 1454 | - public function isNegativeNumber(File $phpcsFile, $start, $end, $allowFloats = false) |
|
| 1455 | - { |
|
| 1423 | + public function isNegativeNumber(File $phpcsFile, $start, $end, $allowFloats = false) { |
|
| 1456 | 1424 | $number = $this->isNumber($phpcsFile, $start, $end, $allowFloats); |
| 1457 | 1425 | |
| 1458 | 1426 | if ($number === false) { |
@@ -1488,8 +1456,7 @@ discard block |
||
| 1488 | 1456 | * number or if it could not be reliably determined |
| 1489 | 1457 | * (variable or calculations and such). |
| 1490 | 1458 | */ |
| 1491 | - protected function isNumber(File $phpcsFile, $start, $end, $allowFloats = false) |
|
| 1492 | - { |
|
| 1459 | + protected function isNumber(File $phpcsFile, $start, $end, $allowFloats = false) { |
|
| 1493 | 1460 | $stringTokens = Tokens::$heredocTokens + Tokens::$stringTokens; |
| 1494 | 1461 | |
| 1495 | 1462 | $validTokens = array(); |
@@ -1656,8 +1623,7 @@ discard block |
||
| 1656 | 1623 | * |
| 1657 | 1624 | * @return bool |
| 1658 | 1625 | */ |
| 1659 | - protected function isNumericCalculation(File $phpcsFile, $start, $end) |
|
| 1660 | - { |
|
| 1626 | + protected function isNumericCalculation(File $phpcsFile, $start, $end) { |
|
| 1661 | 1627 | $arithmeticTokens = Tokens::$arithmeticTokens; |
| 1662 | 1628 | |
| 1663 | 1629 | // phpcs:disable PHPCompatibility.Constants.NewConstants.t_powFound |
@@ -1741,8 +1707,7 @@ discard block |
||
| 1741 | 1707 | * |
| 1742 | 1708 | * @return bool True if short ternary, or false otherwise. |
| 1743 | 1709 | */ |
| 1744 | - public function isShortTernary(File $phpcsFile, $stackPtr) |
|
| 1745 | - { |
|
| 1710 | + public function isShortTernary(File $phpcsFile, $stackPtr) { |
|
| 1746 | 1711 | $tokens = $phpcsFile->getTokens(); |
| 1747 | 1712 | if (isset($tokens[$stackPtr]) === false |
| 1748 | 1713 | || $tokens[$stackPtr]['code'] !== \T_INLINE_THEN |
@@ -1775,8 +1740,7 @@ discard block |
||
| 1775 | 1740 | * |
| 1776 | 1741 | * @return bool |
| 1777 | 1742 | */ |
| 1778 | - public function isShortList(File $phpcsFile, $stackPtr) |
|
| 1779 | - { |
|
| 1743 | + public function isShortList(File $phpcsFile, $stackPtr) { |
|
| 1780 | 1744 | $tokens = $phpcsFile->getTokens(); |
| 1781 | 1745 | |
| 1782 | 1746 | // Check for the existence of the token. |
@@ -1922,8 +1886,7 @@ discard block |
||
| 1922 | 1886 | * |
| 1923 | 1887 | * @return bool |
| 1924 | 1888 | */ |
| 1925 | - public function isVariable(File $phpcsFile, $start, $end, $targetNestingLevel) |
|
| 1926 | - { |
|
| 1889 | + public function isVariable(File $phpcsFile, $start, $end, $targetNestingLevel) { |
|
| 1927 | 1890 | static $tokenBlackList, $bracketTokens; |
| 1928 | 1891 | |
| 1929 | 1892 | // Create the token arrays only once. |
@@ -2016,8 +1979,7 @@ discard block |
||
| 2016 | 1979 | * @return bool True if the token passed is a unary operator. |
| 2017 | 1980 | * False otherwise or if the token is not a T_PLUS/T_MINUS token. |
| 2018 | 1981 | */ |
| 2019 | - public static function isUnaryPlusMinus(File $phpcsFile, $stackPtr) |
|
| 2020 | - { |
|
| 1982 | + public static function isUnaryPlusMinus(File $phpcsFile, $stackPtr) { |
|
| 2021 | 1983 | $tokens = $phpcsFile->getTokens(); |
| 2022 | 1984 | |
| 2023 | 1985 | if (isset($tokens[$stackPtr]) === false |
@@ -47,7 +47,7 @@ |
||
| 47 | 47 | * |
| 48 | 48 | * @since 9.2.0 |
| 49 | 49 | * |
| 50 | - * @return array |
|
| 50 | + * @return integer[] |
|
| 51 | 51 | */ |
| 52 | 52 | public function register() |
| 53 | 53 | { |
@@ -29,62 +29,62 @@ |
||
| 29 | 29 | class ForbiddenAbstractPrivateMethodsSniff extends Sniff |
| 30 | 30 | { |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Valid scopes to check for abstract private methods. |
|
| 34 | - * |
|
| 35 | - * @since 9.2.0 |
|
| 36 | - * |
|
| 37 | - * @var array |
|
| 38 | - */ |
|
| 39 | - public $ooScopeTokens = array( |
|
| 40 | - 'T_CLASS' => true, |
|
| 41 | - 'T_TRAIT' => true, |
|
| 42 | - 'T_ANON_CLASS' => true, |
|
| 43 | - ); |
|
| 32 | + /** |
|
| 33 | + * Valid scopes to check for abstract private methods. |
|
| 34 | + * |
|
| 35 | + * @since 9.2.0 |
|
| 36 | + * |
|
| 37 | + * @var array |
|
| 38 | + */ |
|
| 39 | + public $ooScopeTokens = array( |
|
| 40 | + 'T_CLASS' => true, |
|
| 41 | + 'T_TRAIT' => true, |
|
| 42 | + 'T_ANON_CLASS' => true, |
|
| 43 | + ); |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Returns an array of tokens this test wants to listen for. |
|
| 47 | - * |
|
| 48 | - * @since 9.2.0 |
|
| 49 | - * |
|
| 50 | - * @return array |
|
| 51 | - */ |
|
| 52 | - public function register() |
|
| 53 | - { |
|
| 54 | - return array(\T_FUNCTION); |
|
| 55 | - } |
|
| 45 | + /** |
|
| 46 | + * Returns an array of tokens this test wants to listen for. |
|
| 47 | + * |
|
| 48 | + * @since 9.2.0 |
|
| 49 | + * |
|
| 50 | + * @return array |
|
| 51 | + */ |
|
| 52 | + public function register() |
|
| 53 | + { |
|
| 54 | + return array(\T_FUNCTION); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Processes this test, when one of its tokens is encountered. |
|
| 59 | - * |
|
| 60 | - * @since 9.2.0 |
|
| 61 | - * |
|
| 62 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 63 | - * @param int $stackPtr The position of the current token |
|
| 64 | - * in the stack passed in $tokens. |
|
| 65 | - * |
|
| 66 | - * @return void |
|
| 67 | - */ |
|
| 68 | - public function process(File $phpcsFile, $stackPtr) |
|
| 69 | - { |
|
| 70 | - if ($this->supportsAbove('5.1') === false) { |
|
| 71 | - return; |
|
| 72 | - } |
|
| 57 | + /** |
|
| 58 | + * Processes this test, when one of its tokens is encountered. |
|
| 59 | + * |
|
| 60 | + * @since 9.2.0 |
|
| 61 | + * |
|
| 62 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 63 | + * @param int $stackPtr The position of the current token |
|
| 64 | + * in the stack passed in $tokens. |
|
| 65 | + * |
|
| 66 | + * @return void |
|
| 67 | + */ |
|
| 68 | + public function process(File $phpcsFile, $stackPtr) |
|
| 69 | + { |
|
| 70 | + if ($this->supportsAbove('5.1') === false) { |
|
| 71 | + return; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - if ($this->validDirectScope($phpcsFile, $stackPtr, $this->ooScopeTokens) === false) { |
|
| 75 | - // Function, not method. |
|
| 76 | - return; |
|
| 77 | - } |
|
| 74 | + if ($this->validDirectScope($phpcsFile, $stackPtr, $this->ooScopeTokens) === false) { |
|
| 75 | + // Function, not method. |
|
| 76 | + return; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - $properties = $phpcsFile->getMethodProperties($stackPtr); |
|
| 80 | - if ($properties['scope'] !== 'private' || $properties['is_abstract'] !== true) { |
|
| 81 | - return; |
|
| 82 | - } |
|
| 79 | + $properties = $phpcsFile->getMethodProperties($stackPtr); |
|
| 80 | + if ($properties['scope'] !== 'private' || $properties['is_abstract'] !== true) { |
|
| 81 | + return; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - $phpcsFile->addError( |
|
| 85 | - 'Abstract methods cannot be declared as private since PHP 5.1', |
|
| 86 | - $stackPtr, |
|
| 87 | - 'Found' |
|
| 88 | - ); |
|
| 89 | - } |
|
| 84 | + $phpcsFile->addError( |
|
| 85 | + 'Abstract methods cannot be declared as private since PHP 5.1', |
|
| 86 | + $stackPtr, |
|
| 87 | + 'Found' |
|
| 88 | + ); |
|
| 89 | + } |
|
| 90 | 90 | } |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | */ |
| 52 | 52 | public function register() |
| 53 | 53 | { |
| 54 | - return array(\T_FUNCTION); |
|
| 54 | + return array( \T_FUNCTION ); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
@@ -65,19 +65,19 @@ discard block |
||
| 65 | 65 | * |
| 66 | 66 | * @return void |
| 67 | 67 | */ |
| 68 | - public function process(File $phpcsFile, $stackPtr) |
|
| 68 | + public function process( File $phpcsFile, $stackPtr ) |
|
| 69 | 69 | { |
| 70 | - if ($this->supportsAbove('5.1') === false) { |
|
| 70 | + if ( $this->supportsAbove( '5.1' ) === false ) { |
|
| 71 | 71 | return; |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - if ($this->validDirectScope($phpcsFile, $stackPtr, $this->ooScopeTokens) === false) { |
|
| 74 | + if ( $this->validDirectScope( $phpcsFile, $stackPtr, $this->ooScopeTokens ) === false ) { |
|
| 75 | 75 | // Function, not method. |
| 76 | 76 | return; |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | - $properties = $phpcsFile->getMethodProperties($stackPtr); |
|
| 80 | - if ($properties['scope'] !== 'private' || $properties['is_abstract'] !== true) { |
|
| 79 | + $properties = $phpcsFile->getMethodProperties( $stackPtr ); |
|
| 80 | + if ( $properties[ 'scope' ] !== 'private' || $properties[ 'is_abstract' ] !== true ) { |
|
| 81 | 81 | return; |
| 82 | 82 | } |
| 83 | 83 | |
@@ -26,8 +26,7 @@ discard block |
||
| 26 | 26 | * |
| 27 | 27 | * @since 9.2.0 |
| 28 | 28 | */ |
| 29 | -class ForbiddenAbstractPrivateMethodsSniff extends Sniff |
|
| 30 | -{ |
|
| 29 | +class ForbiddenAbstractPrivateMethodsSniff extends Sniff { |
|
| 31 | 30 | |
| 32 | 31 | /** |
| 33 | 32 | * Valid scopes to check for abstract private methods. |
@@ -49,8 +48,7 @@ discard block |
||
| 49 | 48 | * |
| 50 | 49 | * @return array |
| 51 | 50 | */ |
| 52 | - public function register() |
|
| 53 | - { |
|
| 51 | + public function register() { |
|
| 54 | 52 | return array(\T_FUNCTION); |
| 55 | 53 | } |
| 56 | 54 | |
@@ -65,8 +63,7 @@ discard block |
||
| 65 | 63 | * |
| 66 | 64 | * @return void |
| 67 | 65 | */ |
| 68 | - public function process(File $phpcsFile, $stackPtr) |
|
| 69 | - { |
|
| 66 | + public function process(File $phpcsFile, $stackPtr) { |
|
| 70 | 67 | if ($this->supportsAbove('5.1') === false) { |
| 71 | 68 | return; |
| 72 | 69 | } |
@@ -45,7 +45,7 @@ |
||
| 45 | 45 | * |
| 46 | 46 | * @since 9.2.0 |
| 47 | 47 | * |
| 48 | - * @return array |
|
| 48 | + * @return string[] |
|
| 49 | 49 | */ |
| 50 | 50 | public function register() |
| 51 | 51 | { |
@@ -28,88 +28,88 @@ |
||
| 28 | 28 | class RemovedOrphanedParentSniff extends Sniff |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Class scopes to check the class declaration. |
|
| 33 | - * |
|
| 34 | - * @since 9.2.0 |
|
| 35 | - * |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - public $classScopeTokens = array( |
|
| 39 | - 'T_CLASS' => true, |
|
| 40 | - 'T_ANON_CLASS' => true, |
|
| 41 | - ); |
|
| 31 | + /** |
|
| 32 | + * Class scopes to check the class declaration. |
|
| 33 | + * |
|
| 34 | + * @since 9.2.0 |
|
| 35 | + * |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + public $classScopeTokens = array( |
|
| 39 | + 'T_CLASS' => true, |
|
| 40 | + 'T_ANON_CLASS' => true, |
|
| 41 | + ); |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Returns an array of tokens this test wants to listen for. |
|
| 45 | - * |
|
| 46 | - * @since 9.2.0 |
|
| 47 | - * |
|
| 48 | - * @return array |
|
| 49 | - */ |
|
| 50 | - public function register() |
|
| 51 | - { |
|
| 52 | - return array(\T_PARENT); |
|
| 53 | - } |
|
| 43 | + /** |
|
| 44 | + * Returns an array of tokens this test wants to listen for. |
|
| 45 | + * |
|
| 46 | + * @since 9.2.0 |
|
| 47 | + * |
|
| 48 | + * @return array |
|
| 49 | + */ |
|
| 50 | + public function register() |
|
| 51 | + { |
|
| 52 | + return array(\T_PARENT); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Processes this test, when one of its tokens is encountered. |
|
| 57 | - * |
|
| 58 | - * @since 9.2.0 |
|
| 59 | - * |
|
| 60 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 61 | - * @param int $stackPtr The position of the current token |
|
| 62 | - * in the stack passed in $tokens. |
|
| 63 | - * |
|
| 64 | - * @return void |
|
| 65 | - */ |
|
| 66 | - public function process(File $phpcsFile, $stackPtr) |
|
| 67 | - { |
|
| 68 | - if ($this->supportsAbove('7.4') === false) { |
|
| 69 | - return; |
|
| 70 | - } |
|
| 55 | + /** |
|
| 56 | + * Processes this test, when one of its tokens is encountered. |
|
| 57 | + * |
|
| 58 | + * @since 9.2.0 |
|
| 59 | + * |
|
| 60 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 61 | + * @param int $stackPtr The position of the current token |
|
| 62 | + * in the stack passed in $tokens. |
|
| 63 | + * |
|
| 64 | + * @return void |
|
| 65 | + */ |
|
| 66 | + public function process(File $phpcsFile, $stackPtr) |
|
| 67 | + { |
|
| 68 | + if ($this->supportsAbove('7.4') === false) { |
|
| 69 | + return; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - $tokens = $phpcsFile->getTokens(); |
|
| 72 | + $tokens = $phpcsFile->getTokens(); |
|
| 73 | 73 | |
| 74 | - if (empty($tokens[$stackPtr]['conditions']) === true) { |
|
| 75 | - // Use within the global namespace. Not our concern. |
|
| 76 | - return; |
|
| 77 | - } |
|
| 74 | + if (empty($tokens[$stackPtr]['conditions']) === true) { |
|
| 75 | + // Use within the global namespace. Not our concern. |
|
| 76 | + return; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /* |
|
| 79 | + /* |
|
| 80 | 80 | * Find the class within which this parent keyword is used. |
| 81 | 81 | */ |
| 82 | - $conditions = $tokens[$stackPtr]['conditions']; |
|
| 83 | - $conditions = array_reverse($conditions, true); |
|
| 84 | - $classPtr = false; |
|
| 82 | + $conditions = $tokens[$stackPtr]['conditions']; |
|
| 83 | + $conditions = array_reverse($conditions, true); |
|
| 84 | + $classPtr = false; |
|
| 85 | 85 | |
| 86 | - foreach ($conditions as $ptr => $type) { |
|
| 87 | - if (isset($this->classScopeTokens[$tokens[$ptr]['type']])) { |
|
| 88 | - $classPtr = $ptr; |
|
| 89 | - break; |
|
| 90 | - } |
|
| 91 | - } |
|
| 86 | + foreach ($conditions as $ptr => $type) { |
|
| 87 | + if (isset($this->classScopeTokens[$tokens[$ptr]['type']])) { |
|
| 88 | + $classPtr = $ptr; |
|
| 89 | + break; |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - if ($classPtr === false) { |
|
| 94 | - // Use outside of a class scope. Not our concern. |
|
| 95 | - return; |
|
| 96 | - } |
|
| 93 | + if ($classPtr === false) { |
|
| 94 | + // Use outside of a class scope. Not our concern. |
|
| 95 | + return; |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - if (isset($tokens[$classPtr]['scope_opener']) === false) { |
|
| 99 | - // No scope opener known. Probably a parse error. |
|
| 100 | - return; |
|
| 101 | - } |
|
| 98 | + if (isset($tokens[$classPtr]['scope_opener']) === false) { |
|
| 99 | + // No scope opener known. Probably a parse error. |
|
| 100 | + return; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - $extends = $phpcsFile->findNext(\T_EXTENDS, ($classPtr + 1), $tokens[$classPtr]['scope_opener']); |
|
| 104 | - if ($extends !== false) { |
|
| 105 | - // Class has a parent. |
|
| 106 | - return; |
|
| 107 | - } |
|
| 103 | + $extends = $phpcsFile->findNext(\T_EXTENDS, ($classPtr + 1), $tokens[$classPtr]['scope_opener']); |
|
| 104 | + if ($extends !== false) { |
|
| 105 | + // Class has a parent. |
|
| 106 | + return; |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - $phpcsFile->addError( |
|
| 110 | - 'Using "parent" inside a class without parent is deprecated since PHP 7.4', |
|
| 111 | - $stackPtr, |
|
| 112 | - 'Deprecated' |
|
| 113 | - ); |
|
| 114 | - } |
|
| 109 | + $phpcsFile->addError( |
|
| 110 | + 'Using "parent" inside a class without parent is deprecated since PHP 7.4', |
|
| 111 | + $stackPtr, |
|
| 112 | + 'Deprecated' |
|
| 113 | + ); |
|
| 114 | + } |
|
| 115 | 115 | } |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | */ |
| 50 | 50 | public function register() |
| 51 | 51 | { |
| 52 | - return array(\T_PARENT); |
|
| 52 | + return array( \T_PARENT ); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | /** |
@@ -63,15 +63,15 @@ discard block |
||
| 63 | 63 | * |
| 64 | 64 | * @return void |
| 65 | 65 | */ |
| 66 | - public function process(File $phpcsFile, $stackPtr) |
|
| 66 | + public function process( File $phpcsFile, $stackPtr ) |
|
| 67 | 67 | { |
| 68 | - if ($this->supportsAbove('7.4') === false) { |
|
| 68 | + if ( $this->supportsAbove( '7.4' ) === false ) { |
|
| 69 | 69 | return; |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | $tokens = $phpcsFile->getTokens(); |
| 73 | 73 | |
| 74 | - if (empty($tokens[$stackPtr]['conditions']) === true) { |
|
| 74 | + if ( empty( $tokens[ $stackPtr ][ 'conditions' ] ) === true ) { |
|
| 75 | 75 | // Use within the global namespace. Not our concern. |
| 76 | 76 | return; |
| 77 | 77 | } |
@@ -79,29 +79,29 @@ discard block |
||
| 79 | 79 | /* |
| 80 | 80 | * Find the class within which this parent keyword is used. |
| 81 | 81 | */ |
| 82 | - $conditions = $tokens[$stackPtr]['conditions']; |
|
| 83 | - $conditions = array_reverse($conditions, true); |
|
| 82 | + $conditions = $tokens[ $stackPtr ][ 'conditions' ]; |
|
| 83 | + $conditions = array_reverse( $conditions, true ); |
|
| 84 | 84 | $classPtr = false; |
| 85 | 85 | |
| 86 | - foreach ($conditions as $ptr => $type) { |
|
| 87 | - if (isset($this->classScopeTokens[$tokens[$ptr]['type']])) { |
|
| 86 | + foreach ( $conditions as $ptr => $type ) { |
|
| 87 | + if ( isset( $this->classScopeTokens[ $tokens[ $ptr ][ 'type' ] ] ) ) { |
|
| 88 | 88 | $classPtr = $ptr; |
| 89 | 89 | break; |
| 90 | 90 | } |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - if ($classPtr === false) { |
|
| 93 | + if ( $classPtr === false ) { |
|
| 94 | 94 | // Use outside of a class scope. Not our concern. |
| 95 | 95 | return; |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - if (isset($tokens[$classPtr]['scope_opener']) === false) { |
|
| 98 | + if ( isset( $tokens[ $classPtr ][ 'scope_opener' ] ) === false ) { |
|
| 99 | 99 | // No scope opener known. Probably a parse error. |
| 100 | 100 | return; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | - $extends = $phpcsFile->findNext(\T_EXTENDS, ($classPtr + 1), $tokens[$classPtr]['scope_opener']); |
|
| 104 | - if ($extends !== false) { |
|
| 103 | + $extends = $phpcsFile->findNext( \T_EXTENDS, ( $classPtr + 1 ), $tokens[ $classPtr ][ 'scope_opener' ] ); |
|
| 104 | + if ( $extends !== false ) { |
|
| 105 | 105 | // Class has a parent. |
| 106 | 106 | return; |
| 107 | 107 | } |
@@ -25,8 +25,7 @@ discard block |
||
| 25 | 25 | * |
| 26 | 26 | * @since 9.2.0 |
| 27 | 27 | */ |
| 28 | -class RemovedOrphanedParentSniff extends Sniff |
|
| 29 | -{ |
|
| 28 | +class RemovedOrphanedParentSniff extends Sniff { |
|
| 30 | 29 | |
| 31 | 30 | /** |
| 32 | 31 | * Class scopes to check the class declaration. |
@@ -47,8 +46,7 @@ discard block |
||
| 47 | 46 | * |
| 48 | 47 | * @return array |
| 49 | 48 | */ |
| 50 | - public function register() |
|
| 51 | - { |
|
| 49 | + public function register() { |
|
| 52 | 50 | return array(\T_PARENT); |
| 53 | 51 | } |
| 54 | 52 | |
@@ -63,8 +61,7 @@ discard block |
||
| 63 | 61 | * |
| 64 | 62 | * @return void |
| 65 | 63 | */ |
| 66 | - public function process(File $phpcsFile, $stackPtr) |
|
| 67 | - { |
|
| 64 | + public function process(File $phpcsFile, $stackPtr) { |
|
| 68 | 65 | if ($this->supportsAbove('7.4') === false) { |
| 69 | 66 | return; |
| 70 | 67 | } |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | /** |
| 65 | 65 | * Returns an array of tokens this test wants to listen for. |
| 66 | 66 | * |
| 67 | - * @return array |
|
| 67 | + * @return integer[] |
|
| 68 | 68 | */ |
| 69 | 69 | public function register() |
| 70 | 70 | { |
@@ -167,7 +167,7 @@ discard block |
||
| 167 | 167 | /** |
| 168 | 168 | * Get an array of the non-PHP-version array keys used in a sub-array. |
| 169 | 169 | * |
| 170 | - * @return array |
|
| 170 | + * @return string[] |
|
| 171 | 171 | */ |
| 172 | 172 | protected function getNonVersionArrayKeys() |
| 173 | 173 | { |
@@ -24,311 +24,311 @@ |
||
| 24 | 24 | class NewExecutionDirectivesSniff extends AbstractNewFeatureSniff |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * A list of new execution directives |
|
| 29 | - * |
|
| 30 | - * The array lists : version number with false (not present) or true (present). |
|
| 31 | - * If the execution order is conditional, add the condition as a string to the version nr. |
|
| 32 | - * If's sufficient to list the first version where the execution directive appears. |
|
| 33 | - * |
|
| 34 | - * @var array(string => array(string => int|string|null)) |
|
| 35 | - */ |
|
| 36 | - protected $newDirectives = array( |
|
| 37 | - 'ticks' => array( |
|
| 38 | - '3.1' => false, |
|
| 39 | - '4.0' => true, |
|
| 40 | - 'valid_value_callback' => 'isNumeric', |
|
| 41 | - ), |
|
| 42 | - 'encoding' => array( |
|
| 43 | - '5.2' => false, |
|
| 44 | - '5.3' => '--enable-zend-multibyte', // Directive ignored unless. |
|
| 45 | - '5.4' => true, |
|
| 46 | - 'valid_value_callback' => 'validEncoding', |
|
| 47 | - ), |
|
| 48 | - 'strict_types' => array( |
|
| 49 | - '5.6' => false, |
|
| 50 | - '7.0' => true, |
|
| 51 | - 'valid_values' => array(1), |
|
| 52 | - ), |
|
| 53 | - ); |
|
| 54 | - |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Tokens to ignore when trying to find the value for the directive. |
|
| 58 | - * |
|
| 59 | - * @var array |
|
| 60 | - */ |
|
| 61 | - protected $ignoreTokens = array(); |
|
| 62 | - |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Returns an array of tokens this test wants to listen for. |
|
| 66 | - * |
|
| 67 | - * @return array |
|
| 68 | - */ |
|
| 69 | - public function register() |
|
| 70 | - { |
|
| 71 | - $this->ignoreTokens = Tokens::$emptyTokens; |
|
| 72 | - $this->ignoreTokens[\T_EQUAL] = \T_EQUAL; |
|
| 73 | - |
|
| 74 | - return array(\T_DECLARE); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Processes this test, when one of its tokens is encountered. |
|
| 80 | - * |
|
| 81 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 82 | - * @param int $stackPtr The position of the current token in |
|
| 83 | - * the stack passed in $tokens. |
|
| 84 | - * |
|
| 85 | - * @return void |
|
| 86 | - */ |
|
| 87 | - public function process(File $phpcsFile, $stackPtr) |
|
| 88 | - { |
|
| 89 | - $tokens = $phpcsFile->getTokens(); |
|
| 90 | - |
|
| 91 | - if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === true) { |
|
| 92 | - $openParenthesis = $tokens[$stackPtr]['parenthesis_opener']; |
|
| 93 | - $closeParenthesis = $tokens[$stackPtr]['parenthesis_closer']; |
|
| 94 | - } else { |
|
| 95 | - if (version_compare(PHPCSHelper::getVersion(), '2.3.4', '>=')) { |
|
| 96 | - return; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - // Deal with PHPCS 2.3.0-2.3.3 which do not yet set the parenthesis properly for declare statements. |
|
| 100 | - $openParenthesis = $phpcsFile->findNext(\T_OPEN_PARENTHESIS, ($stackPtr + 1), null, false, null, true); |
|
| 101 | - if ($openParenthesis === false || isset($tokens[$openParenthesis]['parenthesis_closer']) === false) { |
|
| 102 | - return; |
|
| 103 | - } |
|
| 104 | - $closeParenthesis = $tokens[$openParenthesis]['parenthesis_closer']; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - $directivePtr = $phpcsFile->findNext(\T_STRING, ($openParenthesis + 1), $closeParenthesis, false); |
|
| 108 | - if ($directivePtr === false) { |
|
| 109 | - return; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - $directiveContent = $tokens[$directivePtr]['content']; |
|
| 113 | - |
|
| 114 | - if (isset($this->newDirectives[$directiveContent]) === false) { |
|
| 115 | - $error = 'Declare can only be used with the directives %s. Found: %s'; |
|
| 116 | - $data = array( |
|
| 117 | - implode(', ', array_keys($this->newDirectives)), |
|
| 118 | - $directiveContent, |
|
| 119 | - ); |
|
| 120 | - |
|
| 121 | - $phpcsFile->addError($error, $stackPtr, 'InvalidDirectiveFound', $data); |
|
| 122 | - |
|
| 123 | - } else { |
|
| 124 | - // Check for valid directive for version. |
|
| 125 | - $itemInfo = array( |
|
| 126 | - 'name' => $directiveContent, |
|
| 127 | - ); |
|
| 128 | - $this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
|
| 129 | - |
|
| 130 | - // Check for valid directive value. |
|
| 131 | - $valuePtr = $phpcsFile->findNext($this->ignoreTokens, $directivePtr + 1, $closeParenthesis, true); |
|
| 132 | - if ($valuePtr === false) { |
|
| 133 | - return; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - $this->addWarningOnInvalidValue($phpcsFile, $valuePtr, $directiveContent); |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Determine whether an error/warning should be thrown for an item based on collected information. |
|
| 143 | - * |
|
| 144 | - * @param array $errorInfo Detail information about an item. |
|
| 145 | - * |
|
| 146 | - * @return bool |
|
| 147 | - */ |
|
| 148 | - protected function shouldThrowError(array $errorInfo) |
|
| 149 | - { |
|
| 150 | - return ($errorInfo['not_in_version'] !== '' || $errorInfo['conditional_version'] !== ''); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Get the relevant sub-array for a specific item from a multi-dimensional array. |
|
| 156 | - * |
|
| 157 | - * @param array $itemInfo Base information about the item. |
|
| 158 | - * |
|
| 159 | - * @return array Version and other information about the item. |
|
| 160 | - */ |
|
| 161 | - public function getItemArray(array $itemInfo) |
|
| 162 | - { |
|
| 163 | - return $this->newDirectives[$itemInfo['name']]; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Get an array of the non-PHP-version array keys used in a sub-array. |
|
| 169 | - * |
|
| 170 | - * @return array |
|
| 171 | - */ |
|
| 172 | - protected function getNonVersionArrayKeys() |
|
| 173 | - { |
|
| 174 | - return array( |
|
| 175 | - 'valid_value_callback', |
|
| 176 | - 'valid_values', |
|
| 177 | - ); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Retrieve the relevant detail (version) information for use in an error message. |
|
| 183 | - * |
|
| 184 | - * @param array $itemArray Version and other information about the item. |
|
| 185 | - * @param array $itemInfo Base information about the item. |
|
| 186 | - * |
|
| 187 | - * @return array |
|
| 188 | - */ |
|
| 189 | - public function getErrorInfo(array $itemArray, array $itemInfo) |
|
| 190 | - { |
|
| 191 | - $errorInfo = parent::getErrorInfo($itemArray, $itemInfo); |
|
| 192 | - $errorInfo['conditional_version'] = ''; |
|
| 193 | - $errorInfo['condition'] = ''; |
|
| 194 | - |
|
| 195 | - $versionArray = $this->getVersionArray($itemArray); |
|
| 196 | - |
|
| 197 | - if (empty($versionArray) === false) { |
|
| 198 | - foreach ($versionArray as $version => $present) { |
|
| 199 | - if (\is_string($present) === true && $this->supportsBelow($version) === true) { |
|
| 200 | - // We cannot test for compilation option (ok, except by scraping the output of phpinfo...). |
|
| 201 | - $errorInfo['conditional_version'] = $version; |
|
| 202 | - $errorInfo['condition'] = $present; |
|
| 203 | - } |
|
| 204 | - } |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - return $errorInfo; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Get the error message template for this sniff. |
|
| 213 | - * |
|
| 214 | - * @return string |
|
| 215 | - */ |
|
| 216 | - protected function getErrorMsgTemplate() |
|
| 217 | - { |
|
| 218 | - return 'Directive ' . parent::getErrorMsgTemplate(); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Generates the error or warning for this item. |
|
| 224 | - * |
|
| 225 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 226 | - * @param int $stackPtr The position of the relevant token in |
|
| 227 | - * the stack. |
|
| 228 | - * @param array $itemInfo Base information about the item. |
|
| 229 | - * @param array $errorInfo Array with detail (version) information |
|
| 230 | - * relevant to the item. |
|
| 231 | - * |
|
| 232 | - * @return void |
|
| 233 | - */ |
|
| 234 | - public function addError(File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo) |
|
| 235 | - { |
|
| 236 | - if ($errorInfo['not_in_version'] !== '') { |
|
| 237 | - parent::addError($phpcsFile, $stackPtr, $itemInfo, $errorInfo); |
|
| 238 | - } elseif ($errorInfo['conditional_version'] !== '') { |
|
| 239 | - $error = 'Directive %s is present in PHP version %s but will be disregarded unless PHP is compiled with %s'; |
|
| 240 | - $errorCode = $this->stringToErrorCode($itemInfo['name']) . 'WithConditionFound'; |
|
| 241 | - $data = array( |
|
| 242 | - $itemInfo['name'], |
|
| 243 | - $errorInfo['conditional_version'], |
|
| 244 | - $errorInfo['condition'], |
|
| 245 | - ); |
|
| 246 | - |
|
| 247 | - $phpcsFile->addWarning($error, $stackPtr, $errorCode, $data); |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Generates a error or warning for this sniff. |
|
| 254 | - * |
|
| 255 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 256 | - * @param int $stackPtr The position of the execution directive value |
|
| 257 | - * in the token array. |
|
| 258 | - * @param string $directive The directive. |
|
| 259 | - * |
|
| 260 | - * @return void |
|
| 261 | - */ |
|
| 262 | - protected function addWarningOnInvalidValue(File $phpcsFile, $stackPtr, $directive) |
|
| 263 | - { |
|
| 264 | - $tokens = $phpcsFile->getTokens(); |
|
| 265 | - |
|
| 266 | - $value = $tokens[$stackPtr]['content']; |
|
| 267 | - if (isset(Tokens::$stringTokens[$tokens[$stackPtr]['code']]) === true) { |
|
| 268 | - $value = $this->stripQuotes($value); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - $isError = false; |
|
| 272 | - if (isset($this->newDirectives[$directive]['valid_values'])) { |
|
| 273 | - if (\in_array($value, $this->newDirectives[$directive]['valid_values']) === false) { |
|
| 274 | - $isError = true; |
|
| 275 | - } |
|
| 276 | - } elseif (isset($this->newDirectives[$directive]['valid_value_callback'])) { |
|
| 277 | - $valid = \call_user_func(array($this, $this->newDirectives[$directive]['valid_value_callback']), $value); |
|
| 278 | - if ($valid === false) { |
|
| 279 | - $isError = true; |
|
| 280 | - } |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - if ($isError === true) { |
|
| 284 | - $error = 'The execution directive %s does not seem to have a valid value. Please review. Found: %s'; |
|
| 285 | - $errorCode = $this->stringToErrorCode($directive) . 'InvalidValueFound'; |
|
| 286 | - $data = array( |
|
| 287 | - $directive, |
|
| 288 | - $value, |
|
| 289 | - ); |
|
| 290 | - |
|
| 291 | - $phpcsFile->addWarning($error, $stackPtr, $errorCode, $data); |
|
| 292 | - } |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Check whether a value is numeric. |
|
| 298 | - * |
|
| 299 | - * Callback function to test whether the value for an execution directive is valid. |
|
| 300 | - * |
|
| 301 | - * @param mixed $value The value to test. |
|
| 302 | - * |
|
| 303 | - * @return bool |
|
| 304 | - */ |
|
| 305 | - protected function isNumeric($value) |
|
| 306 | - { |
|
| 307 | - return is_numeric($value); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * Check whether a value is a valid encoding. |
|
| 313 | - * |
|
| 314 | - * Callback function to test whether the value for an execution directive is valid. |
|
| 315 | - * |
|
| 316 | - * @param mixed $value The value to test. |
|
| 317 | - * |
|
| 318 | - * @return bool |
|
| 319 | - */ |
|
| 320 | - protected function validEncoding($value) |
|
| 321 | - { |
|
| 322 | - static $encodings; |
|
| 323 | - if (isset($encodings) === false && function_exists('mb_list_encodings')) { |
|
| 324 | - $encodings = mb_list_encodings(); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - if (empty($encodings) || \is_array($encodings) === false) { |
|
| 328 | - // If we can't test the encoding, let it pass through. |
|
| 329 | - return true; |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - return \in_array($value, $encodings, true); |
|
| 333 | - } |
|
| 27 | + /** |
|
| 28 | + * A list of new execution directives |
|
| 29 | + * |
|
| 30 | + * The array lists : version number with false (not present) or true (present). |
|
| 31 | + * If the execution order is conditional, add the condition as a string to the version nr. |
|
| 32 | + * If's sufficient to list the first version where the execution directive appears. |
|
| 33 | + * |
|
| 34 | + * @var array(string => array(string => int|string|null)) |
|
| 35 | + */ |
|
| 36 | + protected $newDirectives = array( |
|
| 37 | + 'ticks' => array( |
|
| 38 | + '3.1' => false, |
|
| 39 | + '4.0' => true, |
|
| 40 | + 'valid_value_callback' => 'isNumeric', |
|
| 41 | + ), |
|
| 42 | + 'encoding' => array( |
|
| 43 | + '5.2' => false, |
|
| 44 | + '5.3' => '--enable-zend-multibyte', // Directive ignored unless. |
|
| 45 | + '5.4' => true, |
|
| 46 | + 'valid_value_callback' => 'validEncoding', |
|
| 47 | + ), |
|
| 48 | + 'strict_types' => array( |
|
| 49 | + '5.6' => false, |
|
| 50 | + '7.0' => true, |
|
| 51 | + 'valid_values' => array(1), |
|
| 52 | + ), |
|
| 53 | + ); |
|
| 54 | + |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Tokens to ignore when trying to find the value for the directive. |
|
| 58 | + * |
|
| 59 | + * @var array |
|
| 60 | + */ |
|
| 61 | + protected $ignoreTokens = array(); |
|
| 62 | + |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Returns an array of tokens this test wants to listen for. |
|
| 66 | + * |
|
| 67 | + * @return array |
|
| 68 | + */ |
|
| 69 | + public function register() |
|
| 70 | + { |
|
| 71 | + $this->ignoreTokens = Tokens::$emptyTokens; |
|
| 72 | + $this->ignoreTokens[\T_EQUAL] = \T_EQUAL; |
|
| 73 | + |
|
| 74 | + return array(\T_DECLARE); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Processes this test, when one of its tokens is encountered. |
|
| 80 | + * |
|
| 81 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 82 | + * @param int $stackPtr The position of the current token in |
|
| 83 | + * the stack passed in $tokens. |
|
| 84 | + * |
|
| 85 | + * @return void |
|
| 86 | + */ |
|
| 87 | + public function process(File $phpcsFile, $stackPtr) |
|
| 88 | + { |
|
| 89 | + $tokens = $phpcsFile->getTokens(); |
|
| 90 | + |
|
| 91 | + if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === true) { |
|
| 92 | + $openParenthesis = $tokens[$stackPtr]['parenthesis_opener']; |
|
| 93 | + $closeParenthesis = $tokens[$stackPtr]['parenthesis_closer']; |
|
| 94 | + } else { |
|
| 95 | + if (version_compare(PHPCSHelper::getVersion(), '2.3.4', '>=')) { |
|
| 96 | + return; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + // Deal with PHPCS 2.3.0-2.3.3 which do not yet set the parenthesis properly for declare statements. |
|
| 100 | + $openParenthesis = $phpcsFile->findNext(\T_OPEN_PARENTHESIS, ($stackPtr + 1), null, false, null, true); |
|
| 101 | + if ($openParenthesis === false || isset($tokens[$openParenthesis]['parenthesis_closer']) === false) { |
|
| 102 | + return; |
|
| 103 | + } |
|
| 104 | + $closeParenthesis = $tokens[$openParenthesis]['parenthesis_closer']; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + $directivePtr = $phpcsFile->findNext(\T_STRING, ($openParenthesis + 1), $closeParenthesis, false); |
|
| 108 | + if ($directivePtr === false) { |
|
| 109 | + return; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + $directiveContent = $tokens[$directivePtr]['content']; |
|
| 113 | + |
|
| 114 | + if (isset($this->newDirectives[$directiveContent]) === false) { |
|
| 115 | + $error = 'Declare can only be used with the directives %s. Found: %s'; |
|
| 116 | + $data = array( |
|
| 117 | + implode(', ', array_keys($this->newDirectives)), |
|
| 118 | + $directiveContent, |
|
| 119 | + ); |
|
| 120 | + |
|
| 121 | + $phpcsFile->addError($error, $stackPtr, 'InvalidDirectiveFound', $data); |
|
| 122 | + |
|
| 123 | + } else { |
|
| 124 | + // Check for valid directive for version. |
|
| 125 | + $itemInfo = array( |
|
| 126 | + 'name' => $directiveContent, |
|
| 127 | + ); |
|
| 128 | + $this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
|
| 129 | + |
|
| 130 | + // Check for valid directive value. |
|
| 131 | + $valuePtr = $phpcsFile->findNext($this->ignoreTokens, $directivePtr + 1, $closeParenthesis, true); |
|
| 132 | + if ($valuePtr === false) { |
|
| 133 | + return; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + $this->addWarningOnInvalidValue($phpcsFile, $valuePtr, $directiveContent); |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Determine whether an error/warning should be thrown for an item based on collected information. |
|
| 143 | + * |
|
| 144 | + * @param array $errorInfo Detail information about an item. |
|
| 145 | + * |
|
| 146 | + * @return bool |
|
| 147 | + */ |
|
| 148 | + protected function shouldThrowError(array $errorInfo) |
|
| 149 | + { |
|
| 150 | + return ($errorInfo['not_in_version'] !== '' || $errorInfo['conditional_version'] !== ''); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Get the relevant sub-array for a specific item from a multi-dimensional array. |
|
| 156 | + * |
|
| 157 | + * @param array $itemInfo Base information about the item. |
|
| 158 | + * |
|
| 159 | + * @return array Version and other information about the item. |
|
| 160 | + */ |
|
| 161 | + public function getItemArray(array $itemInfo) |
|
| 162 | + { |
|
| 163 | + return $this->newDirectives[$itemInfo['name']]; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Get an array of the non-PHP-version array keys used in a sub-array. |
|
| 169 | + * |
|
| 170 | + * @return array |
|
| 171 | + */ |
|
| 172 | + protected function getNonVersionArrayKeys() |
|
| 173 | + { |
|
| 174 | + return array( |
|
| 175 | + 'valid_value_callback', |
|
| 176 | + 'valid_values', |
|
| 177 | + ); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Retrieve the relevant detail (version) information for use in an error message. |
|
| 183 | + * |
|
| 184 | + * @param array $itemArray Version and other information about the item. |
|
| 185 | + * @param array $itemInfo Base information about the item. |
|
| 186 | + * |
|
| 187 | + * @return array |
|
| 188 | + */ |
|
| 189 | + public function getErrorInfo(array $itemArray, array $itemInfo) |
|
| 190 | + { |
|
| 191 | + $errorInfo = parent::getErrorInfo($itemArray, $itemInfo); |
|
| 192 | + $errorInfo['conditional_version'] = ''; |
|
| 193 | + $errorInfo['condition'] = ''; |
|
| 194 | + |
|
| 195 | + $versionArray = $this->getVersionArray($itemArray); |
|
| 196 | + |
|
| 197 | + if (empty($versionArray) === false) { |
|
| 198 | + foreach ($versionArray as $version => $present) { |
|
| 199 | + if (\is_string($present) === true && $this->supportsBelow($version) === true) { |
|
| 200 | + // We cannot test for compilation option (ok, except by scraping the output of phpinfo...). |
|
| 201 | + $errorInfo['conditional_version'] = $version; |
|
| 202 | + $errorInfo['condition'] = $present; |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + return $errorInfo; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Get the error message template for this sniff. |
|
| 213 | + * |
|
| 214 | + * @return string |
|
| 215 | + */ |
|
| 216 | + protected function getErrorMsgTemplate() |
|
| 217 | + { |
|
| 218 | + return 'Directive ' . parent::getErrorMsgTemplate(); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Generates the error or warning for this item. |
|
| 224 | + * |
|
| 225 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 226 | + * @param int $stackPtr The position of the relevant token in |
|
| 227 | + * the stack. |
|
| 228 | + * @param array $itemInfo Base information about the item. |
|
| 229 | + * @param array $errorInfo Array with detail (version) information |
|
| 230 | + * relevant to the item. |
|
| 231 | + * |
|
| 232 | + * @return void |
|
| 233 | + */ |
|
| 234 | + public function addError(File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo) |
|
| 235 | + { |
|
| 236 | + if ($errorInfo['not_in_version'] !== '') { |
|
| 237 | + parent::addError($phpcsFile, $stackPtr, $itemInfo, $errorInfo); |
|
| 238 | + } elseif ($errorInfo['conditional_version'] !== '') { |
|
| 239 | + $error = 'Directive %s is present in PHP version %s but will be disregarded unless PHP is compiled with %s'; |
|
| 240 | + $errorCode = $this->stringToErrorCode($itemInfo['name']) . 'WithConditionFound'; |
|
| 241 | + $data = array( |
|
| 242 | + $itemInfo['name'], |
|
| 243 | + $errorInfo['conditional_version'], |
|
| 244 | + $errorInfo['condition'], |
|
| 245 | + ); |
|
| 246 | + |
|
| 247 | + $phpcsFile->addWarning($error, $stackPtr, $errorCode, $data); |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Generates a error or warning for this sniff. |
|
| 254 | + * |
|
| 255 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 256 | + * @param int $stackPtr The position of the execution directive value |
|
| 257 | + * in the token array. |
|
| 258 | + * @param string $directive The directive. |
|
| 259 | + * |
|
| 260 | + * @return void |
|
| 261 | + */ |
|
| 262 | + protected function addWarningOnInvalidValue(File $phpcsFile, $stackPtr, $directive) |
|
| 263 | + { |
|
| 264 | + $tokens = $phpcsFile->getTokens(); |
|
| 265 | + |
|
| 266 | + $value = $tokens[$stackPtr]['content']; |
|
| 267 | + if (isset(Tokens::$stringTokens[$tokens[$stackPtr]['code']]) === true) { |
|
| 268 | + $value = $this->stripQuotes($value); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + $isError = false; |
|
| 272 | + if (isset($this->newDirectives[$directive]['valid_values'])) { |
|
| 273 | + if (\in_array($value, $this->newDirectives[$directive]['valid_values']) === false) { |
|
| 274 | + $isError = true; |
|
| 275 | + } |
|
| 276 | + } elseif (isset($this->newDirectives[$directive]['valid_value_callback'])) { |
|
| 277 | + $valid = \call_user_func(array($this, $this->newDirectives[$directive]['valid_value_callback']), $value); |
|
| 278 | + if ($valid === false) { |
|
| 279 | + $isError = true; |
|
| 280 | + } |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + if ($isError === true) { |
|
| 284 | + $error = 'The execution directive %s does not seem to have a valid value. Please review. Found: %s'; |
|
| 285 | + $errorCode = $this->stringToErrorCode($directive) . 'InvalidValueFound'; |
|
| 286 | + $data = array( |
|
| 287 | + $directive, |
|
| 288 | + $value, |
|
| 289 | + ); |
|
| 290 | + |
|
| 291 | + $phpcsFile->addWarning($error, $stackPtr, $errorCode, $data); |
|
| 292 | + } |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Check whether a value is numeric. |
|
| 298 | + * |
|
| 299 | + * Callback function to test whether the value for an execution directive is valid. |
|
| 300 | + * |
|
| 301 | + * @param mixed $value The value to test. |
|
| 302 | + * |
|
| 303 | + * @return bool |
|
| 304 | + */ |
|
| 305 | + protected function isNumeric($value) |
|
| 306 | + { |
|
| 307 | + return is_numeric($value); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * Check whether a value is a valid encoding. |
|
| 313 | + * |
|
| 314 | + * Callback function to test whether the value for an execution directive is valid. |
|
| 315 | + * |
|
| 316 | + * @param mixed $value The value to test. |
|
| 317 | + * |
|
| 318 | + * @return bool |
|
| 319 | + */ |
|
| 320 | + protected function validEncoding($value) |
|
| 321 | + { |
|
| 322 | + static $encodings; |
|
| 323 | + if (isset($encodings) === false && function_exists('mb_list_encodings')) { |
|
| 324 | + $encodings = mb_list_encodings(); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + if (empty($encodings) || \is_array($encodings) === false) { |
|
| 328 | + // If we can't test the encoding, let it pass through. |
|
| 329 | + return true; |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + return \in_array($value, $encodings, true); |
|
| 333 | + } |
|
| 334 | 334 | } |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | 'strict_types' => array( |
| 49 | 49 | '5.6' => false, |
| 50 | 50 | '7.0' => true, |
| 51 | - 'valid_values' => array(1), |
|
| 51 | + 'valid_values' => array( 1 ), |
|
| 52 | 52 | ), |
| 53 | 53 | ); |
| 54 | 54 | |
@@ -69,9 +69,9 @@ discard block |
||
| 69 | 69 | public function register() |
| 70 | 70 | { |
| 71 | 71 | $this->ignoreTokens = Tokens::$emptyTokens; |
| 72 | - $this->ignoreTokens[\T_EQUAL] = \T_EQUAL; |
|
| 72 | + $this->ignoreTokens[ \T_EQUAL ] = \T_EQUAL; |
|
| 73 | 73 | |
| 74 | - return array(\T_DECLARE); |
|
| 74 | + return array( \T_DECLARE ); |
|
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | |
@@ -84,56 +84,56 @@ discard block |
||
| 84 | 84 | * |
| 85 | 85 | * @return void |
| 86 | 86 | */ |
| 87 | - public function process(File $phpcsFile, $stackPtr) |
|
| 87 | + public function process( File $phpcsFile, $stackPtr ) |
|
| 88 | 88 | { |
| 89 | 89 | $tokens = $phpcsFile->getTokens(); |
| 90 | 90 | |
| 91 | - if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === true) { |
|
| 92 | - $openParenthesis = $tokens[$stackPtr]['parenthesis_opener']; |
|
| 93 | - $closeParenthesis = $tokens[$stackPtr]['parenthesis_closer']; |
|
| 91 | + if ( isset( $tokens[ $stackPtr ][ 'parenthesis_opener' ], $tokens[ $stackPtr ][ 'parenthesis_closer' ] ) === true ) { |
|
| 92 | + $openParenthesis = $tokens[ $stackPtr ][ 'parenthesis_opener' ]; |
|
| 93 | + $closeParenthesis = $tokens[ $stackPtr ][ 'parenthesis_closer' ]; |
|
| 94 | 94 | } else { |
| 95 | - if (version_compare(PHPCSHelper::getVersion(), '2.3.4', '>=')) { |
|
| 95 | + if ( version_compare( PHPCSHelper::getVersion(), '2.3.4', '>=' ) ) { |
|
| 96 | 96 | return; |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | // Deal with PHPCS 2.3.0-2.3.3 which do not yet set the parenthesis properly for declare statements. |
| 100 | - $openParenthesis = $phpcsFile->findNext(\T_OPEN_PARENTHESIS, ($stackPtr + 1), null, false, null, true); |
|
| 101 | - if ($openParenthesis === false || isset($tokens[$openParenthesis]['parenthesis_closer']) === false) { |
|
| 100 | + $openParenthesis = $phpcsFile->findNext( \T_OPEN_PARENTHESIS, ( $stackPtr + 1 ), null, false, null, true ); |
|
| 101 | + if ( $openParenthesis === false || isset( $tokens[ $openParenthesis ][ 'parenthesis_closer' ] ) === false ) { |
|
| 102 | 102 | return; |
| 103 | 103 | } |
| 104 | - $closeParenthesis = $tokens[$openParenthesis]['parenthesis_closer']; |
|
| 104 | + $closeParenthesis = $tokens[ $openParenthesis ][ 'parenthesis_closer' ]; |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | - $directivePtr = $phpcsFile->findNext(\T_STRING, ($openParenthesis + 1), $closeParenthesis, false); |
|
| 108 | - if ($directivePtr === false) { |
|
| 107 | + $directivePtr = $phpcsFile->findNext( \T_STRING, ( $openParenthesis + 1 ), $closeParenthesis, false ); |
|
| 108 | + if ( $directivePtr === false ) { |
|
| 109 | 109 | return; |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | - $directiveContent = $tokens[$directivePtr]['content']; |
|
| 112 | + $directiveContent = $tokens[ $directivePtr ][ 'content' ]; |
|
| 113 | 113 | |
| 114 | - if (isset($this->newDirectives[$directiveContent]) === false) { |
|
| 114 | + if ( isset( $this->newDirectives[ $directiveContent ] ) === false ) { |
|
| 115 | 115 | $error = 'Declare can only be used with the directives %s. Found: %s'; |
| 116 | 116 | $data = array( |
| 117 | - implode(', ', array_keys($this->newDirectives)), |
|
| 117 | + implode( ', ', array_keys( $this->newDirectives ) ), |
|
| 118 | 118 | $directiveContent, |
| 119 | 119 | ); |
| 120 | 120 | |
| 121 | - $phpcsFile->addError($error, $stackPtr, 'InvalidDirectiveFound', $data); |
|
| 121 | + $phpcsFile->addError( $error, $stackPtr, 'InvalidDirectiveFound', $data ); |
|
| 122 | 122 | |
| 123 | 123 | } else { |
| 124 | 124 | // Check for valid directive for version. |
| 125 | 125 | $itemInfo = array( |
| 126 | 126 | 'name' => $directiveContent, |
| 127 | 127 | ); |
| 128 | - $this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
|
| 128 | + $this->handleFeature( $phpcsFile, $stackPtr, $itemInfo ); |
|
| 129 | 129 | |
| 130 | 130 | // Check for valid directive value. |
| 131 | - $valuePtr = $phpcsFile->findNext($this->ignoreTokens, $directivePtr + 1, $closeParenthesis, true); |
|
| 132 | - if ($valuePtr === false) { |
|
| 131 | + $valuePtr = $phpcsFile->findNext( $this->ignoreTokens, $directivePtr + 1, $closeParenthesis, true ); |
|
| 132 | + if ( $valuePtr === false ) { |
|
| 133 | 133 | return; |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | - $this->addWarningOnInvalidValue($phpcsFile, $valuePtr, $directiveContent); |
|
| 136 | + $this->addWarningOnInvalidValue( $phpcsFile, $valuePtr, $directiveContent ); |
|
| 137 | 137 | } |
| 138 | 138 | } |
| 139 | 139 | |
@@ -145,9 +145,9 @@ discard block |
||
| 145 | 145 | * |
| 146 | 146 | * @return bool |
| 147 | 147 | */ |
| 148 | - protected function shouldThrowError(array $errorInfo) |
|
| 148 | + protected function shouldThrowError( array $errorInfo ) |
|
| 149 | 149 | { |
| 150 | - return ($errorInfo['not_in_version'] !== '' || $errorInfo['conditional_version'] !== ''); |
|
| 150 | + return ( $errorInfo[ 'not_in_version' ] !== '' || $errorInfo[ 'conditional_version' ] !== '' ); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | |
@@ -158,9 +158,9 @@ discard block |
||
| 158 | 158 | * |
| 159 | 159 | * @return array Version and other information about the item. |
| 160 | 160 | */ |
| 161 | - public function getItemArray(array $itemInfo) |
|
| 161 | + public function getItemArray( array $itemInfo ) |
|
| 162 | 162 | { |
| 163 | - return $this->newDirectives[$itemInfo['name']]; |
|
| 163 | + return $this->newDirectives[ $itemInfo[ 'name' ] ]; |
|
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | |
@@ -186,20 +186,20 @@ discard block |
||
| 186 | 186 | * |
| 187 | 187 | * @return array |
| 188 | 188 | */ |
| 189 | - public function getErrorInfo(array $itemArray, array $itemInfo) |
|
| 189 | + public function getErrorInfo( array $itemArray, array $itemInfo ) |
|
| 190 | 190 | { |
| 191 | - $errorInfo = parent::getErrorInfo($itemArray, $itemInfo); |
|
| 192 | - $errorInfo['conditional_version'] = ''; |
|
| 193 | - $errorInfo['condition'] = ''; |
|
| 191 | + $errorInfo = parent::getErrorInfo( $itemArray, $itemInfo ); |
|
| 192 | + $errorInfo[ 'conditional_version' ] = ''; |
|
| 193 | + $errorInfo[ 'condition' ] = ''; |
|
| 194 | 194 | |
| 195 | - $versionArray = $this->getVersionArray($itemArray); |
|
| 195 | + $versionArray = $this->getVersionArray( $itemArray ); |
|
| 196 | 196 | |
| 197 | - if (empty($versionArray) === false) { |
|
| 198 | - foreach ($versionArray as $version => $present) { |
|
| 199 | - if (\is_string($present) === true && $this->supportsBelow($version) === true) { |
|
| 197 | + if ( empty( $versionArray ) === false ) { |
|
| 198 | + foreach ( $versionArray as $version => $present ) { |
|
| 199 | + if ( \is_string( $present ) === true && $this->supportsBelow( $version ) === true ) { |
|
| 200 | 200 | // We cannot test for compilation option (ok, except by scraping the output of phpinfo...). |
| 201 | - $errorInfo['conditional_version'] = $version; |
|
| 202 | - $errorInfo['condition'] = $present; |
|
| 201 | + $errorInfo[ 'conditional_version' ] = $version; |
|
| 202 | + $errorInfo[ 'condition' ] = $present; |
|
| 203 | 203 | } |
| 204 | 204 | } |
| 205 | 205 | } |
@@ -231,20 +231,20 @@ discard block |
||
| 231 | 231 | * |
| 232 | 232 | * @return void |
| 233 | 233 | */ |
| 234 | - public function addError(File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo) |
|
| 234 | + public function addError( File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo ) |
|
| 235 | 235 | { |
| 236 | - if ($errorInfo['not_in_version'] !== '') { |
|
| 237 | - parent::addError($phpcsFile, $stackPtr, $itemInfo, $errorInfo); |
|
| 238 | - } elseif ($errorInfo['conditional_version'] !== '') { |
|
| 236 | + if ( $errorInfo[ 'not_in_version' ] !== '' ) { |
|
| 237 | + parent::addError( $phpcsFile, $stackPtr, $itemInfo, $errorInfo ); |
|
| 238 | + } elseif ( $errorInfo[ 'conditional_version' ] !== '' ) { |
|
| 239 | 239 | $error = 'Directive %s is present in PHP version %s but will be disregarded unless PHP is compiled with %s'; |
| 240 | - $errorCode = $this->stringToErrorCode($itemInfo['name']) . 'WithConditionFound'; |
|
| 240 | + $errorCode = $this->stringToErrorCode( $itemInfo[ 'name' ] ) . 'WithConditionFound'; |
|
| 241 | 241 | $data = array( |
| 242 | - $itemInfo['name'], |
|
| 243 | - $errorInfo['conditional_version'], |
|
| 244 | - $errorInfo['condition'], |
|
| 242 | + $itemInfo[ 'name' ], |
|
| 243 | + $errorInfo[ 'conditional_version' ], |
|
| 244 | + $errorInfo[ 'condition' ], |
|
| 245 | 245 | ); |
| 246 | 246 | |
| 247 | - $phpcsFile->addWarning($error, $stackPtr, $errorCode, $data); |
|
| 247 | + $phpcsFile->addWarning( $error, $stackPtr, $errorCode, $data ); |
|
| 248 | 248 | } |
| 249 | 249 | } |
| 250 | 250 | |
@@ -259,36 +259,36 @@ discard block |
||
| 259 | 259 | * |
| 260 | 260 | * @return void |
| 261 | 261 | */ |
| 262 | - protected function addWarningOnInvalidValue(File $phpcsFile, $stackPtr, $directive) |
|
| 262 | + protected function addWarningOnInvalidValue( File $phpcsFile, $stackPtr, $directive ) |
|
| 263 | 263 | { |
| 264 | 264 | $tokens = $phpcsFile->getTokens(); |
| 265 | 265 | |
| 266 | - $value = $tokens[$stackPtr]['content']; |
|
| 267 | - if (isset(Tokens::$stringTokens[$tokens[$stackPtr]['code']]) === true) { |
|
| 268 | - $value = $this->stripQuotes($value); |
|
| 266 | + $value = $tokens[ $stackPtr ][ 'content' ]; |
|
| 267 | + if ( isset( Tokens::$stringTokens[ $tokens[ $stackPtr ][ 'code' ] ] ) === true ) { |
|
| 268 | + $value = $this->stripQuotes( $value ); |
|
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | $isError = false; |
| 272 | - if (isset($this->newDirectives[$directive]['valid_values'])) { |
|
| 273 | - if (\in_array($value, $this->newDirectives[$directive]['valid_values']) === false) { |
|
| 272 | + if ( isset( $this->newDirectives[ $directive ][ 'valid_values' ] ) ) { |
|
| 273 | + if ( \in_array( $value, $this->newDirectives[ $directive ][ 'valid_values' ] ) === false ) { |
|
| 274 | 274 | $isError = true; |
| 275 | 275 | } |
| 276 | - } elseif (isset($this->newDirectives[$directive]['valid_value_callback'])) { |
|
| 277 | - $valid = \call_user_func(array($this, $this->newDirectives[$directive]['valid_value_callback']), $value); |
|
| 278 | - if ($valid === false) { |
|
| 276 | + } elseif ( isset( $this->newDirectives[ $directive ][ 'valid_value_callback' ] ) ) { |
|
| 277 | + $valid = \call_user_func( array( $this, $this->newDirectives[ $directive ][ 'valid_value_callback' ] ), $value ); |
|
| 278 | + if ( $valid === false ) { |
|
| 279 | 279 | $isError = true; |
| 280 | 280 | } |
| 281 | 281 | } |
| 282 | 282 | |
| 283 | - if ($isError === true) { |
|
| 283 | + if ( $isError === true ) { |
|
| 284 | 284 | $error = 'The execution directive %s does not seem to have a valid value. Please review. Found: %s'; |
| 285 | - $errorCode = $this->stringToErrorCode($directive) . 'InvalidValueFound'; |
|
| 285 | + $errorCode = $this->stringToErrorCode( $directive ) . 'InvalidValueFound'; |
|
| 286 | 286 | $data = array( |
| 287 | 287 | $directive, |
| 288 | 288 | $value, |
| 289 | 289 | ); |
| 290 | 290 | |
| 291 | - $phpcsFile->addWarning($error, $stackPtr, $errorCode, $data); |
|
| 291 | + $phpcsFile->addWarning( $error, $stackPtr, $errorCode, $data ); |
|
| 292 | 292 | } |
| 293 | 293 | } |
| 294 | 294 | |
@@ -302,9 +302,9 @@ discard block |
||
| 302 | 302 | * |
| 303 | 303 | * @return bool |
| 304 | 304 | */ |
| 305 | - protected function isNumeric($value) |
|
| 305 | + protected function isNumeric( $value ) |
|
| 306 | 306 | { |
| 307 | - return is_numeric($value); |
|
| 307 | + return is_numeric( $value ); |
|
| 308 | 308 | } |
| 309 | 309 | |
| 310 | 310 | |
@@ -317,18 +317,18 @@ discard block |
||
| 317 | 317 | * |
| 318 | 318 | * @return bool |
| 319 | 319 | */ |
| 320 | - protected function validEncoding($value) |
|
| 320 | + protected function validEncoding( $value ) |
|
| 321 | 321 | { |
| 322 | 322 | static $encodings; |
| 323 | - if (isset($encodings) === false && function_exists('mb_list_encodings')) { |
|
| 323 | + if ( isset( $encodings ) === false && function_exists( 'mb_list_encodings' ) ) { |
|
| 324 | 324 | $encodings = mb_list_encodings(); |
| 325 | 325 | } |
| 326 | 326 | |
| 327 | - if (empty($encodings) || \is_array($encodings) === false) { |
|
| 327 | + if ( empty( $encodings ) || \is_array( $encodings ) === false ) { |
|
| 328 | 328 | // If we can't test the encoding, let it pass through. |
| 329 | 329 | return true; |
| 330 | 330 | } |
| 331 | 331 | |
| 332 | - return \in_array($value, $encodings, true); |
|
| 332 | + return \in_array( $value, $encodings, true ); |
|
| 333 | 333 | } |
| 334 | 334 | } |
@@ -21,8 +21,7 @@ discard block |
||
| 21 | 21 | * @package PHPCompatibility |
| 22 | 22 | * @author Juliette Reinders Folmer <[email protected]> |
| 23 | 23 | */ |
| 24 | -class NewExecutionDirectivesSniff extends AbstractNewFeatureSniff |
|
| 25 | -{ |
|
| 24 | +class NewExecutionDirectivesSniff extends AbstractNewFeatureSniff { |
|
| 26 | 25 | |
| 27 | 26 | /** |
| 28 | 27 | * A list of new execution directives |
@@ -66,8 +65,7 @@ discard block |
||
| 66 | 65 | * |
| 67 | 66 | * @return array |
| 68 | 67 | */ |
| 69 | - public function register() |
|
| 70 | - { |
|
| 68 | + public function register() { |
|
| 71 | 69 | $this->ignoreTokens = Tokens::$emptyTokens; |
| 72 | 70 | $this->ignoreTokens[\T_EQUAL] = \T_EQUAL; |
| 73 | 71 | |
@@ -84,8 +82,7 @@ discard block |
||
| 84 | 82 | * |
| 85 | 83 | * @return void |
| 86 | 84 | */ |
| 87 | - public function process(File $phpcsFile, $stackPtr) |
|
| 88 | - { |
|
| 85 | + public function process(File $phpcsFile, $stackPtr) { |
|
| 89 | 86 | $tokens = $phpcsFile->getTokens(); |
| 90 | 87 | |
| 91 | 88 | if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === true) { |
@@ -145,8 +142,7 @@ discard block |
||
| 145 | 142 | * |
| 146 | 143 | * @return bool |
| 147 | 144 | */ |
| 148 | - protected function shouldThrowError(array $errorInfo) |
|
| 149 | - { |
|
| 145 | + protected function shouldThrowError(array $errorInfo) { |
|
| 150 | 146 | return ($errorInfo['not_in_version'] !== '' || $errorInfo['conditional_version'] !== ''); |
| 151 | 147 | } |
| 152 | 148 | |
@@ -158,8 +154,7 @@ discard block |
||
| 158 | 154 | * |
| 159 | 155 | * @return array Version and other information about the item. |
| 160 | 156 | */ |
| 161 | - public function getItemArray(array $itemInfo) |
|
| 162 | - { |
|
| 157 | + public function getItemArray(array $itemInfo) { |
|
| 163 | 158 | return $this->newDirectives[$itemInfo['name']]; |
| 164 | 159 | } |
| 165 | 160 | |
@@ -169,8 +164,7 @@ discard block |
||
| 169 | 164 | * |
| 170 | 165 | * @return array |
| 171 | 166 | */ |
| 172 | - protected function getNonVersionArrayKeys() |
|
| 173 | - { |
|
| 167 | + protected function getNonVersionArrayKeys() { |
|
| 174 | 168 | return array( |
| 175 | 169 | 'valid_value_callback', |
| 176 | 170 | 'valid_values', |
@@ -186,8 +180,7 @@ discard block |
||
| 186 | 180 | * |
| 187 | 181 | * @return array |
| 188 | 182 | */ |
| 189 | - public function getErrorInfo(array $itemArray, array $itemInfo) |
|
| 190 | - { |
|
| 183 | + public function getErrorInfo(array $itemArray, array $itemInfo) { |
|
| 191 | 184 | $errorInfo = parent::getErrorInfo($itemArray, $itemInfo); |
| 192 | 185 | $errorInfo['conditional_version'] = ''; |
| 193 | 186 | $errorInfo['condition'] = ''; |
@@ -213,8 +206,7 @@ discard block |
||
| 213 | 206 | * |
| 214 | 207 | * @return string |
| 215 | 208 | */ |
| 216 | - protected function getErrorMsgTemplate() |
|
| 217 | - { |
|
| 209 | + protected function getErrorMsgTemplate() { |
|
| 218 | 210 | return 'Directive ' . parent::getErrorMsgTemplate(); |
| 219 | 211 | } |
| 220 | 212 | |
@@ -231,8 +223,7 @@ discard block |
||
| 231 | 223 | * |
| 232 | 224 | * @return void |
| 233 | 225 | */ |
| 234 | - public function addError(File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo) |
|
| 235 | - { |
|
| 226 | + public function addError(File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo) { |
|
| 236 | 227 | if ($errorInfo['not_in_version'] !== '') { |
| 237 | 228 | parent::addError($phpcsFile, $stackPtr, $itemInfo, $errorInfo); |
| 238 | 229 | } elseif ($errorInfo['conditional_version'] !== '') { |
@@ -259,8 +250,7 @@ discard block |
||
| 259 | 250 | * |
| 260 | 251 | * @return void |
| 261 | 252 | */ |
| 262 | - protected function addWarningOnInvalidValue(File $phpcsFile, $stackPtr, $directive) |
|
| 263 | - { |
|
| 253 | + protected function addWarningOnInvalidValue(File $phpcsFile, $stackPtr, $directive) { |
|
| 264 | 254 | $tokens = $phpcsFile->getTokens(); |
| 265 | 255 | |
| 266 | 256 | $value = $tokens[$stackPtr]['content']; |
@@ -302,8 +292,7 @@ discard block |
||
| 302 | 292 | * |
| 303 | 293 | * @return bool |
| 304 | 294 | */ |
| 305 | - protected function isNumeric($value) |
|
| 306 | - { |
|
| 295 | + protected function isNumeric($value) { |
|
| 307 | 296 | return is_numeric($value); |
| 308 | 297 | } |
| 309 | 298 | |
@@ -317,8 +306,7 @@ discard block |
||
| 317 | 306 | * |
| 318 | 307 | * @return bool |
| 319 | 308 | */ |
| 320 | - protected function validEncoding($value) |
|
| 321 | - { |
|
| 309 | + protected function validEncoding($value) { |
|
| 322 | 310 | static $encodings; |
| 323 | 311 | if (isset($encodings) === false && function_exists('mb_list_encodings')) { |
| 324 | 312 | $encodings = mb_list_encodings(); |
@@ -31,7 +31,7 @@ |
||
| 31 | 31 | /** |
| 32 | 32 | * Returns an array of tokens this test wants to listen for. |
| 33 | 33 | * |
| 34 | - * @return array |
|
| 34 | + * @return string[] |
|
| 35 | 35 | */ |
| 36 | 36 | public function register() |
| 37 | 37 | { |
@@ -28,210 +28,210 @@ |
||
| 28 | 28 | */ |
| 29 | 29 | class NewClosureSniff extends Sniff |
| 30 | 30 | { |
| 31 | - /** |
|
| 32 | - * Returns an array of tokens this test wants to listen for. |
|
| 33 | - * |
|
| 34 | - * @return array |
|
| 35 | - */ |
|
| 36 | - public function register() |
|
| 37 | - { |
|
| 38 | - return array(\T_CLOSURE); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Processes this test, when one of its tokens is encountered. |
|
| 43 | - * |
|
| 44 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 45 | - * @param int $stackPtr The position of the current token |
|
| 46 | - * in the stack passed in $tokens. |
|
| 47 | - * |
|
| 48 | - * @return void |
|
| 49 | - */ |
|
| 50 | - public function process(File $phpcsFile, $stackPtr) |
|
| 51 | - { |
|
| 52 | - if ($this->supportsBelow('5.2')) { |
|
| 53 | - $phpcsFile->addError( |
|
| 54 | - 'Closures / anonymous functions are not available in PHP 5.2 or earlier', |
|
| 55 | - $stackPtr, |
|
| 56 | - 'Found' |
|
| 57 | - ); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /* |
|
| 31 | + /** |
|
| 32 | + * Returns an array of tokens this test wants to listen for. |
|
| 33 | + * |
|
| 34 | + * @return array |
|
| 35 | + */ |
|
| 36 | + public function register() |
|
| 37 | + { |
|
| 38 | + return array(\T_CLOSURE); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Processes this test, when one of its tokens is encountered. |
|
| 43 | + * |
|
| 44 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 45 | + * @param int $stackPtr The position of the current token |
|
| 46 | + * in the stack passed in $tokens. |
|
| 47 | + * |
|
| 48 | + * @return void |
|
| 49 | + */ |
|
| 50 | + public function process(File $phpcsFile, $stackPtr) |
|
| 51 | + { |
|
| 52 | + if ($this->supportsBelow('5.2')) { |
|
| 53 | + $phpcsFile->addError( |
|
| 54 | + 'Closures / anonymous functions are not available in PHP 5.2 or earlier', |
|
| 55 | + $stackPtr, |
|
| 56 | + 'Found' |
|
| 57 | + ); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /* |
|
| 61 | 61 | * Closures can only be declared as static since PHP 5.4. |
| 62 | 62 | */ |
| 63 | - $isStatic = $this->isClosureStatic($phpcsFile, $stackPtr); |
|
| 64 | - if ($this->supportsBelow('5.3') && $isStatic === true) { |
|
| 65 | - $phpcsFile->addError( |
|
| 66 | - 'Closures / anonymous functions could not be declared as static in PHP 5.3 or earlier', |
|
| 67 | - $stackPtr, |
|
| 68 | - 'StaticFound' |
|
| 69 | - ); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - $tokens = $phpcsFile->getTokens(); |
|
| 73 | - |
|
| 74 | - if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) { |
|
| 75 | - // Live coding or parse error. |
|
| 76 | - return; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - $scopeStart = ($tokens[$stackPtr]['scope_opener'] + 1); |
|
| 80 | - $scopeEnd = $tokens[$stackPtr]['scope_closer']; |
|
| 81 | - $usesThis = $this->findThisUsageInClosure($phpcsFile, $scopeStart, $scopeEnd); |
|
| 82 | - |
|
| 83 | - if ($this->supportsBelow('5.3')) { |
|
| 84 | - /* |
|
| 63 | + $isStatic = $this->isClosureStatic($phpcsFile, $stackPtr); |
|
| 64 | + if ($this->supportsBelow('5.3') && $isStatic === true) { |
|
| 65 | + $phpcsFile->addError( |
|
| 66 | + 'Closures / anonymous functions could not be declared as static in PHP 5.3 or earlier', |
|
| 67 | + $stackPtr, |
|
| 68 | + 'StaticFound' |
|
| 69 | + ); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + $tokens = $phpcsFile->getTokens(); |
|
| 73 | + |
|
| 74 | + if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) { |
|
| 75 | + // Live coding or parse error. |
|
| 76 | + return; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + $scopeStart = ($tokens[$stackPtr]['scope_opener'] + 1); |
|
| 80 | + $scopeEnd = $tokens[$stackPtr]['scope_closer']; |
|
| 81 | + $usesThis = $this->findThisUsageInClosure($phpcsFile, $scopeStart, $scopeEnd); |
|
| 82 | + |
|
| 83 | + if ($this->supportsBelow('5.3')) { |
|
| 84 | + /* |
|
| 85 | 85 | * Closures declared within classes only have access to $this since PHP 5.4. |
| 86 | 86 | */ |
| 87 | - if ($usesThis !== false) { |
|
| 88 | - $thisFound = $usesThis; |
|
| 89 | - do { |
|
| 90 | - $phpcsFile->addError( |
|
| 91 | - 'Closures / anonymous functions did not have access to $this in PHP 5.3 or earlier', |
|
| 92 | - $thisFound, |
|
| 93 | - 'ThisFound' |
|
| 94 | - ); |
|
| 87 | + if ($usesThis !== false) { |
|
| 88 | + $thisFound = $usesThis; |
|
| 89 | + do { |
|
| 90 | + $phpcsFile->addError( |
|
| 91 | + 'Closures / anonymous functions did not have access to $this in PHP 5.3 or earlier', |
|
| 92 | + $thisFound, |
|
| 93 | + 'ThisFound' |
|
| 94 | + ); |
|
| 95 | 95 | |
| 96 | - $thisFound = $this->findThisUsageInClosure($phpcsFile, ($thisFound + 1), $scopeEnd); |
|
| 96 | + $thisFound = $this->findThisUsageInClosure($phpcsFile, ($thisFound + 1), $scopeEnd); |
|
| 97 | 97 | |
| 98 | - } while ($thisFound !== false); |
|
| 99 | - } |
|
| 98 | + } while ($thisFound !== false); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /* |
|
| 101 | + /* |
|
| 102 | 102 | * Closures declared within classes only have access to self/parent/static since PHP 5.4. |
| 103 | 103 | */ |
| 104 | - $usesClassRef = $this->findClassRefUsageInClosure($phpcsFile, $scopeStart, $scopeEnd); |
|
| 104 | + $usesClassRef = $this->findClassRefUsageInClosure($phpcsFile, $scopeStart, $scopeEnd); |
|
| 105 | 105 | |
| 106 | - if ($usesClassRef !== false) { |
|
| 107 | - do { |
|
| 108 | - $phpcsFile->addError( |
|
| 109 | - 'Closures / anonymous functions could not use "%s::" in PHP 5.3 or earlier', |
|
| 110 | - $usesClassRef, |
|
| 111 | - 'ClassRefFound', |
|
| 112 | - array(strtolower($tokens[$usesClassRef]['content'])) |
|
| 113 | - ); |
|
| 106 | + if ($usesClassRef !== false) { |
|
| 107 | + do { |
|
| 108 | + $phpcsFile->addError( |
|
| 109 | + 'Closures / anonymous functions could not use "%s::" in PHP 5.3 or earlier', |
|
| 110 | + $usesClassRef, |
|
| 111 | + 'ClassRefFound', |
|
| 112 | + array(strtolower($tokens[$usesClassRef]['content'])) |
|
| 113 | + ); |
|
| 114 | 114 | |
| 115 | - $usesClassRef = $this->findClassRefUsageInClosure($phpcsFile, ($usesClassRef + 1), $scopeEnd); |
|
| 115 | + $usesClassRef = $this->findClassRefUsageInClosure($phpcsFile, ($usesClassRef + 1), $scopeEnd); |
|
| 116 | 116 | |
| 117 | - } while ($usesClassRef !== false); |
|
| 118 | - } |
|
| 119 | - } |
|
| 117 | + } while ($usesClassRef !== false); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - /* |
|
| 121 | + /* |
|
| 122 | 122 | * Check for correct usage. |
| 123 | 123 | */ |
| 124 | - if ($this->supportsAbove('5.4') && $usesThis !== false) { |
|
| 124 | + if ($this->supportsAbove('5.4') && $usesThis !== false) { |
|
| 125 | 125 | |
| 126 | - $thisFound = $usesThis; |
|
| 126 | + $thisFound = $usesThis; |
|
| 127 | 127 | |
| 128 | - do { |
|
| 129 | - /* |
|
| 128 | + do { |
|
| 129 | + /* |
|
| 130 | 130 | * Closures only have access to $this if not declared as static. |
| 131 | 131 | */ |
| 132 | - if ($isStatic === true) { |
|
| 133 | - $phpcsFile->addError( |
|
| 134 | - 'Closures / anonymous functions declared as static do not have access to $this', |
|
| 135 | - $thisFound, |
|
| 136 | - 'ThisFoundInStatic' |
|
| 137 | - ); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /* |
|
| 132 | + if ($isStatic === true) { |
|
| 133 | + $phpcsFile->addError( |
|
| 134 | + 'Closures / anonymous functions declared as static do not have access to $this', |
|
| 135 | + $thisFound, |
|
| 136 | + 'ThisFoundInStatic' |
|
| 137 | + ); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /* |
|
| 141 | 141 | * Closures only have access to $this if used within a class context. |
| 142 | 142 | */ |
| 143 | - elseif ($this->inClassScope($phpcsFile, $stackPtr, false) === false) { |
|
| 144 | - $phpcsFile->addWarning( |
|
| 145 | - 'Closures / anonymous functions only have access to $this if used within a class or when bound to an object using bindTo(). Please verify.', |
|
| 146 | - $thisFound, |
|
| 147 | - 'ThisFoundOutsideClass' |
|
| 148 | - ); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - $thisFound = $this->findThisUsageInClosure($phpcsFile, ($thisFound + 1), $scopeEnd); |
|
| 152 | - |
|
| 153 | - } while ($thisFound !== false); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - // Prevent double reporting for nested closures. |
|
| 157 | - return $scopeEnd; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Check whether the closure is declared as static. |
|
| 163 | - * |
|
| 164 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 165 | - * @param int $stackPtr The position of the current token |
|
| 166 | - * in the stack passed in $tokens. |
|
| 167 | - * |
|
| 168 | - * @return bool |
|
| 169 | - */ |
|
| 170 | - protected function isClosureStatic(File $phpcsFile, $stackPtr) |
|
| 171 | - { |
|
| 172 | - $tokens = $phpcsFile->getTokens(); |
|
| 173 | - $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true); |
|
| 174 | - |
|
| 175 | - return ($prevToken !== false && $tokens[$prevToken]['code'] === \T_STATIC); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Check if the code within a closure uses the $this variable. |
|
| 181 | - * |
|
| 182 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 183 | - * @param int $startToken The position within the closure to continue searching from. |
|
| 184 | - * @param int $endToken The closure scope closer to stop searching at. |
|
| 185 | - * |
|
| 186 | - * @return int|false The stackPtr to the first $this usage if found or false if |
|
| 187 | - * $this is not used. |
|
| 188 | - */ |
|
| 189 | - protected function findThisUsageInClosure(File $phpcsFile, $startToken, $endToken) |
|
| 190 | - { |
|
| 191 | - // Make sure the $startToken is valid. |
|
| 192 | - if ($startToken >= $endToken) { |
|
| 193 | - return false; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - return $phpcsFile->findNext( |
|
| 197 | - \T_VARIABLE, |
|
| 198 | - $startToken, |
|
| 199 | - $endToken, |
|
| 200 | - false, |
|
| 201 | - '$this' |
|
| 202 | - ); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Check if the code within a closure uses "self/parent/static". |
|
| 207 | - * |
|
| 208 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 209 | - * @param int $startToken The position within the closure to continue searching from. |
|
| 210 | - * @param int $endToken The closure scope closer to stop searching at. |
|
| 211 | - * |
|
| 212 | - * @return int|false The stackPtr to the first classRef usage if found or false if |
|
| 213 | - * they are not used. |
|
| 214 | - */ |
|
| 215 | - protected function findClassRefUsageInClosure(File $phpcsFile, $startToken, $endToken) |
|
| 216 | - { |
|
| 217 | - // Make sure the $startToken is valid. |
|
| 218 | - if ($startToken >= $endToken) { |
|
| 219 | - return false; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - $tokens = $phpcsFile->getTokens(); |
|
| 223 | - $classRef = $phpcsFile->findNext(array(\T_SELF, \T_PARENT, \T_STATIC), $startToken, $endToken); |
|
| 224 | - |
|
| 225 | - if ($classRef === false || $tokens[$classRef]['code'] !== \T_STATIC) { |
|
| 226 | - return $classRef; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - // T_STATIC, make sure it is used as a class reference. |
|
| 230 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($classRef + 1), $endToken, true); |
|
| 231 | - if ($next === false || $tokens[$next]['code'] !== \T_DOUBLE_COLON) { |
|
| 232 | - return false; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - return $classRef; |
|
| 236 | - } |
|
| 143 | + elseif ($this->inClassScope($phpcsFile, $stackPtr, false) === false) { |
|
| 144 | + $phpcsFile->addWarning( |
|
| 145 | + 'Closures / anonymous functions only have access to $this if used within a class or when bound to an object using bindTo(). Please verify.', |
|
| 146 | + $thisFound, |
|
| 147 | + 'ThisFoundOutsideClass' |
|
| 148 | + ); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + $thisFound = $this->findThisUsageInClosure($phpcsFile, ($thisFound + 1), $scopeEnd); |
|
| 152 | + |
|
| 153 | + } while ($thisFound !== false); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + // Prevent double reporting for nested closures. |
|
| 157 | + return $scopeEnd; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Check whether the closure is declared as static. |
|
| 163 | + * |
|
| 164 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 165 | + * @param int $stackPtr The position of the current token |
|
| 166 | + * in the stack passed in $tokens. |
|
| 167 | + * |
|
| 168 | + * @return bool |
|
| 169 | + */ |
|
| 170 | + protected function isClosureStatic(File $phpcsFile, $stackPtr) |
|
| 171 | + { |
|
| 172 | + $tokens = $phpcsFile->getTokens(); |
|
| 173 | + $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true); |
|
| 174 | + |
|
| 175 | + return ($prevToken !== false && $tokens[$prevToken]['code'] === \T_STATIC); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Check if the code within a closure uses the $this variable. |
|
| 181 | + * |
|
| 182 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 183 | + * @param int $startToken The position within the closure to continue searching from. |
|
| 184 | + * @param int $endToken The closure scope closer to stop searching at. |
|
| 185 | + * |
|
| 186 | + * @return int|false The stackPtr to the first $this usage if found or false if |
|
| 187 | + * $this is not used. |
|
| 188 | + */ |
|
| 189 | + protected function findThisUsageInClosure(File $phpcsFile, $startToken, $endToken) |
|
| 190 | + { |
|
| 191 | + // Make sure the $startToken is valid. |
|
| 192 | + if ($startToken >= $endToken) { |
|
| 193 | + return false; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + return $phpcsFile->findNext( |
|
| 197 | + \T_VARIABLE, |
|
| 198 | + $startToken, |
|
| 199 | + $endToken, |
|
| 200 | + false, |
|
| 201 | + '$this' |
|
| 202 | + ); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Check if the code within a closure uses "self/parent/static". |
|
| 207 | + * |
|
| 208 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 209 | + * @param int $startToken The position within the closure to continue searching from. |
|
| 210 | + * @param int $endToken The closure scope closer to stop searching at. |
|
| 211 | + * |
|
| 212 | + * @return int|false The stackPtr to the first classRef usage if found or false if |
|
| 213 | + * they are not used. |
|
| 214 | + */ |
|
| 215 | + protected function findClassRefUsageInClosure(File $phpcsFile, $startToken, $endToken) |
|
| 216 | + { |
|
| 217 | + // Make sure the $startToken is valid. |
|
| 218 | + if ($startToken >= $endToken) { |
|
| 219 | + return false; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + $tokens = $phpcsFile->getTokens(); |
|
| 223 | + $classRef = $phpcsFile->findNext(array(\T_SELF, \T_PARENT, \T_STATIC), $startToken, $endToken); |
|
| 224 | + |
|
| 225 | + if ($classRef === false || $tokens[$classRef]['code'] !== \T_STATIC) { |
|
| 226 | + return $classRef; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + // T_STATIC, make sure it is used as a class reference. |
|
| 230 | + $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($classRef + 1), $endToken, true); |
|
| 231 | + if ($next === false || $tokens[$next]['code'] !== \T_DOUBLE_COLON) { |
|
| 232 | + return false; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + return $classRef; |
|
| 236 | + } |
|
| 237 | 237 | } |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | */ |
| 36 | 36 | public function register() |
| 37 | 37 | { |
| 38 | - return array(\T_CLOSURE); |
|
| 38 | + return array( \T_CLOSURE ); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | /** |
@@ -47,9 +47,9 @@ discard block |
||
| 47 | 47 | * |
| 48 | 48 | * @return void |
| 49 | 49 | */ |
| 50 | - public function process(File $phpcsFile, $stackPtr) |
|
| 50 | + public function process( File $phpcsFile, $stackPtr ) |
|
| 51 | 51 | { |
| 52 | - if ($this->supportsBelow('5.2')) { |
|
| 52 | + if ( $this->supportsBelow( '5.2' ) ) { |
|
| 53 | 53 | $phpcsFile->addError( |
| 54 | 54 | 'Closures / anonymous functions are not available in PHP 5.2 or earlier', |
| 55 | 55 | $stackPtr, |
@@ -60,8 +60,8 @@ discard block |
||
| 60 | 60 | /* |
| 61 | 61 | * Closures can only be declared as static since PHP 5.4. |
| 62 | 62 | */ |
| 63 | - $isStatic = $this->isClosureStatic($phpcsFile, $stackPtr); |
|
| 64 | - if ($this->supportsBelow('5.3') && $isStatic === true) { |
|
| 63 | + $isStatic = $this->isClosureStatic( $phpcsFile, $stackPtr ); |
|
| 64 | + if ( $this->supportsBelow( '5.3' ) && $isStatic === true ) { |
|
| 65 | 65 | $phpcsFile->addError( |
| 66 | 66 | 'Closures / anonymous functions could not be declared as static in PHP 5.3 or earlier', |
| 67 | 67 | $stackPtr, |
@@ -71,20 +71,20 @@ discard block |
||
| 71 | 71 | |
| 72 | 72 | $tokens = $phpcsFile->getTokens(); |
| 73 | 73 | |
| 74 | - if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) { |
|
| 74 | + if ( isset( $tokens[ $stackPtr ][ 'scope_opener' ], $tokens[ $stackPtr ][ 'scope_closer' ] ) === false ) { |
|
| 75 | 75 | // Live coding or parse error. |
| 76 | 76 | return; |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | - $scopeStart = ($tokens[$stackPtr]['scope_opener'] + 1); |
|
| 80 | - $scopeEnd = $tokens[$stackPtr]['scope_closer']; |
|
| 81 | - $usesThis = $this->findThisUsageInClosure($phpcsFile, $scopeStart, $scopeEnd); |
|
| 79 | + $scopeStart = ( $tokens[ $stackPtr ][ 'scope_opener' ] + 1 ); |
|
| 80 | + $scopeEnd = $tokens[ $stackPtr ][ 'scope_closer' ]; |
|
| 81 | + $usesThis = $this->findThisUsageInClosure( $phpcsFile, $scopeStart, $scopeEnd ); |
|
| 82 | 82 | |
| 83 | - if ($this->supportsBelow('5.3')) { |
|
| 83 | + if ( $this->supportsBelow( '5.3' ) ) { |
|
| 84 | 84 | /* |
| 85 | 85 | * Closures declared within classes only have access to $this since PHP 5.4. |
| 86 | 86 | */ |
| 87 | - if ($usesThis !== false) { |
|
| 87 | + if ( $usesThis !== false ) { |
|
| 88 | 88 | $thisFound = $usesThis; |
| 89 | 89 | do { |
| 90 | 90 | $phpcsFile->addError( |
@@ -93,35 +93,35 @@ discard block |
||
| 93 | 93 | 'ThisFound' |
| 94 | 94 | ); |
| 95 | 95 | |
| 96 | - $thisFound = $this->findThisUsageInClosure($phpcsFile, ($thisFound + 1), $scopeEnd); |
|
| 96 | + $thisFound = $this->findThisUsageInClosure( $phpcsFile, ( $thisFound + 1 ), $scopeEnd ); |
|
| 97 | 97 | |
| 98 | - } while ($thisFound !== false); |
|
| 98 | + } while ( $thisFound !== false ); |
|
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | /* |
| 102 | 102 | * Closures declared within classes only have access to self/parent/static since PHP 5.4. |
| 103 | 103 | */ |
| 104 | - $usesClassRef = $this->findClassRefUsageInClosure($phpcsFile, $scopeStart, $scopeEnd); |
|
| 104 | + $usesClassRef = $this->findClassRefUsageInClosure( $phpcsFile, $scopeStart, $scopeEnd ); |
|
| 105 | 105 | |
| 106 | - if ($usesClassRef !== false) { |
|
| 106 | + if ( $usesClassRef !== false ) { |
|
| 107 | 107 | do { |
| 108 | 108 | $phpcsFile->addError( |
| 109 | 109 | 'Closures / anonymous functions could not use "%s::" in PHP 5.3 or earlier', |
| 110 | 110 | $usesClassRef, |
| 111 | 111 | 'ClassRefFound', |
| 112 | - array(strtolower($tokens[$usesClassRef]['content'])) |
|
| 112 | + array( strtolower( $tokens[ $usesClassRef ][ 'content' ] ) ) |
|
| 113 | 113 | ); |
| 114 | 114 | |
| 115 | - $usesClassRef = $this->findClassRefUsageInClosure($phpcsFile, ($usesClassRef + 1), $scopeEnd); |
|
| 115 | + $usesClassRef = $this->findClassRefUsageInClosure( $phpcsFile, ( $usesClassRef + 1 ), $scopeEnd ); |
|
| 116 | 116 | |
| 117 | - } while ($usesClassRef !== false); |
|
| 117 | + } while ( $usesClassRef !== false ); |
|
| 118 | 118 | } |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | /* |
| 122 | 122 | * Check for correct usage. |
| 123 | 123 | */ |
| 124 | - if ($this->supportsAbove('5.4') && $usesThis !== false) { |
|
| 124 | + if ( $this->supportsAbove( '5.4' ) && $usesThis !== false ) { |
|
| 125 | 125 | |
| 126 | 126 | $thisFound = $usesThis; |
| 127 | 127 | |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | /* |
| 130 | 130 | * Closures only have access to $this if not declared as static. |
| 131 | 131 | */ |
| 132 | - if ($isStatic === true) { |
|
| 132 | + if ( $isStatic === true ) { |
|
| 133 | 133 | $phpcsFile->addError( |
| 134 | 134 | 'Closures / anonymous functions declared as static do not have access to $this', |
| 135 | 135 | $thisFound, |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | /* |
| 141 | 141 | * Closures only have access to $this if used within a class context. |
| 142 | 142 | */ |
| 143 | - elseif ($this->inClassScope($phpcsFile, $stackPtr, false) === false) { |
|
| 143 | + elseif ( $this->inClassScope( $phpcsFile, $stackPtr, false ) === false ) { |
|
| 144 | 144 | $phpcsFile->addWarning( |
| 145 | 145 | 'Closures / anonymous functions only have access to $this if used within a class or when bound to an object using bindTo(). Please verify.', |
| 146 | 146 | $thisFound, |
@@ -148,9 +148,9 @@ discard block |
||
| 148 | 148 | ); |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | - $thisFound = $this->findThisUsageInClosure($phpcsFile, ($thisFound + 1), $scopeEnd); |
|
| 151 | + $thisFound = $this->findThisUsageInClosure( $phpcsFile, ( $thisFound + 1 ), $scopeEnd ); |
|
| 152 | 152 | |
| 153 | - } while ($thisFound !== false); |
|
| 153 | + } while ( $thisFound !== false ); |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | // Prevent double reporting for nested closures. |
@@ -167,12 +167,12 @@ discard block |
||
| 167 | 167 | * |
| 168 | 168 | * @return bool |
| 169 | 169 | */ |
| 170 | - protected function isClosureStatic(File $phpcsFile, $stackPtr) |
|
| 170 | + protected function isClosureStatic( File $phpcsFile, $stackPtr ) |
|
| 171 | 171 | { |
| 172 | 172 | $tokens = $phpcsFile->getTokens(); |
| 173 | - $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true); |
|
| 173 | + $prevToken = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true, null, true ); |
|
| 174 | 174 | |
| 175 | - return ($prevToken !== false && $tokens[$prevToken]['code'] === \T_STATIC); |
|
| 175 | + return ( $prevToken !== false && $tokens[ $prevToken ][ 'code' ] === \T_STATIC ); |
|
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | |
@@ -186,10 +186,10 @@ discard block |
||
| 186 | 186 | * @return int|false The stackPtr to the first $this usage if found or false if |
| 187 | 187 | * $this is not used. |
| 188 | 188 | */ |
| 189 | - protected function findThisUsageInClosure(File $phpcsFile, $startToken, $endToken) |
|
| 189 | + protected function findThisUsageInClosure( File $phpcsFile, $startToken, $endToken ) |
|
| 190 | 190 | { |
| 191 | 191 | // Make sure the $startToken is valid. |
| 192 | - if ($startToken >= $endToken) { |
|
| 192 | + if ( $startToken >= $endToken ) { |
|
| 193 | 193 | return false; |
| 194 | 194 | } |
| 195 | 195 | |
@@ -212,23 +212,23 @@ discard block |
||
| 212 | 212 | * @return int|false The stackPtr to the first classRef usage if found or false if |
| 213 | 213 | * they are not used. |
| 214 | 214 | */ |
| 215 | - protected function findClassRefUsageInClosure(File $phpcsFile, $startToken, $endToken) |
|
| 215 | + protected function findClassRefUsageInClosure( File $phpcsFile, $startToken, $endToken ) |
|
| 216 | 216 | { |
| 217 | 217 | // Make sure the $startToken is valid. |
| 218 | - if ($startToken >= $endToken) { |
|
| 218 | + if ( $startToken >= $endToken ) { |
|
| 219 | 219 | return false; |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | $tokens = $phpcsFile->getTokens(); |
| 223 | - $classRef = $phpcsFile->findNext(array(\T_SELF, \T_PARENT, \T_STATIC), $startToken, $endToken); |
|
| 223 | + $classRef = $phpcsFile->findNext( array( \T_SELF, \T_PARENT, \T_STATIC ), $startToken, $endToken ); |
|
| 224 | 224 | |
| 225 | - if ($classRef === false || $tokens[$classRef]['code'] !== \T_STATIC) { |
|
| 225 | + if ( $classRef === false || $tokens[ $classRef ][ 'code' ] !== \T_STATIC ) { |
|
| 226 | 226 | return $classRef; |
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | // T_STATIC, make sure it is used as a class reference. |
| 230 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($classRef + 1), $endToken, true); |
|
| 231 | - if ($next === false || $tokens[$next]['code'] !== \T_DOUBLE_COLON) { |
|
| 230 | + $next = $phpcsFile->findNext( Tokens::$emptyTokens, ( $classRef + 1 ), $endToken, true ); |
|
| 231 | + if ( $next === false || $tokens[ $next ][ 'code' ] !== \T_DOUBLE_COLON ) { |
|
| 232 | 232 | return false; |
| 233 | 233 | } |
| 234 | 234 | |
@@ -26,15 +26,13 @@ discard block |
||
| 26 | 26 | * @package PHPCompatibility |
| 27 | 27 | * @author Wim Godden <[email protected]> |
| 28 | 28 | */ |
| 29 | -class NewClosureSniff extends Sniff |
|
| 30 | -{ |
|
| 29 | +class NewClosureSniff extends Sniff { |
|
| 31 | 30 | /** |
| 32 | 31 | * Returns an array of tokens this test wants to listen for. |
| 33 | 32 | * |
| 34 | 33 | * @return array |
| 35 | 34 | */ |
| 36 | - public function register() |
|
| 37 | - { |
|
| 35 | + public function register() { |
|
| 38 | 36 | return array(\T_CLOSURE); |
| 39 | 37 | } |
| 40 | 38 | |
@@ -47,8 +45,7 @@ discard block |
||
| 47 | 45 | * |
| 48 | 46 | * @return void |
| 49 | 47 | */ |
| 50 | - public function process(File $phpcsFile, $stackPtr) |
|
| 51 | - { |
|
| 48 | + public function process(File $phpcsFile, $stackPtr) { |
|
| 52 | 49 | if ($this->supportsBelow('5.2')) { |
| 53 | 50 | $phpcsFile->addError( |
| 54 | 51 | 'Closures / anonymous functions are not available in PHP 5.2 or earlier', |
@@ -167,8 +164,7 @@ discard block |
||
| 167 | 164 | * |
| 168 | 165 | * @return bool |
| 169 | 166 | */ |
| 170 | - protected function isClosureStatic(File $phpcsFile, $stackPtr) |
|
| 171 | - { |
|
| 167 | + protected function isClosureStatic(File $phpcsFile, $stackPtr) { |
|
| 172 | 168 | $tokens = $phpcsFile->getTokens(); |
| 173 | 169 | $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true); |
| 174 | 170 | |
@@ -186,8 +182,7 @@ discard block |
||
| 186 | 182 | * @return int|false The stackPtr to the first $this usage if found or false if |
| 187 | 183 | * $this is not used. |
| 188 | 184 | */ |
| 189 | - protected function findThisUsageInClosure(File $phpcsFile, $startToken, $endToken) |
|
| 190 | - { |
|
| 185 | + protected function findThisUsageInClosure(File $phpcsFile, $startToken, $endToken) { |
|
| 191 | 186 | // Make sure the $startToken is valid. |
| 192 | 187 | if ($startToken >= $endToken) { |
| 193 | 188 | return false; |
@@ -212,8 +207,7 @@ discard block |
||
| 212 | 207 | * @return int|false The stackPtr to the first classRef usage if found or false if |
| 213 | 208 | * they are not used. |
| 214 | 209 | */ |
| 215 | - protected function findClassRefUsageInClosure(File $phpcsFile, $startToken, $endToken) |
|
| 216 | - { |
|
| 210 | + protected function findClassRefUsageInClosure(File $phpcsFile, $startToken, $endToken) { |
|
| 217 | 211 | // Make sure the $startToken is valid. |
| 218 | 212 | if ($startToken >= $endToken) { |
| 219 | 213 | return false; |