PocketMine-MP 5.18.2 git-00e39821f06a4b6d728d35053c2621dbb19369ff
Type.php
1<?php declare(strict_types = 1);
2
3namespace DaveRandom\CallbackValidator;
4
5abstract class Type
6{
10 const WEAK = 0x01;
11
15 const NULLABLE = 0x02;
16
20 const REFERENCE = 0x04;
21
27 public $typeName;
28
34 public $isNullable;
35
41 public $isByReference;
42
48 public $isWeak;
49
55 public $allowsCovariance;
56
62 public $allowsContravariance;
63
70 protected function __construct($typeName, $flags, $allowsCovariance, $allowsContravariance)
71 {
72 $this->typeName = $typeName !== null
73 ? (string)$typeName
74 : null;
75
76 $this->isNullable = (bool)($flags & self::NULLABLE);
77 $this->isByReference = (bool)($flags & self::REFERENCE);
78 $this->isWeak = (bool)($flags & self::WEAK);
79 $this->allowsCovariance = (bool)$allowsCovariance;
80 $this->allowsContravariance = (bool)$allowsContravariance;
81 }
82
91 public function isSatisfiedBy($typeName, $nullable, $byReference)
92 {
93 // By-ref must always be the same
94 if ($byReference xor $this->isByReference) {
95 return false;
96 }
97
98 // Candidate is exact match to prototype
99 if ($typeName === $this->typeName && $nullable === $this->isNullable) {
100 return true;
101 }
102
103 // Test for a covariant match
104 if ($this->allowsCovariance
105 && MatchTester::isMatch($this->typeName, $this->isNullable, $typeName, $nullable, $this->isWeak)) {
106 return true;
107 }
108
109 // Test for a contravariant match
110 if ($this->allowsContravariance
111 && MatchTester::isMatch($typeName, $nullable, $this->typeName, $this->isNullable, $this->isWeak)) {
112 return true;
113 }
114
115 // In weak mode, allow castable scalars as long as nullability matches (invariant)
116 return $this->isWeak
117 && $nullable === $this->isNullable
118 && $typeName !== null
119 && $this->typeName !== null
120 && MatchTester::isWeakScalarMatch($typeName, $this->typeName);
121 }
122}
static isMatch($superTypeName, $superTypeNullable, $subTypeName, $subTypeNullable, $weak)
Definition: MatchTester.php:71
static isWeakScalarMatch($superTypeName, $subTypeName)
Definition: MatchTester.php:43
__construct($typeName, $flags, $allowsCovariance, $allowsContravariance)
Definition: Type.php:70
isSatisfiedBy($typeName, $nullable, $byReference)
Definition: Type.php:91