Created
June 29, 2012 01:24
-
-
Save simshaun/3015112 to your computer and use it in GitHub Desktop.
More reflection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Color {} | |
class UnitOfWeight {} | |
interface Vehicle { | |
/** | |
* I'm a docblock! | |
*/ | |
public function setColor(Color $color, $foo = 'foo', $bar = array('bar1', 'bar2'), $bool = false); | |
public function setWeight(UnitOfWeight $weight); | |
} | |
/*include 'reflection_php5.php'; | |
echo '------------------------------------------------------' . PHP_EOL . PHP_EOL; | |
$r = new SimpleReflection('Vehicle'); | |
foreach ($r->getMethods() as $method) | |
{ | |
echo $r->getSignature($method); | |
echo PHP_EOL . PHP_EOL . '-------------' . PHP_EOL . PHP_EOL; | |
} | |
return;*/ | |
// ------ // | |
/** | |
* Necessary because $param->getClass() requires the class to be loaded. | |
* We don't want to have to load every class. | |
*/ | |
function getClassName(ReflectionParameter $param) { | |
preg_match('/\[\s\<\w+?>\s([\w]+)/s', $param->__toString(), $matches); | |
return isset($matches[1]) ? $matches[1] : null; | |
} | |
echo PHP_EOL . '--------------------------------------------------------' . PHP_EOL; | |
$reflection = new ReflectionClass('Vehicle'); | |
foreach ($reflection->getMethods() as $method) | |
{ | |
echo PHP_EOL . PHP_EOL . '-------------' . PHP_EOL . PHP_EOL; | |
$docBlock = $method->getDocComment(); | |
$modifiers = implode(' ', Reflection::getModifierNames($method->getModifiers())); | |
$name = $method->name; | |
$params = array(); | |
foreach ($method->getParameters() as $param) | |
{ | |
$className = getClassName($param); | |
$sig = ! empty($className) ? $className . ' ' : null; | |
$sig .= '$' . $param->name; | |
if ($param->isOptional()) | |
{ | |
$val = null; | |
$defaultVal = $param->getDefaultValue(); | |
if (is_array($defaultVal)) | |
{ | |
// TODO: Print the string representation of the actual array. | |
$val .= 'array'; | |
} | |
else if (is_string($defaultVal)) | |
{ | |
// TODO: Detect quote marks and wrap with the appropriate style of quote marks. | |
$val .= "'" . $defaultVal . "'"; | |
} | |
else if (is_null($defaultVal)) | |
{ | |
$val .= 'null'; | |
} | |
else if (is_bool($defaultVal)) | |
{ | |
$val .= $defaultVal ? 'true' : 'false'; | |
} | |
$sig .= ' = ' . $val; | |
} | |
$params[] = $sig; | |
} | |
echo ! empty($docBlock) ? $docBlock . PHP_EOL : null; | |
echo ! empty($modifiers) ? $modifiers . ' ' : null; | |
echo "function $name" . '(' . implode(', ', $params) . ')'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment