| 1 |
<?php
|
| 2 |
// $Id: simpletest_unit_token_wrapper.php,v 1.2 2008/03/13 01:46:57 boombatower Exp $
|
| 3 |
|
| 4 |
define('T_NONE', -1);
|
| 5 |
|
| 6 |
class simpletest_unit_token_wrapper {
|
| 7 |
private $tokens = array();
|
| 8 |
private $file_path;
|
| 9 |
private $functions = array();
|
| 10 |
private $function_names = array();
|
| 11 |
|
| 12 |
public function simpletest_unit_token_wrapper($contents, $file_path = NULL) {
|
| 13 |
$this->tokens = token_get_all($contents);
|
| 14 |
$this->file_path = $file_path;
|
| 15 |
}
|
| 16 |
|
| 17 |
public function get_tokens() {
|
| 18 |
return $this->tokens;
|
| 19 |
}
|
| 20 |
|
| 21 |
public function get_file_path() {
|
| 22 |
return $this->file_path;
|
| 23 |
}
|
| 24 |
|
| 25 |
public function find_functions() {
|
| 26 |
reset($this->tokens);
|
| 27 |
while (next($this->tokens) !== FALSE) {
|
| 28 |
if ($this->is_type(T_FUNCTION)) {
|
| 29 |
$function = $this->get_function_info();
|
| 30 |
$this->functions[] = $function;
|
| 31 |
$this->function_names[] = $function['name'];
|
| 32 |
}
|
| 33 |
}
|
| 34 |
return $this->functions;
|
| 35 |
}
|
| 36 |
|
| 37 |
public function get_functions() {
|
| 38 |
return $this->functions;
|
| 39 |
}
|
| 40 |
|
| 41 |
public function get_function_names() {
|
| 42 |
return $this->function_names;
|
| 43 |
}
|
| 44 |
|
| 45 |
public function clean_calls(&$function_names) {
|
| 46 |
foreach ($this->functions as &$function) {
|
| 47 |
$count = count($function['calls']);
|
| 48 |
for ($i = 0; $i < $count; $i++) {
|
| 49 |
if (!in_array($function['calls'][$i], $function_names)) {
|
| 50 |
unset($function['calls'][$i]);
|
| 51 |
}
|
| 52 |
}
|
| 53 |
}
|
| 54 |
}
|
| 55 |
|
| 56 |
private function get_function_info() {
|
| 57 |
$function = array();
|
| 58 |
$function['parameters'] = array();
|
| 59 |
$function['calls'] = array();
|
| 60 |
$block_depth = NULL;
|
| 61 |
$current_paramter = '';
|
| 62 |
$open_call = FALSE;
|
| 63 |
$open_array = FALSE;
|
| 64 |
$reference = FALSE;
|
| 65 |
|
| 66 |
while (next($this->tokens) !== FALSE && $block_depth !== 0) {
|
| 67 |
if (empty($function['name'])) {
|
| 68 |
// If function name hasn't been set then look for first string.
|
| 69 |
if ($this->is_type(T_STRING)) {
|
| 70 |
$function['name'] = $this->value();
|
| 71 |
$current_paramter = '';
|
| 72 |
}
|
| 73 |
}
|
| 74 |
else if ($block_depth === NULL) {
|
| 75 |
// If function body hasn't started than look for parameters
|
| 76 |
switch ($this->type()) {
|
| 77 |
case T_VARIABLE:
|
| 78 |
$function['parameters'][$current_paramter = ($reference ? '&' : '') . $this->value()] = NULL;
|
| 79 |
$reference = FALSE;
|
| 80 |
break;
|
| 81 |
case T_STRING:
|
| 82 |
case T_CONSTANT_ENCAPSED_STRING:
|
| 83 |
case T_LNUMBER:
|
| 84 |
case T_DNUMBER:
|
| 85 |
$function['parameters'][$current_paramter] = $this->value();
|
| 86 |
break;
|
| 87 |
case T_NONE:
|
| 88 |
if ($this->value() == '{') {
|
| 89 |
$block_depth = 1;
|
| 90 |
}
|
| 91 |
else if ($this->value() == '&') {
|
| 92 |
$reference = TRUE;
|
| 93 |
}
|
| 94 |
break;
|
| 95 |
}
|
| 96 |
}
|
| 97 |
else {
|
| 98 |
// Look for internal function calls.
|
| 99 |
switch ($this->type()) {
|
| 100 |
case T_STRING:
|
| 101 |
// Possible function call, check to see if it is drupal function.
|
| 102 |
if (!in_array($open_call = $this->value(), $function['calls'])) {
|
| 103 |
$function['calls'][] = $open_call;
|
| 104 |
}
|
| 105 |
break;
|
| 106 |
case T_NONE:
|
| 107 |
if ($open_call && $this->value() == ')') {
|
| 108 |
$open_call = FALSE;
|
| 109 |
}
|
| 110 |
else if ($this->value() == '{') {
|
| 111 |
$block_depth++;
|
| 112 |
}
|
| 113 |
else if ($this->value() == '}') {
|
| 114 |
$block_depth--;
|
| 115 |
}
|
| 116 |
break;
|
| 117 |
}
|
| 118 |
}
|
| 119 |
}
|
| 120 |
return $function;
|
| 121 |
}
|
| 122 |
|
| 123 |
private function is_type($type) {
|
| 124 |
$token = current($this->tokens);
|
| 125 |
if (is_array($token)) {
|
| 126 |
return $token[0] == $type;
|
| 127 |
}
|
| 128 |
return FALSE;
|
| 129 |
}
|
| 130 |
|
| 131 |
private function type() {
|
| 132 |
$token = current($this->tokens);
|
| 133 |
if (is_array($token)) {
|
| 134 |
return $token[0];
|
| 135 |
}
|
| 136 |
return T_NONE;
|
| 137 |
}
|
| 138 |
|
| 139 |
private function value() {
|
| 140 |
$token = current($this->tokens);
|
| 141 |
if (is_array($token)) {
|
| 142 |
return $token[1];
|
| 143 |
}
|
| 144 |
return $token;
|
| 145 |
}
|
| 146 |
|
| 147 |
public function clear_tokens() {
|
| 148 |
$this->tokens = array();
|
| 149 |
}
|
| 150 |
|
| 151 |
public function print_tokens() {
|
| 152 |
foreach ($this->tokens as $token) {
|
| 153 |
if (is_array($token)) {
|
| 154 |
print(token_name($token[0]) .': '. $token[1] ."\n");
|
| 155 |
}
|
| 156 |
else {
|
| 157 |
print($token ."\n");
|
| 158 |
}
|
| 159 |
}
|
| 160 |
}
|
| 161 |
}
|