Completed
Push — develop ( ea714c...693449 )
by Jaap
9s
created

Method::buildVisibility()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3
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-2018 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 phpDocumentor\Reflection\Location;
16
use phpDocumentor\Reflection\Php\Method as MethodDescriptor;
17
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
18
use phpDocumentor\Reflection\Php\StrategyContainer;
19
use phpDocumentor\Reflection\Php\Visibility;
20
use phpDocumentor\Reflection\TypeResolver;
21
use phpDocumentor\Reflection\Types\Context;
22
use phpDocumentor\Reflection\Types\Mixed_;
23
use PhpParser\Node\NullableType;
24
use PhpParser\Node\Stmt\ClassMethod;
25
26
/**
27
 * Strategy to create MethodDescriptor and arguments when applicable.
28
 */
29
final class Method extends AbstractFactory implements ProjectFactoryStrategy
30
{
31
    /**
32
     * Returns true when the strategy is able to handle the object.
33
     *
34
     * @param object $object object to check.
35
     * @return boolean
36
     */
37 1
    public function matches($object)
38
    {
39 1
        return $object instanceof ClassMethod;
40
    }
41
42
    /**
43
     * Creates an MethodDescriptor out of the given object including its child elements.
44
     *
45
     * @param ClassMethod $object object to convert to an MethodDescriptor
46
     * @param StrategyContainer $strategies used to convert nested objects.
47
     * @param Context $context of the created object
48
     * @return MethodDescriptor
49
     */
50 6
    protected function doCreate($object, StrategyContainer $strategies, Context $context = null)
51
    {
52 6
        $docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context);
53
54 6
        $returnType = null;
55 6
        if ($object->getReturnType() !== null) {
56 2
            $typeResolver = new TypeResolver();
57 2
            if ($object->getReturnType() instanceof NullableType) {
58 1
                $typeString = '?' . $object->getReturnType()->type;
59
            } else {
60 1
                $typeString = (string)$object->getReturnType();
61
            }
62 2
            $returnType = $typeResolver->resolve($typeString, $context);
63
        }
64
65 6
        $method = new MethodDescriptor(
66 6
            $object->fqsen,
67 6
            $this->buildVisibility($object),
68 6
            $docBlock,
0 ignored issues
show
Bug introduced by
It seems like $docBlock defined by $this->createDocBlock($s...DocComment(), $context) on line 52 can also be of type object<phpDocumentor\Reflection\Element>; however, phpDocumentor\Reflection\Php\Method::__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...
69 6
            $object->isAbstract(),
70 6
            $object->isStatic(),
71 6
            $object->isFinal(),
72 6
            new Location($object->getLine()),
73 6
            $returnType
74
        );
75
76 6
        foreach ($object->params as $param) {
77 1
            $method->addArgument($this->createMember($param, $strategies, $context));
0 ignored issues
show
Documentation introduced by
$this->createMember($par... $strategies, $context) is of type object<phpDocumentor\Reflection\Element>, but the function expects a object<phpDocumentor\Reflection\Php\Argument>.

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...
78
        }
79
80 6
        return $method;
81
    }
82
83
    /**
84
     * Converts the visibility of the method to a valid Visibility object.
85
     *
86
     * @param ClassMethod $node
87
     * @return Visibility
88
     */
89 6
    private function buildVisibility(ClassMethod $node)
90
    {
91 6
        if ($node->isPrivate()) {
92 4
            return new Visibility(Visibility::PRIVATE_);
93 2
        } elseif ($node->isProtected()) {
94 1
            return new Visibility(Visibility::PROTECTED_);
95
        }
96
97 1
        return new Visibility(Visibility::PUBLIC_);
98
    }
99
}
100