Completed
Pull Request — develop (#127)
by Chuck
04:03
created

Method::doCreate()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 22
nc 6
nop 3
dl 0
loc 33
ccs 21
cts 21
cp 1
crap 4
rs 8.5806
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @copyright 2010-2018 Mike van Riel<[email protected]>
11
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
12
 * @link      http://phpdoc.org
13
 */
14
15
namespace phpDocumentor\Reflection\Php\Factory;
16
17
use phpDocumentor\Reflection\Location;
18
use phpDocumentor\Reflection\Php\Method as MethodDescriptor;
19
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
20
use phpDocumentor\Reflection\Php\StrategyContainer;
21
use phpDocumentor\Reflection\Php\Visibility;
22
use phpDocumentor\Reflection\TypeResolver;
23
use phpDocumentor\Reflection\Types\Context;
24
use PhpParser\Node\NullableType;
25
use PhpParser\Node\Stmt\ClassMethod;
26
27
/**
28
 * Strategy to create MethodDescriptor and arguments when applicable.
29
 */
30
final class Method extends AbstractFactory implements ProjectFactoryStrategy
31
{
32
    /**
33
     * Returns true when the strategy is able to handle the object.
34
     *
35
     *
36
     * @param mixed $object object to check.
37
     */
38 1
    public function matches($object): bool
39
    {
40 1
        return $object instanceof ClassMethod;
41
    }
42
43
    /**
44
     * Creates an MethodDescriptor out of the given object including its child elements.
45
     *
46
     * @param ClassMethod $object object to convert to an MethodDescriptor
47
     * @param StrategyContainer $strategies used to convert nested objects.
48
     * @param Context $context of the created object
49
     * @return MethodDescriptor
50
     */
51 6
    protected function doCreate($object, StrategyContainer $strategies, ?Context $context = null)
52
    {
53 6
        $docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context);
54
55 6
        $returnType = null;
56 6
        if ($object->getReturnType() !== null) {
57 2
            $typeResolver = new TypeResolver();
58 2
            if ($object->getReturnType() instanceof NullableType) {
59 1
                $typeString = '?' . $object->getReturnType()->type;
60
            } else {
61 1
                $typeString = (string) $object->getReturnType();
62
            }
63
64 2
            $returnType = $typeResolver->resolve($typeString, $context);
65
        }
66
67 6
        $method = new MethodDescriptor(
68 6
            $object->fqsen,
69 6
            $this->buildVisibility($object),
70 6
            $docBlock,
0 ignored issues
show
Bug introduced by
It seems like $docBlock defined by $this->createDocBlock($s...DocComment(), $context) on line 53 can also be of type object<phpDocumentor\Reflection\Element>; however, phpDocumentor\Reflection\Php\Method::__construct() does only seem to accept object<phpDocumentor\Reflection\DocBlock>|null, 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...
71 6
            $object->isAbstract(),
72 6
            $object->isStatic(),
73 6
            $object->isFinal(),
74 6
            new Location($object->getLine()),
75 6
            $returnType
76
        );
77
78 6
        foreach ($object->params as $param) {
79 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...
80
        }
81
82 6
        return $method;
83
    }
84
85
    /**
86
     * Converts the visibility of the method to a valid Visibility object.
87
     *
88
     * @return Visibility
89
     */
90 6
    private function buildVisibility(ClassMethod $node)
91
    {
92 6
        if ($node->isPrivate()) {
93 4
            return new Visibility(Visibility::PRIVATE_);
94 2
        } elseif ($node->isProtected()) {
95 1
            return new Visibility(Visibility::PROTECTED_);
96
        }
97
98 1
        return new Visibility(Visibility::PUBLIC_);
99
    }
100
}
101