Page MenuHomeDevCentral

D3235.id8311.diff
No OneTemporary

D3235.id8311.diff

diff --git a/composer.json b/composer.json
--- a/composer.json
+++ b/composer.json
@@ -42,7 +42,7 @@
"keruald/cache": "0.1.0",
"keruald/commands": "0.0.1",
"keruald/database": "0.4.0",
- "keruald/omnitools": "0.11.0",
+ "keruald/omnitools": "0.11.1",
"keruald/report": "0.1.0"
},
"autoload": {
diff --git a/omnitools/src/Reflection/CodeClass.php b/omnitools/src/Reflection/CodeClass.php
--- a/omnitools/src/Reflection/CodeClass.php
+++ b/omnitools/src/Reflection/CodeClass.php
@@ -42,6 +42,27 @@
return $class->getShortName();
}
+ public static function extractClassName (string $fullClassName) : string {
+ // Trivial cases - no classes
+ if ($fullClassName === "" || $fullClassName[-1] === '\\') {
+ return "";
+ }
+
+ // When class exists, we can use ReflectionClass
+ try {
+ $class = new self($fullClassName);
+ return $class->getShortClassName();
+ } catch (ReflectionException) {
+ }
+
+ // Class doesn't exist in code, we can compute it
+ $pos = strrpos($fullClassName, "\\");
+ return match ($pos) {
+ false => $fullClassName,
+ default => substr($fullClassName, $pos + 1),
+ };
+ }
+
///
/// Represented class constructor helper methods
///
diff --git a/omnitools/tests/Reflection/CodeClassTest.php b/omnitools/tests/Reflection/CodeClassTest.php
--- a/omnitools/tests/Reflection/CodeClassTest.php
+++ b/omnitools/tests/Reflection/CodeClassTest.php
@@ -11,6 +11,7 @@
use Keruald\OmniTools\Network\IPv4Range;
use Keruald\OmniTools\Reflection\CodeClass;
+use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use InvalidArgumentException;
@@ -45,6 +46,27 @@
);
}
+ public static function provideFullyQualifiedClassNames () : iterable {
+ // Example from PSR-4 canonical document
+ // Fully qualified class name, class
+ yield [CodeClass::class, 'CodeClass'];
+ yield ['\Acme\Log\Writer\File_Writer', 'File_Writer'];
+ yield ['\Aura\Web\Response\Status', 'Status'];
+ yield ['\Symfony\Core\Request', 'Request'];
+
+ yield ['Foo', 'Foo'];
+ yield ['', ''];
+ yield ["Foo\\", ''];
+
+ }
+
+ #[DataProvider("provideFullyQualifiedClassNames")]
+ public function testExtractClassName ($class, $expected) {
+ $actual = CodeClass::extractClassName($class);
+
+ $this->assertEquals($expected, $actual);
+ }
+
public function testNewInstanceFromServices () {
$services = [
// Some objects (in different order than the constructor)

File Metadata

Mime Type
text/plain
Expires
Fri, Jan 31, 05:47 (8 h, 49 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2387497
Default Alt Text
D3235.id8311.diff (2 KB)

Event Timeline