diff --git a/src/Symfony/Component/Finder/SplFileInfo.php b/src/Symfony/Component/Finder/SplFileInfo.php index b8143ed799162..ee798dc703f00 100644 --- a/src/Symfony/Component/Finder/SplFileInfo.php +++ b/src/Symfony/Component/Finder/SplFileInfo.php @@ -57,6 +57,13 @@ public function getRelativePathname() return $this->relativePathname; } + public function getFilenameWithoutExtension(): string + { + $filename = $this->getFilename(); + + return \pathinfo($filename, PATHINFO_FILENAME); + } + /** * Returns the contents of the file. * diff --git a/src/Symfony/Component/Finder/Tests/FinderTest.php b/src/Symfony/Component/Finder/Tests/FinderTest.php index 9899c61fa6a5f..6b9a4b00c6d68 100644 --- a/src/Symfony/Component/Finder/Tests/FinderTest.php +++ b/src/Symfony/Component/Finder/Tests/FinderTest.php @@ -1007,6 +1007,40 @@ public function testRelativePathname() $this->assertEquals($ref, $paths); } + public function testGetFilenameWithoutExtension() + { + $finder = $this->buildFinder()->in(self::$tmpDir)->sortByName(); + + $fileNames = []; + + foreach ($finder as $file) { + $fileNames[] = $file->getFilenameWithoutExtension(); + } + + $ref = [ + 'test', + 'toto', + 'test', + 'foo', + 'bar', + 'foo bar', + 'qux', + 'baz_100_1', + 'baz_1_2', + 'qux_0_1', + 'qux_1000_1', + 'qux_1002_0', + 'qux_10_2', + 'qux_12_0', + 'qux_2_0', + ]; + + sort($fileNames); + sort($ref); + + $this->assertEquals($ref, $fileNames); + } + public function testAppendWithAFinder() { $finder = $this->buildFinder();