CodeTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 84.62%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 1
dl 0
loc 51
ccs 11
cts 13
cp 0.8462
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A defIf() 0 4 1
A defLine() 0 6 1
A buildArguments() 0 13 4
1
<?php declare(strict_types=1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 04.09.16 at 10:13
5
 */
6
namespace samsonframework\generator;
7
8
/**
9
 * Trait for generators that can generate internal code.
10
 *
11
 * @author Vitaly Egorov <[email protected]>
12
 */
13
trait CodeTrait
14
{
15
    /** @var array Collection of code lines */
16
    protected $code = [];
17
18
    /**
19
     * Define code condition block.
20
     *
21
     * @return IfGenerator Condition generator instance
22
     */
23
    public function defIf(): IfGenerator
24
    {
25
        return (new IfGenerator($this))->setIndentation($this->indentation);
0 ignored issues
show
Bug introduced by
The property indentation does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
    }
27
28
    /**
29
     * Add function code line.
30
     *
31
     * @param string $code Code line
32
     *
33
     * @return $this
34
     */
35 24
    public function defLine(string $code)
36
    {
37 24
        $this->code[] = $code;
38
39 24
        return $this;
40
    }
41
42
    /**
43
     * Build arguments list with types.
44
     *
45
     * @param array $arguments Arguments collection
46
     * @param array $defaults Arguments default values collection
47
     *
48
     * @return string Arguments list
49
     */
50 20
    protected function buildArguments(array $arguments, array $defaults = []) : string
51
    {
52 20
        $argumentsString = [];
53 20
        $defaults = array_filter($defaults);
54 20
        foreach ($arguments as $argumentName => $argumentType) {
55
            // Group name with type
56 9
            $argumentsString[] = ($argumentType !== null ? $argumentType . ' ' : '') .
57 9
                '$' . $argumentName .
58 9
                (array_key_exists($argumentName, $defaults) ? ' = ' . $defaults[$argumentName] : '');
59
        }
60
61 20
        return implode(', ', $argumentsString);
62
    }
63
}
64