Skip to content

Commit

Permalink
Implement changes to the sniff code asked during review
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigoprimo committed Jul 29, 2024
1 parent 5dd8031 commit d60856e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
17 changes: 12 additions & 5 deletions WordPress/Sniffs/WP/GetMetaFunctionSingleParameterSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ final class GetMetaFunctionSingleParameterSniff extends AbstractFunctionParamete
/**
* List of functions this sniff should examine.
*
* Once support for PHP 5.4 is dropped, it is possible to create two private properties
* representing the two different signatures of get meta functions to remove the duplication
* of the name and position of the parameters.
*
* @link https://developer.wordpress.org/reference/functions/get_comment_meta/
* @link https://developer.wordpress.org/reference/functions/get_metadata/
* @link https://developer.wordpress.org/reference/functions/get_metadata_default/
Expand All @@ -47,8 +51,10 @@ final class GetMetaFunctionSingleParameterSniff extends AbstractFunctionParamete
*
* @since 3.2.0
*
* @var array<string, array> Key is function name, value is an array containing information
* about the name and position of the relevant parameters.
* @var array<string, array<string, array<string, int|string>>> Key is function name, value is an
* array containing information about
* the name and position of the
* relevant parameters.
*/
protected $target_functions = array(
'get_comment_meta' => array(
Expand Down Expand Up @@ -160,13 +166,14 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
return;
}

$tokens = $this->phpcsFile->getTokens();
$tokens = $this->phpcsFile->getTokens();
$message_data = array( $condition['parameter'], $tokens[ $stackPtr ]['content'], $recommended['parameter'] );

$this->phpcsFile->addWarning(
'When passing the $%s parameter to %s(), the $%s parameter must also be passed to make it explicit whether an array or a string is expected.',
'When passing the $%s parameter to %s(), it is recommended to pass the $%s parameter as well to make it explicit whether an array or a string is expected.',
$stackPtr,
'ReturnTypeNotExplicit',
array( $condition['parameter'], $tokens[ $stackPtr ]['content'], $recommended['parameter'] )
$message_data
);
}
}
32 changes: 24 additions & 8 deletions WordPress/Tests/WP/GetMetaFunctionSingleParameterUnitTest.inc
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
<?php

// Invalid calls (missing mandatory parameter), but still useful to check that the sniff does not flag them.
$invalid_but_ok = get_post_meta();
$invalid_but_ok = get_post_meta( single: true, $post_id );
$invalid_but_ok = get_post_meta( single: true );
$invalid_but_ok = get_metadata( 'post' );
/*
* Not the sniff target.
*/
my\ns\get_post_meta($a, $b);
$this->get_post_meta($a, $b);
MyClass::get_post_meta($a, $b);
echo GET_POST_META;
add_action('my_action', get_post_meta(...)); // PHP 8.1 first class callable.

/*
* These should all be okay.
*/
$ok = get_post_meta( $post_id );
$ok = get_post_meta( $post_id, $meta_key, false );
$ok = get_post_meta( $post_id, single: true );
Expand All @@ -15,10 +21,20 @@ $ok = get_metadata( 'post', $post_id, $meta_key, true );
$ok = get_metadata( 'post', $post_id, single: true );
$ok = get_metadata( 'post', $post_id, single: true, meta_key: $meta_key );

$warning = get_post_meta( $post_id, $meta_key );
$warning = get_post_meta( $post_id, key: $meta_key );
// Incorrect function calls, should be ignored by the sniff.
$incorrect_but_ok = get_post_meta();
$incorrect_but_ok = get_post_meta( single: true, $post_id );
$incorrect_but_ok = get_post_meta( single: true );
$incorrect_but_ok = get_metadata( 'post' );

/*
* These should all be flagged with a warning.
*/
$warning = \get_post_meta( $post_id, $meta_key );
implode(', ', get_post_meta( $post_id, $meta_key ));
if (get_post_meta( $post_id, key: $meta_key )) {}
$warning = get_post_meta( $post_id, key: $meta_key, sinngle: true ); // Typo in parameter name.
$warning = get_comment_meta( $comment_id, $meta_key );
echo get_comment_meta( $comment_id, $meta_key );
$warning = get_site_meta( $site_id, $meta_key );
$warning = get_term_meta( $term_id, $meta_key );
$warning = get_user_meta( $user_id, $meta_key );
Expand Down
23 changes: 12 additions & 11 deletions WordPress/Tests/WP/GetMetaFunctionSingleParameterUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ public function getErrorList() {
*/
public function getWarningList() {
return array(
18 => 1,
19 => 1,
20 => 1,
21 => 1,
22 => 1,
23 => 1,
24 => 1,
25 => 1,
26 => 1,
31 => 1,
32 => 1,
33 => 1,
34 => 1,
35 => 1,
36 => 1,
37 => 1,
38 => 1,
39 => 1,
40 => 1,
41 => 1,
42 => 1,
47 => 1,
48 => 1,
);
}
}

0 comments on commit d60856e

Please sign in to comment.