Completed
Pull Request — develop (#92)
by Jaap
02:48
created

Class_::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 0
Metric Value
dl 0
loc 4
c 1
b 0
f 0
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
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\Factory\Middleware\Implements_;
21
use phpDocumentor\Reflection\Php\Factory\Middleware\Statements;
22
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
23
use phpDocumentor\Reflection\Php\StrategyContainer;
24
use phpDocumentor\Reflection\Types\Context;
25
use PhpParser\Node;
26
use PhpParser\Node\Stmt\Class_ as ClassNode;
27
use PhpParser\Node\Stmt\ClassConst;
28
use PhpParser\Node\Stmt\ClassMethod;
29
use PhpParser\Node\Stmt\Property as PropertyNode;
30
use PhpParser\Comment\Doc;
31
use PhpParser\Node\Stmt\TraitUse;
32
33
/**
34
 * Strategy to create a ClassElement including all sub elements.
35
 */
36
// @codingStandardsIgnoreStart
37
final class Class_ extends AbstractFactory implements ProjectFactoryStrategy
38
// @codingStandardsIgnoreEnd
39
{
40
    public function __construct()
41
    {
42
        $middlewares[] = new Implements_();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$middlewares was never initialized. Although not strictly required by PHP, it is generally a good practice to add $middlewares = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
43
        $middlewares[] = new Statements();
44
45
        parent::__construct($middlewares);
46
    }
47
48
49
    /**
50
     * Returns true when the strategy is able to handle the object.
51
     *
52
     * @param object $object object to check.
53
     * @return boolean
54
     */
55 1
    public function matches($object)
56
    {
57 1
        return $object instanceof ClassNode;
58
    }
59
60
    /**
61
     * Creates an ClassElement out of the given object.
62
     * Since an object might contain other objects that need to be converted the $factory is passed so it can be
63
     * used to create nested Elements.
64
     *
65
     * @param ClassNode $object object to convert to an Element
66
     * @param StrategyContainer $strategies used to convert nested objects.
67
     * @param Context $context of the created object
68
     * @return ClassElement
69
     */
70 8
    protected function doCreate($object, StrategyContainer $strategies, Context $context = null)
71
    {
72 8
        $docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context);
73
74 8
        $classElement = new ClassElement(
75 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...
76 8
            $docBlock,
0 ignored issues
show
Bug introduced by
It seems like $docBlock defined by $this->createDocBlock($s...DocComment(), $context) on line 72 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...
77 8
            $object->extends ? new Fqsen('\\' . $object->extends) : null,
78 8
            $object->isAbstract(),
79 8
            $object->isFinal()
80 8
        );
81
82 8
        return $classElement;
83
    }
84
}
85