public function testGetTypeByName() { $string = ezcReflection::getTypeByName('string'); self::assertEquals('string', $string->getTypeName()); $int = ezcReflection::getTypeByName('int'); self::assertEquals('integer', $int->getTypeName()); $webservice = ezcReflection::getTypeByName('TestWebservice'); self::assertEquals('TestWebservice', $webservice->getTypeName()); $class = ezcReflection::getTypeByName('ezcReflectionClass'); self::assertEquals('ezcReflectionClass', $class->getTypeName()); }
/** * Returns a list of types * * @return ezcReflectionType[] */ public function getTypes() { if (is_null($this->types)) { $typeName = parent::getTypeName(); if ($typeName == ezcReflectionTypeMapper::CANONICAL_NAME_NUMBER) { $this->types = array(ezcReflection::getTypeByName(ezcReflectionTypeMapper::CANONICAL_NAME_INTEGER), ezcReflection::getTypeByName(ezcReflectionTypeMapper::CANONICAL_NAME_FLOAT)); } elseif ($typeName == ezcReflectionTypeMapper::CANONICAL_NAME_CALLBACK) { $this->types = array(ezcReflection::getTypeByName(ezcReflectionTypeMapper::CANONICAL_NAME_STRING), ezcReflection::getTypeByName('mixed[]'), ezcReflection::getTypeByName('Closure')); } else { $this->types = array(); } } return $this->types; }
/** * Internal method for parsing array type names. * * @return void */ protected function _parseTypeName() { //* if (strlen($this->typeName) > 0) { // last two chars are [], thus it should be something like string[] //if ( strlen( $this->typeName ) > 2 and substr( $this->typeName, -2 ) == '[]' ) if (preg_match(ezcReflectionTypeMapper::REGEXP_TYPE_NAME_LIST, $this->typeName, $matches)) { $this->isList = true; $this->isMap = false; $this->keyType = ezcReflection::getTypeByName(ezcReflectionTypeMapper::CANONICAL_NAME_INTEGER); $this->valueType = ezcReflection::getTypeByName($matches[1]); } elseif ($this->typeName == ezcReflectionTypeMapper::CANONICAL_NAME_ARRAY) { $this->isList = false; $this->isMap = true; $this->keyType = ezcReflection::getTypeByName(ezcReflectionTypeMapper::CANONICAL_NAME_MIXED); $this->valueType = ezcReflection::getTypeByName(ezcReflectionTypeMapper::CANONICAL_NAME_MIXED); } elseif (preg_match(ezcReflectionTypeMapper::REGEXP_TYPE_NAME_MAP, $this->typeName, $matches)) { $this->isList = false; $this->isMap = true; $this->keyType = ezcReflection::getTypeByName($matches[1]); $this->valueType = ezcReflection::getTypeByName($matches[2]); } } /*/ $seamsToBeMap = false; $pos = strrpos($this->typeName, '['); //there seams to be an array if ($pos !== false) { //proof there is no array map annotation around $posm = strrpos($this->typeName, ')'); if ($posm !== false) { if ($posm < $pos) { $typeName = substr($this->typeName, 0, $pos); $this->arrayType = ezcReflection::getTypeByName($typeName); } } else { $typeName = substr($this->typeName, 0, $pos); $this->arrayType = ezcReflection::getTypeByName($typeName); } } // TO BE DONE: add support for array(integer => mixed) if (preg_match(self::TYPE_NAME_REGEXP, $this->typeName, $matches)) { $type1 = null; $type2 = null; if (isset($matches[3])) { $type1 = ezcReflection::getTypeByName($matches[3]); } if (isset($matches[5])) { $type2 = ezcReflection::getTypeByName($matches[5]); } if ($type1 == null and $type2 != null) { $this->arrayType = $type2; } elseif ($type1 != null and $type2 == null) { $this->arrayType = $type1; } elseif ($type1 != null and $type2 != null) { $this->mapKeyType = $type1; $this->mapValueType = $type2; } } //*/ }
/** * Returns the type defined in PHPDoc annotations * * @return ezcReflectionType * @since PHP 5.1.0 */ function getReturnType() { $re = $this->docParser->getReturnAnnotations(); if (count($re) == 1 and isset($re[0]) and $re[0] instanceof ezcReflectionAnnotationReturn) { return ezcReflection::getTypeByName($re[0]->getTypeName()); } return null; }
/** * Returns the type of this parameter in form of an ezcReflectionType * * A valid type hint for the parameter will be preferred over a type * annotation. * * @return ezcReflectionType * @throws ReflectionException * if a parameter uses 'self' or 'parent' as type hint, but function * is not a class member, if a parameter uses 'parent' as type hint, * although class does not have a parent, or if the class does not * exist */ public function getType() { $typeHint = $this->getClass(); if ($typeHint instanceof ReflectionClass) { return ezcReflection::getTypeByName($typeHint); } else { return $this->type; } }
/** * Determines the type of the property based on source code annotations. * * @return ezcReflectionType Type of the property * @since PHP 5.1.0 */ public function getType() { $vars = $this->docParser->getVarAnnotations(); if (isset($vars[0])) { return ezcReflection::getTypeByName($vars[0]->getTypeName()); } else { return null; } }