Function_   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 10
c 0
b 0
f 0
ccs 16
cts 16
cp 1
wmc 5
lcom 1
cbo 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A matches() 0 4 1
A doCreate() 0 25 4
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\Function_ as FunctionDescriptor;
19
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
20
use phpDocumentor\Reflection\Php\StrategyContainer;
21
use phpDocumentor\Reflection\TypeResolver;
22
use phpDocumentor\Reflection\Types\Context;
23
use PhpParser\Node\NullableType;
24
use PhpParser\Node\Stmt\Function_ as FunctionNode;
25
26
/**
27
 * Strategy to convert Function_ to FunctionDescriptor
28
 *
29
 * @see FunctionDescriptor
30
 * @see \PhpParser\Node\
31
 */
32
// @codingStandardsIgnoreStart
33
final class Function_ extends AbstractFactory implements ProjectFactoryStrategy
34
// @codingStandardsIgnoreEnd
35
{
36 1
    public function matches($object) : bool
37
    {
38 1
        return $object instanceof FunctionNode;
39
    }
40
41
    /**
42
     * Creates a FunctionDescriptor out of the given object including its child elements.
43
     *
44
     * @param \PhpParser\Node\Stmt\Function_ $object object to convert to an Element
45
     * @param StrategyContainer $strategies used to convert nested objects.
46
     * @param Context $context of the created object
47
     * @return FunctionDescriptor
48
     */
49 5
    protected function doCreate($object, StrategyContainer $strategies, ?Context $context = null)
50
    {
51 5
        $docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context);
52
53 5
        $returnType = null;
54 5
        if ($object->getReturnType() !== null) {
55 2
            $typeResolver = new TypeResolver();
56 2
            if ($object->getReturnType() instanceof NullableType) {
57 1
                $typeString = '?' . $object->getReturnType()->type;
58
            } else {
59 1
                $typeString = (string) $object->getReturnType();
60
            }
61
62 2
            $returnType = $typeResolver->resolve($typeString, $context);
63
        }
64
65 5
        $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 51 can also be of type object<phpDocumentor\Reflection\Element>; however, phpDocumentor\Reflection...unction_::__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...
66
67 5
        foreach ($object->params as $param) {
68 1
            $strategy = $strategies->findMatching($param);
69 1
            $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...
70
        }
71
72 5
        return $function;
73
    }
74
}
75