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

Class_::createMember()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 1
Metric Value
dl 0
loc 5
c 2
b 1
f 1
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 3
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
16
use InvalidArgumentException;
17
use phpDocumentor\Reflection\Element;
18
use phpDocumentor\Reflection\Fqsen;
19
use phpDocumentor\Reflection\Php\Class_ as ClassElement;
20
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
21
use phpDocumentor\Reflection\Php\StrategyContainer;
22
use phpDocumentor\Reflection\Types\Context;
23
use PhpParser\Node;
24
use PhpParser\Node\Stmt\Class_ as ClassNode;
25
use PhpParser\Node\Stmt\ClassConst;
26
use PhpParser\Node\Stmt\ClassMethod;
27
use PhpParser\Node\Stmt\Property as PropertyNode;
28
use PhpParser\Comment\Doc;
29
use PhpParser\Node\Stmt\TraitUse;
30
31
/**
32
 * Strategy to create a ClassElement including all sub elements.
33
 */
34
final class Class_ implements ProjectFactoryStrategy
0 ignored issues
show
Complexity introduced by
The class Class_ has a coupling between objects value of 16. 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...
35
{
36
37
    /**
38
     * Returns true when the strategy is able to handle the object.
39
     *
40
     * @param object $object object to check.
41
     * @return boolean
42
     */
43 1
    public function matches($object)
44
    {
45 1
        return $object instanceof ClassNode;
46
    }
47
48
    /**
49
     * Creates an ClassElement out of the given object.
50
     * Since an object might contain other objects that need to be converted the $factory is passed so it can be
51
     * used to create nested Elements.
52
     *
53
     * @param ClassNode $object object to convert to an Element
54
     * @param StrategyContainer $strategies used to convert nested objects.
55
     * @param Context $context of the created object
56
     * @return ClassElement
57
     */
58 9
    public function create($object, StrategyContainer $strategies, Context $context = null)
0 ignored issues
show
Complexity introduced by
This operation has 810 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
59
    {
60 9
        if (!$this->matches($object)) {
61 1
            throw new InvalidArgumentException(
62 1
                sprintf('%s cannot handle objects with the type %s',
63 1
                    __CLASS__,
64 1
                    is_object($object) ? get_class($object) : gettype($object)
65
                )
66
            );
67
        }
68
69 8
        $docBlock = $this->createDocBlock($object->getDocComment(), $strategies, $context);
70
71 8
        $classElement = new ClassElement(
72 8
            $object->fqsen,
0 ignored issues
show
Bug introduced by
The property fqsen does not seem to exist in PhpParser\Node\Stmt\Class_.

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...
73
            $docBlock,
0 ignored issues
show
Bug introduced by
It seems like $docBlock defined by $this->createDocBlock($o... $strategies, $context) on line 69 can also be of type object<phpDocumentor\Reflection\Element>; however, phpDocumentor\Reflection\Php\Class_::__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...
74 8
            $object->extends ? new Fqsen('\\' . $object->extends) : null,
75 8
            $object->isAbstract(),
76 8
            $object->isFinal()
77
        );
78
79 8
        if (isset($object->implements)) {
80 1
            foreach ($object->implements as $interfaceClassName) {
81 1
                $classElement->addInterface(
82 1
                    new Fqsen('\\' . $interfaceClassName->toString())
83
                );
84
            }
85
        }
86
87 8
        if (isset($object->stmts)) {
88 4
            foreach ($object->stmts as $stmt) {
89 4
                switch (get_class($stmt)) {
90
                    case TraitUse::class:
91 1
                        foreach ($stmt->traits as $use) {
0 ignored issues
show
Bug introduced by
Accessing traits on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
92 1
                            $classElement->addUsedTrait(new Fqsen('\\'. $use->toString()));
93
                        }
94 1
                        break;
95
                    case PropertyNode::class:
96 1
                        $properties = new PropertyIterator($stmt);
0 ignored issues
show
Compatibility introduced by
$stmt of type object<PhpParser\Node> is not a sub-type of object<PhpParser\Node\Stmt\Property>. 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...
97 1
                        foreach ($properties as $property) {
98 1
                            $element = $this->createMember($property, $strategies, $context);
99 1
                            $classElement->addProperty($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\Property>. 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...
100
                        }
101 1
                        break;
102
                    case ClassMethod::class:
103 1
                        $method = $this->createMember($stmt, $strategies, $context);
104 1
                        $classElement->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...
105 1
                        break;
106
                    case ClassConst::class:
107 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...
108 1
                        foreach ($constants as $const) {
109 1
                            $element = $this->createMember($const, $strategies, $context);
110 1
                            $classElement->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...
111
                        }
112 4
                        break;
113
                }
114
            }
115
        }
116
117 8
        return $classElement;
118
    }
119
120
    /**
121
     * @param Node|PropertyIterator|ClassConstantIterator $stmt
122
     * @param StrategyContainer $strategies
123
     * @param Context $context
124
     * @return Element
125
     */
126 4
    private function createMember($stmt, StrategyContainer $strategies, Context $context = null)
127
    {
128 4
        $strategy = $strategies->findMatching($stmt);
129 4
        return $strategy->create($stmt, $strategies, $context);
130
    }
131
132
133
    /**
134
     * @param Doc $docBlock
135
     * @param StrategyContainer $strategies
136
     * @param Context $context
137
     * @return null|\phpDocumentor\Reflection\DocBlock
138
     */
139 8
    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...
140
    {
141 8
        if ($docBlock === null) {
142 7
            return null;
143
        }
144
145 1
        return $this->createMember($docBlock, $strategies, $context);
0 ignored issues
show
Documentation introduced by
$docBlock is of type object<PhpParser\Comment\Doc>, but the function expects a object<PhpParser\Node>|o...\ClassConstantIterator>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
146
    }
147
}
148