Completed
Push — master ( 9c59b1...ce9df9 )
by Vitaly
02:20
created

AbstractGenerator::setIndentation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 03.09.16 at 11:37
5
 */
6
namespace samsonphp\generator;
7
8
/**
9
 * Abstract code generator.
10
 *
11
 * @author Vitaly Egorov <[email protected]>
12
 */
13
abstract class AbstractGenerator
14
{
15
    /** @var GenericGenerator Parent class generator */
16
    protected $parent;
17
18
    /** @var array Generated code grouped by generator class name */
19
    protected $generatedCode = [];
20
21
    /** @var int Indentation level */
22
    protected $indentation = 0;
23
24
    /**
25
     * MethodGenerator constructor.
26
     *
27
     * @param GenericGenerator $parent Parent generator
28
     */
29
    public function __construct(AbstractGenerator $parent = null)
30
    {
31
        $this->parent = $parent;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parent can also be of type object<samsonphp\generator\AbstractGenerator>. However, the property $parent is declared as type object<samsonphp\generator\GenericGenerator>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
32
    }
33
34
    /**
35
     * Decrease indentation.
36
     *
37
     * @param int $indentation
38
     *
39
     * @return $this
40
     */
41
    public function setIndentation(int $indentation) : AbstractGenerator
42
    {
43
        $this->indentation = $indentation;
44
45
        return $this;
46
    }
47
48
    /**
49
     * Increase indentation.
50
     *
51
     * @return $this
52
     */
53
    public function increaseIndentation() : AbstractGenerator
54
    {
55
        $this->indentation++;
56
57
        return $this;
58
    }
59
60
    /**
61
     * Close current generator and return parent.
62
     *
63
     * @return AbstractGenerator Parent
64
     */
65
    public function end() : AbstractGenerator
66
    {
67
        // Create array item
68
        $class = get_class($this);
69
        if (!array_key_exists($class, $this->parent->generatedCode)) {
0 ignored issues
show
Bug introduced by
The property generatedCode cannot be accessed from this context as it is declared protected in class samsonphp\generator\AbstractGenerator.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
70
            $this->parent->generatedCode[$class] = '';
0 ignored issues
show
Bug introduced by
The property generatedCode cannot be accessed from this context as it is declared protected in class samsonphp\generator\AbstractGenerator.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
71
        }
72
73
        // Pass generated code to parent
74
        $this->parent->generatedCode[$class] .= $this->code($this->indentation);
0 ignored issues
show
Bug introduced by
The property generatedCode cannot be accessed from this context as it is declared protected in class samsonphp\generator\AbstractGenerator.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
75
76
        return $this->parent;
77
    }
78
79
    /**
80
     * Generate code.
81
     *
82
     * @param int $indentation Code level
83
     *
84
     * @return string Generated code
85
     */
86
    abstract public function code(int $indentation = 0) : string;
87
88
    /**
89
     * Set Comments block.
90
     *
91
     * @return CommentsGenerator Comments block generator
92
     */
93
    public function defComment() : CommentsGenerator
94
    {
95
        return new CommentsGenerator($this);
96
    }
97
98
    /**
99
     * Get indentation string.
100
     *
101
     * @param int $indentation Code level
102
     *
103
     * @return string Indentation string
104
     */
105
    protected function indentation(int $indentation = 0) : string
106
    {
107
        return implode('', $indentation > 0 ? array_fill(0, $indentation, '    ') : []);
108
    }
109
}
110