Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/simpletestlib/reflection_php5.php
Go to the documentation of this file.
00001 <?php
00014 class SimpleReflection {
00015     var $_interface;
00016 
00022     function SimpleReflection($interface) {
00023         $this->_interface = $interface;
00024     }
00025 
00033     function classExists() {
00034         if (! class_exists($this->_interface)) {
00035             return false;
00036         }
00037         $reflection = new ReflectionClass($this->_interface);
00038         return ! $reflection->isInterface();
00039     }
00040 
00047     function classExistsSansAutoload() {
00048         return class_exists($this->_interface, false);
00049     }
00050 
00057     function classOrInterfaceExists() {
00058         return $this->_classOrInterfaceExistsWithAutoload($this->_interface, true);
00059     }
00060 
00067     function classOrInterfaceExistsSansAutoload() {
00068         return $this->_classOrInterfaceExistsWithAutoload($this->_interface, false);
00069     }
00070 
00079     function _classOrInterfaceExistsWithAutoload($interface, $autoload) {
00080         if (function_exists('interface_exists')) {
00081             if (interface_exists($this->_interface, $autoload)) {
00082                 return true;
00083             }
00084         }
00085         return class_exists($this->_interface, $autoload);
00086     }
00087 
00094     function getMethods() {
00095         return array_unique(get_class_methods($this->_interface));
00096     }
00097 
00105     function getInterfaces() {
00106         $reflection = new ReflectionClass($this->_interface);
00107         if ($reflection->isInterface()) {
00108             return array($this->_interface);
00109         }
00110         return $this->_onlyParents($reflection->getInterfaces());
00111     }
00112 
00119     function getInterfaceMethods() {
00120         $methods = array();
00121         foreach ($this->getInterfaces() as $interface) {
00122             $methods = array_merge($methods, get_class_methods($interface));
00123         }
00124         return array_unique($methods);
00125     }
00126 
00134     function _isInterfaceMethod($method) {
00135         return in_array($method, $this->getInterfaceMethods());
00136     }
00137 
00143     function getParent() {
00144         $reflection = new ReflectionClass($this->_interface);
00145         $parent = $reflection->getParentClass();
00146         if ($parent) {
00147             return $parent->getName();
00148         }
00149         return false;
00150     }
00151 
00157     function isAbstract() {
00158         $reflection = new ReflectionClass($this->_interface);
00159         return $reflection->isAbstract();
00160     }
00161 
00167     function isInterface() {
00168         $reflection = new ReflectionClass($this->_interface);
00169         return $reflection->isInterface();
00170     }
00171 
00178     function hasFinal() {
00179         $reflection = new ReflectionClass($this->_interface);
00180         foreach ($reflection->getMethods() as $method) {
00181             if ($method->isFinal()) {
00182                 return true;
00183             }
00184         }
00185         return false;
00186     }
00187 
00196     function _onlyParents($interfaces) {
00197         $parents = array();
00198         $blacklist = array();
00199         foreach ($interfaces as $interface) {
00200             foreach($interfaces as $possible_parent) {
00201                 if ($interface->getName() == $possible_parent->getName()) {
00202                     continue;
00203                 }
00204                 if ($interface->isSubClassOf($possible_parent)) {
00205                     $blacklist[$possible_parent->getName()] = true;
00206                 }
00207             }
00208             if (!isset($blacklist[$interface->getName()])) {
00209                 $parents[] = $interface->getName();
00210             }
00211         }
00212         return $parents;
00213     }
00214 
00221     function _isAbstractMethod($name) {
00222         $interface = new ReflectionClass($this->_interface);
00223         if (! $interface->hasMethod($name)) {
00224             return false;
00225         }
00226         return $interface->getMethod($name)->isAbstract();
00227     }
00228 
00235     function _isConstructor($name) {
00236         return ($name == '__construct') || ($name == $this->_interface);
00237     }
00238 
00245     function _isAbstractMethodInParents($name) {
00246         $interface = new ReflectionClass($this->_interface);
00247         $parent = $interface->getParentClass();
00248         while($parent) {
00249             if (! $parent->hasMethod($name)) {
00250                 return false;
00251             }
00252             if ($parent->getMethod($name)->isAbstract()) {
00253                 return true;
00254             }
00255             $parent = $parent->getParentClass();
00256         }
00257         return false;
00258     }
00259 
00266     function _isStaticMethod($name) {
00267         $interface = new ReflectionClass($this->_interface);
00268         if (! $interface->hasMethod($name)) {
00269             return false;
00270         }
00271         return $interface->getMethod($name)->isStatic();
00272     }
00273 
00282     function getSignature($name) {
00283         if ($name == '__set') {
00284             return 'function __set($key, $value)';
00285         }
00286         if ($name == '__call') {
00287             return 'function __call($method, $arguments)';
00288         }
00289         if (version_compare(phpversion(), '5.1.0', '>=')) {
00290             if (in_array($name, array('__get', '__isset', $name == '__unset'))) {
00291                 return "function {$name}(\$key)";
00292             }
00293         }
00294         if ($name == '__toString') {
00295             return "function $name()";
00296         }
00297         if ($this->_isInterfaceMethod($name) ||
00298                 $this->_isAbstractMethod($name) ||
00299                 $this->_isAbstractMethodInParents($name) ||
00300                 $this->_isStaticMethod($name)) {
00301             return $this->_getFullSignature($name);
00302         }
00303         return "function $name()";
00304     }
00305 
00314     function _getFullSignature($name) {
00315         $interface = new ReflectionClass($this->_interface);
00316         $method = $interface->getMethod($name);
00317         $reference = $method->returnsReference() ? '&' : '';
00318         $static = $method->isStatic() ? 'static ' : '';
00319         return "{$static}function $reference$name(" .
00320                 implode(', ', $this->_getParameterSignatures($method)) .
00321                 ")";
00322     }
00323 
00332     function _getParameterSignatures($method) {
00333         $signatures = array();
00334         foreach ($method->getParameters() as $parameter) {
00335             $signature = '';
00336             $type = $parameter->getClass();
00337             if (is_null($type) && version_compare(phpversion(), '5.1.0', '>=') && $parameter->isArray()) {
00338                 $signature .= 'array ';
00339             } elseif (!is_null($type)) {
00340                 $signature .= $type->getName() . ' ';
00341             }
00342             if ($parameter->isPassedByReference()) {
00343                 $signature .= '&';
00344             }
00345             $signature .= '$' . $this->_suppressSpurious($parameter->getName());
00346             if ($this->_isOptional($parameter)) {
00347                 $signature .= ' = null';
00348             }
00349             $signatures[] = $signature;
00350         }
00351         return $signatures;
00352     }
00353 
00362     function _suppressSpurious($name) {
00363         return str_replace(array('[', ']', ' '), '', $name);
00364     }
00365 
00373     function _isOptional($parameter) {
00374         if (method_exists($parameter, 'isOptional')) {
00375             return $parameter->isOptional();
00376         }
00377         return false;
00378     }
00379 }
00380 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations