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

Argument::create()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 4
Bugs 0 Features 1
Metric Value
dl 0
loc 18
c 4
b 0
f 1
ccs 13
cts 13
cp 1
rs 9.2
cc 4
eloc 10
nc 3
nop 3
crap 4
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\Php\Argument as ArgumentDescriptor;
17
use phpDocumentor\Reflection\Php\Factory;
18
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
19
use phpDocumentor\Reflection\Php\StrategyContainer;
20
use phpDocumentor\Reflection\PrettyPrinter;
21
use phpDocumentor\Reflection\Types\Context;
22
use PhpParser\Node\Param;
23
24
/**
25
 * Strategy to convert Param to Argument
26
 *
27
 * @see \phpDocumentor\Descriptor\Argument
28
 * @see \PhpParser\Node\Arg
29
 */
30
final class Argument extends AbstractFactory implements ProjectFactoryStrategy
31
{
32
    /**
33
     * @var PrettyPrinter
34
     */
35
    private $valueConverter;
36
37
    /**
38
     * Initializes the object.
39
     *
40
     * @param PrettyPrinter $prettyPrinter
41
     */
42 3
    public function __construct(PrettyPrinter $prettyPrinter)
43
    {
44 3
        $this->valueConverter = $prettyPrinter;
45 3
    }
46
47
    /**
48
     * Returns true when the strategy is able to handle the object.
49
     *
50
     * @param Param $object object to check.
51
     * @return boolean
52
     */
53 1
    public function matches($object)
54
    {
55 1
        return $object instanceof Param;
56
    }
57
58
    /**
59
     * Creates an ArgumentDescriptor out of the given object.
60
     * Since an object might contain other objects that need to be converted the $factory is passed so it can be
61
     * used to create nested Elements.
62
     *
63
     * @param Param $object object to convert to an Element
64
     * @param StrategyContainer $strategies used to convert nested objects.
65
     * @param Context $context of the created object
66
     * @return ArgumentDescriptor
67
     */
68 1
    protected function doCreate($object, StrategyContainer $strategies, Context $context = null)
69
    {
70 1
        $default = null;
71 1
        if ($object->default !== null) {
72 1
            $default = $this->valueConverter->prettyPrintExpr($object->default);
73 1
        }
74
75 1
        return new ArgumentDescriptor($object->name, $default, $object->byRef, $object->variadic);
76
    }
77
}
78