Test Failed
Push — master ( 8b1958...86b2db )
by Vitaly
02:32
created

FunctionGenerator::defDescription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
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 samsonframework\generator;
7
8
/**
9
 * Function generation class.
10
 *
11
 * @author Vitaly Egorov <[email protected]>
12
 */
13
class FunctionGenerator extends AbstractGenerator
14
{
15
    use CodeTrait;
16
17
    /** @var string Function name */
18
    protected $name;
19
20
    /** @var array Collection of function arguments */
21
    protected $arguments = [];
22
23
    /** @var array Collection of function arguments descriptions */
24
    protected $argumentDescriptions = [];
25
26
    /** @var array Collection of function arguments default values */
27
    protected $argumentDefaults = [];
28
29
    /** @var string Return type hint */
30
    protected $returnType;
31
32
    /** @var FunctionCommentsGenerator */
33
    protected $commentGenerator;
34
35
    /**
36
     * FunctionGenerator constructor.
37
     *
38
     * @param string            $name   Function name
39
     * @param AbstractGenerator $parent Parent Generator
40
     */
41 20
    public function __construct(string $name, AbstractGenerator $parent = null)
42
    {
43 20
        $this->name = $name;
44
45 20
        $this->commentGenerator = (new FunctionCommentsGenerator($this->arguments, $this->argumentDescriptions, $this))
0 ignored issues
show
Deprecated Code introduced by
The class samsonframework\generato...nctionCommentsGenerator has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
46 20
            ->setIndentation($this->indentation);
47
48 20
        parent::__construct($parent);
49 20
    }
50
51
    /**
52
     * Set function argument.
53
     *
54
     * @param string      $name        Argument name
55
     * @param string|null $type        Argument type
56
     * @param string      $description Argument description
57
     * @param mixed      $defaultValue Argument default value
58
     *
59
     * @return FunctionGenerator
60
     */
61 8
    public function defArgument(string $name, string $type = null, string $description = null, $defaultValue = null) : FunctionGenerator
62
    {
63 8
        $this->arguments[$name] = $type;
64 8
        $this->argumentDescriptions[$name] = $description;
65 8
        $this->argumentDefaults[$name] = $defaultValue;
66
67 8
        $this->commentGenerator->defParam($name, $type, $description);
68
69 8
        return $this;
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75 6
    public function setIndentation(int $indentation): AbstractGenerator
76
    {
77
        // Set comments indentation
78 6
        $this->commentGenerator->setIndentation($indentation);
79
80 6
        return parent::setIndentation($indentation); // TODO: Change the autogenerated stub
81
    }
82
83
    /**
84
     * Set function description comment.
85
     *
86
     * @param array $description Function description
87
     *
88
     * @return FunctionGenerator
89
     */
90
    public function defDescription(array $description): FunctionGenerator
91
    {
92
        // Append description lines
93
        foreach ($description as $line) {
94
            $this->commentGenerator->defLine($line);
95
        }
96
97
        // Add one empty line at the end
98
        $this->commentGenerator->defLine('');
99
100
        return $this;
101
    }
102
103
    /**
104
     * Set return value type
105
     *
106
     * @param string|null $type Return type hint
107
     *
108
     * @return FunctionGenerator
109
     */
110 1
    public function defReturnType(string $type) : FunctionGenerator
111
    {
112 1
        $this->returnType = $type;
113
114 1
        return $this;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 15
    public function code(int $indentation = 0) : string
121
    {
122 15
        $innerIndentation = $this->indentation(1);
123
124
        // Get return type code
125 15
        $returnType = $this->returnType ? ' : ' . $this->returnType : '';
126
127
        $formattedCode = [
128 15
            $this->buildDefinition() . '(' . $this->buildArguments($this->arguments, $this->argumentDefaults) . ')' . $returnType,
129 15
            '{'
130
        ];
131
132
        // Prepend inner indentation to code
133 15
        foreach ($this->code as $codeLine) {
134 9
            $formattedCode[] = $innerIndentation.$codeLine;
135
        }
136
137 15
        $formattedCode[] = '}';
138
139 15
        $code = implode("\n" . $this->indentation($indentation), $formattedCode);
140
141
        // Add comments
142 15
        if (array_key_exists(FunctionCommentsGenerator::class, $this->generatedCode)) {
143 7
            $code = $this->generatedCode[FunctionCommentsGenerator::class] . "\n" . $code;
144
        }
145
146 15
        return $code;
147
    }
148
149
    /**
150
     * Build function definition.
151
     *
152
     * @return string Function definition
153
     */
154 5
    protected function buildDefinition()
155
    {
156 5
        return 'function ' . $this->name;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162 7
    public function defComment() : CommentsGenerator
163
    {
164 7
        return $this->commentGenerator;
165
    }
166
}
167