Version 1
[yaffs-website] / web / modules / contrib / devel / kint / kint / parsers / custom / classmethods.php
1 <?php
2
3 class Kint_Parsers_ClassMethods extends kintParser
4 {
5         private static $cache = array();
6
7         protected function _parse( &$variable )
8         {
9                 if ( !KINT_PHP53 || !is_object( $variable ) ) return false;
10
11                 $className = get_class( $variable );
12
13                 # assuming class definition will not change inside one request
14                 if ( !isset( self::$cache[ $className ] ) ) {
15                         $reflection = new ReflectionClass( $variable );
16
17                         $public = $private = $protected = array();
18
19                         // Class methods
20                         foreach ( $reflection->getMethods() as $method ) {
21                                 $params = array();
22
23                                 // Access type
24                                 $access = implode( ' ', Reflection::getModifierNames( $method->getModifiers() ) );
25
26                                 // Method parameters
27                                 foreach ( $method->getParameters() as $param ) {
28                                         $paramString = '';
29
30                                         if ( $param->isArray() ) {
31                                                 $paramString .= 'array ';
32                                         } else {
33                                                 try {
34                                                         if ( $paramClassName = $param->getClass() ) {
35                                                                 $paramString .= $paramClassName->name . ' ';
36                                                         }
37                                                 } catch ( ReflectionException $e ) {
38                                                         preg_match( '/\[\s\<\w+?>\s([\w]+)/s', $param->__toString(), $matches );
39                                                         $paramClassName = isset( $matches[1] ) ? $matches[1] : '';
40
41                                                         $paramString .= ' UNDEFINED CLASS (' . $paramClassName . ') ';
42                                                 }
43                                         }
44
45                                         $paramString .= ( $param->isPassedByReference() ? '&' : '' ) . '$' . $param->getName();
46
47                                         if ( $param->isDefaultValueAvailable() ) {
48                                                 if ( is_array( $param->getDefaultValue() ) ) {
49                                                         $arrayValues = array();
50                                                         foreach ( $param->getDefaultValue() as $key => $value ) {
51                                                                 $arrayValues[] = $key . ' => ' . $value;
52                                                         }
53
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() === '' ) {
62                                                         $defaultValue = '""';
63                                                 } else {
64                                                         $defaultValue = $param->getDefaultValue();
65                                                 }
66
67                                                 $paramString .= ' = ' . $defaultValue;
68                                         }
69
70                                         $params[] = $paramString;
71                                 }
72
73                                 $output = new kintVariableData;
74
75                                 // Simple DocBlock parser, look for @return
76                                 if ( ( $docBlock = $method->getDocComment() ) ) {
77                                         $matches = array();
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
83                                                         # and saves resources
84                                                         $output->type = self::escape( $lines['return'], 'UTF-8' );
85                                                 }
86                                         }
87                                 }
88
89                                 $output->name   = ( $method->returnsReference() ? '&' : '' ) . $method->getName() . '('
90                                         . implode( ', ', $params ) . ')';
91                                 $output->access = $access;
92
93                                 if ( is_string( $docBlock ) ) {
94                                         $lines = array();
95                                         foreach ( explode( "\n", $docBlock ) as $line ) {
96                                                 $line = trim( $line );
97
98                                                 if ( in_array( $line, array( '/**', '/*', '*/' ) ) ) {
99                                                         continue;
100                                                 } elseif ( strpos( $line, '*' ) === 0 ) {
101                                                         $line = substr( $line, 1 );
102                                                 }
103
104                                                 $lines[] = self::escape( trim( $line ), 'UTF-8' );
105                                         }
106
107                                         $output->extendedValue = implode( "\n", $lines ) . "\n\n";
108                                 }
109
110                                 $declaringClass     = $method->getDeclaringClass();
111                                 $declaringClassName = $declaringClass->getName();
112
113                                 if ( $declaringClassName !== $className ) {
114                                         $output->extendedValue .= "<small>Inherited from <i>{$declaringClassName}</i></small>\n";
115                                 }
116
117                                 $fileName = Kint::shortenPath( $method->getFileName() ) . ':' . $method->getStartLine();
118                                 $output->extendedValue .= "<small>Defined in {$fileName}</small>";
119
120                                 $sortName = $access . $method->getName();
121
122                                 if ( $method->isPrivate() ) {
123                                         $private[ $sortName ] = $output;
124                                 } elseif ( $method->isProtected() ) {
125                                         $protected[ $sortName ] = $output;
126                                 } else {
127                                         $public[ $sortName ] = $output;
128                                 }
129                         }
130
131                         if ( !$private && !$protected && !$public ) {
132                                 self::$cache[ $className ] = false;
133                         }
134
135                         ksort( $public );
136                         ksort( $protected );
137                         ksort( $private );
138
139                         self::$cache[ $className ] = $public + $protected + $private;
140                 }
141
142                 if ( count( self::$cache[ $className ] ) === 0 ) {
143                         return false;
144                 }
145
146                 $this->value = self::$cache[ $className ];
147                 $this->type  = 'Available methods';
148                 $this->size  = count( self::$cache[ $className ] );
149         }
150 }