Skip to content

Commit

Permalink
Infer type from interface func @return doc
Browse files Browse the repository at this point in the history
  • Loading branch information
klesun committed Aug 29, 2018
1 parent 74263ff commit 0879071
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static F<FuncCtx, L<DeepType>> findMethRetType(Method meth)
return (FuncCtx funcCtx) -> {
L<Method> impls = list(meth);
if (meth.isAbstract()) {
impls = findOverridingMethods(meth);
impls.addAll(findOverridingMethods(meth));
// ignore $this and args in implementations
// since there may be dozens of them (Laravel)
funcCtx = new FuncCtx(funcCtx.getSearch());
Expand All @@ -138,7 +138,8 @@ public static F<FuncCtx, L<DeepType>> findMethRetType(Method meth)
return impls.fap(m -> list(
ClosRes.getReturnedValue(m, finalCtx).types,
opt(meth.getDocComment()).map(doc -> doc.getReturnTag())
.fap(tag -> parseReturnDoc(tag, finalCtx).types)
.fap(tag -> parseReturnDoc(tag, finalCtx).types),
opt(m.getReturnType()).fap(rt -> list(new DeepType(rt, rt.getType())))
).fap(a -> a));
};
}
Expand Down
9 changes: 9 additions & 0 deletions tests/src/DeepTest/IKonohaCitizen.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@ interface IKonohaCitizen
* ]
*/
public function payTaxes();

/**
* @return array [
* 'whoShouldBeTheHokage' => 'Kakashi',
* 'whoStealsFromTreasury' => 'Sakura',
* 'whoShouldNotExist' => 'Boruto',
* ]
*/
public function getHonestOpinion();
}
5 changes: 5 additions & 0 deletions tests/src/DeepTest/Konohamaru.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ public function payTaxes()
'familyTax' => '0',
];
}

public function getHonestOpinion()
{
return json_decode(file_get_contents(__DIR__.'/honest_opinion.json'));
}
}
5 changes: 5 additions & 0 deletions tests/src/DeepTest/Naruto.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public function payTaxes()
'familyTax' => '0', // Naruto has no family
];
}

public function getHonestOpinion()
{
return json_decode(file_get_contents('http://narutohonestopinion.org/json'));
}
}
14 changes: 13 additions & 1 deletion tests/src/DeepTest/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ public static function provideBuiltIns()
return $list;
}

private static function makeKonohaCitizen()
private static function makeKonohaCitizen(): IKonohaCitizen
{
if (rand() % 2) {
$konohanian = Naruto::kageBunshin();
Expand Down Expand Up @@ -2202,6 +2202,18 @@ public function provideAssocBuiltIns($handle)
return $list;
}

public function provideIfcReturnDoc()
{
$honestOpinion = static::makeKonohaCitizen()->getHonestOpinion();
$honestOpinion[''];
$list[] = [$honestOpinion, [
'whoShouldBeTheHokage' => [],
'whoStealsFromTreasury' => [],
'whoShouldNotExist' => [],
]];
return $list;
}

//=============================
// following are not implemented yet
//=============================
Expand Down

0 comments on commit 0879071

Please sign in to comment.