/usr/share/php/SuperClosure/Analyzer/TokenAnalyzer.php is in php-superclosure 2.2.0-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 107 108 109 110 111 112 113 114 115 116 117 118 | <?php namespace SuperClosure\Analyzer;
use SuperClosure\Exception\ClosureAnalysisException;
/**
* This is the token based analyzer.
*
* We're using Uses reflection and tokenization to analyze a closure and
* determine its code and context. This is much faster than the AST based
* implementation.
*/
class TokenAnalyzer extends ClosureAnalyzer
{
public function determineCode(array &$data)
{
$this->determineTokens($data);
$data['code'] = implode('', $data['tokens']);
$data['hasThis'] = (strpos($data['code'], '$this') !== false);
}
private function determineTokens(array &$data)
{
$potential = $this->determinePotentialTokens($data['reflection']);
$braceLevel = $index = $step = $insideUse = 0;
$data['tokens'] = $data['context'] = [];
foreach ($potential as $token) {
$token = new Token($token);
switch ($step) {
// Handle tokens before the function declaration.
case 0:
if ($token->is(T_FUNCTION)) {
$data['tokens'][] = $token;
$step++;
}
break;
// Handle tokens inside the function signature.
case 1:
$data['tokens'][] = $token;
if ($insideUse) {
if ($token->is(T_VARIABLE)) {
$varName = trim($token, '$ ');
$data['context'][$varName] = null;
} elseif ($token->is('&')) {
$data['hasRefs'] = true;
}
} elseif ($token->is(T_USE)) {
$insideUse++;
}
if ($token->is('{')) {
$step++;
$braceLevel++;
}
break;
// Handle tokens inside the function body.
case 2:
$data['tokens'][] = $token;
if ($token->is('{')) {
$braceLevel++;
} elseif ($token->is('}')) {
$braceLevel--;
if ($braceLevel === 0) {
$step++;
}
}
break;
// Handle tokens after the function declaration.
case 3:
if ($token->is(T_FUNCTION)) {
throw new ClosureAnalysisException('Multiple closures '
. 'were declared on the same line of code. Could not '
. 'determine which closure was the intended target.'
);
}
break;
}
}
}
private function determinePotentialTokens(\ReflectionFunction $reflection)
{
// Load the file containing the code for the function.
$fileName = $reflection->getFileName();
if (!is_readable($fileName)) {
throw new ClosureAnalysisException(
"Cannot read the file containing the closure: \"{$fileName}\"."
);
}
$code = '';
$file = new \SplFileObject($fileName);
$file->seek($reflection->getStartLine() - 1);
while ($file->key() < $reflection->getEndLine()) {
$code .= $file->current();
$file->next();
}
$code = trim($code);
if (strpos($code, '<?php') !== 0) {
$code = "<?php\n" . $code;
}
return token_get_all($code);
}
protected function determineContext(array &$data)
{
// Get the values of the variables that are closed upon in "use".
$values = $data['reflection']->getStaticVariables();
// Construct the context by combining the variable names and values.
foreach ($data['context'] as $name => &$value) {
if (isset($values[$name])) {
$value = $values[$name];
}
}
}
}
|