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
|
|
|
* Class GenericGenerator |
10
|
|
|
* |
11
|
|
|
* @author Vitaly Egorov <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class GenericGenerator extends AbstractGenerator |
14
|
|
|
{ |
15
|
|
|
/** @var ClassGenerator[] Collection of classes */ |
16
|
|
|
protected $classes = []; |
17
|
|
|
|
18
|
|
|
/** @var FunctionGenerator[] Collection of functions */ |
19
|
|
|
protected $functions = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Set function. |
23
|
|
|
* |
24
|
|
|
* @param string $name Function |
25
|
|
|
* @param bool $isStatic Flag that function is static |
26
|
|
|
* |
27
|
|
|
* @return FunctionGenerator New function generator instance |
28
|
|
|
*/ |
29
|
|
|
public function defFunction(string $name, bool $isStatic = false) : FunctionGenerator |
30
|
|
|
{ |
31
|
|
|
return $this->functions[] = new FunctionGenerator($this, $name, $isStatic); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set static function. |
36
|
|
|
* |
37
|
|
|
* @param string $name Function |
38
|
|
|
* |
39
|
|
|
* @return FunctionGenerator New function generator instance |
40
|
|
|
*/ |
41
|
|
|
public function defStaticFunction(string $name) : FunctionGenerator |
42
|
|
|
{ |
43
|
|
|
return $this->defFunction($name, false); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set class. |
48
|
|
|
* |
49
|
|
|
* @param string $name Class name |
50
|
|
|
* |
51
|
|
|
* @return ClassGenerator |
52
|
|
|
*/ |
53
|
|
|
public function defClass(string $name) : ClassGenerator |
54
|
|
|
{ |
55
|
|
|
return $this->classes[] = new ClassGenerator($this, $name); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Generate code. |
60
|
|
|
* |
61
|
|
|
* @param int $indentation Code level |
62
|
|
|
* |
63
|
|
|
* @return string Generated code |
64
|
|
|
*/ |
65
|
|
|
public function code($indentation = 0) |
66
|
|
|
{ |
67
|
|
|
foreach ($this->classes as $classGenerator) { |
68
|
|
|
$this->code[] = $classGenerator->code($indentation); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
foreach ($this->functions as $functionGenerator) { |
72
|
|
|
$this->code[] = $functionGenerator->code($indentation); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return implode("\n".$this->indentation($indentation), $this->code); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
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: