AbstractGenerator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 97
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A indentation() 0 4 2
A increaseIndentation() 0 6 1
A end() 0 13 2
code() 0 1 ?
A defComment() 0 4 1
A setIndentation() 0 6 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
     * Increase indentation.
36
     *
37
     * @return $this
38
     */
39
    public function increaseIndentation() : AbstractGenerator
40
    {
41
        $this->indentation++;
42
43
        return $this;
44
    }
45
46
    /**
47
     * Close current generator and return parent.
48
     *
49
     * @return AbstractGenerator Parent
50
     */
51
    public function end() : AbstractGenerator
52
    {
53
        // Create array item
54
        $class = get_class($this);
55
        if (!array_key_exists($class, $this->parent->generatedCode)) {
56
            $this->parent->generatedCode[$class] = '';
57
        }
58
59
        // Pass generated code to parent
60
        $this->parent->generatedCode[$class] .= $this->code($this->indentation);
61
62
        return $this->parent;
63
    }
64
65
    /**
66
     * Generate code.
67
     *
68
     * @param int $indentation Code level
69
     *
70
     * @return string Generated code
71
     */
72
    abstract public function code(int $indentation = 0) : string;
73
74
    /**
75
     * Set Comments block.
76
     *
77
     * @return CommentsGenerator Comments block generator
78
     */
79
    public function defComment() : CommentsGenerator
80
    {
81
        return (new CommentsGenerator($this))->setIndentation($this->indentation);
82
    }
83
84
    /**
85
     * Decrease indentation.
86
     *
87
     * @param int $indentation
88
     *
89
     * @return $this
90
     */
91
    public function setIndentation(int $indentation) : AbstractGenerator
92
    {
93
        $this->indentation = $indentation;
94
95
        return $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