ArgumentGenerator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 50
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B generate() 0 22 6
1
<?php
2
3
namespace Leaditin\Code\Generator;
4
5
use Leaditin\Code\Argument;
6
7
/**
8
 * @package Leaditin\Code
9
 * @author Igor Vuckovic <[email protected]>
10
 * @license MIT
11
 */
12
class ArgumentGenerator extends Generator
13
{
14
    /**
15
     * @var TypeGenerator
16
     */
17
    protected $typeGenerator;
18
19
    /**
20
     * @var ValueGenerator
21
     */
22
    protected $valueGenerator;
23
24
    /**
25
     * @param TypeGenerator $typeGenerator
26
     * @param ValueGenerator $valueGenerator
27
     */
28 6
    public function __construct(TypeGenerator $typeGenerator, ValueGenerator $valueGenerator)
29
    {
30 6
        $this->typeGenerator = $typeGenerator;
31 6
        $this->valueGenerator = $valueGenerator;
32 6
    }
33
34
    /**
35
     * @param Argument $argument
36
     *
37
     * @return string
38
     */
39 6
    public function generate(Argument $argument): string
40
    {
41 6
        $output = preg_replace('/\?(\w+)/', '$1', $this->typeGenerator->generate($argument->type())) . ' ';
42
43 6
        if ($argument->isReference() && $argument->type()->isScalar()) {
44 1
            $output .= ' &';
45
        }
46
47 6
        if ($argument->isVariadic()) {
48 1
            $output .= ' ...';
49
        }
50
51 6
        $output .= '$' . $argument->name();
52
53 6
        if ($argument->defaultValue()->value() === null && !$argument->type()->isNullable()) {
54 4
            return trim(preg_replace('/\s+/', ' ', $output));
55
        }
56
57 2
        $output .= ' = ' . $this->valueGenerator->generate($argument->defaultValue());
58
59 2
        return trim(preg_replace('/\s+/', ' ', $output));
60
    }
61
}
62