Skip to content

Commit

Permalink
Handle self, static and parent inside FunctionType::isInstance()
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Jan 16, 2021
1 parent 21e7fd6 commit 09e6f17
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ XP Framework Core ChangeLog

### Bugfixes

* Handle `self`, `static` and `parent` return and parameter types inside
`lang.FunctionType::isInstance()`.
(@thekid)
* Fixed issue #255: Function return types not taken into account - @thekid

## 10.5.1 / 2020-11-29
Expand Down
20 changes: 15 additions & 5 deletions src/main/php/lang/FunctionType.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,29 @@ public function literal(): string {
* @return var
*/
protected function verify($r, $signature, $false, $class= null) {
$details= $class ? XPClass::detailsForMethod($class, $r->getName()) : null;
if ($class) {
$details= XPClass::detailsForMethod($class, $r->getName());
$resolve= [
'static' => function() use($class) { return new XPClass($class); },
'self' => function() use($class) { return new XPClass($class); },
'parent' => function() use($class) { return new XPClass($class->getParentClass()); },
];
} else {
$details= null;
$resolve= [];
}

$t= $r->getReturnType();
if (null === $t) {
$returns= isset($details[DETAIL_RETURNS]) ? Type::forName($details[DETAIL_RETURNS]) : null;
} else if ($t instanceof \ReflectionUnionType) {
$union= [];
foreach ($t->getTypes() as $c) {
$union[]= Type::forName($c->getName());
$union[]= Type::resolve($c->getName(), $resolve);
}
$returns= new TypeUnion($union);
} else {
$returns= Type::forName(PHP_VERSION_ID >= 70100 ? $t->getName() : $t->__toString());
$returns= Type::resolve(PHP_VERSION_ID >= 70100 ? $t->getName() : $t->__toString(), $resolve);
}

// Verify return type
Expand Down Expand Up @@ -122,11 +132,11 @@ protected function verify($r, $signature, $false, $class= null) {
if ($t instanceof \ReflectionUnionType) {
$union= [];
foreach ($t->getTypes() as $u) {
$union[]= Type::forName($u->getName());
$union[]= Type::resolve($u->getName(), $resolve);
}
$param= new TypeUnion($union);
} else {
$param= Type::forName(PHP_VERSION_ID >= 70100 ? $t->getName() : $t->__toString());
$param= Type::resolve(PHP_VERSION_ID >= 70100 ? $t->getName() : $t->__toString(), $resolve);
}

if (!$type->isAssignableFrom($param)) {
Expand Down

0 comments on commit 09e6f17

Please sign in to comment.