Skip to content

Commit 71e20c5

Browse files
authored
NoSilencedErrors: improve consistency metrics (#1843)
NoSilencedErrors: improve consistency metrics
2 parents 38b6211 + a1777cf commit 71e20c5

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

WordPress/Sniffs/PHP/NoSilencedErrorsSniff.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -179,22 +179,24 @@ public function process_token( $stackPtr ) {
179179
$this->custom_whitelist = $this->merge_custom_array( $this->custom_whitelist, array(), false );
180180
$this->custom_whitelist = array_map( 'strtolower', $this->custom_whitelist );
181181

182-
if ( true === $this->use_default_whitelist || ! empty( $this->custom_whitelist ) ) {
183-
/*
184-
* Check if the error silencing is done for one of the whitelisted functions.
185-
*/
186-
$next_non_empty = $this->phpcsFile->findNext( $this->empty_tokens, ( $stackPtr + 1 ), null, true, null, true );
187-
if ( false !== $next_non_empty && \T_STRING === $this->tokens[ $next_non_empty ]['code'] ) {
188-
$has_parenthesis = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $next_non_empty + 1 ), null, true, null, true );
189-
if ( false !== $has_parenthesis && \T_OPEN_PARENTHESIS === $this->tokens[ $has_parenthesis ]['code'] ) {
190-
$function_name = strtolower( $this->tokens[ $next_non_empty ]['content'] );
191-
if ( ( true === $this->use_default_whitelist
192-
&& isset( $this->function_whitelist[ $function_name ] ) === true )
193-
|| in_array( $function_name, $this->custom_whitelist, true ) === true
194-
) {
195-
$this->phpcsFile->recordMetric( $stackPtr, 'Error silencing', 'whitelisted function call: ' . $function_name );
196-
return;
197-
}
182+
/*
183+
* Check if the error silencing is done for one of the whitelisted functions.
184+
*
185+
* @internal The function call name determination is done even when there is no whitelist active
186+
* to allow the metrics to be more informative.
187+
*/
188+
$next_non_empty = $this->phpcsFile->findNext( $this->empty_tokens, ( $stackPtr + 1 ), null, true, null, true );
189+
if ( false !== $next_non_empty && \T_STRING === $this->tokens[ $next_non_empty ]['code'] ) {
190+
$has_parenthesis = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $next_non_empty + 1 ), null, true, null, true );
191+
if ( false !== $has_parenthesis && \T_OPEN_PARENTHESIS === $this->tokens[ $has_parenthesis ]['code'] ) {
192+
$function_name = strtolower( $this->tokens[ $next_non_empty ]['content'] );
193+
if ( ( true === $this->use_default_whitelist
194+
&& isset( $this->function_whitelist[ $function_name ] ) === true )
195+
|| ( ! empty( $this->custom_whitelist )
196+
&& in_array( $function_name, $this->custom_whitelist, true ) === true )
197+
) {
198+
$this->phpcsFile->recordMetric( $stackPtr, 'Error silencing', 'whitelisted function call: ' . $function_name );
199+
return;
198200
}
199201
}
200202
}
@@ -228,7 +230,7 @@ public function process_token( $stackPtr ) {
228230
);
229231

230232
if ( isset( $function_name ) ) {
231-
$this->phpcsFile->recordMetric( $stackPtr, 'Error silencing', $function_name );
233+
$this->phpcsFile->recordMetric( $stackPtr, 'Error silencing', '@' . $function_name );
232234
} else {
233235
$this->phpcsFile->recordMetric( $stackPtr, 'Error silencing', $found );
234236
}

0 commit comments

Comments
 (0)