Skip to content

Commit 051b88c

Browse files
committed
Namespace prefixes fix.
1 parent e8f1ebc commit 051b88c

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Exception/ReflectionApiException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@ public static function forFinalAbstract(): self
4040
{
4141
return new self("Expression cannot be final and abstract at the same time.");
4242
}
43+
44+
public static function forUnknownType(string $type): self
45+
{
46+
return new self("Passed type {$type} is not recognized. Did you forgot to include it in the autoload?");
47+
}
4348
}

src/ReflectionApi/RuntimeMethod.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Igni\Utils\ReflectionApi;
44

55
use Igni\Utils\Exception\ReflectionApiException;
6+
use Igni\Utils\ReflectionApi;
67

78
final class RuntimeMethod implements CodeGenerator
89
{
@@ -16,6 +17,8 @@ final class RuntimeMethod implements CodeGenerator
1617
private $body = [];
1718
private $name;
1819

20+
private const DEFAULT_TYPES = ['string', 'int', 'object', 'bool', 'float', 'array', 'callable', 'void'];
21+
1922
public function __construct(string $name)
2023
{
2124
$this->name = $name;
@@ -28,6 +31,10 @@ public function getName(): string
2831

2932
public function setReturnType(string $type): self
3033
{
34+
if (!in_array($type, self::DEFAULT_TYPES) && !class_exists($type) && !interface_exists($type)) {
35+
throw ReflectionApiException::forUnknownType($type);
36+
}
37+
3138
$this->returnType = $type;
3239

3340
return $this;
@@ -97,7 +104,11 @@ public function generateCode(): string
97104

98105
$code .= ')';
99106
if ($this->returnType) {
100-
$code .= ": {$this->returnType}";
107+
if (class_exists($this->returnType) || interface_exists($this->returnType)) {
108+
$code .= ": \\{$this->returnType}";
109+
} else {
110+
$code .= ": {$this->returnType}";
111+
}
101112
}
102113

103114
if ($this->isAbstract()) {

0 commit comments

Comments
 (0)