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; |
|
|
|
|
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)) { |
|
|
|
|
70
|
|
|
$this->parent->generatedCode[$class] = ''; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Pass generated code to parent |
74
|
|
|
$this->parent->generatedCode[$class] .= $this->code($this->indentation); |
|
|
|
|
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
|
|
|
|
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 theid
property of an instance of theAccount
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.