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