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

Function_   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 84.62%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 43
ccs 11
cts 13
cp 0.8462
rs 10
c 2
b 0
f 0
wmc 4
lcom 1
cbo 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A matches() 0 4 1
A doCreate() 0 19 3
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\Stmt\Function_ as FunctionNode;
26
27
/**
28
 * Strategy to convert Function_ to FunctionDescriptor
29
 *
30
 * @see FunctionDescriptor
31
 * @see \PhpParser\Node\
32
 */
33
// @codingStandardsIgnoreStart
34
final class Function_ extends AbstractFactory implements ProjectFactoryStrategy
35
// @codingStandardsIgnoreEnd
36
{
37
38
    /**
39
     * Returns true when the strategy is able to handle the object.
40
     *
41
     * @param FunctionNode $object object to check.
42
     * @return boolean
43
     */
44 1
    public function matches($object)
45
    {
46 1
        return $object instanceof FunctionNode;
47
    }
48
49
    /**
50
     * Creates an FunctionDescriptor out of the given object including its child elements.
51
     *
52
     * @param \PhpParser\Node\Stmt\Function_ $object object to convert to an Element
53
     * @param StrategyContainer $strategies used to convert nested objects.
54
     * @param Context $context of the created object
55
     * @return FunctionDescriptor
56
     */
57 3
    protected function doCreate($object, StrategyContainer $strategies, Context $context = null)
58
    {
59 3
        $docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context);
60
61 3
        $returnType = null;
62 3
        if ($object->returnType !== null) {
63
            $typeResolver = new TypeResolver();
64
            $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...
65
        }
66
67 3
        $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 59 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...
68
69 3
        foreach ($object->params as $param) {
70 1
            $strategy = $strategies->findMatching($param);
71 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...
72
        }
73
74 3
        return $function;
75
    }
76
}
77