Test Failed
Push — master ( b804f2...c730e0 )
by Vitaly
02:49
created

FunctionGenerator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 87.18%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 150
ccs 34
cts 39
cp 0.8718
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A defArgument() 0 15 1
A setIndentation() 0 7 1
A defDescription() 0 12 2
A defReturnType() 0 6 1
A code() 0 19 4
A buildDefinition() 0 4 1
A defComment() 0 4 1
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 CommentsGenerator */
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 CommentsGenerator($this))
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(
62
        string $name,
63
        string $type = null,
64
        string $description = null,
65
        $defaultValue = null
66
    ): FunctionGenerator
67
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
68 8
        $this->arguments[$name] = $type;
69 8
        $this->argumentDescriptions[$name] = $description;
70 8
        $this->argumentDefaults[$name] = $defaultValue;
71
72 8
        $this->commentGenerator->defParam($name, $type, $description);
73
74 8
        return $this;
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80 6
    public function setIndentation(int $indentation): AbstractGenerator
81
    {
82
        // Set comments indentation
83 6
        $this->commentGenerator->setIndentation($indentation);
84
85 6
        return parent::setIndentation($indentation);
86
    }
87
88
    /**
89
     * Set function description comment.
90
     *
91
     * @param array $description Function description
92
     *
93
     * @return FunctionGenerator
94
     */
95
    public function defDescription(array $description): FunctionGenerator
96
    {
97
        // Append description lines
98
        foreach ($description as $line) {
99
            $this->commentGenerator->defLine($line);
100
        }
101
102
        // Add one empty line at the end
103
        $this->commentGenerator->defLine('');
104
105
        return $this;
106
    }
107
108
    /**
109
     * Set return value type
110
     *
111
     * @param string|null $type Return type hint
112
     *
113
     * @return FunctionGenerator
114
     */
115 1
    public function defReturnType(string $type): FunctionGenerator
116
    {
117 1
        $this->returnType = $type;
118
119 1
        return $this;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 15
    public function code(): string
126
    {
127 15
        $formattedCode[] =
0 ignored issues
show
Coding Style Comprehensibility introduced by
$formattedCode was never initialized. Although not strictly required by PHP, it is generally a good practice to add $formattedCode = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
128 15
            $this->indentation($this->indentation) . $this->buildDefinition()
129 15
            . '(' . $this->buildArguments($this->arguments, $this->argumentDefaults) . ')'
130 15
            . ($this->returnType ? ' : ' . $this->returnType : '');
131
132
        // Prepend inner indentation to code
133 15
        $innerIndentation = $this->indentation(1);
134 15
        $formattedCode[] = '{';
135 15
        foreach ($this->code as $codeLine) {
136 9
            $formattedCode[] = $innerIndentation . $codeLine;
137
        }
138 15
        $formattedCode[] = '}';
139
140 15
        $comments = $this->getNestedCode(CommentsGenerator::class);
141
142 15
        return "\n\n" . ($comments !== '' ? $comments . "\n" : '') . implode("\n" . $this->indentation($this->indentation), $formattedCode);
143
    }
144
145
    /**
146
     * Build function definition.
147
     *
148
     * @return string Function definition
149
     */
150 5
    protected function buildDefinition()
151
    {
152 5
        return 'function ' . $this->name;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158 7
    public function defComment(): CommentsGenerator
159
    {
160 7
        return $this->commentGenerator;
161
    }
162
}
163