Completed
Push — master ( 08c0b3...86733d )
by Jaap
39s queued 36s
created

Uses::resolveFqsen()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 2
cts 2
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
declare(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
14
namespace phpDocumentor\Reflection\DocBlock\Tags;
15
16
use phpDocumentor\Reflection\DocBlock\Description;
17
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
18
use phpDocumentor\Reflection\Fqsen;
19
use phpDocumentor\Reflection\FqsenResolver;
20
use phpDocumentor\Reflection\Types\Context as TypeContext;
21
use phpDocumentor\Reflection\Utils;
22
use Webmozart\Assert\Assert;
23
use function array_key_exists;
24
use function explode;
25
26
/**
27
 * Reflection class for a {@}uses tag in a Docblock.
28
 */
29
final class Uses extends BaseTag implements Factory\StaticMethod
0 ignored issues
show
Deprecated Code introduced by
The interface phpDocumentor\Reflection...gs\Factory\StaticMethod has been deprecated with message: This contract is totally covered by Tag contract. Every class using StaticMethod also use Tag

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
30
{
31
    /** @var string */
32
    protected $name = 'uses';
33
34
    /** @var Fqsen */
35
    protected $refers;
36
37
    /**
38 3
     * Initializes this tag.
39
     */
40 3
    public function __construct(Fqsen $refers, ?Description $description = null)
41 3
    {
42 3
        $this->refers      = $refers;
43
        $this->description = $description;
44
    }
45
46
    public static function create(
47 5
        string $body,
48
        ?FqsenResolver $resolver = null,
49
        ?DescriptionFactory $descriptionFactory = null,
50
        ?TypeContext $context = null
51
    ) : self {
52
        Assert::notNull($resolver);
53 5
        Assert::notNull($descriptionFactory);
54 4
55
        $parts = Utils::pregSplit('/\s+/Su', $body, 2);
56 1
57
        return new static(
58 1
            self::resolveFqsen($parts[0], $resolver, $context),
59 1
            $descriptionFactory->create($parts[1] ?? '', $context)
60 1
        );
61
    }
62
63
    private static function resolveFqsen(string $parts, ?FqsenResolver $fqsenResolver, ?TypeContext $context) : Fqsen
64
    {
65
        Assert::notNull($fqsenResolver);
66
        $fqsenParts = explode('::', $parts);
67
        $resolved = $fqsenResolver->resolve($fqsenParts[0], $context);
68
69 1
        if (!array_key_exists(1, $fqsenParts)) {
70
            return $resolved;
71 1
        }
72
73
        return new Fqsen($resolved . '::' . $fqsenParts[1]);
74
    }
75
76
    /**
77
     * Returns the structural element this tag refers to.
78
     */
79 1
    public function getReference() : Fqsen
80
    {
81 1
        return $this->refers;
82
    }
83
84
    /**
85
     * Returns a string representation of this tag.
86
     */
87
    public function __toString() : string
88
    {
89
        return $this->refers . ' ' . (string) $this->description;
90
    }
91
}
92