Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jan 24, 2020
1 parent a5251f1 commit 633898a
Showing 1 changed file with 55 additions and 21 deletions.
76 changes: 55 additions & 21 deletions src/Util/Configuration/Extension.php
Expand Up @@ -10,6 +10,8 @@
namespace PHPUnit\Util\Configuration;

use PHPUnit\Framework\Exception;
use PHPUnit\Framework\TestListener;
use PHPUnit\Runner\Hook;

final class Extension
{
Expand All @@ -35,27 +37,6 @@ public function __construct(string $className, string $sourceFile, array $argume
$this->arguments = $arguments;
}

public function createInstance(): object
{
$this->ensureClassExists();

try {
$reflector = new \ReflectionClass($this->className);
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}

if (!$this->hasArguments()) {
return $reflector->newInstance();
}

return $reflector->newInstanceArgs($this->arguments);
}

public function className(): string
{
return $this->className;
Expand All @@ -81,6 +62,59 @@ public function arguments(): array
return $this->arguments;
}

public function createHookInstance(): Hook
{
$object = $this->createInstance();

if (!$object instanceof Hook) {
throw new Exception(
\sprintf(
'Class "%s" does not implement a PHPUnit\Runner\Hook interface',
$this->className
)
);
}

return $object;
}

public function createTestListenerInstance(): TestListener
{
$object = $this->createInstance();

if (!$object instanceof TestListener) {
throw new Exception(
\sprintf(
'Class "%s" does not implement the PHPUnit\Framework\TestListener interface',
$this->className
)
);
}

return $object;
}

private function createInstance(): object
{
$this->ensureClassExists();

try {
$reflector = new \ReflectionClass($this->className);
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}

if (!$this->hasArguments()) {
return $reflector->newInstance();
}

return $reflector->newInstanceArgs($this->arguments);
}

/**
* @throws Exception
*/
Expand Down

0 comments on commit 633898a

Please sign in to comment.