Skip to content

Commit

Permalink
Merge pull request #5 from zumba/PHP8-237-remove-deprecated-message
Browse files Browse the repository at this point in the history
PHP8-237 - Changed deprecated ReflectionParameter method
  • Loading branch information
jrbasso authored Jul 20, 2021
2 parents 74787d6 + 2418a0a commit 1695d27
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 25 deletions.
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ parameters:
paths:
- src
- test
excludePaths:
- test/Stub/SimpleDependencyProvider/MultiTypeCommandHandler.php
31 changes: 23 additions & 8 deletions src/Provider/SimpleDependencyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Zumba\CQRS\Provider;

use ReflectionClass;
use ReflectionNamedType;
use Zumba\CQRS\Command\Command;
use Zumba\CQRS\Command\Handler as CommandHandler;
use Zumba\CQRS\DTO;
Expand Down Expand Up @@ -66,7 +68,7 @@ public function getQueryHandler(Query $query): QueryHandler
*/
protected static function extract(string $className): array
{
$image = new \ReflectionClass($className);
$image = new ReflectionClass($className);
$constructor = $image->getConstructor();
if ($constructor === null) {
return [];
Expand All @@ -77,11 +79,16 @@ protected static function extract(string $className): array
}
$dependencies = [];
foreach ($parameters as $parameter) {
$dependency = $parameter->getClass();
if ($dependency === null) {
$dependencyType = $parameter->getType();
if ($dependencyType === null) {
static::fail($parameter);
return [];
}
if (!$dependencyType instanceof ReflectionNamedType || !class_exists($dependencyType->getName())) {
static::fail($parameter);
return [];
}
$dependency = new ReflectionClass($dependencyType->getName());
if (!$dependency->isInstantiable()) {
static::fail($parameter);
return [];
Expand Down Expand Up @@ -117,22 +124,30 @@ protected static function areAllParamsOptional(\ReflectionMethod $method): bool
*/
protected static function fail(\ReflectionParameter $parameter): void
{
if (is_null($parameter->getClass())) {
$parameterType = $parameter->getType();
if (!$parameterType instanceof ReflectionNamedType) {
throw new InvalidDependency(sprintf(
"Don't be a night elf! `\$%s` has multiple types.",
$parameter->getName()
));
}
if (!class_exists($parameterType->getName())) {
throw new InvalidDependency(sprintf(
"Don't be a night elf! `%s` is not a valid class.",
"Don't be a night elf! `\$%s` typehint is not a valid class.",
$parameter->getName()
));
}
if (!$parameter->getClass()->isInstantiable()) {
$parameterClass = new ReflectionClass($parameterType->getName());
if (!$parameterClass->isInstantiable()) {
throw new InvalidDependency(sprintf(
"Don't be a night elf! `%s %s` cannot be instantiated.",
$parameter->getClass()->getName(),
$parameterClass->getName(),
"$" . $parameter->getName()
));
}
throw new InvalidDependency(sprintf(
"Don't be a night elf! `%s %s` has required params.",
$parameter->getClass()->getName(),
$parameterClass->getName(),
"$" . $parameter->getName()
));
}
Expand Down
32 changes: 15 additions & 17 deletions test/Provider/SimpleDependencyProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Zumba\CQRS\Provider\SimpleDependencyProvider;
use Zumba\CQRS\Test\Stub\SimpleDependencyProvider\EmptyConstructorCommand;
use Zumba\CQRS\Test\Stub\SimpleDependencyProvider\EmptyConstructorCommandHandler;
use Zumba\CQRS\Test\Stub\SimpleDependencyProvider\MultiTypeCommand;
use Zumba\CQRS\Test\Stub\SimpleDependencyProvider\NonOptionalCommand;
use Zumba\CQRS\Test\Stub\SimpleDependencyProvider\NotValidCommand;
use Zumba\CQRS\Test\Stub\SimpleDependencyProvider\OptionalParamConstructorCommand;
Expand All @@ -17,33 +18,27 @@

class SimpleDependencyProviderTest extends TestCase
{
/**
* @var int
*/
private $existingErrorLevel;

public function setUp(): void
public function testMultiTypeParam(): void
{
parent::setUp();
$this->existingErrorLevel = error_reporting();
// for now, ignore deprecated errors: to avoid error in most of the tests:
// Method ReflectionParameter::getClass() is deprecated
// todo: refactor to not use getClass anymore, then remove this work-around
error_reporting($this->existingErrorLevel & ~E_DEPRECATED);
}
if (version_compare(PHP_VERSION, '8.0.0', '<')) {
$this->markTestSkipped('Test only application to PHP 8.0+.');
}

public function tearDown(): void
{
parent::tearDown();
// restore original error reporting level
error_reporting($this->existingErrorLevel);
$provider = new SimpleDependencyProvider();
$dto = new MultiTypeCommand();
$this->expectException(InvalidDependency::class);
$this->expectExceptionMessage("Don't be a night elf! `\$multiType` has multiple types.");
$provider->getCommandHandler($dto);
}

public function testNonOptionalParam(): void
{
$provider = new SimpleDependencyProvider();
$dto = new NonOptionalCommand();
$this->expectException(InvalidDependency::class);
$testingClass = 'Zumba\CQRS\Test\Stub\SimpleDependencyProvider\NonOptionalParamConstructor';
$this->expectExceptionMessage("Don't be a night elf! `{$testingClass} \$notSimple` has required params.");
$provider->getCommandHandler($dto);
}

Expand All @@ -52,6 +47,8 @@ public function testNonInstantiable(): void
$provider = new SimpleDependencyProvider();
$dto = new PrivateConstructorCommand();
$this->expectException(InvalidDependency::class);
$testingClass = 'Zumba\CQRS\Test\Stub\SimpleDependencyProvider\PrivateConstructor';
$this->expectExceptionMessage("Don't be a night elf! `{$testingClass} \$notSimple` cannot be instantiated.");
$provider->getCommandHandler($dto);
}

Expand All @@ -60,6 +57,7 @@ public function testNonValidCommandHandler(): void
$provider = new SimpleDependencyProvider();
$dto = new NotValidCommand();
$this->expectException(InvalidDependency::class);
$this->expectExceptionMessage("Don't be a night elf! `\$notValid` typehint is not a valid class.");
$provider->getCommandHandler($dto);
}

Expand Down
11 changes: 11 additions & 0 deletions test/Stub/SimpleDependencyProvider/MultiTypeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Zumba\CQRS\Test\Stub\SimpleDependencyProvider;

use Zumba\CQRS\Command\Command;

class MultiTypeCommand extends Command
{
}
23 changes: 23 additions & 0 deletions test/Stub/SimpleDependencyProvider/MultiTypeCommandHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Zumba\CQRS\Test\Stub\SimpleDependencyProvider;

use Zumba\CQRS\Command\Command;
use Zumba\CQRS\Command\CommandResponse;
use Zumba\CQRS\CommandService;
use Zumba\CQRS\Command\Handler;

class MultiTypeCommandHandler implements Handler
{
/** @phpstan-ignore-next-line */
public function __construct(Command|CommandResponse $multiType)
{
}

public function handle(Command $command, CommandService $commandService): CommandResponse
{
return CommandResponse::fromSuccess();
}
}

0 comments on commit 1695d27

Please sign in to comment.