diff --git a/src/Exception/Reflection/CannotResolveClassNameException.php b/src/Exception/Reflection/CannotResolveClassNameException.php index ff5191f..faaf4d0 100644 --- a/src/Exception/Reflection/CannotResolveClassNameException.php +++ b/src/Exception/Reflection/CannotResolveClassNameException.php @@ -21,13 +21,12 @@ class CannotResolveClassNameException extends Exception /** * Creates exception * - * @param array|object|string $source Source of the class's / trait's name. It can be an array of objects, - * namespaces, object or namespace. - * @param bool $forClass (optional) If is set to true, message of this exception for class is - * prepared. Otherwise - for trait. + * @param string $source Source of name of the class or trait + * @param bool $forClass (optional) If is set to true, message of this exception for class is prepared. Otherwise + * - for trait. * @return CannotResolveClassNameException */ - public static function create($source, bool $forClass = true): CannotResolveClassNameException + public static function create(string $source, bool $forClass = true): CannotResolveClassNameException { $forWho = 'trait'; $value = ''; @@ -37,7 +36,7 @@ class CannotResolveClassNameException extends Exception } if (is_scalar($source)) { - $value = sprintf(' %s', (string)$source); + $value = sprintf(' %s', $source); } $template = 'Name of %s from given \'%s\'%s cannot be resolved. Is there everything ok?'; diff --git a/src/Utilities/Reflection.php b/src/Utilities/Reflection.php index 82c522a..96ccce7 100644 --- a/src/Utilities/Reflection.php +++ b/src/Utilities/Reflection.php @@ -437,7 +437,7 @@ class Reflection // Oops, cannot resolve class if (null === $className) { - throw CannotResolveClassNameException::create($class); + throw CannotResolveClassNameException::create(''); } $childClasses = []; @@ -541,12 +541,12 @@ class Reflection // Oops, cannot resolve class if (null === $className || '' === $className) { - throw CannotResolveClassNameException::create($class); + throw CannotResolveClassNameException::create(''); } // Oops, cannot resolve trait if (null === $traitName || '' === $traitName) { - throw new CannotResolveClassNameException($class, false); + throw CannotResolveClassNameException::create('', false); } $reflection = new ReflectionClass($className); diff --git a/tests/Exception/Reflection/CannotResolveClassNameExceptionTest.php b/tests/Exception/Reflection/CannotResolveClassNameExceptionTest.php index 8106936..afce710 100644 --- a/tests/Exception/Reflection/CannotResolveClassNameExceptionTest.php +++ b/tests/Exception/Reflection/CannotResolveClassNameExceptionTest.php @@ -34,16 +34,23 @@ class CannotResolveClassNameExceptionTest extends BaseTestCase ); } + public function testCreateUsingDefaults(): void + { + $exception = CannotResolveClassNameException::create(stdClass::class); + $expectedMessage = 'Name of class from given \'string\' stdClass cannot be resolved. Is there everything ok?'; + + static::assertSame($expectedMessage, $exception->getMessage()); + } + /** - * @param array|object|string $source Source of the class's / trait's name. It can be an array of objects, - * namespaces, object or namespace. - * @param bool $forClass If is set to true, message of this exception for class is prepared. - * Otherwise - for trait. - * @param string $expectedMessage Expected exception's message + * @param string $source Source of name of the class or trait + * @param bool $forClass (optional) If is set to true, message of this exception for class is prepared. + * Otherwise - for trait. + * @param string $expectedMessage Expected exception's message * * @dataProvider provideClassName */ - public function testCreate($source, bool $forClass, string $expectedMessage): void + public function testCreate(string $source, bool $forClass, string $expectedMessage): void { $exception = CannotResolveClassNameException::create($source, $forClass); static::assertSame($expectedMessage, $exception->getMessage()); @@ -70,12 +77,9 @@ class CannotResolveClassNameExceptionTest extends BaseTestCase ]; yield[ - [ - new stdClass(), - new stdClass(), - ], + stdClass::class, true, - 'Name of class from given \'array\' cannot be resolved. Is there everything ok?', + 'Name of class from given \'string\' stdClass cannot be resolved. Is there everything ok?', ]; } } diff --git a/tests/Utilities/ReflectionTest.php b/tests/Utilities/ReflectionTest.php index 7a0aaef..edf5344 100644 --- a/tests/Utilities/ReflectionTest.php +++ b/tests/Utilities/ReflectionTest.php @@ -125,7 +125,7 @@ class ReflectionTest extends BaseTestCase * @param mixed $invalidClass Empty value, e.g. "" * @dataProvider provideEmptyValue */ - public function testGetChildClassesInvalidClass($invalidClass) + public function testGetChildClassesInvalidClass($invalidClass): void { $this->expectException(CannotResolveClassNameException::class); @@ -206,7 +206,7 @@ class ReflectionTest extends BaseTestCase * * @dataProvider provideInvalidClassAndTrait */ - public function testUsesTraitInvalidClass($class, $trait) + public function testUsesTraitInvalidClass($class, $trait): void { $this->expectException(CannotResolveClassNameException::class); self::assertNull(Reflection::usesTrait($class, $trait)); @@ -577,7 +577,7 @@ class ReflectionTest extends BaseTestCase * * @return Generator */ - public function provideInvalidClassAndTrait() + public function provideInvalidClassAndTrait(): ?Generator { yield[ '', @@ -593,11 +593,6 @@ class ReflectionTest extends BaseTestCase 0, 0, ]; - - yield[ - [], - [], - ]; } /**