|
3 | 3 | namespace WP_Parser;
|
4 | 4 |
|
5 | 5 | use phpDocumentor\Reflection\BaseReflector;
|
| 6 | +use PhpParser\Node; |
6 | 7 | use PHPParser_PrettyPrinter_Default;
|
7 | 8 |
|
8 | 9 | /**
|
|
11 | 12 | class Hook_Reflector extends BaseReflector {
|
12 | 13 |
|
13 | 14 | /**
|
14 |
| - * @return string |
15 |
| - */ |
16 |
| - public function getName() { |
17 |
| - $printer = new PHPParser_PrettyPrinter_Default; |
18 |
| - return $this->cleanupName( $printer->prettyPrintExpr( $this->node->args[0]->value ) ); |
19 |
| - } |
20 |
| - |
21 |
| - /** |
22 |
| - * @param string $name |
| 15 | + * Hook names are the first argument to actions and filters. |
23 | 16 | *
|
24 |
| - * @return string |
| 17 | + * These are expected to be string values or concatenations of strings and variables. |
| 18 | + * |
| 19 | + * Example: |
| 20 | + * |
| 21 | + * from: apply_filters( 'option_' . $option_name, $option_value ); |
| 22 | + * name: option_{$option_name} |
| 23 | + * |
| 24 | + * from: do_action( 'wp_insert_post', $post_ID, $post, true ); |
| 25 | + * name: wp_insert_post |
| 26 | + * |
| 27 | + * from: do_action( "{$old_status}_to_{$new_status}", $post ); |
| 28 | + * name: {$old_status}_to_{$new_status} |
| 29 | + * |
| 30 | + * from: do_action( $filter_name, $args ); |
| 31 | + * name: {$filter_name} |
| 32 | + * |
| 33 | + * @param ?Node $node Which node to examine; defaults to the parser's current node. |
| 34 | + * @return string Represents the hook's name, including any interpolations into the hook name. |
25 | 35 | */
|
26 |
| - private function cleanupName( $name ) { |
27 |
| - $matches = array(); |
28 |
| - |
29 |
| - // quotes on both ends of a string |
30 |
| - if ( preg_match( '/^[\'"]([^\'"]*)[\'"]$/', $name, $matches ) ) { |
31 |
| - return $matches[1]; |
| 36 | + public function getName( $node = null ) { |
| 37 | + if ( null === $node ) { |
| 38 | + $node = $this->node->args[0]->value; |
32 | 39 | }
|
| 40 | + $printer = new PHPParser_PrettyPrinter_Default; |
| 41 | + $name = $printer->prettyPrintExpr( $node ); |
33 | 42 |
|
34 |
| - // two concatenated things, last one of them a variable |
35 |
| - if ( preg_match( |
36 |
| - '/(?:[\'"]([^\'"]*)[\'"]\s*\.\s*)?' . // First filter name string (optional) |
37 |
| - '(\$[^\s]*)' . // Dynamic variable |
38 |
| - '(?:\s*\.\s*[\'"]([^\'"]*)[\'"])?/', // Second filter name string (optional) |
39 |
| - $name, $matches ) ) { |
| 43 | + if ( $node instanceof \PhpParser\Node\Scalar\String_ ) { |
| 44 | + // "'action'" -> "action" |
| 45 | + return $node->value; |
| 46 | + } elseif ( $node instanceof \PhpParser\Node\Scalar\Encapsed ) { |
| 47 | + // '"action_{$var}"' -> 'action_{$var}' |
| 48 | + $name = ''; |
40 | 49 |
|
41 |
| - if ( isset( $matches[3] ) ) { |
42 |
| - return $matches[1] . '{' . $matches[2] . '}' . $matches[3]; |
43 |
| - } else { |
44 |
| - return $matches[1] . '{' . $matches[2] . '}'; |
| 50 | + foreach ( $node->parts as $part ) { |
| 51 | + if ( is_string( $part ) ) { |
| 52 | + $name .= $part; |
| 53 | + } else { |
| 54 | + $name .= $this->getName( $part ); |
| 55 | + } |
45 | 56 | }
|
| 57 | + |
| 58 | + return $name; |
| 59 | + } elseif ( $node instanceof \PhpParser\Node\Expr\BinaryOp\Concat ) { |
| 60 | + // '"action_" . $var' -> 'action_{$var}' |
| 61 | + return $this->getName( $node->left ) . $this->getName( $node->right ); |
| 62 | + } elseif ( $node instanceof \PhpParser\Node\Expr\PropertyFetch ) { |
| 63 | + // '$this->action' -> '{$this->action}' |
| 64 | + return "{{$name}}"; |
| 65 | + } elseif ( $node instanceof \PhpParser\Node\Expr\Variable ) { |
| 66 | + // '$action' -> '{$action}' |
| 67 | + return "{\${$node->name}}"; |
46 | 68 | }
|
47 | 69 |
|
| 70 | + /* |
| 71 | + * If none of these known constructions match, then |
| 72 | + * fallback to the pretty-printed version of the node. |
| 73 | + * |
| 74 | + * For improving the quality of the hook-name generation, |
| 75 | + * replace this return statement by throwing an exception |
| 76 | + * to determine which cases aren't handled, and then add |
| 77 | + * them above. |
| 78 | + */ |
48 | 79 | return $name;
|
49 | 80 | }
|
50 | 81 |
|
|
0 commit comments