3 class Kint_Parsers_ClassMethods extends kintParser
5 private static $cache = array();
7 protected function _parse( &$variable )
9 if ( !KINT_PHP53 || !is_object( $variable ) ) return false;
11 $className = get_class( $variable );
13 # assuming class definition will not change inside one request
14 if ( !isset( self::$cache[ $className ] ) ) {
15 $reflection = new ReflectionClass( $variable );
17 $public = $private = $protected = array();
20 foreach ( $reflection->getMethods() as $method ) {
24 $access = implode( ' ', Reflection::getModifierNames( $method->getModifiers() ) );
27 foreach ( $method->getParameters() as $param ) {
30 if ( $param->isArray() ) {
31 $paramString .= 'array ';
34 if ( $paramClassName = $param->getClass() ) {
35 $paramString .= $paramClassName->name . ' ';
37 } catch ( ReflectionException $e ) {
38 preg_match( '/\[\s\<\w+?>\s([\w]+)/s', $param->__toString(), $matches );
39 $paramClassName = isset( $matches[1] ) ? $matches[1] : '';
41 $paramString .= ' UNDEFINED CLASS (' . $paramClassName . ') ';
45 $paramString .= ( $param->isPassedByReference() ? '&' : '' ) . '$' . $param->getName();
47 if ( $param->isDefaultValueAvailable() ) {
48 if ( is_array( $param->getDefaultValue() ) ) {
49 $arrayValues = array();
50 foreach ( $param->getDefaultValue() as $key => $value ) {
51 $arrayValues[] = $key . ' => ' . $value;
54 $defaultValue = 'array(' . implode( ', ', $arrayValues ) . ')';
55 } elseif ( $param->getDefaultValue() === null ) {
56 $defaultValue = 'NULL';
57 } elseif ( $param->getDefaultValue() === false ) {
58 $defaultValue = 'false';
59 } elseif ( $param->getDefaultValue() === true ) {
60 $defaultValue = 'true';
61 } elseif ( $param->getDefaultValue() === '' ) {
64 $defaultValue = $param->getDefaultValue();
67 $paramString .= ' = ' . $defaultValue;
70 $params[] = $paramString;
73 $output = new kintVariableData;
75 // Simple DocBlock parser, look for @return
76 if ( ( $docBlock = $method->getDocComment() ) ) {
78 if ( preg_match_all( '/@(\w+)\s+(.*)\r?\n/m', $docBlock, $matches ) ) {
79 $lines = array_combine( $matches[1], $matches[2] );
80 if ( isset( $lines['return'] ) ) {
81 $output->operator = '->';
82 # since we're outputting code, assumption that the string is utf8 is most likely correct
84 $output->type = self::escape( $lines['return'], 'UTF-8' );
89 $output->name = ( $method->returnsReference() ? '&' : '' ) . $method->getName() . '('
90 . implode( ', ', $params ) . ')';
91 $output->access = $access;
93 if ( is_string( $docBlock ) ) {
95 foreach ( explode( "\n", $docBlock ) as $line ) {
96 $line = trim( $line );
98 if ( in_array( $line, array( '/**', '/*', '*/' ) ) ) {
100 } elseif ( strpos( $line, '*' ) === 0 ) {
101 $line = substr( $line, 1 );
104 $lines[] = self::escape( trim( $line ), 'UTF-8' );
107 $output->extendedValue = implode( "\n", $lines ) . "\n\n";
110 $declaringClass = $method->getDeclaringClass();
111 $declaringClassName = $declaringClass->getName();
113 if ( $declaringClassName !== $className ) {
114 $output->extendedValue .= "<small>Inherited from <i>{$declaringClassName}</i></small>\n";
117 $fileName = Kint::shortenPath( $method->getFileName() ) . ':' . $method->getStartLine();
118 $output->extendedValue .= "<small>Defined in {$fileName}</small>";
120 $sortName = $access . $method->getName();
122 if ( $method->isPrivate() ) {
123 $private[ $sortName ] = $output;
124 } elseif ( $method->isProtected() ) {
125 $protected[ $sortName ] = $output;
127 $public[ $sortName ] = $output;
131 if ( !$private && !$protected && !$public ) {
132 self::$cache[ $className ] = false;
139 self::$cache[ $className ] = $public + $protected + $private;
142 if ( count( self::$cache[ $className ] ) === 0 ) {
146 $this->value = self::$cache[ $className ];
147 $this->type = 'Available methods';
148 $this->size = count( self::$cache[ $className ] );