Completed
Push — develop ( 523147...71572c )
by Jaap
10s
created

Argument   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 14
cts 21
cp 0.6667
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A matches() 0 4 1
A doCreate() 0 17 3
A createType() 0 11 2
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\Php\Argument as ArgumentDescriptor;
18
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
19
use phpDocumentor\Reflection\Php\StrategyContainer;
20
use phpDocumentor\Reflection\PrettyPrinter;
21
use phpDocumentor\Reflection\Type;
22
use phpDocumentor\Reflection\TypeResolver;
23
use phpDocumentor\Reflection\Types\Context;
24
use PhpParser\Node\Expr\Variable;
25
use PhpParser\Node\NullableType;
26
use PhpParser\Node\Param;
27
use Webmozart\Assert\Assert;
28
29
/**
30
 * Strategy to convert Param to Argument
31
 *
32
 * @see \phpDocumentor\Descriptor\Argument
33
 * @see \PhpParser\Node\Arg
34
 */
35
final class Argument extends AbstractFactory implements ProjectFactoryStrategy
36
{
37
    /**
38
     * @var PrettyPrinter
39
     */
40
    private $valueConverter;
41
42
    /**
43
     * Initializes the object.
44
     */
45 3
    public function __construct(PrettyPrinter $prettyPrinter)
46
    {
47 3
        $this->valueConverter = $prettyPrinter;
48 3
    }
49
50 1
    public function matches($object): bool
51
    {
52 1
        return $object instanceof Param;
53
    }
54
55
    /**
56
     * Creates an ArgumentDescriptor out of the given object.
57
     * Since an object might contain other objects that need to be converted the $factory is passed so it can be
58
     * used to create nested Elements.
59
     *
60
     * @param Param $object object to convert to an Element
61
     * @param StrategyContainer $strategies used to convert nested objects.
62
     * @param Context $context of the created object
63
     * @return ArgumentDescriptor
64
     */
65 1
    protected function doCreate($object, StrategyContainer $strategies, ?Context $context = null)
66
    {
67 1
        Assert::isInstanceOf($object, Param::class);
68 1
        Assert::isInstanceOf($object->var, Variable::class);
69 1
        $default = null;
70 1
        if ($object->default !== null) {
71 1
            $default = $this->valueConverter->prettyPrintExpr($object->default);
72
        }
73
74 1
        $argumentDescriptor = new ArgumentDescriptor((string) $object->var->name, $default, $object->byRef, $object->variadic);
75
76 1
        if (!empty($object->type)) {
77
            $argumentDescriptor->addType($this->createType($object));
78
        }
79
80 1
        return $argumentDescriptor;
81
    }
82
83
    private function createType(Param $arg, ?Context $context = null): Type
84
    {
85
        $typeResolver = new TypeResolver();
86
        if ($arg->type instanceof NullableType) {
87
            $typeString = '?' . $arg->type->type;
88
        } else {
89
            $typeString = (string) $arg->type;
90
        }
91
92
        return $typeResolver->resolve($typeString, $context);
93
    }
94
}
95