Skip to content

Commit

Permalink
Namespace prefixes fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkraczkowski committed May 14, 2018
1 parent e8f1ebc commit 051b88c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Exception/ReflectionApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ public static function forFinalAbstract(): self
{
return new self("Expression cannot be final and abstract at the same time.");
}

public static function forUnknownType(string $type): self
{
return new self("Passed type {$type} is not recognized. Did you forgot to include it in the autoload?");
}
}
13 changes: 12 additions & 1 deletion src/ReflectionApi/RuntimeMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Igni\Utils\ReflectionApi;

use Igni\Utils\Exception\ReflectionApiException;
use Igni\Utils\ReflectionApi;

final class RuntimeMethod implements CodeGenerator
{
Expand All @@ -16,6 +17,8 @@ final class RuntimeMethod implements CodeGenerator
private $body = [];
private $name;

private const DEFAULT_TYPES = ['string', 'int', 'object', 'bool', 'float', 'array', 'callable', 'void'];

public function __construct(string $name)
{
$this->name = $name;
Expand All @@ -28,6 +31,10 @@ public function getName(): string

public function setReturnType(string $type): self
{
if (!in_array($type, self::DEFAULT_TYPES) && !class_exists($type) && !interface_exists($type)) {
throw ReflectionApiException::forUnknownType($type);
}

$this->returnType = $type;

return $this;
Expand Down Expand Up @@ -97,7 +104,11 @@ public function generateCode(): string

$code .= ')';
if ($this->returnType) {
$code .= ": {$this->returnType}";
if (class_exists($this->returnType) || interface_exists($this->returnType)) {
$code .= ": \\{$this->returnType}";
} else {
$code .= ": {$this->returnType}";
}
}

if ($this->isAbstract()) {
Expand Down

0 comments on commit 051b88c

Please sign in to comment.