public function testParamProducesCorrectDocBlockLine() { $this->tag->setParamName('foo'); $this->tag->setDatatype('string'); $this->tag->setDescription('bar bar bar'); $this->assertEquals('@param string $foo bar bar bar', $this->tag->generate()); }
public function testTagGettersAndSetters() { $paramTag = new Tag\ParamTag(); $paramTag->setDatatype('string'); $returnTag = new Tag\ReturnTag(); $returnTag->setDatatype('int'); $this->docBlockGenerator->setTag(array('name' => 'blah')); $this->docBlockGenerator->setTag($paramTag); $this->docBlockGenerator->setTag($returnTag); $this->assertEquals(3, count($this->docBlockGenerator->getTags())); $target = <<<EOS /** * @blah * @param string * @return int */ EOS; $this->assertEquals($target, $this->docBlockGenerator->generate()); }
private function handleValueMethod(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class, $all = true) { $type = $prop->getType(); $docblock = new DocBlockGenerator('Construct'); $paramTag = new ParamTag("value", "mixed"); $paramTag->setTypes($type ? $this->getPhpType($type) : "mixed"); $docblock->setTag($paramTag); $param = new ParameterGenerator("value"); if ($type && !$this->isNativeType($type)) { $param->setType($this->getPhpType($type)); } $method = new MethodGenerator("__construct", [$param]); $method->setDocBlock($docblock); $method->setBody("\$this->value(\$value);"); $generator->addMethodFromGenerator($method); $docblock = new DocBlockGenerator('Gets or sets the inner value'); $paramTag = new ParamTag("value", "mixed"); if ($type && $type instanceof PHPClassOf) { $paramTag->setTypes($this->getPhpType($type->getArg()->getType()) . "[]"); } elseif ($type) { $paramTag->setTypes($this->getPhpType($prop->getType())); } $docblock->setTag($paramTag); $returnTag = new ReturnTag("mixed"); if ($type && $type instanceof PHPClassOf) { $returnTag->setTypes($this->getPhpType($type->getArg()->getType()) . "[]"); } elseif ($type) { $returnTag->setTypes($this->getPhpType($type)); } $docblock->setTag($returnTag); $param = new ParameterGenerator("value"); $param->setDefaultValue(null); if ($type && !$this->isNativeType($type)) { $param->setType($this->getPhpType($type)); } $method = new MethodGenerator("value", []); $method->setDocBlock($docblock); $methodBody = "if (\$args = func_get_args()) {" . PHP_EOL; $methodBody .= " \$this->" . $prop->getName() . " = \$args[0];" . PHP_EOL; $methodBody .= "}" . PHP_EOL; $methodBody .= "return \$this->" . $prop->getName() . ";" . PHP_EOL; $method->setBody($methodBody); $generator->addMethodFromGenerator($method); $docblock = new DocBlockGenerator('Gets a string value'); $docblock->setTag(new ReturnTag("string")); $method = new MethodGenerator("__toString"); $method->setDocBlock($docblock); $method->setBody("return strval(\$this->" . $prop->getName() . ");"); $generator->addMethodFromGenerator($method); }
protected function generateDocblock($requestTypeName, $requestType, $returnType) { $docblock = $this->getDocblockGenerator(); $scalarTypes = $this->getScalarTypes(); $isScalarReturn = false; if (isset($scalarTypes[$returnType])) { $returnType = $scalarTypes[$returnType]; $isScalarReturn = true; } if ($requestNamespace = $this->getRequestNamespace()) { $requestType = Soap::REQUEST_NAMESPACE_ALIAS . '\\' . $requestType; if (!$isScalarReturn) { $returnType = Soap::RESPONSE_NAMESPACE_ALIAS . '\\' . $returnType; } } $apiTag = new CodeGenerator\DocBlock\Tag(); $apiTag->setName('api'); $docblock->setTag($apiTag); $parameterTag = new CodeGenerator\DocBlock\Tag\ParamTag(); $parameterTag->setDataType($requestType); $parameterTag->setParamName(lcfirst($requestTypeName)); $docblock->setTag($parameterTag); $returnTag = new CodeGenerator\DocBlock\Tag\ReturnTag(); $returnTag->setDataType($returnType); $docblock->setTag($returnTag); return $docblock; }
public function generate() { $method = $this->getMethodGenerator(); $api = $this->getApi(); $operation = $this->getOperation(); $method->setName($operation->getNickname()); $parameterClassGenerator = $this->getParameterClassGenerator(); $parameterClassGenerator->setName($operation->getNickname()); $parameterGenerator = $this->getParameterGenerator(); $propertyGenerator = $this->getPropertyGenerator(); $docBlockGenerator = $this->getDocblockGenerator(); $docBlockGenerator->setShortDescription($operation->getSummary()); $docBlockGenerator->setLongDescription(strip_tags($operation->getNotes())); $hasRequired = false; $routeParams = array(); foreach ($operation->getParameters() as $swaggerParameter) { switch ($swaggerParameter->getParamType()) { case self::PARAM_TYPE_PATH: if ($swaggerParameter->getRequired()) { $hasRequired = true; } $parameters = $method->getParameters(); if (isset($parameters[$swaggerParameter->getName()])) { continue; } $parameter = clone $parameterGenerator; $parameter->setName($swaggerParameter->getName()); $method->setParameter($parameter); $paramTag = new CodeGenerator\DocBlock\Tag\ParamTag(); $paramTag->setDataType($swaggerParameter::getType($swaggerParameter->getDataType())); $paramTag->setParamName($swaggerParameter->getName()); $paramDescription = $swaggerParameter->getDescription(); $paramTag->setDescription($paramDescription); $docBlockGenerator->setTag($paramTag); $routeParams[] = $parameter; break; case self::PARAM_TYPE_QUERY: case self::PARAM_TYPE_FORM: $propertyName = lcfirst($swaggerParameter->getName()); if (($postParamStart = strpos($propertyName, '[')) !== false) { $propertyPostParams = substr($propertyName, $postParamStart); $propertyName = substr($propertyName, 0, $postParamStart); echo "{$method->getName()}::\${$propertyName} has: {$propertyPostParams}" . PHP_EOL; } if ($parameterClassGenerator->hasProperty($propertyName)) { continue; } $property = clone $propertyGenerator; $property->setName($propertyName); $propertyDocblock = new CodeGenerator\DocBlockGenerator(); $propertyDocblock->setShortDescription($swaggerParameter->getDescription()); $propertyDescription = ''; if ($allowableValues = $swaggerParameter->getAllowableValues()) { if ($allowableValues->getValueType() == 'LIST') { $propertyDescription .= 'Allowable values: [' . implode(', ', $allowableValues->getValues()) . ']' . PHP_EOL; } } $propertyDocblock->setLongDescription($propertyDescription); if (!$this->isDocBlockEmpty($propertyDocblock)) { $property->setDocBlock($propertyDocblock); } $parameterClassGenerator->addPropertyFromGenerator($property); $getterGenerator = $this->generateParameterGetter($propertyName, $swaggerParameter); $setterGenerator = $this->generateParameterSetter($propertyName, $swaggerParameter); $parameterClassGenerator->addMethods(array($getterGenerator, $setterGenerator)); break; } } if (count($parameterClassGenerator->getProperties())) { $queryParameter = clone $parameterGenerator; $queryParameter->setName($operation->getNickname()); $queryParameter->setType(RestGenerator::REQUEST_NAMESPACE_ALIAS . '\\' . $operation->getNickname()); if (!$hasRequired) { $queryParameter->setDefaultValue(new CodeGenerator\ValueGenerator(null)); } $paramTag = new CodeGenerator\DocBlock\Tag\ParamTag(); $paramTag->setDataType(RestGenerator::REQUEST_NAMESPACE_ALIAS . '\\' . $operation->getNickname()); $paramTag->setParamName($operation->getNickname()); if (!$hasRequired) { $paramTag->setDescription(' = null'); } $docBlockGenerator->setTag($paramTag); $method->setParameter($queryParameter); } else { $queryParameter = null; } $method->setDocBlock($docBlockGenerator); $body = $this->getBody($routeParams, $queryParameter); $method->setBody($body); }
public function testConstructorWithOptions() { $this->tag->setOptions(array('variableName' => 'foo', 'types' => array('string'), 'description' => 'description')); $tagWithOptionsFromConstructor = new ParamTag('foo', array('string'), 'description'); $this->assertEquals($this->tag->generate(), $tagWithOptionsFromConstructor->generate()); }