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

Trait_::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
namespace phpDocumentor\Reflection\Php\Factory;
14
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\Php\Trait_ as TraitElement;
21
use phpDocumentor\Reflection\Types\Context;
22
use PhpParser\Comment\Doc;
23
use PhpParser\Node;
24
use PhpParser\Node\Stmt\ClassMethod;
25
use PhpParser\Node\Stmt\Property as PropertyNode;
26
use PhpParser\Node\Stmt\Trait_ as TraitNode;
27
use PhpParser\Node\Stmt\TraitUse;
28
29
final class Trait_ extends AbstractFactory implements ProjectFactoryStrategy
0 ignored issues
show
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...
30
{
31
    /**
32
     * Returns true when the strategy is able to handle the object.
33
     *
34
     * @param TraitNode $object object to check.
35
     * @return boolean
36
     */
37 1
    public function matches($object)
38
    {
39 1
        return $object instanceof TraitNode;
40
    }
41
42
    /**
43
     * Creates an TraitElement out of the given object.
44
     * Since an object might contain other objects that need to be converted the $factory is passed so it can be
45
     * used to create nested Elements.
46
     *
47
     * @param TraitNode $object object to convert to an TraitElement
48
     * @param StrategyContainer $strategies used to convert nested objects.
49
     * @param Context $context
50
     * @return TraitElement
51
     */
52 5
    protected function doCreate($object, StrategyContainer $strategies, Context $context = null)
53
    {
54 5
        $docBlock = $this->createDocBlock($object->getDocComment(), $strategies, $context);
55
56 5
        $trait = new TraitElement($object->fqsen, $docBlock);
0 ignored issues
show
Bug introduced by
The property fqsen does not seem to exist in PhpParser\Node\Stmt\Trait_.

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 54 can also be of type object<phpDocumentor\Reflection\Element>; however, phpDocumentor\Reflection\Php\Trait_::__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...
57
58 5
        if (isset($object->stmts)) {
59 3
            foreach ($object->stmts as $stmt) {
60 3
                switch (get_class($stmt)) {
61 3
                    case PropertyNode::class:
62 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...
63 1
                        foreach ($properties as $property) {
64 1
                            $element = $this->createMember($property, $strategies, $context);
65 1
                            $trait->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...
66 1
                        }
67 1
                        break;
68 2
                    case ClassMethod::class:
69 1
                        $method = $this->createMember($stmt, $strategies, $context);
70 1
                        $trait->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...
71 1
                        break;
72 1
                    case TraitUse::class:
73 2
                        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...
74 1
                            $trait->addUsedTrait(new Fqsen('\\'. $use->toString()));
75 1
                        }
76 1
                        break;
77 3
                }
78 3
            }
79 3
        }
80
81 5
        return $trait;
82
    }
83
}
84