From 693bcc45af33236e4ca3260a26a8094e92995249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=E9=87=91=E5=8F=AF=E6=98=8E?= Date: Tue, 19 Sep 2023 20:02:07 +0200 Subject: [PATCH] fix(php): handle case where use statements contain a single name (#621) --- helper/src/php/parser.rs | 16 ++++++++++++---- .../php/functions-with-namespaces.vader | 6 +++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/helper/src/php/parser.rs b/helper/src/php/parser.rs index 386751fe..9f818458 100644 --- a/helper/src/php/parser.rs +++ b/helper/src/php/parser.rs @@ -165,20 +165,28 @@ impl<'a> PhpParser<'a> { .count() > 0; if !is_alias { + // Get the `qualified_name` node kind, which are + // usages like: `use Foo\Bar;`. We might also run + // into `use Foo;` which has the node kind `name`, + // which we ignore. let fqn_node = child_node .children(&mut child_node.walk()) .filter(|node| node.kind() == "qualified_name") - .next() - .unwrap(); + .next(); + + if !fqn_node.is_some() { + continue; + } let fqn_name = fqn_node - .children(&mut fqn_node.walk()) + .unwrap() + .children(&mut fqn_node.unwrap().walk()) .filter(|node| node.kind() == "name") .next() .and_then(|node| Some(self.get_node_text(&node))); if fqn_name.unwrap() == property_type { - let mut fqn_text = self.get_node_text(&fqn_node); + let mut fqn_text = self.get_node_text(&fqn_node.unwrap()); // Make sure FQN always starts with a backslash. if !fqn_text.starts_with('\\') { diff --git a/test/filetypes/php/functions-with-namespaces.vader b/test/filetypes/php/functions-with-namespaces.vader index f9b127c6..40399250 100644 --- a/test/filetypes/php/functions-with-namespaces.vader +++ b/test/filetypes/php/functions-with-namespaces.vader @@ -8,16 +8,20 @@ Given php (function where the type should result in its FQN): + :6\ \ Expect php (generated comment containing the FQN):