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

Function_   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 48
ccs 16
cts 16
cp 1
rs 10
c 1
b 0
f 0
wmc 5
lcom 1
cbo 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A matches() 0 4 1
B 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
    /**
37
     * Returns true when the strategy is able to handle the object.
38
     *
39
     *
40
     * @param mixed $object object to check.
41
     */
42 1
    public function matches($object): bool
43
    {
44 1
        return $object instanceof FunctionNode;
45
    }
46
47
    /**
48
     * Creates an FunctionDescriptor out of the given object including its child elements.
49
     *
50
     * @param \PhpParser\Node\Stmt\Function_ $object object to convert to an Element
51
     * @param StrategyContainer $strategies used to convert nested objects.
52
     * @param Context $context of the created object
53
     * @return FunctionDescriptor
54
     */
55 5
    protected function doCreate($object, StrategyContainer $strategies, ?Context $context = null)
56
    {
57 5
        $docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context);
58
59 5
        $returnType = null;
60 5
        if ($object->getReturnType() !== null) {
61 2
            $typeResolver = new TypeResolver();
62 2
            if ($object->getReturnType() instanceof NullableType) {
63 1
                $typeString = '?' . $object->getReturnType()->type;
64
            } else {
65 1
                $typeString = (string) $object->getReturnType();
66
            }
67
68 2
            $returnType = $typeResolver->resolve($typeString, $context);
69
        }
70
71 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 57 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...
72
73 5
        foreach ($object->params as $param) {
74 1
            $strategy = $strategies->findMatching($param);
75 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...
76
        }
77
78 5
        return $function;
79
    }
80
}
81