@@ -19,35 +19,40 @@ class ObjectGraphGenerator
19
19
{
20
20
private const DEFAULT_SEED = 1 ;
21
21
22
- /** @var Generator */
23
- private $ fakerInstance ;
24
-
25
- /** @var PropertyInfoExtractor */
26
- private $ propertyInfo ;
27
-
28
- /** @var array */
29
- private $ registry ;
30
-
31
- /** @var array */
32
- private $ temporaryRegistry = [];
22
+ private Generator $ fakerInstance ;
23
+ private PropertyInfoExtractor $ propertyInfo ;
24
+ /** @var array<mixed> */
25
+ private array $ registry ;
26
+ /** @var array<mixed> */
27
+ private array $ temporaryRegistry = [];
33
28
29
+ /**
30
+ * @param array<mixed> $registry
31
+ */
34
32
public function __construct (array $ registry = [])
35
33
{
36
34
$ this ->fakerInstance = Factory::create ();
37
35
38
- $ phpDocExtractor = new PhpDocExtractor ();
36
+ $ phpDocExtractor = new PhpDocExtractor ();
39
37
$ reflectionExtractor = new ReflectionExtractor ();
40
- $ typeExtractors = [$ phpDocExtractor , $ reflectionExtractor ];
41
- $ this ->propertyInfo = new PropertyInfoExtractor ([], $ typeExtractors , [], [], []);
38
+ $ typeExtractors = [$ phpDocExtractor , $ reflectionExtractor ];
39
+ $ this ->propertyInfo = new PropertyInfoExtractor ([], $ typeExtractors , [], [], []);
42
40
$ this ->fakerInstance ->seed (self ::DEFAULT_SEED );
43
41
$ this ->registry = $ registry ;
44
42
}
45
43
44
+ /**
45
+ * @param class-string $className
46
+ */
46
47
public function generate (string $ className ): object
47
48
{
48
49
return $ this ->generateObject ($ className );
49
50
}
50
51
52
+ /**
53
+ * @param class-string $className
54
+ * @param array<mixed> $config
55
+ */
51
56
public function generateWithTemporaryConfig (string $ className , array $ config ): object
52
57
{
53
58
$ this ->temporaryRegistry = $ config ;
@@ -58,18 +63,16 @@ public function generateWithTemporaryConfig(string $className, array $config): o
58
63
}
59
64
60
65
/**
61
- * @param string $className
62
- *
63
- * @return mixed|object
66
+ * @param class-string $className
64
67
* @throws ReflectionException
65
68
*/
66
- private function generateObject (string $ className )
69
+ private function generateObject (string $ className ): object
67
70
{
68
71
if ($ this ->isInRegistry ($ className )) {
69
72
return $ this ->getFromRegistry ($ className );
70
73
}
71
74
72
- $ class = new ReflectionClass ($ className );
75
+ $ class = new ReflectionClass ($ className );
73
76
$ factoryMethod = $ this ->findFactoryMethod ($ class );
74
77
75
78
if ($ factoryMethod === null ) {
@@ -78,6 +81,7 @@ private function generateObject(string $className)
78
81
79
82
$ arguments = array_map (
80
83
function (ReflectionParameter $ parameter ) use ($ className ) {
84
+ /** @var array<mixed> $type */
81
85
$ type = $ this ->propertyInfo ->getTypes ($ className , $ parameter ->getName ());
82
86
83
87
return $ this ->generateArgument ($ type [0 ], $ className , $ parameter ->getName ());
@@ -90,13 +94,17 @@ function (ReflectionParameter $parameter) use ($className) {
90
94
: $ factoryMethod ->invokeArgs (null , $ arguments );
91
95
}
92
96
97
+ /**
98
+ * @param ReflectionClass<object> $class
99
+ */
93
100
private function findFactoryMethod (ReflectionClass $ class ): ?ReflectionMethod
94
101
{
95
102
try {
96
103
return $ class ->getMethod ('createNew ' );
97
104
} catch (ReflectionException $ e ) {
98
105
// Do nothing here
99
106
}
107
+
100
108
try {
101
109
return $ class ->getMethod ('create ' );
102
110
} catch (ReflectionException $ e ) {
@@ -105,47 +113,57 @@ private function findFactoryMethod(ReflectionClass $class): ?ReflectionMethod
105
113
}
106
114
107
115
/**
108
- * @param Type $type
109
- *
110
- * @param string $className
111
- * @param string $argumentName
112
- *
113
116
* @return mixed
114
117
* @throws ReflectionException
115
118
*/
116
119
private function generateArgument (Type $ type , string $ className , string $ argumentName )
117
120
{
118
121
$ faker = $ type ->isNullable () ? $ this ->fakerInstance ->optional () : $ this ->fakerInstance ;
119
122
$ key = sprintf ('%s:%s ' , $ className , $ argumentName );
123
+
120
124
if ($ this ->isInRegistry ($ key )) {
121
125
return $ this ->getFromRegistry ($ key );
122
126
}
123
127
124
128
switch ($ type ->getBuiltinType ()) {
125
129
case Type::BUILTIN_TYPE_INT :
126
130
return $ faker ->randomNumber (5 );
131
+
127
132
case Type::BUILTIN_TYPE_FLOAT :
128
133
return $ faker ->randomFloat ();
134
+
129
135
case Type::BUILTIN_TYPE_STRING :
130
136
return $ faker ->text (100 );
137
+
131
138
case Type::BUILTIN_TYPE_BOOL :
132
139
return $ faker ->boolean ();
140
+
133
141
case Type::BUILTIN_TYPE_ARRAY :
134
142
$ collection = [];
143
+
135
144
if ($ type ->isCollection ()) {
136
145
$ collection = array_map (
137
146
function () use ($ argumentName , $ className , $ type ) {
138
- return $ this ->generateArgument ($ type ->getCollectionValueType (), $ className , $ argumentName );
147
+ /** @var Type $collectionValueType */
148
+ $ collectionValueType = $ type ->getCollectionValueType ();
149
+
150
+ return $ this ->generateArgument ($ collectionValueType , $ className , $ argumentName );
139
151
},
140
152
range (0 , $ faker ->numberBetween (0 , 10 ))
141
153
);
142
154
}
155
+
143
156
return $ faker ->passthrough ($ collection );
157
+
144
158
case Type::BUILTIN_TYPE_OBJECT :
145
- if ($ type ->getClassName () === 'DateTime ' ) {
159
+ /** @var class-string $className */
160
+ $ className = $ type ->getClassName ();
161
+
162
+ if ($ className === 'DateTime ' ) {
146
163
return $ faker ->dateTime ();
147
164
}
148
- return $ faker ->passthrough ($ this ->generateObject ($ type ->getClassName ()));
165
+
166
+ return $ faker ->passthrough ($ this ->generateObject ($ className ));
149
167
}
150
168
}
151
169
@@ -154,11 +172,15 @@ private function isInRegistry(string $key): bool
154
172
return array_key_exists ($ key , $ this ->temporaryRegistry ) || array_key_exists ($ key , $ this ->registry );
155
173
}
156
174
175
+ /**
176
+ * @return mixed
177
+ */
157
178
private function getFromRegistry (string $ key )
158
179
{
159
180
if (isset ($ this ->temporaryRegistry [$ key ])) {
160
181
return $ this ->temporaryRegistry [$ key ]($ this , $ this ->fakerInstance );
161
182
}
183
+
162
184
return $ this ->registry [$ key ]($ this , $ this ->fakerInstance );
163
185
}
164
186
}
0 commit comments