<?php
// TODO: show full signature of each element?
require_once __DIR__ . '/codeexplorer_api.inc.php';
define('ICON_TYPE_FUNCTION', 1);
define('ICON_TYPE_CONSTRUCTOR', 2);
define('ICON_TYPE_DESTRUCTOR', 3);
define('ICON_TYPE_MAGICMETHOD', 4);
define('ICON_TYPE_INTERFACE', 7);
define('ICON_TYPE_ERROR', 11);
class MyFastPHPIcon extends FastPHPIcon {
public function imageIndex() {
if (($this->getType() == ICON_TYPE_CLASS
) && (!$this->isAbstract())) return 0; // class
else if (($this->getType() == ICON_TYPE_CLASS
) && ( $this->isAbstract())) return 1; // abstract class
else if (($this->getType() == ICON_TYPE_INTERFACE
) ) return 2; // interface
else if (($this->getType() == ICON_TYPE_TRAIT
) ) return 3; // trait
else if (($this->getType() == ICON_TYPE_CONST
) && ($this->isPrivate()) ) return 4; // private const
else if (($this->getType() == ICON_TYPE_VAR
) && ($this->isPrivate()) ) return 5; // private var
else if (($this->isMethod()) && ($this->isPrivate()) && (!$this->isAbstract())) return 6; // private function
else if (($this->isMethod()) && ($this->isPrivate()) && ( $this->isAbstract())) return 7; // private abstract function
else if (($this->getType() == ICON_TYPE_CONST
) && ($this->isProtected()) ) return 8; // protected const
else if (($this->getType() == ICON_TYPE_VAR
) && ($this->isProtected()) ) return 9; // protected var
else if (($this->isMethod()) && ($this->isProtected()) && (!$this->isAbstract())) return 10; // protected function
else if (($this->isMethod()) && ($this->isProtected()) && ( $this->isAbstract())) return 11; // protected abstract function
else if (($this->getType() == ICON_TYPE_CONST
) && ($this->isPublic()) ) return 12; // public const
else if (($this->getType() == ICON_TYPE_VAR
) && ($this->isPublic()) ) return 13; // public var
else if (($this->isMethod()) && ($this->isPublic()) && (!$this->isAbstract())) return 14; // public function
else if (($this->isMethod()) && ($this->isPublic()) && ( $this->isAbstract())) return 15; // public abstract function
else if (($this->getType() == ICON_TYPE_TODO
) ) return 16; // To-Do comment
else return -1;
}
public function isMethod() {
return (($this->getType() == ICON_TYPE_FUNCTION
) ||
($this->getType() == ICON_TYPE_CONSTRUCTOR
) ||
($this->getType() == ICON_TYPE_DESTRUCTOR
) ||
($this->getType() == ICON_TYPE_MAGICMETHOD
));
}
}
class MyFastPHPCodeExplorer {
public function handle($code) {
// Quick'n'Dirty fix to correctly parse the line
// test(XYZ::class)
// Quick'n'Dirty fix to correctly parse the line
// $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
$wait_function = false;
$wait_const = false;
$wait_class = false;
$wait_trait = false;
$icon = new MyFastPHPIcon();
$wait_interface = false;
$wait_abstract_func_list_end = false;
$dep = 0;
$insideFuncAry = array();
if (!$token) {
FastPHPWriter::outputLeafNode($icon, 0, 'SYNTAX ERROR');
}
foreach ($token as $data) {
if ($data == '{') $dep++;
if ($data == '}') {
$dep--;
if ((count($levelAry) > 0) && (self::array_peek($levelAry) == $dep)) {
FastPHPWriter::outputDecreaseLevel();
}
if ((count($insideFuncAry) > 0) && (self::array_peek($insideFuncAry) == $dep)) {
}
}
$token = (!is_array($data)) ?
null : $data[0];
$value = (!is_array($data)) ?
null : $data[1];
$line = (!is_array($data)) ?
null : $data[2];
if ($value == '${') $dep++; // TODO: "${...}" ??? "{$...}" ???
if ($wait_function && ($data == '{')) {
$wait_function = false; // Anonymous functions do not have a name
}
if ($wait_function && ($token == T_STRING)) {
$wait_function = false;
if ($icon->isAbstract()) {
$desc = "abstract function $value()";
$wait_abstract_func_list_end = true;
} else {
$desc = "function $value()";
$insideFuncAry[] = $dep;
}
if ($value == '__construct') { // TODO: auch eine methode mit dem namen der klasse soll eine konstruktor sein
$icon->setType(ICON_TYPE_CONSTRUCTOR
);
} else if ($value == '__destruct') {
$icon->setType(ICON_TYPE_DESTRUCTOR
);
} else if (substr($value, 0, 2) == '__') {
$icon->setType(ICON_TYPE_MAGICMETHOD
);
} else {
$icon->setType(ICON_TYPE_FUNCTION
);
}
FastPHPWriter::outputLeafNode($icon, $line, $desc);
}
if ($wait_class && ($token == T_STRING)) {
if ($icon->isAbstract()) {
$desc = "Abstract Class $value\n";
} else {
$desc = "Class $value\n";
}
$wait_class = false;
$levelAry[] = $dep;
FastPHPWriter::outputLeafNode($icon, $line, $desc);
FastPHPWriter::outputIncreaseLevel();
}
if ($wait_trait && ($token == T_STRING)) {
$desc = "Trait $value\n";
$wait_trait = false;
$levelAry[] = $dep;
FastPHPWriter::outputLeafNode($icon, $line, $desc);
FastPHPWriter::outputIncreaseLevel();
}
if ($wait_interface && ($token == T_STRING)) {
$desc = "Interface $value\n";
$wait_interface = false;
$levelAry[] = $dep;
$icon->setType(ICON_TYPE_INTERFACE
);
FastPHPWriter::outputLeafNode($icon, $line, $desc);
FastPHPWriter::outputIncreaseLevel();
}
if ($wait_const && ($token == T_STRING)) {
$desc = "const $value\n";
$wait_const = false;
FastPHPWriter::outputLeafNode($icon, $line, $desc);
}
if ((!$wait_abstract_func_list_end) && (count($levelAry) > 0) && (count($insideFuncAry) == 0) && ($token == T_VARIABLE
)) {
$desc = "$value\n";
FastPHPWriter::outputLeafNode($icon, $line, $desc);
}
if ($token == T_PRIVATE) $icon->setPrivate();
if ($token == T_PROTECTED) $icon->setProtected();
if ($token == T_PUBLIC) $icon->setPublic();
if ($token == T_ABSTRACT) $icon->setAbstract();
if ($token == T_FINAL) $icon->setFinal();
if ($token == T_STATIC) $icon->setStatic();
if (($data == ';') || ($data == '{') || ($data == '}')) {
$wait_abstract_func_list_end = false;
}
if ($token == T_FUNCTION) {
$wait_function = true;
}
if ($token == T_CLASS) {
$wait_class = true;
$dep = 0;
}
if ($token == T_INTERFACE) {
$wait_interface = true;
$dep = 0;
}
if ($token == T_TRAIT) {
$wait_trait = true;
$dep = 0;
}
if ($token == T_CONST) {
$wait_const = true;
}
if (($token == T_COMMENT) && self::isToDoDescription($value)) {
foreach ($comment_lines as $line_no => $comment_line) {
if (self::isToDoDescription($comment_line)) {
// Because a To-Do-entry can stand everywhere (e.g. between a "private" and a "function" keyword)
// we shall not alter the $icon instance
$todoIcon = clone $icon;
$todoIcon->setType(ICON_TYPE_TODO
);
FastPHPWriter::outputLeafNode($todoIcon, $line+$line_no, self::stripComment($comment_line));
}
}
}
}
}
private static function isToDoDescription($comment) {
return ((stripos($comment, 'TODO') !== false) ||
(stripos($comment, 'BUGBUG') !== false) ||
(stripos($comment, 'FIXME') !== false) ||
(stripos($comment, 'XXX') !== false));
}
private static function stripComment($x) {
return $x;
}
private static /*final*/ function array_peek($array) {
return $array[count($array)-1];
}
}
$parser = new MyFastPHPCodeExplorer();
while (true) {
try {
$code = FastPHPReader::readCodeFromEditor();
} catch (FastPHPExitSignalReceivedException $e) {
}
FastPHPWriter::outputHeader();
$parser->handle($code);
FastPHPWriter::outputExit();
FastPHPWriter::signalOutputEnd();
}