Completed
Push — develop ( 7275cb...ea714c )
by Jaap
14s
created

Function_::doCreate()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

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