summaryrefslogtreecommitdiff
path: root/vendor/sebastian/type/src/IterableType.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/sebastian/type/src/IterableType.php')
-rw-r--r--vendor/sebastian/type/src/IterableType.php76
1 files changed, 76 insertions, 0 deletions
diff --git a/vendor/sebastian/type/src/IterableType.php b/vendor/sebastian/type/src/IterableType.php
new file mode 100644
index 000000000..c5bc6627b
--- /dev/null
+++ b/vendor/sebastian/type/src/IterableType.php
@@ -0,0 +1,76 @@
+<?php declare(strict_types=1);
+/*
+ * This file is part of sebastian/type.
+ *
+ * (c) Sebastian Bergmann <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+namespace SebastianBergmann\Type;
+
+use function assert;
+use function class_exists;
+use function is_iterable;
+use ReflectionClass;
+use ReflectionException;
+
+final class IterableType extends Type
+{
+ /**
+ * @var bool
+ */
+ private $allowsNull;
+
+ public function __construct(bool $nullable)
+ {
+ $this->allowsNull = $nullable;
+ }
+
+ /**
+ * @throws RuntimeException
+ */
+ public function isAssignable(Type $other): bool
+ {
+ if ($this->allowsNull && $other instanceof NullType) {
+ return true;
+ }
+
+ if ($other instanceof self) {
+ return true;
+ }
+
+ if ($other instanceof SimpleType) {
+ return is_iterable($other->value());
+ }
+
+ if ($other instanceof ObjectType) {
+ $className = $other->className()->qualifiedName();
+ assert(class_exists($className));
+
+ try {
+ return (new ReflectionClass($className))->isIterable();
+ // @codeCoverageIgnoreStart
+ } catch (ReflectionException $e) {
+ throw new RuntimeException(
+ $e->getMessage(),
+ (int) $e->getCode(),
+ $e
+ );
+ // @codeCoverageIgnoreEnd
+ }
+ }
+
+ return false;
+ }
+
+ public function name(): string
+ {
+ return 'iterable';
+ }
+
+ public function allowsNull(): bool
+ {
+ return $this->allowsNull;
+ }
+}