Completed
Pull Request — develop (#91)
by Jaap
02:52
created

Property::matches()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 4
c 1
b 0
f 1
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
     * This file is part of phpDocumentor.
4
     *
5
     * For the full copyright and license information, please view the LICENSE
6
     * file that was distributed with this source code.
7
     *
8
     * @copyright 2010-2015 Mike van Riel<[email protected]>
9
     * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
     * @link      http://phpdoc.org
11
     */
12
13
namespace phpDocumentor\Reflection\Php\Factory;
14
15
use InvalidArgumentException;
16
use phpDocumentor\Reflection\Php\Factory;
17
use phpDocumentor\Reflection\Php\Property as PropertyDescriptor;
18
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
19
use phpDocumentor\Reflection\Php\StrategyContainer;
20
use phpDocumentor\Reflection\Php\Visibility;
21
use phpDocumentor\Reflection\PrettyPrinter;
22
use phpDocumentor\Reflection\Types\Context;
23
use PhpParser\Comment\Doc;
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
     * @param PrettyPrinter $prettyPrinter
42
     */
43 6
    public function __construct(PrettyPrinter $prettyPrinter)
44
    {
45 6
        $this->valueConverter = $prettyPrinter;
46 6
    }
47
48
    /**
49
     * Returns true when the strategy is able to handle the object.
50
     *
51
     * @param object $object object to check.
52
     * @return boolean
53
     */
54 1
    public function matches($object)
55
    {
56 1
        return $object instanceof PropertyIterator;
57
    }
58
59
    /**
60
     * Creates an PropertyDescriptor out of the given object.
61
     * Since an object might contain other objects that need to be converted the $factory is passed so it can be
62
     * used to create nested Elements.
63
     *
64
     * @param PropertyIterator $object object to convert to an PropertyDescriptor
65
     * @param StrategyContainer $strategies used to convert nested objects.
66
     * @param Context $context
67
     * @return PropertyDescriptor
68
     */
69 4
    protected function doCreate($object, StrategyContainer $strategies, Context $context = null)
70
    {
71 4
        $visibility = $this->buildVisibility($object);
72 4
        $default = null;
73 4
        if ($object->getDefault() !== null) {
74 4
            $default = $this->valueConverter->prettyPrintExpr($object->getDefault());
75 4
        }
76 4
        $docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context);
77
78 4
        return new PropertyDescriptor($object->getFqsen(), $visibility, $docBlock, $default, $object->isStatic());
0 ignored issues
show
Bug introduced by
It seems like $docBlock defined by $this->createDocBlock($s...DocComment(), $context) on line 76 can also be of type object<phpDocumentor\Reflection\Element>; however, phpDocumentor\Reflection...Property::__construct() does only seem to accept null|object<phpDocumentor\Reflection\DocBlock>, 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...
79
    }
80
81
    /**
82
     * Converts the visibility of the property to a valid Visibility object.
83
     *
84
     * @param PropertyIterator $node
85
     * @return Visibility
86
     */
87 4
    private function buildVisibility(PropertyIterator $node)
88
    {
89 4
        if ($node->isPrivate()) {
90 2
            return new Visibility(Visibility::PRIVATE_);
91 2
        } elseif ($node->isProtected()) {
92 1
            return new Visibility(Visibility::PROTECTED_);
93
        }
94
95 1
        return new Visibility(Visibility::PUBLIC_);
96
    }
97
}
98