Completed
Pull Request — develop (#87)
by Jaap
03:38
created

Interface_::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
14
namespace phpDocumentor\Reflection\Php\Factory;
15
use InvalidArgumentException;
16
use phpDocumentor\Reflection\Element;
17
use phpDocumentor\Reflection\Fqsen;
18
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
19
use phpDocumentor\Reflection\Php\StrategyContainer;
20
use phpDocumentor\Reflection\Types\Context;
21
use PhpParser\Comment\Doc;
22
use PhpParser\Node;
23
use PhpParser\Node\Stmt\ClassConst;
24
use PhpParser\Node\Stmt\ClassMethod;
25
use PhpParser\Node\Stmt\Interface_ as InterfaceNode;
26
use phpDocumentor\Reflection\Php\Interface_ as InterfaceElement;
27
28
/**
29
 * Strategy to create a InterfaceElement including all sub elements.
30
 */
31
final class Interface_ implements ProjectFactoryStrategy
0 ignored issues
show
Complexity introduced by
The class Interface_ has a coupling between objects value of 13. Consider to reduce the number of dependencies under 13.
Loading history...
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
32
{
33
34
    /**
35
     * Returns true when the strategy is able to handle the object.
36
     *
37
     * @param InterfaceNode $object object to check.
38
     * @return boolean
39
     */
40 1
    public function matches($object)
41
    {
42 1
        return $object instanceof InterfaceNode;
43
    }
44
45
    /**
46
     * Creates an Interface_ out of the given object.
47
     * Since an object might contain other objects that need to be converted the $factory is passed so it can be
48
     * used to create nested Elements.
49
     *
50
     * @param InterfaceNode $object object to convert to an Element
51
     * @param StrategyContainer $strategies used to convert nested objects.
52
     * @param Context $context of the created object
53
     * @return InterfaceElement
54
     */
55 5
    public function create($object, StrategyContainer $strategies, Context $context = null)
56
    {
57 5
        if (!$this->matches($object)) {
58 1
            throw new InvalidArgumentException(
59 1
                sprintf('%s cannot handle objects with the type %s',
60 1
                    __CLASS__,
61 1
                    is_object($object) ? get_class($object) : gettype($object)
62
                )
63
            );
64
        }
65
66 4
        $docBlock = $this->createDocBlock($object->getDocComment(), $strategies, $context);
67 4
        $parents = array();
68 4
        foreach ($object->extends as $extend) {
69
            $parents['\\' . (string)$extend] = new Fqsen('\\' . (string)$extend);
70
        }
71
72 4
        $interface = new InterfaceElement($object->fqsen, $parents, $docBlock);
0 ignored issues
show
Bug introduced by
The property fqsen does not seem to exist in PhpParser\Node\Stmt\Interface_.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
It seems like $docBlock defined by $this->createDocBlock($o... $strategies, $context) on line 66 can also be of type object<phpDocumentor\Reflection\Element>; however, phpDocumentor\Reflection...terface_::__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...
73
74 4
        if (isset($object->stmts)) {
75 2
            foreach ($object->stmts as $stmt) {
76 2
                switch (get_class($stmt)) {
77
                    case ClassMethod::class:
78 1
                        $method = $this->createMember($stmt, $strategies, $context);
79 1
                        $interface->addMethod($method);
0 ignored issues
show
Compatibility introduced by
$method of type object<phpDocumentor\Reflection\Element> is not a sub-type of object<phpDocumentor\Reflection\Php\Method>. It seems like you assume a concrete implementation of the interface phpDocumentor\Reflection\Element to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
80 1
                        break;
81
                    case ClassConst::class:
82 1
                        $constants = new ClassConstantIterator($stmt);
0 ignored issues
show
Compatibility introduced by
$stmt of type object<PhpParser\Node> is not a sub-type of object<PhpParser\Node\Stmt\ClassConst>. It seems like you assume a concrete implementation of the interface PhpParser\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
83 1
                        foreach ($constants as $const) {
84 1
                            $element = $this->createMember($const, $strategies, $context);
85 1
                            $interface->addConstant($element);
0 ignored issues
show
Compatibility introduced by
$element of type object<phpDocumentor\Reflection\Element> is not a sub-type of object<phpDocumentor\Reflection\Php\Constant>. It seems like you assume a concrete implementation of the interface phpDocumentor\Reflection\Element to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
86
                        }
87 2
                        break;
88
                }
89
            }
90
        }
91
92 4
        return $interface;
93
    }
94
95
    /**
96
     * @param Node|ClassConstantIterator|Doc $stmt
97
     * @param StrategyContainer $strategies
98
     * @param Context $context
99
     * @return Element
100
     */
101 3
    private function createMember($stmt, StrategyContainer $strategies, Context $context = null)
102
    {
103 3
        $strategy = $strategies->findMatching($stmt);
104 3
        return $strategy->create($stmt, $strategies, $context);
105
    }
106
107
    /**
108
     * @param Doc $docBlock
109
     * @param StrategyContainer $strategies
110
     * @param Context $context
111
     * @return null|\phpDocumentor\Reflection\DocBlock
112
     */
113 4
    private function createDocBlock(Doc $docBlock = null, StrategyContainer $strategies, Context $context = null)
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
114
    {
115 4
        if ($docBlock === null) {
116 3
            return null;
117
        }
118
119 1
        return $this->createMember($docBlock, $strategies, $context);
120
    }
121
}
122