Completed
Push — develop ( 5c2bf4...eb0c60 )
by Mike
03:22
created

ClassConstant::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
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\Constant as ConstantElement;
19
use phpDocumentor\Reflection\Php\StrategyContainer;
20
use phpDocumentor\Reflection\PrettyPrinter;
21
use phpDocumentor\Reflection\Types\Context;
22
23
/**
24
 * Strategy to convert ClassConstantIterator to ConstantElement
25
 *
26
 * @see ConstantElement
27
 * @see ClassConstantIterator
28
 */
29
final class ClassConstant extends AbstractFactory
30
{
31
    /**
32
     * @var PrettyPrinter
33
     */
34
    private $valueConverter;
35
36
    /**
37
     * Initializes the object.
38
     */
39
    public function __construct(PrettyPrinter $prettyPrinter)
40
    {
41
        $this->valueConverter = $prettyPrinter;
42
    }
43
44
    public function matches($object): bool
45
    {
46
        return $object instanceof ClassConstantIterator;
47
    }
48
49
    /**
50
     * Creates an Constant out of the given object.
51
     * Since an object might contain other objects that need to be converted the $factory is passed so it can be
52
     * used to create nested Elements.
53
     *
54
     * @param ClassConstantIterator $object object to convert to an Element
55
     * @param StrategyContainer $strategies used to convert nested objects.
56
     * @param Context $context of the created object
57
     * @return ConstantElement
58
     */
59
    protected function doCreate($object, StrategyContainer $strategies, ?Context $context = null)
60
    {
61
        $docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context);
62
        $default = null;
63
        if ($object->getValue() !== null) {
64
            $default = $this->valueConverter->prettyPrintExpr($object->getValue());
65
        }
66
67
        return new ConstantElement($object->getFqsen(), $docBlock, $default, new Location($object->getLine()));
0 ignored issues
show
Bug introduced by
It seems like $docBlock defined by $this->createDocBlock($s...DocComment(), $context) on line 61 can also be of type object<phpDocumentor\Reflection\Element>; however, phpDocumentor\Reflection...Constant::__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...
68
    }
69
}
70