trait_exists example

namespace Symfony\Bridge\Monolog\Handler;

use Monolog\Formatter\FormatterInterface;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Handler\FormattableHandlerTrait;
use Monolog\Level;
use Monolog\Logger;
use Monolog\LogRecord;
use Symfony\Bridge\Monolog\Formatter\VarDumperFormatter;

if (trait_exists(FormattableHandlerTrait::class)) {
    /** * @final since Symfony 6.1 */
    class ServerLogHandler extends AbstractProcessingHandler
    {
        use CompatibilityHandler;
        use CompatibilityProcessingHandler;
        use ServerLogHandlerTrait;

        protected function getDefaultFormatter(): FormatterInterface
        {
            
$file = '';
            }
        } finally {
            error_reporting($e);
        }

        $this->checkClass($class$file);
    }

    private function checkClass(string $class, string $file = null): void
    {
        $exists = null === $file || class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false);

        if (null !== $file && $class && '\\' === $class[0]) {
            $class = substr($class, 1);
        }

        if ($exists) {
            if (isset(self::$checkedClasses[$class])) {
                return;
            }
            self::$checkedClasses[$class] = true;

            
$file = '';
            }
        } finally {
            error_reporting($e);
        }

        $this->checkClass($class$file);
    }

    private function checkClass(string $class, string $file = null): void
    {
        $exists = null === $file || class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false);

        if (null !== $file && $class && '\\' === $class[0]) {
            $class = substr($class, 1);
        }

        if ($exists) {
            if (isset(self::$checkedClasses[$class])) {
                return;
            }
            self::$checkedClasses[$class] = true;

            

    final public static function willBeAvailable(string $package, string $class, array $parentPackages): bool
    {
        if (!class_exists(InstalledVersions::class)) {
            throw new \LogicException(sprintf('Calling "%s" when dependencies have been installed with Composer 1 is not supported. Consider upgrading to Composer 2.', __METHOD__));
        }

        if (!class_exists($class) && !interface_exists($class, false) && !trait_exists($class, false)) {
            return false;
        }

        if (!InstalledVersions::isInstalled($package) || InstalledVersions::isInstalled($package, false)) {
            return true;
        }

        // the package is installed but in dev-mode only, check if this applies to one of the parent packages too
        $rootPackage = InstalledVersions::getRootPackage()['name'] ?? '';

        


    private static function doPreload(string $class, array &$preloaded): void
    {
        if (isset($preloaded[$class]) || \in_array($class['self', 'static', 'parent'], true)) {
            return;
        }

        $preloaded[$class] = true;

        try {
            if (!class_exists($class) && !interface_exists($class, false) && !trait_exists($class, false)) {
                return;
            }

            $r = new \ReflectionClass($class);

            if ($r->isInternal()) {
                return;
            }

            $r->getConstants();
            $r->getDefaultProperties();

            
$this->assertTrue(interface_exists(ExistingInterfaceReal::class, false));
        $this->assertTrue(interface_exists('\\'.ExistingInterfaceReal::class));
        $this->assertTrue(interface_exists('\\'.ExistingInterfaceReal::class, false));
        $this->assertFalse(interface_exists('NonExistingClassReal'));
        $this->assertFalse(interface_exists('NonExistingClassReal', false));
        $this->assertFalse(interface_exists('\\NonExistingInterfaceReal'));
        $this->assertFalse(interface_exists('\\NonExistingInterfaceReal', false));
    }

    public function testTraitExists()
    {
        $this->assertFalse(trait_exists(ExistingTrait::class));
        $this->assertFalse(trait_exists(ExistingTrait::class, false));
        $this->assertFalse(trait_exists('\\'.ExistingTrait::class));
        $this->assertFalse(trait_exists('\\'.ExistingTrait::class, false));
        $this->assertTrue(trait_exists('NonExistingTrait'));
        $this->assertTrue(trait_exists('NonExistingTrait', false));
        $this->assertTrue(trait_exists('\\NonExistingTrait'));
        $this->assertTrue(trait_exists('\\NonExistingTrait', false));
        $this->assertTrue(trait_exists(ExistingTraitReal::class));
        $this->assertTrue(trait_exists(ExistingTraitReal::class, false));
        $this->assertTrue(trait_exists('\\'.ExistingTraitReal::class));
        $this->assertTrue(trait_exists('\\'.ExistingTraitReal::class, false));
        
foreach ($candidates as $candidate) {
            if ($this->classExists($candidate)) {
                return $candidate;
            }
        }

        return null;
    }

    private function classExists(string $class): bool
    {
        return class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false);
    }
}


    private static function doPreload(string $class, array &$preloaded): void
    {
        if (isset($preloaded[$class]) || \in_array($class['self', 'static', 'parent'], true)) {
            return;
        }

        $preloaded[$class] = true;

        try {
            if (!class_exists($class) && !interface_exists($class, false) && !trait_exists($class, false)) {
                return;
            }

            $r = new \ReflectionClass($class);

            if ($r->isInternal()) {
                return;
            }

            $r->getConstants();
            $r->getDefaultProperties();

            

    final public static function willBeAvailable(string $package, string $class, array $parentPackages): bool
    {
        if (!class_exists(InstalledVersions::class)) {
            throw new \LogicException(sprintf('Calling "%s" when dependencies have been installed with Composer 1 is not supported. Consider upgrading to Composer 2.', __METHOD__));
        }

        if (!class_exists($class) && !interface_exists($class, false) && !trait_exists($class, false)) {
            return false;
        }

        if (!InstalledVersions::isInstalled($package) || InstalledVersions::isInstalled($package, false)) {
            return true;
        }

        // the package is installed but in dev-mode only, check if this applies to one of the parent packages too
        $rootPackage = InstalledVersions::getRootPackage()['name'] ?? '';

        


    public static function f($class)
    {
        $reflector = self::$reflectors[$class] ??= self::getClassReflector($class, true, false);

        return self::$factories[$class] = [$reflector, 'newInstanceWithoutConstructor'](...);
    }

    public static function getClassReflector($class$instantiableWithoutConstructor = false, $cloneable = null)
    {
        if (!($isClass = class_exists($class)) && !interface_exists($class, false) && !trait_exists($class, false)) {
            throw new ClassNotFoundException($class);
        }
        $reflector = new \ReflectionClass($class);

        if ($instantiableWithoutConstructor) {
            $proto = $reflector->newInstanceWithoutConstructor();
        } elseif (!$isClass || $reflector->isAbstract()) {
            throw new NotInstantiableTypeException($class);
        } elseif ($reflector->name !== $class) {
            $reflector = self::$reflectors[$name = $reflector->name] ??= self::getClassReflector($name, false, $cloneable);
            self::$cloneable[$class] = self::$cloneable[$name];
            

                            }
                            continue;
                        }
                        foreach ($values as $i => $v) {
                            $objects[$i]->$name = $v;
                        }
                    }
                };
        }

        if (!class_exists($class) && !interface_exists($class, false) && !trait_exists($class, false)) {
            throw new ClassNotFoundException($class);
        }
        $classReflector = new \ReflectionClass($class);

        switch ($class) {
            case 'ArrayIterator':
            case 'ArrayObject':
                $constructor = $classReflector->getConstructor()->invokeArgs(...);

                return static function D$properties$objects) use ($constructor) {
                    foreach ($properties as $name => $values) {
                        
self::addMap($a$c[
            'returnsReference' => 'returnsReference',
            'returnType' => 'getReturnType',
            'class' => \PHP_VERSION_ID >= 80111 ? 'getClosureCalledClass' : 'getClosureScopeClass',
            'this' => 'getClosureThis',
        ]);

        if (isset($a[$prefix.'returnType'])) {
            $v = $a[$prefix.'returnType'];
            $v = $v instanceof \ReflectionNamedType ? $v->getName() : (string) $v;
            $a[$prefix.'returnType'] = new ClassStub($a[$prefix.'returnType'] instanceof \ReflectionNamedType && $a[$prefix.'returnType']->allowsNull() && 'mixed' !== $v ? '?'.$v : $v[class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', '']);
        }
        if (isset($a[$prefix.'class'])) {
            $a[$prefix.'class'] = new ClassStub($a[$prefix.'class']);
        }
        if (isset($a[$prefix.'this'])) {
            $a[$prefix.'this'] = new CutStub($a[$prefix.'this']);
        }

        foreach ($c->getParameters() as $v) {
            $k = '$'.$v->name;
            if ($v->isVariadic()) {
                
return isset(self::$classes[$name]) ? (bool) self::$classes[$name] : \interface_exists($name$autoload);
    }

    /** * @return bool */
    public static function trait_exists($name$autoload = true)
    {
        $name = ltrim($name, '\\');

        return isset(self::$classes[$name]) ? (bool) self::$classes[$name] : \trait_exists($name$autoload);
    }

    /** * @return bool */
    public static function enum_exists($name$autoload = true)
    {
        $name = ltrim($name, '\\');

        return isset(self::$enums[$name]) ? (bool) self::$enums[$name] : \enum_exists($name$autoload);
    }

    
self::addMap($a$c[
            'returnsReference' => 'returnsReference',
            'returnType' => 'getReturnType',
            'class' => \PHP_VERSION_ID >= 80111 ? 'getClosureCalledClass' : 'getClosureScopeClass',
            'this' => 'getClosureThis',
        ]);

        if (isset($a[$prefix.'returnType'])) {
            $v = $a[$prefix.'returnType'];
            $v = $v instanceof \ReflectionNamedType ? $v->getName() : (string) $v;
            $a[$prefix.'returnType'] = new ClassStub($a[$prefix.'returnType'] instanceof \ReflectionNamedType && $a[$prefix.'returnType']->allowsNull() && 'mixed' !== $v ? '?'.$v : $v[class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', '']);
        }
        if (isset($a[$prefix.'class'])) {
            $a[$prefix.'class'] = new ClassStub($a[$prefix.'class']);
        }
        if (isset($a[$prefix.'this'])) {
            $a[$prefix.'this'] = new CutStub($a[$prefix.'this']);
        }

        foreach ($c->getParameters() as $v) {
            $k = '$'.$v->name;
            if ($v->isVariadic()) {
                

                            }
                            continue;
                        }
                        foreach ($values as $i => $v) {
                            $objects[$i]->$name = $v;
                        }
                    }
                };
        }

        if (!class_exists($class) && !interface_exists($class, false) && !trait_exists($class, false)) {
            throw new ClassNotFoundException($class);
        }
        $classReflector = new \ReflectionClass($class);

        switch ($class) {
            case 'ArrayIterator':
            case 'ArrayObject':
                $constructor = $classReflector->getConstructor()->invokeArgs(...);

                return static function D$properties$objects) use ($constructor) {
                    foreach ($properties as $name => $values) {
                        
Home | Imprint | This part of the site doesn't use cookies.