Skip to content

Commit

Permalink
Improve and fix the generic types for the ResultMockFactory (#1459)
Browse files Browse the repository at this point in the history
* Improve and fix the generic types for the ResultMockFactory

* Add inline var comments to overcome bad Psalm type inference
  • Loading branch information
stof authored Jun 23, 2023
1 parent 1d4506a commit e3e24bf
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Core/src/Test/ResultMockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class ResultMockFactory
* ResultMockFactory::createFailing(SendEmailResponse::class, 400, 'invalid value');
* </code>
*
* @template T
* @template T of Result
*
* @psalm-param class-string<T> $class
*
* @return Result|T
* @return T
*/
public static function createFailing(
string $class,
Expand All @@ -49,6 +49,7 @@ public static function createFailing(
$client = new MockHttpClient($httpResponse);
$response = new Response($client->request('POST', 'http://localhost'), $client, new NullLogger());

/** @psalm-var \ReflectionClass<T> $reflectionClass */
$reflectionClass = new \ReflectionClass($class);

return $reflectionClass->newInstance($response);
Expand All @@ -61,11 +62,11 @@ public static function createFailing(
* ResultMockFactory::create(SendEmailResponse::class, ['MessageId'=>'foo123']);
* </code>
*
* @template T
* @template T of Result
*
* @psalm-param class-string<T> $class
*
* @return Result|T
* @return T
*/
public static function create(string $class, array $data = [])
{
Expand All @@ -83,6 +84,7 @@ public static function create(string $class, array $data = [])
$initializedProperty = $reflectionClass->getProperty('initialized');
$initializedProperty->setAccessible(true);

/** @psalm-var \ReflectionClass<T> $reflectionClass */
$reflectionClass = new \ReflectionClass($class);
$object = $reflectionClass->newInstance($response);
if (Result::class !== $class) {
Expand Down Expand Up @@ -124,11 +126,11 @@ public static function create(string $class, array $data = [])
/**
* Instantiate a Waiter class with a final state.
*
* @template T
* @template T of Waiter
*
* @psalm-param class-string<T> $class
*
* @return Result|T
* @return T
*/
public static function waiter(string $class, string $finalState)
{
Expand All @@ -152,6 +154,7 @@ public static function waiter(string $class, string $finalState)
$propertyState = $reflectionClass->getProperty('finalState');
$propertyState->setAccessible(true);

/** @psalm-var \ReflectionClass<T> $reflectionClass */
$reflectionClass = new \ReflectionClass($class);
$result = $reflectionClass->newInstanceWithoutConstructor();
$propertyResponse->setValue($result, $response);
Expand Down

0 comments on commit e3e24bf

Please sign in to comment.