Skip to content

Commit 4d2d91f

Browse files
committed
feat: reflection creation via array spec
To be able to use the ::class pseudy-constant in test cases when specifiying targets, this commit will allow array specification: ['!' => FQCN::class]
1 parent 9b6bb57 commit 4d2d91f

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/Utils/Instance.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* Creates object instances.
1818
*
1919
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
20+
*
21+
* @since 2.x create reflection via array spec
2022
*/
2123
final class Instance
2224
{
@@ -50,6 +52,9 @@ public static function reflection($fqcnOrObject): \ReflectionClass
5052
* if __$fqcn__ is a string and starts with "!", a \ReflectionClass
5153
* object is returned.
5254
*
55+
* if __$fqcn__ is an array with the key "!", a \ReflectionClass
56+
* object is created from the value of that key (which must be an FQCN or an object)
57+
*
5358
* if __$fqcn__ is an array, the first element is used as FQCN and
5459
* all other elements are used as constructor arguments - other
5560
* arguments passed in are ignored.
@@ -58,12 +63,18 @@ public static function reflection($fqcnOrObject): \ReflectionClass
5863
* @param mixed ...$arguments
5964
*
6065
* @return object
66+
*
67+
* @since 2.x Create reflection via array spec (['!' => FQCN/Object])
6168
*/
6269
public static function create($fqcn, ...$arguments): object
6370
{
6471
if (is_array($fqcn)) {
65-
$arguments = array_slice($fqcn, 1);
66-
$fqcn = reset($fqcn);
72+
if (isset($fqcn['!']) && is_string($fqcn['!'])) {
73+
return self::reflection($fqcn['!']);
74+
} else {
75+
$arguments = array_slice($fqcn, 1);
76+
$fqcn = reset($fqcn);
77+
}
6778
}
6879

6980
if (!is_string($fqcn)) {

test/TestUtilsTest/Utils/InstanceTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ public function __construct(...$args)
6868
[$fqcn, [], $fqcn],
6969
[$fqcn, ['arg1'], $fqcn, 'arg1'],
7070
[$fqcn, ['arg1'], [$fqcn, 'arg1'], 'arg2'],
71-
[\ReflectionClass::class, false, "!$fqcn"]
71+
[\ReflectionClass::class, false, "!$fqcn"],
72+
[\ReflectionClass::class, false, ['!' => $fqcn]],
7273
];
7374
}
7475

0 commit comments

Comments
 (0)