Skip to content

Commit 693bcc4

Browse files
committed
fix(php): handle case where use statements contain a single name (#621)
1 parent 2056efd commit 693bcc4

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Diff for: helper/src/php/parser.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,28 @@ impl<'a> PhpParser<'a> {
165165
.count() > 0;
166166

167167
if !is_alias {
168+
// Get the `qualified_name` node kind, which are
169+
// usages like: `use Foo\Bar;`. We might also run
170+
// into `use Foo;` which has the node kind `name`,
171+
// which we ignore.
168172
let fqn_node = child_node
169173
.children(&mut child_node.walk())
170174
.filter(|node| node.kind() == "qualified_name")
171-
.next()
172-
.unwrap();
175+
.next();
176+
177+
if !fqn_node.is_some() {
178+
continue;
179+
}
173180

174181
let fqn_name = fqn_node
175-
.children(&mut fqn_node.walk())
182+
.unwrap()
183+
.children(&mut fqn_node.unwrap().walk())
176184
.filter(|node| node.kind() == "name")
177185
.next()
178186
.and_then(|node| Some(self.get_node_text(&node)));
179187

180188
if fqn_name.unwrap() == property_type {
181-
let mut fqn_text = self.get_node_text(&fqn_node);
189+
let mut fqn_text = self.get_node_text(&fqn_node.unwrap());
182190

183191
// Make sure FQN always starts with a backslash.
184192
if !fqn_text.starts_with('\\') {

Diff for: test/filetypes/php/functions-with-namespaces.vader

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@
88

99
Given php (function where the type should result in its FQN):
1010
<?php
11+
use Closure;
12+
use Exception;
1113
use Symfony\Component\HttpFoundation\Response;
1214

1315
function myFunction(Response $p1): Response {}
1416

1517
Do (trigger doge):
16-
:4\<CR>
18+
:6\<CR>
1719
\<C-d>
1820

1921
Expect php (generated comment containing the FQN):
2022
<?php
23+
use Closure;
24+
use Exception;
2125
use Symfony\Component\HttpFoundation\Response;
2226

2327
/**

0 commit comments

Comments
 (0)