Property::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @copyright 2010-2018 Mike van Riel<[email protected]>
11
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
12
 * @link      http://phpdoc.org
13
 */
14
15
namespace phpDocumentor\Reflection\Php\Factory;
16
17
use phpDocumentor\Reflection\Location;
18
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
19
use phpDocumentor\Reflection\Php\Property as PropertyDescriptor;
20
use phpDocumentor\Reflection\Php\StrategyContainer;
21
use phpDocumentor\Reflection\Php\Visibility;
22
use phpDocumentor\Reflection\PrettyPrinter;
23
use phpDocumentor\Reflection\Types\Context;
24
25
/**
26
 * Strategy to convert PropertyIterator to PropertyDescriptor
27
 *
28
 * @see PropertyDescriptor
29
 * @see PropertyIterator
30
 */
31
final class Property extends AbstractFactory implements ProjectFactoryStrategy
32
{
33
    /**
34
     * @var PrettyPrinter
35
     */
36
    private $valueConverter;
37
38
    /**
39
     * Initializes the object.
40
     */
41 6
    public function __construct(PrettyPrinter $prettyPrinter)
42
    {
43 6
        $this->valueConverter = $prettyPrinter;
44 6
    }
45
46 1
    public function matches($object): bool
47
    {
48 1
        return $object instanceof PropertyIterator;
49
    }
50
51
    /**
52
     * Creates an PropertyDescriptor out of the given object.
53
     *
54
     * Since an object might contain other objects that need to be converted the $factory is passed so it can be
55
     * used to create nested Elements.
56
     *
57
     * @param PropertyIterator $object object to convert to an PropertyDescriptor
58
     * @param StrategyContainer $strategies used to convert nested objects.
59
     * @return PropertyDescriptor
60
     */
61 4
    protected function doCreate($object, StrategyContainer $strategies, ?Context $context = null)
62
    {
63 4
        $visibility = $this->buildVisibility($object);
64 4
        $default = null;
65 4
        if ($object->getDefault() !== null) {
66 4
            $default = $this->valueConverter->prettyPrintExpr($object->getDefault());
0 ignored issues
show
Bug introduced by
It seems like $object->getDefault() targeting phpDocumentor\Reflection...yIterator::getDefault() can also be of type string; however, PhpParser\PrettyPrinterAbstract::prettyPrintExpr() does only seem to accept object<PhpParser\Node\Expr>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
67
        }
68
69 4
        $docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context);
70
71 4
        return new PropertyDescriptor(
72 4
            $object->getFqsen(),
73 4
            $visibility,
74 4
            $docBlock,
0 ignored issues
show
Bug introduced by
It seems like $docBlock defined by $this->createDocBlock($s...DocComment(), $context) on line 69 can also be of type object<phpDocumentor\Reflection\Element>; however, phpDocumentor\Reflection...Property::__construct() does only seem to accept object<phpDocumentor\Reflection\DocBlock>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
75 4
            $default,
76 4
            $object->isStatic(),
77 4
            new Location($object->getLine())
78
        );
79
    }
80
81
    /**
82
     * Converts the visibility of the property to a valid Visibility object.
83
     */
84 4
    private function buildVisibility(PropertyIterator $node): Visibility
85
    {
86 4
        if ($node->isPrivate()) {
87 2
            return new Visibility(Visibility::PRIVATE_);
88 2
        } elseif ($node->isProtected()) {
89 1
            return new Visibility(Visibility::PROTECTED_);
90
        }
91
92 1
        return new Visibility(Visibility::PUBLIC_);
93
    }
94
}
95