Skip to content

Commit

Permalink
Types: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 17, 2023
1 parent dd7e59f commit 3dabec5
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/Utils/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ public static function fromString(string $type): self

/**
* Resolves 'self', 'static' and 'parent' to the actual class name.
* @param \ReflectionFunctionAbstract|\ReflectionParameter|\ReflectionProperty $reflection
* @param \ReflectionFunctionAbstract|\ReflectionParameter|\ReflectionProperty $of
*/
public static function resolve(string $type, $reflection): string
public static function resolve(string $type, $of): string
{
$lower = strtolower($type);
if ($reflection instanceof \ReflectionFunction) {
if ($of instanceof \ReflectionFunction) {
return $type;
} elseif ($lower === 'self' || $lower === 'static') {
return $reflection->getDeclaringClass()->name;
} elseif ($lower === 'parent' && $reflection->getDeclaringClass()->getParentClass()) {
return $reflection->getDeclaringClass()->getParentClass()->name;
return $of->getDeclaringClass()->name;
} elseif ($lower === 'parent' && $of->getDeclaringClass()->getParentClass()) {
return $of->getDeclaringClass()->getParentClass()->name;
} else {
return $type;
}
Expand Down Expand Up @@ -218,31 +218,31 @@ public function isClassKeyword(): bool
/**
* Verifies type compatibility. For example, it checks if a value of a certain type could be passed as a parameter.
*/
public function allows(string $type): bool
public function allows(string $subtype): bool
{
if ($this->types === ['mixed']) {
return true;
}

$type = self::fromString($type);
$subtype = self::fromString($subtype);

if ($this->isIntersection()) {
if (!$type->isIntersection()) {
if (!$subtype->isIntersection()) {
return false;
}

return Arrays::every($this->types, function ($currentType) use ($type) {
return Arrays::every($this->types, function ($currentType) use ($subtype) {
$builtin = Reflection::isBuiltinType($currentType);
return Arrays::some($type->types, function ($testedType) use ($currentType, $builtin) {
return Arrays::some($subtype->types, function ($testedType) use ($currentType, $builtin) {
return $builtin
? strcasecmp($currentType, $testedType) === 0
: is_a($testedType, $currentType, true);
});
});
}

$method = $type->isIntersection() ? 'some' : 'every';
return Arrays::$method($type->types, function ($testedType) {
$method = $subtype->isIntersection() ? 'some' : 'every';
return Arrays::$method($subtype->types, function ($testedType) {
$builtin = Validators::isBuiltinType($testedType);
return Arrays::some($this->types, function ($currentType) use ($testedType, $builtin) {
return $builtin
Expand Down

0 comments on commit 3dabec5

Please sign in to comment.