1<?php
2
3declare(strict_types=1);
4
5/**
6 * This file is part of phpDocumentor.
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 *
11 * @link      http://phpdoc.org
12 */
13
14namespace phpDocumentor\Reflection\DocBlock\Tags;
15
16use phpDocumentor\Reflection\DocBlock\Description;
17use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
18use phpDocumentor\Reflection\Type;
19use phpDocumentor\Reflection\TypeResolver;
20use phpDocumentor\Reflection\Types\Context as TypeContext;
21use Webmozart\Assert\Assert;
22
23/**
24 * Reflection class for a {@}return tag in a Docblock.
25 */
26final class Return_ extends TagWithType implements Factory\StaticMethod
27{
28    public function __construct(Type $type, ?Description $description = null)
29    {
30        $this->name        = 'return';
31        $this->type        = $type;
32        $this->description = $description;
33    }
34
35    public static function create(
36        string $body,
37        ?TypeResolver $typeResolver = null,
38        ?DescriptionFactory $descriptionFactory = null,
39        ?TypeContext $context = null
40    ) : self {
41        Assert::notNull($typeResolver);
42        Assert::notNull($descriptionFactory);
43
44        [$type, $description] = self::extractTypeFromBody($body);
45
46        $type        = $typeResolver->resolve($type, $context);
47        $description = $descriptionFactory->create($description, $context);
48
49        return new static($type, $description);
50    }
51
52    public function __toString() : string
53    {
54        if ($this->description) {
55            $description = $this->description->render();
56        } else {
57            $description = '';
58        }
59
60        $type = $this->type ? '' . $this->type : 'mixed';
61
62        return $type . ($description !== '' ? ($type !== '' ? ' ' : '') . $description : '');
63    }
64}
65