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

Method::create()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 4

Importance

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