Example #1
0
 /**
  * @covers \PHP\Manipulator\TokenContainer\Iterator
  */
 public function testIteratorClass()
 {
     $reflection = new \ReflectionClass('PHP\\Manipulator\\TokenContainer\\Iterator');
     $this->assertTrue($reflection->isIterateable());
     $this->assertTrue($reflection->implementsInterface('SeekableIterator'));
     $this->assertTrue($reflection->implementsInterface('Countable'));
     $this->assertTrue($reflection->hasMethod('previous'));
 }
 /**
  * Will sanitize mixed vars by capping recursion
  * and format them in a designer-friendly way by displaying objects' methods stubs
  * A bit dirty as this should be in another Class, but hey
  *
  * @param $value
  * @param int $maxRecursionDepth  The maximum depth of recursion
  * @param int $recursionDepth     The depth of recursion (used internally)
  * @return array|string
  */
 public function sanitize($value, $maxRecursionDepth = self::MAX_DEPTH, $recursionDepth = 0)
 {
     if (is_resource($value)) {
         return 'Resource';
     }
     if (is_array($value)) {
         return $this->sanitizeIterateable($value, $maxRecursionDepth, $recursionDepth);
     }
     if ($value instanceof InvokerException) {
         return $value->getMessage();
     }
     if (is_object($value)) {
         $class = new \ReflectionClass(get_class($value));
         if ($recursionDepth >= $maxRecursionDepth) {
             // We're full, just scrap the vital data
             $classInfo = $class->getName();
             if ($class->hasMethod('getId')) {
                 $getIdMethod = $class->getMethod('getId');
                 if (!$getIdMethod->getNumberOfRequiredParameters()) {
                     $id = $getIdMethod->invoke($value);
                     $classInfo .= $id !== null ? ' #' . $id : ' (no id)';
                 }
             }
             if ($class->hasMethod('__toString')) {
                 // robustness, we don't care about perf
                 try {
                     $classInfo .= ' ' . (string) $value;
                 } catch (\Exception $e) {
                     $classInfo .= '(string) casting throws Exception, please report or fix';
                 }
             }
             if ($value instanceof \DateTime) {
                 $classInfo .= ' : ' . $value->format('Y-m-d H:i:s');
             }
             return $classInfo;
         } else {
             // Get all accessors and their values
             $data = array();
             $data['class'] = '<span title="' . $class->getName() . '">' . $class->getShortName() . '</span>';
             if ($class->isIterateable()) {
                 $data['iterateable'] = $this->sanitizeIterateable($value, $maxRecursionDepth, $recursionDepth);
             } else {
                 $data['accessors'] = array();
                 foreach ($class->getMethods() as $method) {
                     if ($method->isPublic() && preg_match(self::METHOD_IS_ACCESSOR_REGEX, $method->getName())) {
                         $methodInfo = $method->getName() . '(';
                         foreach ($method->getParameters() as $parameter) {
                             $methodInfo .= '$' . $parameter->getName() . ', ';
                         }
                         $methodInfo = ($method->getNumberOfParameters() ? substr($methodInfo, 0, -2) : $methodInfo) . ')';
                         if (!$method->getNumberOfRequiredParameters()) {
                             // Get the value, we don't need params
                             try {
                                 $methodValue = $method->invoke($value);
                                 $data['accessors'][$methodInfo] = $this->sanitize($methodValue, $maxRecursionDepth, $recursionDepth + 1);
                             } catch (\Exception $e) {
                                 $data['accessors'][$methodInfo] = $this->sanitize(new InvokerException('Couldn\'t invoke method: Exception "' . get_class($e) . '" with message "' . $e->getMessage() . '"'), $maxRecursionDepth, $recursionDepth + 1);
                             }
                         } else {
                             // Get only method name and its params
                             $data['accessors'][] = $methodInfo;
                         }
                     }
                 }
             }
             return $data;
         }
     }
     if (is_string($value)) {
         $value = '(string) ' . $value;
     }
     if (is_int($value)) {
         $value = '(int) ' . $value;
     }
     if (is_float($value)) {
         $value = '(float) ' . $value;
     }
     if (is_null($value)) {
         $value = 'null';
     }
     if (is_bool($value)) {
         if ($value) {
             $value = '(bool) true';
         } else {
             $value = '(bool) false';
         }
     }
     return $value;
 }
Example #3
0
 /**
  * @dataProvider dataProvider
  */
 public function testContainerIsIterator($container, $set)
 {
     $reflection = new \ReflectionClass($container);
     $this->assertTrue($reflection->isIterateable());
 }
Example #4
0
print "\n";
print "--- isInstance() ---\n";
var_dump($rb->isInstance(new B()));
var_dump($rb->isInstance(new C()));
print "\n";
print "--- isInstantiable() ---\n";
var_dump($rb->isInstantiable());
print "\n";
print "--- isInterface() ---\n";
var_dump($rb->isInterface());
print "\n";
print "--- isInternal() ---\n";
var_dump($rb->isInternal());
print "\n";
print "--- isIterateable() ---\n";
var_dump($rb->isIterateable());
print "\n";
print "--- isSubclassOf() ---\n";
var_dump($rb->isSubclassOf('A'));
var_dump($rb->isSubclassOf('C'));
print "\n";
print "--- isUserDefined() ---\n";
var_dump($rb->isUserDefined());
print "\n";
print "--- newInstance() ---\n";
var_dump($rb->newInstance());
print "\n";
print "--- newInstanceArgs() ---\n";
var_dump($rb->newInstanceArgs());
print "\n";
print "--- get_defined_functions() ---\n";
Example #5
0
 /**
  *  handles the m* calls
  *  
  *  if you have a list of values, then you can use this function to run the same function on
  *  all the values (similar to array_map). If your values are objects, then you can do stuff
  *  like: out::m($list_of_obj,'->getOne()->isvalid()'). lets, say you want to get see what
  *  the trim output of your list of strings, then do: out::m($list_of_str,'trim');         
  *      
  *  @todo allow the method names to be an array were the first index is the name and
  *        the second -> N is the arguments to pass into the method (eg, array('method',$arg1,$arg2,...))   
  *      
  *  @param  string  $method the externally called method
  *  @param  array $func_arg_list  the args passed into $method
  *  @return out_call   
  */
 private static function fHandle($method, $func_arg_list = array())
 {
     $call_handler = null;
     $list = $func_arg_list[0];
     $method_list = array_slice($func_arg_list, 1);
     $is_valid_list = false;
     if (!empty($method_list)) {
         $is_valid_list = is_array($list);
         if (empty($is_valid_list) && is_object($list)) {
             // http://www.php.net/manual/en/reflectionclass.isiterateable.php
             $rclass = new ReflectionClass(get_class($val));
             $is_valid_list = $rclass->isIterateable();
         }
         //if
     }
     //if
     if ($is_valid_list) {
         $call_handler = self::getCall($method);
         $format_handler = new out_format($call_handler->config());
         $output = array();
         foreach ($list as $key => $val) {
             foreach ($method_list as $method) {
                 if (is_object($val)) {
                     $output[] = sprintf('%d - %s', $key, $format_handler->wrap('span', get_class($val), out_config::COLOR_INDEX));
                     if ($method[0] == '-') {
                         // the method is something like: ->getThis()->get() so eval it...
                         $arg_handler = new out_arg($method, eval(sprintf('return $val%s;', $method)));
                         $arg_handler->config($call_handler->config());
                         $output[] = $format_handler->indent("\t", $arg_handler->out());
                     } else {
                         if (method_exists($val, $method)) {
                             $arg_handler = new out_arg(sprintf('->%s()', $method), call_user_func(array($val, $method)));
                             $arg_handler->config($call_handler->config());
                             $output[] = $format_handler->indent("\t", $arg_handler->out());
                         } else {
                             $output[] = $format_handler->indent("\t", sprintf('->%s() undefined', $method));
                         }
                         //if/else
                     }
                     //if/else
                 } else {
                     $output[] = sprintf('%d - %s', $key, $format_handler->wrap('span', $val, out_config::COLOR_INDEX));
                     // since $val isn't an object, then use it as the value to pass to a function...
                     $arg_handler = new out_arg(sprintf('%s(%s)', $method, $val), call_user_func($method, $val));
                     $arg_handler->config($call_handler->config());
                     $output[] = $format_handler->indent("\t", $arg_handler->out());
                 }
                 //if/else
             }
             //foreach
         }
         //foreach
         $output[] = '';
         $arg_handler = new out_arg('', join("\r\n", $output));
         $arg_handler->type(out_arg::TYPE_STRING_GENERATED);
         $call_handler->set($arg_handler);
     }
     //if
     return $call_handler;
 }
{
    public function getIterator()
    {
    }
}
class ExtendsIteratorImpl extends IteratorImpl
{
}
class ExtendsIteratorAggregateImpl extends IterarorAggregateImpl
{
}
class A
{
}
$classes = array('Traversable', 'Iterator', 'IteratorAggregate', 'ExtendsIterator', 'ExtendsIteratorAggregate', 'IteratorImpl', 'IterarorAggregateImpl', 'ExtendsIteratorImpl', 'ExtendsIteratorAggregateImpl', 'A');
foreach ($classes as $class) {
    $rc = new ReflectionClass($class);
    echo "Is {$class} iterable? ";
    var_dump($rc->isIterateable());
}
echo "\nTest invalid params:\n";
$rc = new ReflectionClass('IteratorImpl');
var_dump($rc->isIterateable(null));
var_dump($rc->isIterateable(null, null));
var_dump($rc->isIterateable(1));
var_dump($rc->isIterateable(1.5));
var_dump($rc->isIterateable(true));
var_dump($rc->isIterateable('X'));
var_dump($rc->isIterateable(null));
echo "\nTest static invocation:\n";
ReflectionClass::isIterateable();
 /**
  * {@inheritdoc}
  */
 public function isIterateable()
 {
     return $this->reflectionClass->isIterateable();
 }
Example #8
0
 /**
  * 构造函数
  *
  * @param mixed $class
  */
 private function __construct($class)
 {
     if (!$class instanceof ReflectionClass) {
         $class = new ReflectionClass($class);
     }
     $this->name = $class->getName();
     $this->filename = $class->getFilename();
     $this->is_interface = $class->isInterface();
     $this->is_abstract = $class->isAbstract();
     $this->is_final = $class->isFinal();
     $this->is_iterateable = $class->isIterateable();
     $this->start_line = $class->getStartLine();
     $this->end_line = $class->getEndLine();
     $this->_reflection_class = $class;
 }
function dump_iterateable($obj)
{
    $reflection = new ReflectionClass($obj);
    var_dump($reflection->isIterateable());
}