Skip to content

Commit 75e90b7

Browse files
dmsnellsirreal
andcommitted
Pull in updates from WordPress#251
Co-authored-by: Jon Surrell <jonsurrell@git.wordpress.org>
1 parent 6a498ea commit 75e90b7

5 files changed

Lines changed: 98 additions & 79 deletions

lib/class-file-reflector.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ public function enterNode( \PhpParser\Node $node ) {
126126
// we don't ignore them, we'll end up picking up docblocks that are already
127127
// associated with a named element, and so aren't really from a non-
128128
// documentable element after all.
129-
if ( ! $this->isNodeDocumentable( $node ) && 'Name' !== $node->getType() && ( $docblock = $node->getDocComment() ) ) {
129+
if (
130+
! $this->isNodeDocumentable( $node )
131+
&& 'Name' !== $node->getType()
132+
&& 'Name_FullyQualified' !== $node->getType()
133+
&& ( $docblock = $node->getDocComment() ) ) {
130134
$this->last_doc = $docblock;
131135
}
132136
}
@@ -197,8 +201,10 @@ public function leaveNode( \PhpParser\Node $node ) {
197201
* @return bool
198202
*/
199203
protected function isFilter( \PhpParser\Node $node ) {
200-
// Ignore variable functions
201-
if ( 'Name' !== $node->name->getType() ) {
204+
if (
205+
'Name' !== $node->name->getType() &&
206+
'Name_FullyQualified' !== $node->name->getType()
207+
) {
202208
return false;
203209
}
204210

lib/class-method-call-reflector.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public function getName() {
3939
$caller = '\\' . $caller->toString();
4040
} elseif ( $caller instanceof \PhpParser\Node\Name ) {
4141
$caller = $caller->toString();
42+
} elseif ( $caller instanceof \PhpParser\Node\Stmt\Class_ ) {
43+
$caller = $caller->isAnonymous()
44+
? 'class@anonymous'
45+
: $caller->name;
4246
}
4347

4448
$caller = $this->_resolveName( $caller );
@@ -134,13 +138,12 @@ protected function _getClassMapping() {
134138
*
135139
* @return string The resolved class name.
136140
*/
137-
protected function _resolveName( $class ) {
141+
protected function _resolveName( string $class ): string {
138142

139143
if ( ! $this->called_in_class ) {
140144
return $class;
141145
}
142146

143-
144147
switch ( $class ) {
145148
case '$this':
146149
case 'self':

lib/class-pretty-printer.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22

33
namespace WP_Parser;
44

5-
use PhpParser\Node\Arg;
6-
75
/**
86
* Extends default printer for arguments.
97
*/
108
class Pretty_Printer extends \PhpParser\PrettyPrinter\Standard {
119
/**
1210
* Pretty prints an argument.
1311
*
14-
* @param PhpParser\Node\Arg $node Expression argument
12+
* @param \PhpParser\Node\Arg $node Expression argument
1513
*
1614
* @return string Pretty printed argument
1715
*/
18-
public function prettyPrintArg( Arg $node ) {
16+
public function prettyPrintArg( \PhpParser\Node\Arg $node ) {
1917
return str_replace( "\n" . $this->noIndentToken, "\n", $this->p( $node ) );
2018
}
2119
}

lib/class-static-method-call-reflector.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@ class Static_Method_Call_Reflector extends Method_Call_Reflector {
1212
*
1313
* @return string[] Index 0 is the class name, 1 is the method name.
1414
*/
15-
public function getName() {
15+
public function getName(): array {
1616
$class = $this->node->class;
1717
$prefix = ( is_a( $class, 'PhpParser\Node\Name\FullyQualified' ) ) ? '\\' : '';
18-
$class = $prefix . $this->_resolveName( implode( '\\', $class->parts ) );
18+
19+
if ( $class instanceof \PhpParser\Node\Stmt\Class_ && $class->isAnonymous() ) {
20+
$class = 'class@anonymous';
21+
} elseif ( $class instanceof \PhpParser\Node\Expr\Variable ) {
22+
// Static calls like `$foo::bar()`
23+
$class = "\${$class->name}";
24+
} else {
25+
$class = $prefix . $this->_resolveName( implode( '\\', $class->parts ) );
26+
}
1927

2028
return array( $class, $this->getShortName() );
2129
}

lib/runner.php

Lines changed: 72 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -47,88 +47,92 @@ function get_wp_files( $directory ) {
4747
function parse_files( $files, $root ) {
4848
$output = array();
4949

50-
foreach ( $files as $filename ) {
51-
// echo "\n\e[90mRunning file '\e[35m{$filename}\e[m\n";
52-
$file = new File_Reflector( $filename );
53-
54-
$path = ltrim( substr( $filename, strlen( $root ) ), DIRECTORY_SEPARATOR );
55-
$file->setFilename( $path );
56-
57-
$file->process();
50+
try {
51+
foreach ( $files as $filename ) {
52+
$file = new File_Reflector( $filename );
5853

59-
// TODO proper exporter
60-
$out = array(
61-
'file' => export_docblock( $file ),
62-
'path' => str_replace( DIRECTORY_SEPARATOR, '/', $file->getFilename() ),
63-
'root' => $root,
64-
);
54+
$path = ltrim( substr( $filename, strlen( $root ) ), DIRECTORY_SEPARATOR );
55+
$file->setFilename( $path );
6556

66-
if ( ! empty( $file->uses ) ) {
67-
$out['uses'] = export_uses( $file->uses );
68-
}
57+
$file->process();
6958

70-
foreach ( $file->getIncludes() as $include ) {
71-
$out['includes'][] = array(
72-
'name' => $include->getName(),
73-
'line' => $include->getLineNumber(),
74-
'type' => $include->getType(),
59+
// TODO proper exporter
60+
$out = array(
61+
'file' => export_docblock( $file ),
62+
'path' => str_replace( DIRECTORY_SEPARATOR, '/', $file->getFilename() ),
63+
'root' => $root,
7564
);
76-
}
7765

78-
foreach ( $file->getConstants() as $constant ) {
79-
$out['constants'][] = array(
80-
'name' => $constant->getShortName(),
81-
'line' => $constant->getLineNumber(),
82-
'value' => $constant->getValue(),
83-
);
84-
}
66+
if ( ! empty( $file->uses ) ) {
67+
$out['uses'] = export_uses( $file->uses );
68+
}
8569

86-
if ( ! empty( $file->uses['hooks'] ) ) {
87-
$out['hooks'] = export_hooks( $file->uses['hooks'] );
88-
}
70+
foreach ( $file->getIncludes() as $include ) {
71+
$out['includes'][] = array(
72+
'name' => $include->getName(),
73+
'line' => $include->getLineNumber(),
74+
'type' => $include->getType(),
75+
);
76+
}
8977

90-
foreach ( $file->getFunctions() as $function ) {
91-
$func = array(
92-
'name' => $function->getShortName(),
93-
'namespace' => $function->getNamespace(),
94-
'aliases' => $function->getNamespaceAliases(),
95-
'line' => $function->getLineNumber(),
96-
'end_line' => $function->getNode()->getAttribute( 'endLine' ),
97-
'arguments' => export_arguments( $function->getArguments() ),
98-
'doc' => export_docblock( $function ),
99-
'hooks' => array(),
100-
);
78+
foreach ( $file->getConstants() as $constant ) {
79+
$out['constants'][] = array(
80+
'name' => $constant->getShortName(),
81+
'line' => $constant->getLineNumber(),
82+
'value' => $constant->getValue(),
83+
);
84+
}
10185

102-
if ( ! empty( $function->uses ) ) {
103-
$func['uses'] = export_uses( $function->uses );
86+
if ( ! empty( $file->uses['hooks'] ) ) {
87+
$out['hooks'] = export_hooks( $file->uses['hooks'] );
88+
}
10489

105-
if ( ! empty( $function->uses['hooks'] ) ) {
106-
$func['hooks'] = export_hooks( $function->uses['hooks'] );
90+
foreach ( $file->getFunctions() as $function ) {
91+
$func = array(
92+
'name' => $function->getShortName(),
93+
'namespace' => $function->getNamespace(),
94+
'aliases' => $function->getNamespaceAliases(),
95+
'line' => $function->getLineNumber(),
96+
'end_line' => $function->getNode()->getAttribute( 'endLine' ),
97+
'arguments' => export_arguments( $function->getArguments() ),
98+
'doc' => export_docblock( $function ),
99+
'hooks' => array(),
100+
);
101+
102+
if ( ! empty( $function->uses ) ) {
103+
$func['uses'] = export_uses( $function->uses );
104+
105+
if ( ! empty( $function->uses['hooks'] ) ) {
106+
$func['hooks'] = export_hooks( $function->uses['hooks'] );
107+
}
107108
}
108-
}
109109

110-
$out['functions'][] = $func;
111-
}
110+
$out['functions'][] = $func;
111+
}
112112

113-
foreach ( $file->getClasses() as $class ) {
114-
$class_data = array(
115-
'name' => $class->getShortName(),
116-
'namespace' => $class->getNamespace(),
117-
'line' => $class->getLineNumber(),
118-
'end_line' => $class->getNode()->getAttribute( 'endLine' ),
119-
'final' => $class->isFinal(),
120-
'abstract' => $class->isAbstract(),
121-
'extends' => $class->getParentClass(),
122-
'implements' => $class->getInterfaces(),
123-
'properties' => export_properties( $class->getProperties() ),
124-
'methods' => export_methods( $class->getMethods() ),
125-
'doc' => export_docblock( $class ),
126-
);
113+
foreach ( $file->getClasses() as $class ) {
114+
$class_data = array(
115+
'name' => $class->getShortName(),
116+
'namespace' => $class->getNamespace(),
117+
'line' => $class->getLineNumber(),
118+
'end_line' => $class->getNode()->getAttribute( 'endLine' ),
119+
'final' => $class->isFinal(),
120+
'abstract' => $class->isAbstract(),
121+
'extends' => $class->getParentClass(),
122+
'implements' => $class->getInterfaces(),
123+
'properties' => export_properties( $class->getProperties() ),
124+
'methods' => export_methods( $class->getMethods() ),
125+
'doc' => export_docblock( $class ),
126+
);
127+
128+
$out['classes'][] = $class_data;
129+
}
127130

128-
$out['classes'][] = $class_data;
131+
$output[] = $out;
129132
}
130-
131-
$output[] = $out;
133+
} catch ( \Exception | \Error $e ) {
134+
error_log( \sprintf( 'Error processing file [%s]: %s', $filename, $e->getMessage() ) );
135+
throw $e;
132136
}
133137

134138
return $output;

0 commit comments

Comments
 (0)