forked from troelskn/phpweaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.inc.php
106 lines (101 loc) · 3.57 KB
/
transform.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
interface Transformer extends Scanner {
function getOutput();
}
/** Just a dummy really */
class PassthruTransformer implements Transformer {
protected $output = "";
function accept(Token $token) {
$this->output .= $token->getText();
}
function getOutput() {
return $this->output;
}
}
interface BufferEditor {
function editBuffer(TokenBuffer $buffer);
}
class PassthruBufferEditor implements BufferEditor {
function editBuffer(TokenBuffer $buffer) {}
}
class DocCommentEditorTransformer implements Transformer {
protected $function_body_scanner;
protected $modifiers_scanner;
protected $parameters_scanner;
protected $editor;
protected $state = 0;
protected $buffer;
function __construct(FunctionBodyScanner $function_body_scanner, ModifiersScanner $modifiers_scanner, FunctionParametersScanner $parameters_scanner, BufferEditor $editor) {
$this->function_body_scanner = $function_body_scanner;
$this->modifiers_scanner = $modifiers_scanner;
$this->parameters_scanner = $parameters_scanner;
$this->editor = $editor;
$this->buffer = new TokenBuffer();
}
function accept(Token $token) {
if ($token->isA(T_DOC_COMMENT)) {
$this->state = 1;
$this->raiseBuffer();
} elseif ($this->state === 0 && ($this->modifiers_scanner->isActive() || $token->isA(T_FUNCTION))) {
$this->state = 1;
$this->raiseBuffer();
} elseif ($this->state > 0 && $this->function_body_scanner->isActive()) {
$this->editor->editBuffer($this->buffer);
$this->state = 0;
$this->flushBuffers();
} elseif ($token->isA(T_INTERFACE) || $token->isA(T_CLASS) || ($token->isA(T_VARIABLE) && !$this->parameters_scanner->isActive())) {
$this->state = 0;
$this->flushBuffers();
}
$this->buffer->append($token);
}
function raiseBuffer() {
$this->flushBuffers();
$this->buffer = $this->buffer->raise();
}
function flushBuffers() {
while ($this->buffer->hasSuper()) {
$this->buffer = $this->buffer->flush();
}
}
function getOutput() {
$this->flushBuffers();
return $this->buffer->toText();
}
}
/** Uses result from a trace to construct docblocks */
class TracerDocBlockEditor implements BufferEditor {
protected $signatures;
protected $class_scanner;
protected $function_body_scanner;
function __construct(Signatures $signatures, ClassScanner $class_scanner, FunctionBodyScanner $function_body_scanner) {
$this->signatures = $signatures;
$this->class_scanner = $class_scanner;
$this->function_body_scanner = $function_body_scanner;
}
function getCommentFor($func, $class = "") {
if ($this->signatures->has($func, $class)) {
$signature = $this->signatures->get($func, $class);
$doc = "/**\n";
foreach ($signature->getArguments() as $argument) {
$doc .= ' * @param ' . $argument->getType() . "\n";
}
$doc .= ' * @return ' . $signature->getReturnType() . "\n";
$doc .= " *" . "/";
return $doc;
}
}
function editBuffer(TokenBuffer $buffer) {
$text = $this->getCommentFor($this->function_body_scanner->getName(), $this->class_scanner->getCurrentClass());
if (!$text) {
return;
}
if (!$buffer->getFirstToken()->isA(T_DOC_COMMENT)) {
$buffer->prepend(new Token("\n ", -1, $buffer->getFirstToken()->getDepth()));
$buffer->prepend(new Token('/** */', T_DOC_COMMENT, $buffer->getFirstToken()->getDepth()));
}
$current = $buffer->getFirstToken();
$new_token = new Token($text, $current->getToken(), $current->getDepth());
$buffer->replaceToken($current, $new_token);
}
}