Skip to content

Commit cc027e1

Browse files
committed
Let all ::fromClass() factories throw RuntimeException and require class-string.
1 parent 1a813f1 commit cc027e1

File tree

5 files changed

+14
-21
lines changed

5 files changed

+14
-21
lines changed

src/ClassFilesIA/ClassFilesIA.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,10 @@ public static function psr4Up(string $directory, string $namespace, int $nLevels
3636
}
3737

3838
/**
39-
* @param string $class
39+
* @param class-string $class
4040
* @param int $nLevelsUp
4141
*
4242
* @return \Ock\ClassFilesIterator\NamespaceDirectory
43-
*
44-
* @throws \ReflectionException
45-
* Class does not exist.
4643
*/
4744
public static function psr4FromClass(string $class, int $nLevelsUp = 0): NamespaceDirectory {
4845
$result = NamespaceDirectory::createFromClass($class);
@@ -79,9 +76,6 @@ public static function multiple(array $classFilesIAs): ClassFilesIAInterface {
7976
* @param class-string[] $classes
8077
*
8178
* @return \Ock\ClassFilesIterator\ClassFilesIA\ClassFilesIAInterface
82-
*
83-
* @throws \ReflectionException
84-
* One of the classes does not exist.
8579
*/
8680
public static function psr4FromClasses(array $classes): ClassFilesIAInterface {
8781
return self::multiple(array_map(

src/ClassFilesIA/ClassFilesIA_Psr4.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public static function create(string $directory, string $namespace): self {
3434
* @param int $nLevelsUp
3535
*
3636
* @return \Ock\ClassFilesIterator\ClassFilesIA\ClassFilesIAInterface
37-
* @throws \ReflectionException
3837
*/
3938
public static function createFromClass(string $class, int $nLevelsUp = 0): ClassFilesIAInterface {
4039
$nsDir = NamespaceDirectory::createFromClass($class)

src/NamespaceDirectory.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,22 @@ public static function fromClass(string $class): self {
5757
/**
5858
* Creates a namespace directory based on a class name.
5959
*
60-
* @param class-string|string $class
60+
* @param class-string $class
6161
* A class in the namespace directory.
6262
*
6363
* @return self
6464
* Namespace directory of this very class file.
65-
*
66-
* @throws \ReflectionException
67-
* Class does not exist.
6865
*/
6966
public static function createFromClass(string $class): self {
70-
// Accept that `new \ReflectionClass()` will throw an exception if the class
71-
// does not exist.
72-
// @phpstan-ignore argument.type
73-
$reflClass = new \ReflectionClass($class);
67+
try {
68+
$reflClass = new \ReflectionClass($class);
69+
}
70+
// The $class parameter is annotated as class-string, but we don't want to
71+
// rely on it.
72+
// @phpstan-ignore catch.neverThrown
73+
catch (\ReflectionException $e) {
74+
throw new \RuntimeException($e->getMessage(), 0, $e);
75+
}
7476
return self::fromReflectionClass($reflClass);
7577
}
7678

tests/src/ClassFilesIATest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function testCreateFromClass(): void {
6666
$f(Fig::class, -3),
6767
);
6868
// @phpstan-ignore argument.type
69-
$this->callAndAssertException(\ReflectionException::class, fn () => $f(__NAMESPACE__ . '\\NonExistingClass'));
69+
$this->callAndAssertException(\RuntimeException::class, fn () => $f(__NAMESPACE__ . '\\NonExistingClass'));
7070
$this->callAndAssertException(\RuntimeException::class, fn () => $f(Fig::class, 5));
7171
$this->callAndAssertException(\RuntimeException::class, fn () => $f(Fig::class, -2));
7272
}
@@ -101,9 +101,6 @@ public function testEmpty(): void {
101101
$this->assertFalse($classFilesIA->getIterator()->valid());
102102
}
103103

104-
/**
105-
* @throws \ReflectionException
106-
*/
107104
public function testConcat(): void {
108105
$concat = new ClassFilesIA_Concat([
109106
ClassFilesIA::psr4FromClass(Fig::class),

tests/src/NamespaceDirectoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public function testCreateFromClass(): void {
6262
);
6363
// Does not work with non-existing class.
6464
$this->callAndAssertException(
65-
\ReflectionException::class,
65+
\RuntimeException::class,
66+
// @phpstan-ignore argument.type
6667
fn () => NamespaceDirectory::createFromClass(__NAMESPACE__ . '\\NonExistingClass'),
6768
);
6869
// Does not work with built-in class.

0 commit comments

Comments
 (0)