From 1c4f7ab3b838b23afb2ee4dab14acbf75956e952 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 22 Mar 2022 12:24:31 +0300 Subject: * add phpunit as a dev dependency * add some basic tests for UrlHelper::rewrite_relative() * fix UrlHelper::rewrite_relative() to work better on non-absolute relative URL paths --- .../Instantiator/Exception/ExceptionInterface.php | 12 + .../Exception/InvalidArgumentException.php | 50 ++++ .../Exception/UnexpectedValueException.php | 59 +++++ .../src/Doctrine/Instantiator/Instantiator.php | 260 +++++++++++++++++++++ .../Instantiator/InstantiatorInterface.php | 24 ++ 5 files changed, 405 insertions(+) create mode 100644 vendor/doctrine/instantiator/src/Doctrine/Instantiator/Exception/ExceptionInterface.php create mode 100644 vendor/doctrine/instantiator/src/Doctrine/Instantiator/Exception/InvalidArgumentException.php create mode 100644 vendor/doctrine/instantiator/src/Doctrine/Instantiator/Exception/UnexpectedValueException.php create mode 100644 vendor/doctrine/instantiator/src/Doctrine/Instantiator/Instantiator.php create mode 100644 vendor/doctrine/instantiator/src/Doctrine/Instantiator/InstantiatorInterface.php (limited to 'vendor/doctrine/instantiator/src/Doctrine') diff --git a/vendor/doctrine/instantiator/src/Doctrine/Instantiator/Exception/ExceptionInterface.php b/vendor/doctrine/instantiator/src/Doctrine/Instantiator/Exception/ExceptionInterface.php new file mode 100644 index 000000000..e6a5195f2 --- /dev/null +++ b/vendor/doctrine/instantiator/src/Doctrine/Instantiator/Exception/ExceptionInterface.php @@ -0,0 +1,12 @@ + $reflectionClass + * + * @template T of object + */ + public static function fromAbstractClass(ReflectionClass $reflectionClass): self + { + return new self(sprintf( + 'The provided class "%s" is abstract, and cannot be instantiated', + $reflectionClass->getName() + )); + } + + public static function fromEnum(string $className): self + { + return new self(sprintf( + 'The provided class "%s" is an enum, and cannot be instantiated', + $className + )); + } +} diff --git a/vendor/doctrine/instantiator/src/Doctrine/Instantiator/Exception/UnexpectedValueException.php b/vendor/doctrine/instantiator/src/Doctrine/Instantiator/Exception/UnexpectedValueException.php new file mode 100644 index 000000000..4e55ac525 --- /dev/null +++ b/vendor/doctrine/instantiator/src/Doctrine/Instantiator/Exception/UnexpectedValueException.php @@ -0,0 +1,59 @@ + $reflectionClass + * + * @template T of object + */ + public static function fromSerializationTriggeredException( + ReflectionClass $reflectionClass, + Exception $exception + ): self { + return new self( + sprintf( + 'An exception was raised while trying to instantiate an instance of "%s" via un-serialization', + $reflectionClass->getName() + ), + 0, + $exception + ); + } + + /** + * @phpstan-param ReflectionClass $reflectionClass + * + * @template T of object + */ + public static function fromUncleanUnSerialization( + ReflectionClass $reflectionClass, + string $errorString, + int $errorCode, + string $errorFile, + int $errorLine + ): self { + return new self( + sprintf( + 'Could not produce an instance of "%s" via un-serialization, since an error was triggered ' + . 'in file "%s" at line "%d"', + $reflectionClass->getName(), + $errorFile, + $errorLine + ), + 0, + new Exception($errorString, $errorCode) + ); + } +} diff --git a/vendor/doctrine/instantiator/src/Doctrine/Instantiator/Instantiator.php b/vendor/doctrine/instantiator/src/Doctrine/Instantiator/Instantiator.php new file mode 100644 index 000000000..d616fa4db --- /dev/null +++ b/vendor/doctrine/instantiator/src/Doctrine/Instantiator/Instantiator.php @@ -0,0 +1,260 @@ + $className + * + * @return object + * @phpstan-return T + * + * @throws ExceptionInterface + * + * @template T of object + */ + public function instantiate($className) + { + if (isset(self::$cachedCloneables[$className])) { + /** + * @phpstan-var T + */ + $cachedCloneable = self::$cachedCloneables[$className]; + + return clone $cachedCloneable; + } + + if (isset(self::$cachedInstantiators[$className])) { + $factory = self::$cachedInstantiators[$className]; + + return $factory(); + } + + return $this->buildAndCacheFromFactory($className); + } + + /** + * Builds the requested object and caches it in static properties for performance + * + * @phpstan-param class-string $className + * + * @return object + * @phpstan-return T + * + * @template T of object + */ + private function buildAndCacheFromFactory(string $className) + { + $factory = self::$cachedInstantiators[$className] = $this->buildFactory($className); + $instance = $factory(); + + if ($this->isSafeToClone(new ReflectionClass($instance))) { + self::$cachedCloneables[$className] = clone $instance; + } + + return $instance; + } + + /** + * Builds a callable capable of instantiating the given $className without + * invoking its constructor. + * + * @phpstan-param class-string $className + * + * @phpstan-return callable(): T + * + * @throws InvalidArgumentException + * @throws UnexpectedValueException + * @throws ReflectionException + * + * @template T of object + */ + private function buildFactory(string $className): callable + { + $reflectionClass = $this->getReflectionClass($className); + + if ($this->isInstantiableViaReflection($reflectionClass)) { + return [$reflectionClass, 'newInstanceWithoutConstructor']; + } + + $serializedString = sprintf( + '%s:%d:"%s":0:{}', + is_subclass_of($className, Serializable::class) ? self::SERIALIZATION_FORMAT_USE_UNSERIALIZER : self::SERIALIZATION_FORMAT_AVOID_UNSERIALIZER, + strlen($className), + $className + ); + + $this->checkIfUnSerializationIsSupported($reflectionClass, $serializedString); + + return static function () use ($serializedString) { + return unserialize($serializedString); + }; + } + + /** + * @phpstan-param class-string $className + * + * @phpstan-return ReflectionClass + * + * @throws InvalidArgumentException + * @throws ReflectionException + * + * @template T of object + */ + private function getReflectionClass(string $className): ReflectionClass + { + if (! class_exists($className)) { + throw InvalidArgumentException::fromNonExistingClass($className); + } + + if (PHP_VERSION_ID >= 80100 && enum_exists($className, false)) { + throw InvalidArgumentException::fromEnum($className); + } + + $reflection = new ReflectionClass($className); + + if ($reflection->isAbstract()) { + throw InvalidArgumentException::fromAbstractClass($reflection); + } + + return $reflection; + } + + /** + * @phpstan-param ReflectionClass $reflectionClass + * + * @throws UnexpectedValueException + * + * @template T of object + */ + private function checkIfUnSerializationIsSupported(ReflectionClass $reflectionClass, string $serializedString): void + { + set_error_handler(static function (int $code, string $message, string $file, int $line) use ($reflectionClass, &$error): bool { + $error = UnexpectedValueException::fromUncleanUnSerialization( + $reflectionClass, + $message, + $code, + $file, + $line + ); + + return true; + }); + + try { + $this->attemptInstantiationViaUnSerialization($reflectionClass, $serializedString); + } finally { + restore_error_handler(); + } + + if ($error) { + throw $error; + } + } + + /** + * @phpstan-param ReflectionClass $reflectionClass + * + * @throws UnexpectedValueException + * + * @template T of object + */ + private function attemptInstantiationViaUnSerialization(ReflectionClass $reflectionClass, string $serializedString): void + { + try { + unserialize($serializedString); + } catch (Exception $exception) { + throw UnexpectedValueException::fromSerializationTriggeredException($reflectionClass, $exception); + } + } + + /** + * @phpstan-param ReflectionClass $reflectionClass + * + * @template T of object + */ + private function isInstantiableViaReflection(ReflectionClass $reflectionClass): bool + { + return ! ($this->hasInternalAncestors($reflectionClass) && $reflectionClass->isFinal()); + } + + /** + * Verifies whether the given class is to be considered internal + * + * @phpstan-param ReflectionClass $reflectionClass + * + * @template T of object + */ + private function hasInternalAncestors(ReflectionClass $reflectionClass): bool + { + do { + if ($reflectionClass->isInternal()) { + return true; + } + + $reflectionClass = $reflectionClass->getParentClass(); + } while ($reflectionClass); + + return false; + } + + /** + * Checks if a class is cloneable + * + * Classes implementing `__clone` cannot be safely cloned, as that may cause side-effects. + * + * @phpstan-param ReflectionClass $reflectionClass + * + * @template T of object + */ + private function isSafeToClone(ReflectionClass $reflectionClass): bool + { + return $reflectionClass->isCloneable() + && ! $reflectionClass->hasMethod('__clone') + && ! $reflectionClass->isSubclassOf(ArrayIterator::class); + } +} diff --git a/vendor/doctrine/instantiator/src/Doctrine/Instantiator/InstantiatorInterface.php b/vendor/doctrine/instantiator/src/Doctrine/Instantiator/InstantiatorInterface.php new file mode 100644 index 000000000..10508b562 --- /dev/null +++ b/vendor/doctrine/instantiator/src/Doctrine/Instantiator/InstantiatorInterface.php @@ -0,0 +1,24 @@ + $className + * + * @return object + * @phpstan-return T + * + * @throws ExceptionInterface + * + * @template T of object + */ + public function instantiate($className); +} -- cgit v1.2.3