|
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 Collection of code lines */ |
|
19
|
|
|
protected $code = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* MethodGenerator constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param GenericGenerator $parent Parent generator |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(GenericGenerator $parent = null) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->parent = $parent; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Close current generator and return parent. |
|
33
|
|
|
* |
|
34
|
|
|
* @return AbstractGenerator Parent |
|
35
|
|
|
*/ |
|
36
|
|
|
public function end() : AbstractGenerator |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->parent; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get indentation string. |
|
43
|
|
|
* |
|
44
|
|
|
* @param int $indentation Code level |
|
45
|
|
|
* |
|
46
|
|
|
* @return string Indentation string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function indentation($indentation = 0) : string |
|
49
|
|
|
{ |
|
50
|
|
|
return implode('', $indentation > 0 ? array_fill(0, $indentation, ' ') : []); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Add function code line. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $code Code line |
|
57
|
|
|
* |
|
58
|
|
|
* @return AbstractGenerator |
|
59
|
|
|
*/ |
|
60
|
|
|
public function defLine(string $code) : AbstractGenerator |
|
61
|
|
|
{ |
|
62
|
|
|
$this->code[] = $code; |
|
63
|
|
|
|
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set Comments block. |
|
69
|
|
|
* |
|
70
|
|
|
* @return CommentsGenerator Comments block generator |
|
71
|
|
|
*/ |
|
72
|
|
|
public function defComment() : CommentsGenerator |
|
73
|
|
|
{ |
|
74
|
|
|
return new CommentsGenerator($this); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Generate code. |
|
79
|
|
|
* |
|
80
|
|
|
* @param int $indentation Code level |
|
81
|
|
|
* |
|
82
|
|
|
* @return string Generated code |
|
83
|
|
|
*/ |
|
84
|
|
|
abstract public function code($indentation = 0) : string; |
|
85
|
|
|
} |
|
86
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: