private function getMethodData(ReflectionMethod $reflectionMethod, $classContent, &$classData) { //TODO: parse local variable $modifiers = Reflection::getModifierNames($reflectionMethod->getModifiers()); $startLine = $reflectionMethod->getStartLine(); $endLine = $reflectionMethod->getEndline(); $parameters = $reflectionMethod->getParameters(); $docComment = $reflectionMethod->getDocComment(); if (!$docComment) { $docComment = ""; } $parsedComment = $this->parseDocComment($docComment, 'method'); $out = array(); $out['params'] = array(); $out['docComment'] = $this->trimDocComment($docComment); $out['inheritdoc'] = $parsedComment['inheritdoc']; $out['startLine'] = $startLine; $out['endLine'] = $endLine; $origin = $reflectionMethod->getDeclaringClass()->getFileName() == false ? "" : $reflectionMethod->getDeclaringClass()->getFileName(); $origin = $this->normalizePath($origin); $out['origin'] = $origin; $params = array(); foreach ($parameters as $parameter) { $parameterName = '$' . $parameter->getName(); try { $parameterClass = $parameter->getClass(); } catch (\Exception $e) { $parameterClass = ""; } $parameterType = ""; //not defined try to find in the doccomment if (empty($parameterClass)) { if (array_key_exists($parameterName, $parsedComment['params'])) { $parameterType = $parsedComment['params'][$parameterName]; } } else { $parameterType = $parameter->getClass()->getName(); } $params[] = $parameterType . ' ' . $parameterName; $out['params'][$parameterName] = $parameterType; } if (array_key_exists('return', $parsedComment) && $parsedComment['return'] != "") { $returnType = ""; $arrayReturn = 0; $returnTypes = explode('|', trim($parsedComment['return'])); foreach ($returnTypes as $rType) { if (preg_match('/\\[\\]$/', $rType)) { $arrayReturn = 1; $rType = trim($rType, "[]"); } if (!$this->isScalar($rType)) { $returnType = $rType; if ($returnType[0] == '\\') { $returnType = substr($returnType, 1); } break; } } if (empty($returnType)) { $out['return'] = ""; } else { $out['return'] = $this->guessClass($returnType, $classData['namespaces']); } $out['array_return'] = $arrayReturn; } $return = empty($parsedComment['return']) ? "none" : $parsedComment['return']; $out['signature'] = '(' . join(', ', $params) . ') : ' . $return; foreach ($modifiers as $modifier) { $classData['methods']['modifier'][$modifier][] = $reflectionMethod->name; } $classData['methods']['all'][$reflectionMethod->name] = $out; }
<pre> <?php class Counter { private static $c = 0; public static final function increment() { return ++self::$c; } } // Создание экземпляра класса ReflectionMethod $method = new ReflectionMethod('Counter', 'increment'); // exit; // Вывод основной информации printf("===> %s%s%s%s%s%s%s метод '%s' (который является %s)\n" . " объявлен в %s\n" . " строки с %d по %d\n" . " имеет модификаторы %d[%s]\n", $method->isInternal() ? 'Встроенный' : 'Пользовательский', $method->isAbstract() ? ' абстрактный' : '', $method->isFinal() ? ' финальный' : '', $method->isPublic() ? ' public' : '', $method->isPrivate() ? ' private' : '', $method->isProtected() ? ' protected' : '', $method->isStatic() ? ' статический' : '', $method->getName(), $method->isConstructor() ? 'конструктором' : 'обычным методом', $method->getFileName(), $method->getStartLine(), $method->getEndline(), $method->getModifiers(), implode(' ', Reflection::getModifierNames($method->getModifiers()))); // Вывод статических переменных, если они есть if ($statics = $method->getStaticVariables()) { printf("---> Статическая переменная: %s\n", var_export($statics, 1)); } // Вызов метода printf("---> Результат вызова: "); $result = $method->invoke(3); echo $result; ?> </pre>
class Counter { private static $c = 0; /** * Increment counter * * @final * @static * @access public * @return int */ public static final function increment() { return ++self::$c; } } // Create an instance of the ReflectionMethod class $method = new ReflectionMethod('Counter', 'increment'); // Print out basic information printf("===> The %s%s%s%s%s%s%s method '%s' (which is %s)\n" . " declared in %s\n" . " lines %d to %d\n" . " having the modifiers %d[%s]\n", $method->isInternal() ? 'internal' : 'user-defined', $method->isAbstract() ? ' abstract' : '', $method->isFinal() ? ' final' : '', $method->isPublic() ? ' public' : '', $method->isPrivate() ? ' private' : '', $method->isProtected() ? ' protected' : '', $method->isStatic() ? ' static' : '', $method->getName(), $method->isConstructor() ? 'the constructor' : 'a regular method', $method->getFileName(), $method->getStartLine(), $method->getEndline(), $method->getModifiers(), implode(' ', Reflection::getModifierNames($method->getModifiers()))); echo "\n"; // Print documentation comment printf("---> Documentation:\n %s\n", var_export($method->getDocComment(), 1)); // Print static variables if existant if ($statics = $method->getStaticVariables()) { printf("---> Static variables: %s\n", var_export($statics, 1)); } echo "\n"; // Invoke the method printf("---> Invocation results in: "); var_dump($method->invoke(NULL));
<?php $hostDir = dirname(__FILE__); $hostDir = $hostDir . '\\class'; $filesNames = scanDir($hostDir); foreach ($filesNames as $fileName) { if ($fileName == "." || $fileName === "..") { continue; } else { //echo $fileUrl = $fileName; include 'class/' . $fileName; } } foreach (get_declared_classes() as $class) { $reflectionClass = new ReflectionClass($class); if ($reflectionClass->isUserDefined()) { //echo $reflectionClass->getEndLine(); //Reflection::export($reflectionClass); //Reflection::getName($reflectionClass); $className = $reflectionClass->getName(); $methodsName = $reflectionClass->getMethods(); // var_dump($methodsName);exit; foreach ($methodsName as $method) { $method = new ReflectionMethod($className, $method->name); $lines = $method->getEndline() - $method->getStartLine(); echo "类名:" . $className . "方法名:" . $method->name . "行数:" . $lines; } } }
/** * Extract the function code from its class. * * @param String $actionsCode * @param ReflectionMethod $method */ public function extractCode($actionsCode, ReflectionMethod $method) { $code = explode("\n", $actionsCode); $slice = array_slice($code, $method->getStartLine() - 1, $method->getEndline() - $method->getStartLine() + 1); $this->code = implode("\n", $slice); $this->characters = strlen(str_replace(array("\n", "\r"), '', $this->code)); $this->codeLength = count($slice); }