ArrowFunction::manageExprDependency()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Murtukov\PHPCodeGenerator;
6
7
class ArrowFunction extends AbstractFunction
8
{
9
    /** @var GeneratorInterface|string */
10
    private $expression;
11
12 2
    public function __construct($expression = null, string $returnType = '')
13
    {
14 2
        $this->signature = new Signature('', Modifier::NONE, $returnType, 'fn');
15 2
        $this->expression = $this->manageExprDependency($expression);
16
17 2
        $this->dependencyAwareChildren[] = $this->signature;
18 2
    }
19
20 2
    public static function new($expression = null, string $returnType = '')
21
    {
22 2
        return new self($expression, $returnType);
23
    }
24
25 3
    public function generate(): string
26
    {
27 3
        $expression = Utils::stringify($this->expression);
28
29 3
        return "$this->signature => $expression";
30
    }
31
32
    /**
33
     * @return GeneratorInterface|string
34
     */
35 1
    public function getExpression()
36
    {
37 1
        return $this->expression;
38
    }
39
40
    /**
41
     * @param mixed $expression
42
     */
43 1
    public function setExpression($expression): self
44
    {
45 1
        $this->expression = $this->manageExprDependency($expression);
46
47 1
        return $this;
48
    }
49
50 2
    protected function manageExprDependency($value)
51
    {
52 2
        if ($value instanceof DependencyAwareGenerator) {
53 1
            $this->dependencyAwareChildren['expr'] = $value;
54
        } else {
55 2
            unset($this->dependencyAwareChildren['expr']);
56
        }
57
58 2
        return $value;
59
    }
60
}
61