/** * Creates a Descriptor from the provided data. * * @param ArgumentReflector $data * @param ParamDescriptor[] $params * * @return ArgumentDescriptor */ public function create($data, $params = array()) { $argumentDescriptor = new ArgumentDescriptor(); $argumentDescriptor->setName($data->getName()); $argumentDescriptor->setTypes($data->getType() ? array($data->getType()) : array()); /** @var ParamDescriptor $tag */ foreach ($params as $tag) { if ($tag->getVariableName() == $data->getName()) { $argumentDescriptor->setDescription($tag->getDescription()); $types = $tag->getTypes() ?: array($data->getType() ?: 'mixed'); $argumentDescriptor->setTypes($types); } } $argumentDescriptor->setDefault($data->getDefault()); return $argumentDescriptor; }
/** * Overwrites the type and description in the Argument Descriptor with that from the tag if the names match. * * @param ArgumentReflector $argument * @param ParamDescriptor $paramDescriptor * @param ArgumentDescriptor $argumentDescriptor * * @return void */ protected function overwriteTypeAndDescriptionFromParamTag(ArgumentReflector $argument, ParamDescriptor $paramDescriptor, ArgumentDescriptor $argumentDescriptor) { if ($paramDescriptor->getVariableName() != $argument->getName()) { return; } $argumentDescriptor->setDescription($paramDescriptor->getDescription()); $argumentDescriptor->setTypes($paramDescriptor->getTypes() ?: $this->builder->buildDescriptor(new Collection(array($argument->getType() ?: 'mixed')))); }
/** * Creates a new Descriptor from the given Reflector. * * @param MethodTag $data * * @return MethodDescriptor */ public function create($data) { $descriptor = new MethodDescriptor($data->getName()); $descriptor->setDescription($data->getDescription()); $descriptor->setMethodName($data->getMethodName()); $response = new ReturnDescriptor('return'); $response->setTypes($data->getTypes()); $descriptor->setResponse($response); foreach ($data->getArguments() as $argument) { if (count($argument) > 1) { list($argumentType, $argumentName) = $argument; } else { $argumentName = current($argument); $argumentType = 'mixed'; } $argumentDescriptor = new ArgumentDescriptor(); $argumentDescriptor->setTypes(array($argumentType)); $argumentDescriptor->setName($argumentName); $descriptor->getArguments()->set($argumentName, $argumentDescriptor); } return $descriptor; }
/** * Checks if there is a variadic argument in the `@param` tags and adds it to the list of Arguments in * the Descriptor unless there is already one present. * * @param MethodReflector $data * @param MethodDescriptor $methodDescriptor * * @return void */ protected function addVariadicArgument($data, $methodDescriptor) { if (!$data->getDocBlock()) { return; } $paramTags = $data->getDocBlock()->getTagsByName('param'); /** @var ParamTag $lastParamTag */ $lastParamTag = end($paramTags); if (!$lastParamTag) { return; } if ($lastParamTag->isVariadic() && !in_array($lastParamTag->getVariableName(), array_keys($methodDescriptor->getArguments()->getAll()))) { $types = $this->builder->buildDescriptor(new Collection($lastParamTag->getTypes())); $argument = new ArgumentDescriptor(); $argument->setName($lastParamTag->getVariableName()); $argument->setTypes($types); $argument->setDescription($lastParamTag->getDescription()); $argument->setLine($methodDescriptor->getLine()); $argument->setVariadic(true); $methodDescriptor->getArguments()->set($argument->getName(), $argument); } }
/** * @covers phpDocumentor\Descriptor\ArgumentDescriptor::getTypes * @covers phpDocumentor\Descriptor\ArgumentDescriptor::setTypes */ public function testSetAndGetTypes() { $this->assertSame(array(), $this->fixture->getTypes()); $this->fixture->setTypes(array(1)); $this->assertSame(array(1), $this->fixture->getTypes()); }
/** * Construct an argument descriptor given the array representing an argument with a Method Tag in the Reflection * component. * * @param string[] $argument * * @return ArgumentDescriptor */ private function createArgumentDescriptorForMagicMethod($argument) { $argumentType = null; $argumentName = null; $argumentDefault = false; // false means we have not encountered the '=' yet. foreach ($argument as $part) { $part = trim($part); if (!$part) { continue; } if (!$argumentType && $part[0] != '$') { $argumentType = $part; } elseif (!$argumentName) { $argumentName = $part; } elseif ($argumentName && !$argumentType) { $argumentType = $part; } elseif ($part == '=') { $argumentDefault = null; } elseif ($argumentDefault === null) { $argumentDefault = $part; } } if ($argumentDefault === false) { $argumentDefault = null; } // if no name is set but a type is then the input is malformed and we correct for it if ($argumentType && !$argumentName) { $argumentName = $argumentType; $argumentType = null; } // if there is no type then we assume it is 'mixed' if (!$argumentType) { $argumentType = 'mixed'; } $argumentDescriptor = new ArgumentDescriptor(); $argumentDescriptor->setTypes($this->builder->buildDescriptor(new Collection(array($argumentType)))); $argumentDescriptor->setName($argumentName[0] == '$' ? $argumentName : '$' . $argumentName); $argumentDescriptor->setDefault($argumentDefault); return $argumentDescriptor; }