Skip to content

Commit 438db42

Browse files
committed
migrate use & const statements into block (more generic)
1 parent f72fcbb commit 438db42

2 files changed

Lines changed: 38 additions & 48 deletions

File tree

src/block.js

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*!
2-
* Copyright (C) 2016 Glayzzle (BSD3 License)
2+
* Copyright (C) 2017 Glayzzle (BSD3 License)
33
* @authors https://github.com/glayzzle/php-reflection/graphs/contributors
44
* @url http://glayzzle.com
55
*/
6-
"use strict";
6+
'use strict';
77

88
var node = require('./node');
99
var ptr = require('./ptr');
@@ -100,6 +100,15 @@ block.prototype.consumeChild = function(ast) {
100100
}
101101
}
102102

103+
// consome trait
104+
else if (ast.kind === 'trait') {
105+
var trait = ptr.create('trait', this, ast);
106+
this.traits.push(trait);
107+
if (this.type !== 'namespace') {
108+
this.getNamespace().traits.push(trait);
109+
}
110+
}
111+
103112
// consome a namespace (from inner statements like declare)
104113
else if (ast.kind === 'namespace') {
105114
node.create('namespace', this.getFile(), ast);
@@ -173,6 +182,29 @@ block.prototype.consumeChild = function(ast) {
173182
ptr.create('define', this, ast)
174183
);
175184
}
185+
186+
// namespace constants
187+
else if (ast.kind === 'constant') {
188+
this.getNamespace().constants.push(
189+
ptr.create('constant', this, ast)
190+
);
191+
}
192+
193+
// namespace use statements
194+
else if (ast.kind === 'usegroup') {
195+
var prefix = ast.name.name;
196+
for(var i = 0; i < ast.items.length; i++) {
197+
var alias = ast.items[i].alias;
198+
var name = ast.items[i].name.name;
199+
if (name[0] !== '\\') name = '\\' + name;
200+
if (!alias) {
201+
alias = name.split('\\');
202+
alias = alias[alias.length - 1];
203+
}
204+
this.getNamespace().uses[alias] = prefix + name;
205+
}
206+
}
207+
176208
// @todo : variables by global statement
177209
// @todo : variables by static statement
178210

@@ -190,23 +222,7 @@ block.prototype.consumeChild = function(ast) {
190222
}
191223
}
192224

193-
194-
/* else if (type === 'trait') {
195-
this.traits.push(
196-
new node.builders['trait'](this, node)
197-
);
198-
} else if (type === 'call') {
199-
if (
200-
ast[1][0] === 'ns' &&
201-
ast[1][1].length === 1 &&
202-
ast[1][1][0] === 'define'
203-
) {
204-
this.defines.push(
205-
new node.builders['define'](this, node)
206-
);
207-
}
208-
}
209-
// @todo use, var */
225+
/* @todo var */
210226
};
211227

212228
module.exports = block;

src/namespace.js

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*!
2-
* Copyright (C) 2016 Glayzzle (BSD3 License)
2+
* Copyright (C) 2017 Glayzzle (BSD3 License)
33
* @authors https://github.com/glayzzle/php-reflection/graphs/contributors
44
* @url http://glayzzle.com
55
*/
6-
"use strict";
6+
'use strict';
77
var block = require('./block');
88
var ptr = require('./ptr');
99

@@ -27,36 +27,10 @@ var namespace = block.extends('namespace');
2727
* @protected Consumes the current ast node
2828
*/
2929
namespace.prototype.consume = function(ast) {
30-
31-
var self = this;
3230
this.name = '\\' + ast.name.name;
3331
this.uses = {};
3432
this.constants = [];
35-
36-
/**
37-
* Iterator over each namespace item
38-
*/
39-
ast.children.forEach(function(item) {
40-
if (item.kind === 'constant') {
41-
self.constants.push(
42-
ptr.create('constant', self, item)
43-
);
44-
} else if (item.kind === 'usegroup') {
45-
var prefix = item.name.name;
46-
for(var i = 0; i < item.items.length; i++) {
47-
var alias = item.items[i].alias;
48-
var name = item.items[i].name.name;
49-
if (name[0] !== '\\') name = '\\' + name;
50-
if (!alias) {
51-
alias = name.split('\\');
52-
alias = alias[alias.length - 1];
53-
}
54-
self.uses[alias] = prefix + name;
55-
}
56-
} else {
57-
self.consumeChild(item);
58-
}
59-
});
33+
this.scanForChilds(ast.children);
6034
};
6135

6236
/**

0 commit comments

Comments
 (0)