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

Function_::doCreate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4286
nc 2
cc 2
eloc 7
nop 3
crap 2
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\Php\Factory;
18
use phpDocumentor\Reflection\Php\Function_ as FunctionDescriptor;
19
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
20
use phpDocumentor\Reflection\Php\StrategyContainer;
21
use phpDocumentor\Reflection\Types\Context;
22
use PhpParser\Comment\Doc;
23
use PhpParser\Node\Stmt\Function_ as FunctionNode;
24
25
/**
26
 * Strategy to convert Function_ to FunctionDescriptor
27
 *
28
 * @see FunctionDescriptor
29
 * @see \PhpParser\Node\
30
 */
31
final class Function_ extends AbstractFactory implements ProjectFactoryStrategy
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
32
{
33
34
    /**
35
     * Returns true when the strategy is able to handle the object.
36
     *
37
     * @param FunctionNode $object object to check.
38
     * @return boolean
39
     */
40 1
    public function matches($object)
41
    {
42 1
        return $object instanceof FunctionNode;
43
    }
44
45
    /**
46
     * Creates an FunctionDescriptor out of the given object including its child elements.
47
     *
48
     * @param \PhpParser\Node\Stmt\Function_ $object object to convert to an Element
49
     * @param StrategyContainer $strategies used to convert nested objects.
50
     * @param Context $context of the created object
51
     * @return FunctionDescriptor
52
     */
53 3
    protected function doCreate($object, StrategyContainer $strategies, Context $context = null)
54
    {
55 3
        $docBlock = $this->createDocBlock($object->getDocComment(), $strategies, $context);
56
57 3
        $function = new FunctionDescriptor($object->fqsen, $docBlock);
0 ignored issues
show
Bug introduced by
The property fqsen does not seem to exist in PhpParser\Node\Stmt\Function_.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
It seems like $docBlock defined by $this->createDocBlock($o... $strategies, $context) on line 55 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...
58
59 3
        foreach ($object->params as $param) {
60 1
            $strategy = $strategies->findMatching($param);
61 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...
62 3
        }
63
64 3
        return $function;
65
    }
66
}
67