Skip to content

Commit

Permalink
Merge pull request #2050 from jaapio/feature/type-system-fixes
Browse files Browse the repository at this point in the history
Restore orginal pre v3 type system
  • Loading branch information
jaapio committed Dec 31, 2018
2 parents 9dbd4b0 + a1f07ab commit 3bc77aa
Show file tree
Hide file tree
Showing 42 changed files with 201 additions and 156 deletions.
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions config/services.yaml
Expand Up @@ -41,20 +41,20 @@ parameters:
- 'tags'
- 'arguments'
'phpDocumentor\Descriptor\ArgumentDescriptor':
- 'types'
- 'type'
'phpDocumentor\Descriptor\PropertyDescriptor':
- 'tags'
- 'types'
- 'type'
'phpDocumentor\Descriptor\ConstantDescriptor':
- 'tags'
- 'types'
'phpDocumentor\Descriptor\Tag\ParamDescriptor': ['types']
'phpDocumentor\Descriptor\Tag\ReturnDescriptor': ['types']
- 'type'
'phpDocumentor\Descriptor\Tag\ParamDescriptor': ['type']
'phpDocumentor\Descriptor\Tag\ReturnDescriptor': ['type']
'phpDocumentor\Descriptor\Tag\SeeDescriptor': ['reference']
'phpDocumentor\Descriptor\Tag\UsesDescriptor': ['reference']
'phpDocumentor\Descriptor\Type\CollectionDescriptor':
- 'baseType'
- 'types'
- 'type'
- 'keyTypes'
services:
_defaults:
Expand Down
25 changes: 16 additions & 9 deletions src/phpDocumentor/Descriptor/ArgumentDescriptor.php
Expand Up @@ -15,6 +15,8 @@

namespace phpDocumentor\Descriptor;

use phpDocumentor\Reflection\Type;

/**
* Descriptor representing a single Argument of a method or function.
*/
Expand All @@ -23,8 +25,8 @@ class ArgumentDescriptor extends DescriptorAbstract implements Interfaces\Argume
/** @var MethodDescriptor $method */
protected $method;

/** @var string[] $type an array of normalized types that should be in this Argument */
protected $types = [];
/** @var Type|null $type normalized type of this argument */
protected $type = null;

/** @var string|null $default the default value for an argument or null if none is provided */
protected $default;
Expand All @@ -46,22 +48,27 @@ public function setMethod(MethodDescriptor $method)
/**
* {@inheritDoc}
*/
public function setTypes($types)
public function setType(?Type $type)
{
$this->types = $types;
$this->type = $type;
}

/**
* {@inheritDoc}
*/
public function getTypes()
public function getType(): ?Type
{
$countable = $this->types instanceof \Countable || is_array($this->types);
if ((!$countable || count($this->types) === 0) && $this->getInheritedElement() !== null) {
$this->setTypes($this->getInheritedElement()->getTypes());
if ($this->type === null && $this->getInheritedElement() !== null) {
$this->setType($this->getInheritedElement()->getType());
}

return $this->types;
return $this->type;
}

public function getTypes(): array
{
trigger_error('Please use getType', E_USER_DEPRECATED);
return [$this->getType()];
}

/**
Expand Down
Expand Up @@ -36,7 +36,7 @@ public function create($data, $params = [])
{
$argumentDescriptor = new ArgumentDescriptor();
$argumentDescriptor->setName($data->getName());
$argumentDescriptor->setTypes($data->getTypes());
$argumentDescriptor->setType($data->getType());

foreach ($params as $paramDescriptor) {
$this->overwriteTypeAndDescriptionFromParamTag($data, $paramDescriptor, $argumentDescriptor);
Expand All @@ -61,6 +61,6 @@ protected function overwriteTypeAndDescriptionFromParamTag(
}

$argumentDescriptor->setDescription($paramDescriptor->getDescription());
$argumentDescriptor->setTypes($paramDescriptor->getTypes());
$argumentDescriptor->setType($paramDescriptor->getType());
}
}
Expand Up @@ -123,7 +123,7 @@ protected function addVariadicArgument(Method $data, MethodDescriptor $methodDes

$argument = new ArgumentDescriptor();
$argument->setName($lastParamTag->getVariableName());
$argument->setTypes($types);
$argument->setType($types);
$argument->setDescription($lastParamTag->getDescription());
$argument->setLine($methodDescriptor->getLine());
$argument->setVariadic(true);
Expand Down
Expand Up @@ -45,7 +45,7 @@ public function create($data)
$descriptor->setStatic($data->isStatic());

$response = new ReturnDescriptor('return');
$response->setTypes($data->getReturnType());
$response->setType($data->getReturnType());
$descriptor->setResponse($response);

foreach ($data->getArguments() as $argument) {
Expand All @@ -63,7 +63,7 @@ public function create($data)
private function createArgumentDescriptorForMagicMethod(string $name, Type $type): ArgumentDescriptor
{
$argumentDescriptor = new ArgumentDescriptor();
$argumentDescriptor->setTypes(AssemblerAbstract::deduplicateTypes($type));
$argumentDescriptor->setType(AssemblerAbstract::deduplicateTypes($type));
$argumentDescriptor->setName($name);

return $argumentDescriptor;
Expand Down
Expand Up @@ -39,7 +39,7 @@ public function create($data)
$descriptor = new ParamDescriptor($data->getName());
$descriptor->setDescription($data->getDescription());
$descriptor->setVariableName($data->getVariableName());
$descriptor->setTypes(AssemblerAbstract::deduplicateTypes($data->getType()));
$descriptor->setType(AssemblerAbstract::deduplicateTypes($data->getType()));

return $descriptor;
}
Expand Down
Expand Up @@ -39,7 +39,7 @@ public function create($data)
$descriptor = new PropertyDescriptor($data->getName());
$descriptor->setVariableName($data->getVariableName());
$descriptor->setDescription($data->getDescription());
$descriptor->setTypes(AssemblerAbstract::deduplicateTypes($data->getType()));
$descriptor->setType(AssemblerAbstract::deduplicateTypes($data->getType()));

return $descriptor;
}
Expand Down
Expand Up @@ -39,8 +39,7 @@ public function create($data)
{
$descriptor = new ReturnDescriptor($data->getName());
$descriptor->setDescription($data->getDescription());

$descriptor->setTypes(AssemblerAbstract::deduplicateTypes($data->getType()));
$descriptor->setType(AssemblerAbstract::deduplicateTypes($data->getType()));

return $descriptor;
}
Expand Down
Expand Up @@ -38,7 +38,7 @@ public function create($data)
{
$descriptor = new ThrowsDescriptor($data->getName());
$descriptor->setDescription($data->getDescription());
$descriptor->setTypes(AssemblerAbstract::deduplicateTypes($data->getType()));
$descriptor->setType(AssemblerAbstract::deduplicateTypes($data->getType()));

return $descriptor;
}
Expand Down
Expand Up @@ -40,7 +40,7 @@ public function create($data)
$descriptor->setDescription($data->getDescription());
$descriptor->setVariableName($data->getVariableName());

$descriptor->setTypes(AssemblerAbstract::deduplicateTypes($data->getType()));
$descriptor->setType(AssemblerAbstract::deduplicateTypes($data->getType()));

return $descriptor;
}
Expand Down
2 changes: 1 addition & 1 deletion src/phpDocumentor/Descriptor/ClassDescriptor.php
Expand Up @@ -284,7 +284,7 @@ public function getMagicProperties()
$property = new PropertyDescriptor();
$property->setName(ltrim($propertyTag->getVariableName(), '$'));
$property->setDescription($propertyTag->getDescription());
$property->setTypes($propertyTag->getTypes());
$property->setTypes($propertyTag->getType());
$property->setParent($this);

$properties->add($property);
Expand Down
9 changes: 7 additions & 2 deletions src/phpDocumentor/Descriptor/ConstantDescriptor.php
Expand Up @@ -26,7 +26,7 @@ class ConstantDescriptor extends DescriptorAbstract implements Interfaces\Consta
/** @var ClassDescriptor|InterfaceDescriptor|null $parent */
protected $parent;

/** @var string[]|null $type */
/** @var Type $type */
protected $types;

/** @var string $value */
Expand Down Expand Up @@ -74,12 +74,17 @@ public function setTypes(Type $types)
* {@inheritDoc}
*/
public function getTypes()
{
return [$this->getType()];
}

public function getType()
{
if ($this->types === null) {
/** @var VarDescriptor $var */
$var = $this->getVar()->get(0);
if ($var) {
return $var->getTypes();
return $var->getType();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/phpDocumentor/Descriptor/FunctionDescriptor.php
Expand Up @@ -61,7 +61,7 @@ public function getArguments()
public function getResponse(): ReturnDescriptor
{
$definedReturn = new ReturnDescriptor('return');
$definedReturn->setTypes($this->returnType);
$definedReturn->setType($this->returnType);

/** @var Collection|null $returnTags */
$returnTags = $this->getTags()->get('return');
Expand Down
12 changes: 6 additions & 6 deletions src/phpDocumentor/Descriptor/Interfaces/ArgumentInterface.php
Expand Up @@ -15,7 +15,7 @@

namespace phpDocumentor\Descriptor\Interfaces;

use phpDocumentor\Descriptor\Collection;
use phpDocumentor\Reflection\Type;

/**
* Describes the public interface for a descriptor of an Argument.
Expand All @@ -32,23 +32,23 @@ interface ArgumentInterface extends ElementInterface
* backslash. Types that do not represent a class/interface/trait should be written in lowercaps and should not be
* preceded by a backslash.
*
* @param Collection $types An Collection of normalized types that should be in this Argument
* @param ?Type $type Type of this agument represented as a reflection type.
*
* @link https://github.com/phpDocumentor/phpDocumentor2/blob/develop/docs/PSR.md#appendix-a-types Definition of a
* type.
*
* @todo update link to point to the final destination for the PHPDoc Standard.
*/
public function setTypes($types);
public function setType(?Type $type);

/**
* Returns a normalized list of types.
* Returns a normalized Types.
*
* @see self::setTypes() for details on what types represent.
*
* @return Collection
* @return Type|null
*/
public function getTypes();
public function getType(): ?Type;

/**
* Sets the default value for an argument expressed as a string.
Expand Down
2 changes: 1 addition & 1 deletion src/phpDocumentor/Descriptor/MethodDescriptor.php
Expand Up @@ -173,7 +173,7 @@ public function getArguments()
public function getResponse(): ReturnDescriptor
{
$definedReturn = new ReturnDescriptor('return');
$definedReturn->setTypes($this->returnType);
$definedReturn->setType($this->returnType);

/** @var Collection|null $returnTags */
$returnTags = $this->getReturn();
Expand Down
9 changes: 6 additions & 3 deletions src/phpDocumentor/Descriptor/PropertyDescriptor.php
Expand Up @@ -103,13 +103,16 @@ public function setTypes(Type $types)
*/
public function getTypes()
{
if (!$this->types) {
$this->types = new Collection();
return [$this->getType()];
}

public function getType()
{
if ($this->types === null) {
/** @var VarDescriptor $var */
$var = $this->getVar()->getIterator()->current();
if ($var) {
$this->types = $var->getTypes();
return $var->getType();
}
}

Expand Down
18 changes: 17 additions & 1 deletion src/phpDocumentor/Descriptor/Tag/BaseTypes/TypedAbstract.php
Expand Up @@ -31,13 +31,29 @@ abstract class TypedAbstract extends TagDescriptor
*/
public function setTypes(Type $types = null)
{
trigger_error('Use setType, because type is an object', E_USER_DEPRECATED);
$this->types = $types;
}

/**
* Sets a list of types associated with this tag.
*/
public function setType(Type $types = null)
{
$this->types = $types;
}


/**
* Returns the list of types associated with this tag.
*/
public function getTypes(): ?Type
public function getTypes(): array
{
trigger_error('Use getType, because type is an object', E_USER_DEPRECATED);
return array_filter([$this->types]);
}

public function getType()
{
return $this->types;
}
Expand Down
2 changes: 1 addition & 1 deletion src/phpDocumentor/Descriptor/TraitDescriptor.php
Expand Up @@ -130,7 +130,7 @@ public function getMagicProperties()
$property = new PropertyDescriptor();
$property->setName(ltrim($propertyTag->getVariableName(), '$'));
$property->setDescription($propertyTag->getDescription());
$property->setTypes($propertyTag->getTypes());
$property->setTypes($propertyTag->getType());
$property->setParent($this);

$properties->add($property);
Expand Down
Expand Up @@ -46,16 +46,7 @@ public function convert(\DOMElement $parent, ArgumentDescriptor $argument)
$child->appendChild(new \DOMElement('default'))
->appendChild(new \DOMText((string) $argument->getDefault()));

$types = $argument->getTypes();

$typeStrings = [];
foreach ($types as $type) {
$typeStrings[] = $type instanceof DescriptorAbstract
? $type->getFullyQualifiedStructuralElementName()
: $type;
}

$child->appendChild(new \DOMElement('type', implode('|', $typeStrings)));
$child->appendChild(new \DOMElement('type', (string) $argument->getType()));

return $child;
}
Expand Down
Expand Up @@ -107,7 +107,7 @@ protected function addTypes(TagDescriptor $tag, \DOMElement $child)
$typeString = '';

if ($tag instanceof TypedAbstract) {
$types = $tag->getTypes();
$types = $tag->getType();

if ($types instanceof \IteratorAggregate) {
foreach ($types as $type) {
Expand Down

0 comments on commit 3bc77aa

Please sign in to comment.