Skip to content

Commit

Permalink
Using native Reflection.
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaposa committed Jun 11, 2017
1 parent 26acc3f commit 9fa71c2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
}
],
"require": {
"php": "^7.0",
"roave/better-reflection": "^1.2"
"php": "^7.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7 || ^6.0",
Expand Down
24 changes: 11 additions & 13 deletions src/Cascader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

namespace Cascader;

use BetterReflection\Reflection\ReflectionClass;
use BetterReflection\Reflection\ReflectionParameter;
use BetterReflection\Reflector\Exception\IdentifierNotFound;
use Cascader\Exception\InvalidClassException;
use Cascader\Exception\InvalidOptionsException;
use Cascader\Exception\OptionNotSetException;
use phpDocumentor\Reflection\Types\Object_;
use ReflectionClass;
use ReflectionParameter;

class Cascader
{
Expand All @@ -33,11 +31,11 @@ public function create(string $className, array $options)
return new $className(...$arguments);
}

protected function getReflectionClass(string $className)
protected function getReflectionClass(string $className) : ReflectionClass
{
try {
$reflectionClass = ReflectionClass::createFromName($className);
} catch (IdentifierNotFound $ex) {
$reflectionClass = new ReflectionClass($className);
} catch (\ReflectionException $ex) {
throw InvalidClassException::forNonExistingClass($className);
}

Expand All @@ -50,13 +48,15 @@ protected function getReflectionClass(string $className)

protected function marshalArguments(ReflectionClass $reflectionClass, Options $options) : array
{
if (! $reflectionClass->hasMethod('__construct')) {
$constructor = $reflectionClass->getConstructor();

if (null === $constructor) {
return [];
}

$arguments = [];

$constructorParameters = $reflectionClass->getConstructor()->getParameters();
$constructorParameters = $constructor->getParameters();

foreach ($constructorParameters as $parameter) {
$arguments[] = $this->resolveArgument($parameter, $options);
Expand All @@ -71,10 +71,8 @@ protected function resolveArgument(ReflectionParameter $parameter, Options $opti
$argument = $options->get($parameter->getName());

if (null !== ($parameterType = $parameter->getType())) {
$parameterTypeObject = $parameterType->getTypeObject();

if (is_array($argument) && $parameterTypeObject instanceof Object_) {
$argument = $this->create((string) $parameterTypeObject->getFqsen(), $argument);
if (is_array($argument) && ! $parameterType->isBuiltin()) {
$argument = $this->create((string) $parameterType, $argument);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Exception/InvalidOptionsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Cascader\Exception;

use BetterReflection\Reflection\ReflectionParameter;
use InvalidArgumentException;
use ReflectionParameter;

class InvalidOptionsException extends InvalidArgumentException implements ExceptionInterface
{
Expand Down

0 comments on commit 9fa71c2

Please sign in to comment.