public function testReflectionOnTraitName() { $qualified = self::TEST_FIXTURE_TRAIT; $lastSlashPosition = StringInfo::searchPositionFromRight($qualified, '\\'); $name = substr($qualified, $lastSlashPosition + 1); $namespace = substr($qualified, 0, $lastSlashPosition); $namespaceSections = explode('\\', $namespace); $r = Inspect::useTrait($qualified); $this->assertTrue($r instanceof TraitInspector); $this->assertSame($qualified, $r->nameQualified()); $this->assertSame($name, $r->nameUnQualified()); $this->assertSame($name, $r->name()); $this->assertSame($name, $r->name(false)); $this->assertSame($qualified, $r->name(true)); $this->assertSame($namespace, $r->namespaceName()); $this->assertSame($namespaceSections, $r->namespaceSections()); $r = Inspect::using($qualified); $this->assertTrue($r instanceof TraitInspector); $this->assertSame($qualified, $r->nameQualified()); $this->assertSame($name, $r->nameUnQualified()); $this->assertSame($name, $r->name()); $this->assertSame($name, $r->name(false)); $this->assertSame($qualified, $r->name(true)); $this->assertSame($namespace, $r->namespaceName()); $this->assertSame($namespaceSections, $r->namespaceSections()); $trait = FixtureTraitTwo::class; $r = Inspect::useTrait($trait); $this->assertTrue($r instanceof TraitInspector); $this->assertSame($trait, $r->nameQualified()); $this->expectException(InvalidArgumentException::class); $r = Inspect::useTrait($trait . '\\InvalidTraitName'); }
/** * Validate the requested object instance or class name exists. * * @param string|object $object The object instance or class name * @param bool $static Whether this is a static call or not * * @internal * * @throws BadFunctionCallException * * @return string|object */ private static function validateClass($object, $static) { try { $class = Inspect::using($object)->nameQualified(); } catch (\Exception $e) { throw BadFunctionCallException::create()->setMessage('Could not validate call class (got "%s" with message "%s").', var_export($object, true), $e->getMessage()); } return $static ? $class : $object; }