Skip to content

Commit 7987f5d

Browse files
committed
Drop ->withRealpathRoot().
1 parent 3418ad2 commit 7987f5d

9 files changed

+28
-140
lines changed

README.md

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,38 @@ $basic_namespace_dir = NamespaceDirectory::create($basedir . '/src', 'Acme\\Foo'
4242
// The directory is determined automatically with ReflectionClass::getFileName().
4343
$namespace_dir = NamespaceDirectory::fromKnownClass(VenusFlyTrap::class);
4444

45-
// Iterate class files.
46-
function foo(ClassFilesIAInterface $classFilesIA): void {
47-
foreach ($classFilesIA as $file => $class) {
48-
$rc = new \ReflectionClass($class);
49-
// Call realpath() to be sure.
50-
assert($rc->getFileName() === realpath($file));
51-
}
52-
// Or do this to skip the realpath() call:
53-
foreach ($classFilesIA->withRealpathRoot() as $file => $class) {
54-
$rc = new \ReflectionClass($class);
55-
assert($rc->getFileName() === $file);
45+
// Iterate class names by integer key.
46+
function foo(ClassNamesIAInterface $classNamesIA): void {
47+
foreach ($classNamesIA as $class) {
48+
assert(class_exists($class));
5649
}
5750
}
5851

59-
// Iterate class names.
60-
function foo(ClassNamesIAInterface $classNamesIA): void {
61-
foreach ($classNamesIA as $class) {
52+
// Iterate class names with file path as key.
53+
function foo(ClassFilesIAInterface $classNamesIA): void {
54+
foreach ($classNamesIA as $file => $class) {
55+
assert(file_exists($file));
6256
assert(class_exists($class));
6357
}
6458
}
59+
60+
// Get reflection classes.
61+
function foo(ClassFilesIAInterface $classFilesIA): void {
62+
foreach ($classFilesIA as $file => $class) {
63+
try {
64+
$rc = new \ReflectionClass($class);
65+
}
66+
catch (\ReflectionException|\Error) {
67+
// Skip non-existing classes / interfaces / traits.
68+
// Skip if a base class or interface is missing.
69+
// Unfortunately, missing traits still cause fatal error.
70+
continue;
71+
}
72+
// Ignore if the class is defined elsewhere.
73+
if ($rc->getFileName() !== $file && realpath($rc->getFileName()) !== realpath($file)) {
74+
continue;
75+
}
76+
do_something($rc);
77+
}
78+
}
6579
```

src/ClassFilesIA/ClassFilesIAInterface.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,4 @@ interface ClassFilesIAInterface extends ClassNamesIAInterface {
1717
*/
1818
public function getIterator(): \Iterator;
1919

20-
/**
21-
* Gets a version where all base paths are sent through ->realpath().
22-
*
23-
* This is useful when comparing the path to \ReflectionClass::getFileName().
24-
*
25-
* Implementations might use an optimization where they only send the base
26-
* path through realpath(), assuming that the subdirectories do not contain
27-
* symlinks.
28-
*
29-
* @return static
30-
*/
31-
public function withRealpathRoot(): static;
32-
3320
}

src/ClassFilesIA/ClassFilesIA_Concat.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,4 @@ public function getIterator(): \Iterator {
2020
}
2121
}
2222

23-
/**
24-
* {@inheritdoc}
25-
*/
26-
public function withRealpathRoot(): static {
27-
$clone = clone $this;
28-
foreach ($clone->classFilesIAs as $i => $classFilesIA) {
29-
$clone->classFilesIAs[$i] = $classFilesIA->withRealpathRoot();
30-
}
31-
return $clone;
32-
}
3323
}

src/ClassFilesIA/ClassFilesIA_Empty.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
class ClassFilesIA_Empty implements ClassFilesIAInterface {
66

7-
use RealpathRootThisTrait;
8-
97
/**
108
* {@inheritdoc}
119
*/

src/ClassFilesIA/ClassFilesIA_NamespaceDirectoryPsr4.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,6 @@ public function __construct(
5757
private readonly string $terminatedNamespace,
5858
) {}
5959

60-
/**
61-
* {@inheritdoc}
62-
*/
63-
public function withRealpathRoot(): static {
64-
$clone = clone $this;
65-
$realpath = realpath($this->directory);
66-
// @todo Properly handle this case.
67-
assert($realpath !== false, "Cannot get realpath for '$this->directory'. Perhaps the directory does not exist.");
68-
$clone->directory = $realpath;
69-
return $clone;
70-
}
71-
7260
/**
7361
* {@inheritdoc}
7462
*/

src/ClassFilesIA/RealpathRootThisTrait.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/NamespaceDirectory.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,6 @@ private function __construct(
9797
private readonly string $terminatedNamespace,
9898
) {}
9999

100-
/**
101-
* Gets a version where all base paths are sent through ->realpath().
102-
*
103-
* @return static
104-
*/
105-
public function withRealpathRoot(): static {
106-
$clone = clone $this;
107-
$realpath = realpath($this->directory);
108-
if ($realpath === false) {
109-
throw new \RuntimeException("Failed to realpath('$this->directory')");
110-
}
111-
$clone->directory = $realpath;
112-
return $clone;
113-
}
114-
115100
/**
116101
* @param string $namespace
117102
*

tests/src/ClassFilesIATest.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,6 @@ public function testCreateFromNsdirObject(): void {
7878
$this->assertEquals(new ClassFilesIA_Empty(), $f(__DIR__ . '/non/existing/subdir'));
7979
}
8080

81-
public function testWithRealpathRoot(): void {
82-
$c = fn (string $dir) => new ClassFilesIA_NamespaceDirectoryPsr4($dir, 'Acme\Zoo\\');
83-
$realpath = realpath(__DIR__) ?: $this->fail();
84-
$crooked_path = __DIR__ . '/../src';
85-
$this->assertFalse(str_ends_with($realpath, '/../src'));
86-
$obj_with_realpath = $c($realpath);
87-
$this->assertEquals($obj_with_realpath, $c($crooked_path)->withRealpathRoot());
88-
89-
// A new clone is returned even though values are the same.
90-
// Perhaps this will change in the future.
91-
$this->assertEquals($obj_with_realpath, $obj_with_realpath->withRealpathRoot());
92-
$this->assertNotSame($obj_with_realpath, $obj_with_realpath->withRealpathRoot());
93-
94-
// Test effect on the iterator.
95-
$this->assertSame($realpath, dirname($obj_with_realpath->getIterator()->key()));
96-
$this->assertSame($crooked_path, dirname($c($crooked_path)->getIterator()->key()));
97-
}
98-
9981
/**
10082
* @throws \ReflectionException
10183
*/
@@ -117,7 +99,6 @@ public function testGetIterator(): void {
11799
public function testEmpty(): void {
118100
$classFilesIA = new ClassFilesIA_Empty();
119101
$this->assertFalse($classFilesIA->getIterator()->valid());
120-
$this->assertSame($classFilesIA, $classFilesIA->withRealpathRoot());
121102
}
122103

123104
/**
@@ -140,17 +121,6 @@ public function testConcat(): void {
140121
[__DIR__ . '/Fixtures/Acme/Animal/RedSquirrel.php', RedSquirrel::class],
141122
[__DIR__ . '/Fixtures/Acme/Plant/Tree/Fig.php', Fig::class],
142123
], $actual);
143-
144-
$this->assertEquals(
145-
new ClassFilesIA_Concat([
146-
ClassFilesIA::psr4(realpath(__DIR__) ?: '?', 'Acme\Zoo'),
147-
ClassFilesIA::psr4(realpath(__DIR__ . '/Fixtures') ?: '?', 'Acme\Zoo'),
148-
]),
149-
(new ClassFilesIA_Concat([
150-
ClassFilesIA::psr4(__DIR__ . '/../src', 'Acme\Zoo'),
151-
ClassFilesIA::psr4(__DIR__ . '/../src/Fixtures', 'Acme\Zoo'),
152-
]))->withRealpathRoot(),
153-
);
154124
}
155125

156126
public function testFactoryPsr4(): void {

tests/src/NamespaceDirectoryTest.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -85,31 +85,6 @@ public function testFromReflectionClass(): void {
8585
);
8686
}
8787

88-
public function testWithRealpathRoot(): void {
89-
$c = fn (string $dir) => NamespaceDirectory::create($dir, 'Acme\Zoo');
90-
$realpath = realpath(__DIR__) ?: $this->fail();
91-
$crooked_path = __DIR__ . '/../src';
92-
$this->assertFalse(str_ends_with($realpath, '/../src'));
93-
$obj_with_realpath = $c($realpath);
94-
$this->assertEquals($obj_with_realpath, $c($crooked_path)->withRealpathRoot());
95-
96-
// A new clone is returned even though values are the same.
97-
// Perhaps this will change in the future.
98-
$this->assertEquals($obj_with_realpath, $obj_with_realpath->withRealpathRoot());
99-
$this->assertNotSame($obj_with_realpath, $obj_with_realpath->withRealpathRoot());
100-
101-
// Test effect on the iterator.
102-
$this->assertSame($realpath, dirname($obj_with_realpath->getIterator()->key()));
103-
$this->assertSame($crooked_path, dirname($c($crooked_path)->getIterator()->key()));
104-
105-
$this->callAndAssertException(
106-
\RuntimeException::class,
107-
$this
108-
->nsdir(__DIR__ . '/non/existing/path', 'Acme\Zoo')
109-
->withRealpathRoot(...),
110-
);
111-
}
112-
11388
public function testFindNamespace(): void {
11489
$nsdir = $this->nsdir('path/to/package/src/Animal/Mammal', 'Acme\Zoo\Animal\Mammal');
11590
$this->assertSame($nsdir, $nsdir->findNamespace('Acme\Zoo\Animal\Mammal'));

0 commit comments

Comments
 (0)