Page MenuHomePhabricator

D9792.id23651.diff
No OneTemporary

D9792.id23651.diff

diff --git a/src/applications/diviner/atomizer/DivinerPHPAtomizer.php b/src/applications/diviner/atomizer/DivinerPHPAtomizer.php
--- a/src/applications/diviner/atomizer/DivinerPHPAtomizer.php
+++ b/src/applications/diviner/atomizer/DivinerPHPAtomizer.php
@@ -13,10 +13,66 @@
$file_data,
$future->resolve());
- $atoms = array();
-
$root = $tree->getRootNode();
+ $ns_declarations = $root->selectDescendantsOfType('n_NAMESPACE');
+
+ if (!count($ns_declarations)) {
+ return $this->atomizeNodeInNS($file_name, $root, null);
+ }
+
+ $atom_vectors = array();
+
+ foreach ($ns_declarations as $ns_declaration) {
+ if ($ns = $ns_declaration->getChildByIndex(0)->getConcreteString()) {
+ $ns .= '\\'; // A trailing NS separator is necessary
+ }
+ else {
+ $ns = null;
+ }
+
+ $ns_root = $ns_declaration; // "namespace Foo { }" has children
+
+ // "namespace Foo;" has siblings instead of children, this generates a
+ // new tree from them to re-parse by building a string buffer from the
+ // namespace declaration until the next one (or EOF) is reached
+ if ($ns_declaration->getChildByIndex(1)->getTypeName() == 'n_EMPTY') {
+ $buf = '<?php ';
+ $is_current_ns = false;
+ foreach ($root->getChildByIndex(0)->getChildren() as $sibling) {
+ $sibling_nss = $sibling->selectDescendantsOfType('n_NAMESPACE');
+ // This is a NS declaration
+ if (count($sibling_nss) == 1) {
+ foreach ($sibling_nss as $sibling_ns) {
+ // In effect, if ($subling_ns == $ns_declaration) {}
+ if ($sibling_ns->getChildByIndex(0)->getConcreteString().'\\' == $ns) {
+ $is_current_ns = true;
+ $buf .= $sibling->getConcreteString();
+ }
+ else {
+ $is_current_ns = false;
+ }
+ }
+ }
+ // Not a namespace declaration
+ else {
+ if ($is_current_ns) {
+ $buf .= $sibling->getSemanticString();
+ }
+ }
+ }
+ $derived_tree = XHPASTTree::newFromData($buf);
+ $ns_root = $derived_tree->getRootNode();
+ }
+ $atom_vectors[] = $this->atomizeNodeInNS($file_name,
+ $ns, $ns_root);
+ }
+
+ return array_mergev($atom_vectors);
+ }
+
+ private function atomizeNodeInNS($file_name, XHPASTNode $root, $namespace) {
+ $atoms = array();
$func_decl = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION');
foreach ($func_decl as $func) {
@@ -28,6 +84,7 @@
}
$atom = $this->newAtom(DivinerAtom::TYPE_FUNCTION)
+ ->setContext($namespace)
->setName($name->getConcreteString())
->setLine($func->getLineNumber())
->setFile($file_name);
@@ -50,6 +107,7 @@
$name = $class->getChildByIndex(1, 'n_CLASS_NAME');
$atom = $this->newAtom($atom_type)
+ ->setContext($namespace)
->setName($name->getConcreteString())
->setFile($file_name)
->setLine($class->getLineNumber());
@@ -112,6 +170,7 @@
$this->parseParams($matom, $method);
+ $matom->setContext($namespace);
$matom->setName($method->getChildByIndex(2)->getConcreteString());
$matom->setLine($method->getLineNumber());
$matom->setFile($file_name);

File Metadata

Mime Type
text/plain
Expires
Oct 10 2025, 2:42 AM (10 w, 5 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
11622320
Default Alt Text
D9792.id23651.diff (3 KB)

Event Timeline