Completed
Push — master ( f60759...069a78 )
by Jaap
17s queued 10s
created

Property::__toString()   B

Complexity

Conditions 8
Paths 128

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 6
cts 6
cp 1
rs 8.2111
c 0
b 0
f 0
cc 8
nc 128
nop 0
crap 8
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\Type;
19
use phpDocumentor\Reflection\TypeResolver;
20
use phpDocumentor\Reflection\Types\Context as TypeContext;
21
use phpDocumentor\Reflection\Utils;
22
use Webmozart\Assert\Assert;
23
use function array_shift;
24
use function array_unshift;
25
use function implode;
26
use function strpos;
27
use function substr;
28
use const PREG_SPLIT_DELIM_CAPTURE;
29
30
/**
31
 * Reflection class for a {@}property tag in a Docblock.
32
 */
33
final class Property extends TagWithType 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...
34
{
35
    /** @var string|null */
36
    protected $variableName;
37
38
    public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null)
39
    {
40
        Assert::string($variableName);
41 5
42
        $this->name         = 'property';
43 5
        $this->variableName = $variableName;
44
        $this->type         = $type;
45 4
        $this->description  = $description;
46 4
    }
47 4
48 4
    public static function create(
49
        string $body,
50
        ?TypeResolver $typeResolver = null,
51
        ?DescriptionFactory $descriptionFactory = null,
52
        ?TypeContext $context = null
53 5
    ) : self {
54
        Assert::stringNotEmpty($body);
55
        Assert::notNull($typeResolver);
56
        Assert::notNull($descriptionFactory);
57
58
        [$firstPart, $body] = self::extractTypeFromBody($body);
0 ignored issues
show
Bug introduced by
The variable $firstPart does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
59 5
        $type               = null;
60 3
        $parts              = Utils::pregSplit('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE);
61
        $variableName = '';
62 1
63 1
        // if the first item that is encountered is not a variable; it is a type
64 1
        if ($firstPart && $firstPart[0] !== '$') {
65
            $type = $typeResolver->resolve($firstPart, $context);
66
        } else {
67 1
            // first part is not a type; we should prepend it to the parts array for further processing
68 1
            array_unshift($parts, $firstPart);
69 1
        }
70
71
        // if the next item starts with a $ it must be the variable name
72
        if (isset($parts[0]) && strpos($parts[0], '$') === 0) {
73 1
            $variableName = array_shift($parts);
74 1
            if ($type) {
75 1
                array_shift($parts);
76
            }
77 1
78 1
            Assert::notNull($variableName);
79
80
            $variableName = substr($variableName, 1);
81
        }
82 1
83
        $description = $descriptionFactory->create(implode('', $parts), $context);
84 1
85
        return new static($variableName, $type, $description);
86
    }
87
88
    /**
89
     * Returns the variable's name.
90
     */
91
    public function getVariableName() : ?string
92 1
    {
93
        return $this->variableName;
94 1
    }
95
96
    /**
97
     * Returns a string representation for this tag.
98
     */
99
    public function __toString() : string
100
    {
101
        if ($this->description) {
102 1
            $description = $this->description->render();
103
        } else {
104 1
            $description = '';
105
        }
106
107
        if ($this->variableName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->variableName of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
108
            $variableName = '$' . $this->variableName;
109
        } else {
110
            $variableName = '';
111
        }
112 1
113
        $type = (string) $this->type;
114 1
115 1
        return $type
116 1
            . ($variableName !== '' ? ($type !== '' ? ' ' : '') . $variableName : '')
117
            . ($description !== '' ? ($type !== '' || $variableName !== '' ? ' ' : '') . $description : '');
118
    }
119
}
120