ParameterNodeParser   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 49
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A invoke() 0 19 5
A __construct() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of PhpUnitGen.
5
 *
6
 * (c) 2017-2018 Paul Thébaud <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PhpUnitGen\Parser\NodeParser;
13
14
use PhpParser\Node;
15
use PhpUnitGen\Exception\Exception;
16
use PhpUnitGen\Model\ModelInterface\FunctionModelInterface;
17
use PhpUnitGen\Model\ParameterModel;
18
use PhpUnitGen\Model\PropertyInterface\NodeInterface;
19
20
/**
21
 * Class ParameterNodeParser.
22
 *
23
 * @author     Paul Thébaud <[email protected]>.
24
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
25
 * @license    https://opensource.org/licenses/MIT The MIT license.
26
 * @link       https://github.com/paul-thebaud/phpunit-generator
27
 * @since      Class available since Release 2.0.0.
28
 */
29
class ParameterNodeParser extends AbstractNodeParser
30
{
31
    /**
32
     * @var TypeNodeParser $typeNodeParser The type node parser.
33
     */
34
    protected $typeNodeParser;
35
36
    /**
37
     * @var ValueNodeParser $valueNodeParser The value node parser.
38
     */
39
    protected $valueNodeParser;
40
41
    /**
42
     * AttributeNodeParser constructor.
43
     *
44
     * @param TypeNodeParser  $typeNodeParser  The type node parser.
45
     * @param ValueNodeParser $valueNodeParser The value node parser.
46
     */
47
    public function __construct(TypeNodeParser $typeNodeParser, ValueNodeParser $valueNodeParser)
48
    {
49
        $this->typeNodeParser  = $typeNodeParser;
50
        $this->valueNodeParser = $valueNodeParser;
51
    }
52
53
    /**
54
     * Parse a node to update the parent node model.
55
     *
56
     * @param mixed         $node   The node to parse.
57
     * @param NodeInterface $parent The parent node.
58
     */
59
    public function invoke($node, NodeInterface $parent): void
60
    {
61
        if (! $node instanceof Node\Param || ! $parent instanceof FunctionModelInterface) {
62
            throw new Exception('ParameterNodeParser is made to parse a function parameter node');
63
        }
64
65
        $parameter = new ParameterModel();
66
        $parameter->setParentNode($parent);
67
        $parameter->setName($node->var->name);
0 ignored issues
show
Bug introduced by
It seems like $node->var->name can also be of type PhpParser\Node\Expr; however, parameter $name of PhpUnitGen\Model\ParameterModel::setName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
        $parameter->setName(/** @scrutinizer ignore-type */ $node->var->name);
Loading history...
Bug introduced by
The property name does not seem to exist on PhpParser\Node\Expr\Error.
Loading history...
68
        $parameter->setIsVariadic($node->variadic);
69
70
        if ($node->type !== null) {
71
            $this->typeNodeParser->invoke($node->type, $parameter);
72
        }
73
        if ($node->default !== null) {
74
            $this->valueNodeParser->invoke($node->default, $parameter);
75
        }
76
77
        $parent->addParameter($parameter);
78
    }
79
}
80