Completed
Push — develop ( 66de6a...7afc9d )
by Jaap
04:46 queued 03:01
created

Method::doCreate()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3.0123

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 4
nop 3
dl 0
loc 27
ccs 16
cts 18
cp 0.8889
crap 3.0123
rs 8.8571
c 0
b 0
f 0
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 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\Stmt\ClassMethod;
24
25
/**
26
 * Strategy to create MethodDescriptor and arguments when applicable.
27
 */
28
final class Method extends AbstractFactory implements ProjectFactoryStrategy
29
{
30
    /**
31
     * Returns true when the strategy is able to handle the object.
32
     *
33
     * @param object $object object to check.
34
     * @return boolean
35
     */
36 1
    public function matches($object)
37
    {
38 1
        return $object instanceof ClassMethod;
39
    }
40
41
    /**
42
     * Creates an MethodDescriptor out of the given object including its child elements.
43
     *
44
     * @param ClassMethod $object object to convert to an MethodDescriptor
45
     * @param StrategyContainer $strategies used to convert nested objects.
46
     * @param Context $context of the created object
47
     * @return MethodDescriptor
48
     */
49 4
    protected function doCreate($object, StrategyContainer $strategies, Context $context = null)
50
    {
51 4
        $docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context);
52
53 4
        $returnType = null;
54 4
        if ($object->returnType !== null) {
55
            $typeResolver = new TypeResolver();
56
            $returnType = $typeResolver->resolve($object->returnType, $context);
0 ignored issues
show
Bug introduced by
It seems like $object->returnType can also be of type object<PhpParser\Node\Name> or object<PhpParser\Node\NullableType>; however, phpDocumentor\Reflection\TypeResolver::resolve() does only seem to accept string, 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
59 4
        $method = new MethodDescriptor(
60 4
            $object->fqsen,
61 4
            $this->buildVisibility($object),
62 4
            $docBlock,
0 ignored issues
show
Bug introduced by
It seems like $docBlock defined by $this->createDocBlock($s...DocComment(), $context) on line 51 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...
63 4
            $object->isAbstract(),
64 4
            $object->isStatic(),
65 4
            $object->isFinal(),
66 4
            new Location($object->getLine()),
67 4
            $returnType
68
        );
69
70 4
        foreach ($object->params as $param) {
71 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...
72
        }
73
74 4
        return $method;
75
    }
76
77
    /**
78
     * Converts the visibility of the method to a valid Visibility object.
79
     *
80
     * @param ClassMethod $node
81
     * @return Visibility
82
     */
83 4
    private function buildVisibility(ClassMethod $node)
84
    {
85 4
        if ($node->isPrivate()) {
86 2
            return new Visibility(Visibility::PRIVATE_);
87 2
        } elseif ($node->isProtected()) {
88 1
            return new Visibility(Visibility::PROTECTED_);
89
        }
90
91 1
        return new Visibility(Visibility::PUBLIC_);
92
    }
93
}
94