Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F45817062
D4189.id10967.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
D4189.id10967.diff
View Options
diff --git a/omnitools/src/IO/Directory.php b/omnitools/src/IO/Directory.php
--- a/omnitools/src/IO/Directory.php
+++ b/omnitools/src/IO/Directory.php
@@ -2,6 +2,7 @@
namespace Keruald\OmniTools\IO;
+use Keruald\OmniTools\Collections\HashMap;
use Keruald\OmniTools\Collections\Vector;
class Directory {
@@ -52,11 +53,10 @@
return is_writable($this->path);
}
- /**
- * @return array<string, string>
- */
- public function getPathInfo () : array {
- return pathinfo($this->path);
+ public function getPathInfo () : HashMap {
+ $info = pathinfo($this->path);
+
+ return HashMap::from($info);
}
public function getParentDirectory () : string {
@@ -75,14 +75,21 @@
* Gets files in the directory matching a specific pattern,
* using the PHP glob function.
*
- * @return File[]
+ * @return Vector<File>
+ */
+ public function glob (string $pattern) : Vector {
+ $glob = glob("$this->path/$pattern");
+
+ return Vector::from($glob)
+ ->map(fn ($file) => new File($file));
+ }
+
+ /**
+ * @return Vector<File>
*/
- public function glob (string $pattern) : array {
- return array_map(
- function ($file) {
- return new File($file);
- }, glob("$this->path/$pattern")
- );
+ public function globFiles (string $pattern) : Vector {
+ return $this->glob($pattern)
+ ->filter(fn (File $file) => is_file($file->getPath()));
}
/**
@@ -93,21 +100,17 @@
* @return int
*/
public function countFiles (string $pattern = "*") : int {
- $matches = $this->glob($pattern);
- $filter = fn (File $file) => is_file($file->getPath());
-
- return count(array_filter($matches, $filter));
+ return $this->globFiles($pattern)->count();
}
/**
- * @return Directory[]
+ * @return Vector<Directory>
*/
- public function getSubdirectories () : array {
- return array_map(
- function ($dir) {
- return new Directory($dir);
- }, glob("$this->path/*", GLOB_ONLYDIR)
- );
+ public function getSubdirectories () : Vector {
+ $glob = glob("$this->path/*", GLOB_ONLYDIR);
+
+ return Vector::from($glob)
+ ->map(fn ($dir) => new Directory($dir));
}
///
diff --git a/omnitools/src/IO/File.php b/omnitools/src/IO/File.php
--- a/omnitools/src/IO/File.php
+++ b/omnitools/src/IO/File.php
@@ -3,6 +3,8 @@
namespace Keruald\OmniTools\IO;
+use Keruald\OmniTools\Collections\HashMap;
+
use RuntimeException;
class File {
@@ -53,8 +55,10 @@
return is_readable($this->path);
}
- public function getPathInfo () : array {
- return pathinfo($this->path);
+ public function getPathInfo () : HashMap {
+ $info = pathinfo($this->path);
+
+ return HashMap::from($info);
}
public function getDirectory () : string {
diff --git a/omnitools/src/Registration/PSR4/PSR4Namespace.php b/omnitools/src/Registration/PSR4/PSR4Namespace.php
--- a/omnitools/src/Registration/PSR4/PSR4Namespace.php
+++ b/omnitools/src/Registration/PSR4/PSR4Namespace.php
@@ -3,6 +3,7 @@
namespace Keruald\OmniTools\Registration\PSR4;
+use Keruald\OmniTools\Collections\Vector;
use Keruald\OmniTools\IO\Directory;
use Keruald\OmniTools\IO\File;
@@ -26,13 +27,13 @@
* @return string[]
*/
public function discover () : array {
- $files = (new Directory($this->baseDirectory))
- ->glob("*.php");
+ $map = fn(File $file) => $this->namespacePrefix
+ . "\\" . $file->getFileNameWithoutExtension();
- return array_map(function (File $file) {
- return $this->namespacePrefix
- . "\\" . $file->getFileNameWithoutExtension();
- }, $files);
+ return (new Directory($this->baseDirectory))
+ ->globFiles("*.php")
+ ->map($map)
+ ->toArray();
}
/**
diff --git a/omnitools/tests/IO/DirectoryTest.php b/omnitools/tests/IO/DirectoryTest.php
--- a/omnitools/tests/IO/DirectoryTest.php
+++ b/omnitools/tests/IO/DirectoryTest.php
@@ -2,6 +2,7 @@
namespace Keruald\OmniTools\Tests\IO;
+use Keruald\OmniTools\Collections\Vector;
use Keruald\OmniTools\IO\Directory;
use Keruald\OmniTools\IO\File;
@@ -42,10 +43,10 @@
}
public function testGlob () : void {
- $expected = [
+ $expected = Vector::from([
new File($this->path . '/Bar.php'),
new File($this->path . '/Foo.php'),
- ];
+ ]);
$actual = $this->directory->glob("*.php");
$this->assertEquals($expected, $actual);
@@ -126,7 +127,7 @@
'filename' => self::TEST_DIRECTORY,
];
- $this->assertEquals($expected, $this->directory->getPathInfo());
+ $this->assertEquals($expected, $this->directory->getPathInfo()->toArray());
}
public function testGetParentDirectory () : void {
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Aug 24, 15:56 (8 h, 52 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4033234
Default Alt Text
D4189.id10967.diff (4 KB)
Attached To
Mode
D4189: Use Vector and HashMap in IO classes
Attached
Detach File
Event Timeline
Log In to Comment