/** * Get all use statements. * * @return array A list with all found use statements. */ private function parseUseStatements() { $statements = array(); while ($token = $this->next()) { if ($token[0] === T_USE) { $statements = array_merge($statements, $this->parseUseStatement()); continue; } else { if ($token[0] !== T_NAMESPACE || $this->parseNamespace() != $this->class->getNamespaceName()) { continue; } } // Get fresh array for new namespace. This is to prevent the parser to collect the use statements // for a previous namespace with the same name. This is the case if a namespace is defined twice // or if a namespace with the same name is commented out. $statements = array(); } return $statements; }
/** * Test if can remove a previous stored annotation list. */ public function testRemoveAnnotationList() { /* @var \com\mohiva\common\lang\AnnotationReflector $class */ $class = new ReflectionClass(self::TEST_CLASS); $docReflector = new ReflectionDocComment($class); $annotationList = $docReflector->getAnnotationList(); $key = new HashKey(Hash::ALGO_SHA1, 'php://temp'); $adapter = new ResourceAdapter(new TempResourceContainer(TempFileResource::TYPE)); $container = new AnnotationContainer($adapter, $key); $container->store($class->getDocComment(), $annotationList); $container->remove($class->getDocComment()); $this->assertFalse($container->exists($class->getDocComment())); }
/** * Test if the `getAnnotationList` method return a list of all annotations in a doc comment. */ public function testGetAnnotationList() { $class = new ReflectionClass('\\com\\mohiva\\test\\resources\\common\\lang\\AnnotationTest'); $this->assertInstanceOf('com\\mohiva\\common\\lang\\AnnotationList', $class->getAnnotationList()); }
/** * Create an instance of the given annotation and construct it with the params. * * @param string $annotationName The name of the annotation. * @param array $annotationParams The params for the annotation. * @return \com\mohiva\common\lang\annotations\Annotation An annotation instance. * @throws \com\mohiva\common\io\exceptions\ClassNotFoundException if the annotation class cannot be found. */ private function getAnnotationInstance($annotationName, array $annotationParams) { try { $class = new ReflectionClass($annotationName); } catch (ClassNotFoundException $e) { $message = "The annotation class `{$annotationName}` cannot be found; "; $message .= "called in DocBlock for: {$this->context->getLocation()}; "; throw new ClassNotFoundException($message, null, $e); } // Get all method params $methodParams = array(); $methodDefaultValues = array(); $constructor = $class->getConstructor(); $constructorParams = $constructor->getParameters(); foreach ($constructorParams as $constructorParam) { /* @var \ReflectionParameter $constructorParam */ $paramName = $constructorParam->getName(); if ($constructorParam->isDefaultValueAvailable()) { $default = $constructorParam->getDefaultValue(); $methodDefaultValues[$paramName] = $default; $methodParams[$paramName] = $default; } else { $methodParams[$paramName] = null; } } // Instantiate the class $methodName = array_key_exists(0, $annotationParams) ? 'Unnamed' : 'Named'; $instance = $class->newInstanceArgs($this->{"get{$methodName}InstanceParams"}($annotationName, $methodParams, $methodDefaultValues, $annotationParams)); return $instance; }
/** * Test if the `DoubleParameterAnnotation` class has correct values. */ public function testDoubleParameterAnnotation() { $class = 'com\\mohiva\\test\\resources\\common\\lang\\annotations\\DoubleParameter'; $reflection = new ReflectionClass(self::TEST_CLASS); $iterator = $reflection->getAnnotationList()->getAnnotations(DoubleParameter::NAME)->getIterator(); /* @var \com\mohiva\test\resources\common\lang\annotations\DoubleParameter $annotation */ $annotation = $iterator->current(); $this->assertInstanceOf($class, $annotation); $this->assertInternalType(PHPUnitType::TYPE_STRING, $annotation->getParam1()); $this->assertInternalType(PHPUnitType::TYPE_STRING, $annotation->getParam2()); $this->assertEquals($annotation->getParam1(), "pa\"r\"am1\""); $this->assertEquals($annotation->getParam2(), "param\"2"); $iterator->next(); $annotation = $iterator->current(); $this->assertInstanceOf($class, $annotation); $this->assertInternalType(PHPUnitType::TYPE_STRING, $annotation->getParam1()); $this->assertInternalType(PHPUnitType::TYPE_STRING, $annotation->getParam2()); $this->assertEquals($annotation->getParam1(), "pa\\'r\\'am1\""); $this->assertEquals($annotation->getParam2(), "param\\'2"); $iterator->next(); $annotation = $iterator->current(); $this->assertInstanceOf($class, $annotation); $this->assertInternalType(PHPUnitType::TYPE_STRING, $annotation->getParam1()); $this->assertInternalType(PHPUnitType::TYPE_STRING, $annotation->getParam2()); $this->assertEquals($annotation->getParam1(), "pa'r'am1"); $this->assertEquals($annotation->getParam2(), 'param"2'); $iterator->next(); $annotation = $iterator->current(); $this->assertInstanceOf($class, $annotation); $this->assertInternalType(PHPUnitType::TYPE_STRING, $annotation->getParam1()); $this->assertInternalType(PHPUnitType::TYPE_STRING, $annotation->getParam2()); $this->assertEquals($annotation->getParam1(), 'pa\'r\'am1\''); $this->assertEquals($annotation->getParam2(), 'param\'2'); $iterator->next(); $annotation = $iterator->current(); $this->assertInstanceOf($class, $annotation); $this->assertInternalType(PHPUnitType::TYPE_STRING, $annotation->getParam1()); $this->assertInternalType(PHPUnitType::TYPE_STRING, $annotation->getParam2()); $this->assertEquals($annotation->getParam1(), 'pa\\"r\\"am1\''); $this->assertEquals($annotation->getParam2(), 'param\\"2'); }
/** * Gets a list of properties. * * @param int $filter The optional filter, for filtering desired property types. It's configured * using the `ReflectionProperty` constants. * * @return ReflectionProperty[] An array of `ReflectionProperty` objects. */ public function getProperties($filter = null) { $properties = array(); foreach (parent::getProperties($filter) as $property) { /* @var \ReflectionProperty $property */ $properties[] = new ReflectionProperty($property->class, $property->name); } return $properties; }