Completed
Push — master ( e7e919...9700e8 )
by Vitaly
02:18
created

FunctionGenerator::buildArguments()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 0
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 03.09.16 at 11:30
5
 */
6
namespace samsonphp\generator;
7
8
/**
9
 * Function generation class.
10
 *
11
 * @author Vitaly Egorov <[email protected]>
12
 * @method FunctionGenerator defLine(string $code);
13
 */
14
class FunctionGenerator extends AbstractGenerator
15
{
16
    /** @var string Function name */
17
    protected $name;
18
19
    /** @var array Collection of function arguments */
20
    protected $arguments = [];
21
22
    /** @var array Collection of function arguments descriptions */
23
    protected $argumentDescriptions = [];
24
25
    /**
26
     * FunctionGenerator constructor.
27
     *
28
     * @param GenericGenerator $parent Parent Generator
29
     * @param string           $name Function name
30
     */
31
    public function __construct(string $name, GenericGenerator $parent = null)
32
    {
33
        $this->name = $name;
34
35
        parent::__construct($parent);
36
    }
37
38
    /**
39
     * Set function argument.
40
     *
41
     * @param string      $name        Argument name
42
     * @param string|null $type        Argument type
43
     * @param string      $description Argument description
44
     *
45
     * @return FunctionGenerator
46
     */
47
    public function defArgument(string $name, string $type = null, string $description = null) : FunctionGenerator
48
    {
49
        $this->arguments[$name] = $type;
50
        $this->argumentDescriptions[$name] = $description;
51
52
        return $this;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function code(int $indentation = 0) : string
59
    {
60
        $innerIndentation = $this->indentation(1);
61
62
        $formattedCode = [
63
            $this->buildDefinition() . '(' . $this->buildArguments() . ')',
64
            '{'
65
        ];
66
        // Prepend inner indentation to code
67
        foreach ($this->code as $codeLine) {
68
            $formattedCode[] = $innerIndentation.$codeLine;
69
        }
70
        $formattedCode[] = '}';
71
72
        $this->generatedCode .= implode("\n".$this->indentation($indentation), $formattedCode);
73
74
        return $this->generatedCode;
75
    }
76
77
    /**
78
     * Build function arguments.
79
     *
80
     * @return string
81
     */
82 View Code Duplication
    protected function buildArguments() : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $argumentsString = [];
85
        foreach ($this->arguments as $argumentName => $argumentType) {
86
            // Group name with type
87
            $argumentsString[] = ($argumentType !== null ? $argumentType . ' ' : '') . '$' . $argumentName;
88
        }
89
90
        return implode(', ', $argumentsString);
91
    }
92
93
    /**
94
     * Build function arguments.
95
     *
96
     * @return string
97
     */
98 View Code Duplication
    protected function buildArguments() : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100
        $argumentsString = [];
101
        foreach ($this->arguments as $argumentName => $argumentType) {
102
            // Group name with type
103
            $argumentsString[] = ($argumentType !== null ? $argumentType . ' ' : '') . '$' . $argumentName;
104
        }
105
106
        return implode(', ', $argumentsString);
107
    }
108
109
    /**
110
     * Build function definition.
111
     *
112
     * @return string Function definition
113
     */
114
    protected function buildDefinition()
115
    {
116
        return 'function ' . $this->name;
117
    }
118
}
119