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

Constant   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 4
c 4
b 0
f 2
lcom 1
cbo 4
dl 0
loc 49
ccs 0
cts 12
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A matches() 0 4 1
A doCreate() 0 10 2
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
14
namespace phpDocumentor\Reflection\Php\Factory;
15
16
use phpDocumentor\Reflection\Php\Constant as ConstantElement;
17
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
18
use phpDocumentor\Reflection\Php\StrategyContainer;
19
use phpDocumentor\Reflection\PrettyPrinter;
20
use phpDocumentor\Reflection\Types\Context;
21
use PhpParser\Comment\Doc;
22
23
/**
24
 * Strategy to convert ClassConstantIterator to ConstantElement
25
 *
26
 * @see ConstantElement
27
 * @see ClassConstantIterator
28
 */
29
final class Constant extends AbstractFactory implements ProjectFactoryStrategy
30
{
31
    /**
32
     * @var PrettyPrinter
33
     */
34
    private $valueConverter;
35
36
    /**
37
     * Initializes the object.
38
     *
39
     * @param PrettyPrinter $prettyPrinter
40
     */
41
    public function __construct(PrettyPrinter $prettyPrinter)
42
    {
43
        $this->valueConverter = $prettyPrinter;
44
    }
45
46
    /**
47
     * Returns true when the strategy is able to handle the object.
48
     *
49
     * @param object $object object to check.
50
     * @return boolean
51
     */
52
    public function matches($object)
53
    {
54
        return $object instanceof ClassConstantIterator;
55
    }
56
57
    /**
58
     * Creates an Constant out of the given object.
59
     * Since an object might contain other objects that need to be converted the $factory is passed so it can be
60
     * used to create nested Elements.
61
     *
62
     * @param ClassConstantIterator $object object to convert to an Element
63
     * @param StrategyContainer $strategies used to convert nested objects.
64
     * @param Context $context of the created object
65
     * @return Constant
66
     */
67
    protected function doCreate($object, StrategyContainer $strategies, Context $context = null)
68
    {
69
        $docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context);
70
        $default = null;
71
        if ($object->getValue() !== null) {
72
            $default = $this->valueConverter->prettyPrintExpr($object->getValue());
73
        }
74
75
        return new ConstantElement($object->getFqsen(), $docBlock, $default);
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...Constant::__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...
76
    }
77
}
78