Skip to content

Commit f96dc69

Browse files
authored
feat!: make Path::dirname() null if no parent dir (#120)
1 parent 869a437 commit f96dc69

File tree

6 files changed

+43
-6
lines changed

6 files changed

+43
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ $node->path()->toString(); // string - the full path
8686
$node->path()->name(); // string - filename with extension
8787
$node->path()->basename(); // string - filename without extension
8888
$node->path()->extension(); // string|null - file extension
89-
$node->path()->dirname(); // string - the parent directory
89+
$node->path()->dirname(); // string|null - the parent directory (or null if there is none)
9090

9191
$node->dsn(); // Zenstruck\Filesystem\Node\Dsn
9292
$node->dsn()->toString(); // string - <filesystem-name>://<full-path>
9393
(string) $node->dsn(); // same as above
9494
$node->dsn()->path(); // Zenstruck\Filesystem\Node\Path
9595
$node->dsn()->filesystem(); // string - name of the filesystem this node belongs to
9696

97-
$node->directory(); // Zenstruck\Filesystem\Node\Directory|null - parent directory object
97+
$node->directory(); // Zenstruck\Filesystem\Node\Directory|null - parent directory object (or null if there is none)
9898

9999
$node->visibility(); // string - ie "public" or "private"
100100
$node->lastModified(); // \DateTimeImmutable (in currently configured timezone)

src/Filesystem/Node/FlysystemNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function directory(): ?Directory
5151
{
5252
$dirname = $this->path()->dirname();
5353

54-
return '.' === $dirname ? null : new FlysystemDirectory($dirname, $this->operator);
54+
return null === $dirname ? null : new FlysystemDirectory($dirname, $this->operator);
5555
}
5656

5757
public function lastModified(): \DateTimeImmutable

src/Filesystem/Node/Path.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,15 @@ public function basename(): string
6262
return $this->nameParts()[0];
6363
}
6464

65-
public function dirname(): string
65+
public function dirname(): ?string
6666
{
67-
return \dirname($this->value);
67+
if (!$this->value) {
68+
return null;
69+
}
70+
71+
$dirname = \dirname($this->value);
72+
73+
return '.' === $dirname ? '' : $dirname;
6874
}
6975

7076
/**

tests/Filesystem/Node/DirectoryTests.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public function metadata(): void
3939

4040
$dir = $this->createDirectory(fixture('sub1'), 'foo');
4141

42-
$this->assertNull($dir->directory());
42+
$this->assertSame('', $dir->directory()->path()->toString());
43+
$this->assertNull($dir->directory()->directory());
4344
}
4445

4546
/**

tests/Filesystem/Node/PathTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,22 @@ public function can_handle_multi_part_extensions(): void
3131
$this->assertSame('bar', $path->basename());
3232
$this->assertSame('foo', $path->dirname());
3333
}
34+
35+
/**
36+
* @test
37+
* @dataProvider dirnameProvider
38+
*/
39+
public function dirname(string $path, ?string $expected): void
40+
{
41+
$this->assertSame($expected, (new Path($path))->dirname());
42+
}
43+
44+
public static function dirnameProvider(): iterable
45+
{
46+
yield 'empty' => ['', null];
47+
yield 'one level' => ['foo', ''];
48+
yield 'one level trailing slash' => ['foo/', ''];
49+
yield 'two levels' => ['foo/bar', 'foo'];
50+
yield 'two levels trailing slash' => ['foo/bar/', 'foo'];
51+
}
3452
}

tests/FilesystemTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,18 @@ public function can_chmod(): void
263263
$this->assertSame('private', $fs->file('some/file.txt')->visibility());
264264
}
265265

266+
/**
267+
* @test
268+
*/
269+
public function root_parent_dir(): void
270+
{
271+
$fs = $this->createFilesystem();
272+
273+
$fs->write('foo/bar.txt', 'content');
274+
275+
$this->assertNull($fs->directory()->directory());
276+
}
277+
266278
/**
267279
* @test
268280
* @dataProvider writeValueProvider

0 commit comments

Comments
 (0)