Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Dec 22, 2023
1 parent 78893c9 commit 0326dfc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 74 deletions.
79 changes: 5 additions & 74 deletions src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,121 +139,52 @@ private function reflectorForClass(string $className): ReflectionClass
// @codeCoverageIgnoreEnd
}

/**
* @throws ReflectionException
*/
private function isUserDefinedFunction(string $functionName): bool
{
if (!function_exists($functionName)) {
return false;
}

try {
return (new ReflectionFunction($functionName))->isUserDefined();
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd
return (new ReflectionFunction($functionName))->isUserDefined();
}

/**
* @throws ReflectionException
*/
private function isUserDefinedClass(string $className): bool
{
if (!class_exists($className)) {
return false;
}

try {
return (new ReflectionClass($className))->isUserDefined();
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd
return (new ReflectionClass($className))->isUserDefined();
}

/**
* @throws ReflectionException
*/
private function isUserDefinedInterface(string $interfaceName): bool
{
if (!interface_exists($interfaceName)) {
return false;
}

try {
return (new ReflectionClass($interfaceName))->isUserDefined();
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd
return (new ReflectionClass($interfaceName))->isUserDefined();
}

/**
* @throws ReflectionException
*/
private function isUserDefinedTrait(string $traitName): bool
{
if (!trait_exists($traitName)) {
return false;
}

try {
return (new ReflectionClass($traitName))->isUserDefined();
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd
return (new ReflectionClass($traitName))->isUserDefined();
}

/**
* @throws ReflectionException
*/
private function isUserDefinedMethod(string $className, string $methodName): bool
{
if (!class_exists($className)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
}

if (!method_exists($className, $methodName)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
}

try {
return (new ReflectionMethod($className, $methodName))->isUserDefined();
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd
return (new ReflectionMethod($className, $methodName))->isUserDefined();
}
}
8 changes: 8 additions & 0 deletions tests/unit/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public function testCanMapStringWithClassNameAndMethodNameToCodeUnitObjects(): v
$this->assertSame(FixtureClass::class . '::publicMethod', $units->asArray()[0]->name());
}

#[TestDox('Cannot map "ClassName::methodName" string to code unit objects when method does not exist')]
public function testCannotMapStringWithClassNameAndMethodNameToCodeUnitObjectsWhenMethodDoesNotExist(): void
{
$this->expectException(InvalidCodeUnitException::class);

(new Mapper)->stringToCodeUnits(FixtureClass::class . '::doesNotExist');
}

#[TestDox('Can map "InterfaceName" string to code unit objects')]
public function testCanMapStringWithInterfaceNameToCodeUnitObjects(): void
{
Expand Down

0 comments on commit 0326dfc

Please sign in to comment.