PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
ParameterType.php
1<?php declare(strict_types = 1);
2
3namespace DaveRandom\CallbackValidator;
4
5final class ParameterType extends Type
6{
10 const CONTRAVARIANT = 0x01 << 8;
11
16 const COVARIANT = 0x02 << 8;
17
21 const VARIADIC = 0x04 << 8;
22
26 const OPTIONAL = 0x08 << 8;
27
33 private $parameterName;
34
40 public $isVariadic;
41
47 public $isOptional;
48
56 public static function createFromReflectionParameter($reflection, $flags = 0)
57 {
58 $parameterName = $reflection->getName();
59
60 if ($reflection->isPassedByReference()) {
61 $flags |= self::REFERENCE;
62 }
63
64 if ($reflection->isVariadic()) {
65 $flags |= self::VARIADIC;
66 }
67
68 if ($reflection->isOptional()) {
69 $flags |= self::OPTIONAL;
70 }
71
72 $typeName = null;
73 $typeReflection = $reflection->getType();
74
75 if ($typeReflection instanceof \ReflectionNamedType) {
76 $typeName = $typeReflection->getName();
77
78 if ($typeReflection->allowsNull()) {
79 $flags |= self::NULLABLE;
80 }
81 } elseif ($typeReflection !== null) {
82 throw new \LogicException("Unsupported reflection type " . get_class($typeReflection));
83 }
84
85 return new self($parameterName, $typeName, $flags);
86 }
87
93 public function __construct($parameterName, $typeName = null, $flags = self::CONTRAVARIANT)
94 {
95 $flags = (int)$flags;
96
97 parent::__construct($typeName, $flags, ($flags & self::COVARIANT) !== 0, ($flags & self::CONTRAVARIANT) !== 0);
98
99 $this->parameterName = (string)$parameterName;
100 $this->isOptional = (bool)($flags & self::OPTIONAL);
101 $this->isVariadic = (bool)($flags & self::VARIADIC);
102 }
103
107 public function __toString()
108 {
109 $string = '';
110
111 if ($this->typeName !== null) {
112 if ($this->isNullable) {
113 $string .= '?';
114 }
115
116 $string .= $this->typeName . ' ';
117 }
118
119 if ($this->isByReference) {
120 $string .= '&';
121 }
122
123 if ($this->isVariadic) {
124 $string .= '...';
125 }
126
127 return $string . '$' . $this->parameterName;
128 }
129}
static createFromReflectionParameter($reflection, $flags=0)
__construct($parameterName, $typeName=null, $flags=self::CONTRAVARIANT)