Skip to content

Commit

Permalink
Provide string completion in array_intersect()
Browse files Browse the repository at this point in the history
  • Loading branch information
klesun committed May 22, 2018
1 parent cb9a445 commit 7ab23fa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ private static Opt<MultiType> resolveInArrayNeedle(StringLiteralExpression lit,
return opt(lit.getParent())
.fop(toCast(ParameterList.class))
.flt(lst -> L(lst.getParameters()).gat(0)
.flt(arg -> arg.isEquivalentTo(lit)).has()
)
.flt(arg -> arg.isEquivalentTo(lit)).has())
.flt(lst -> opt(lst.getParent())
.fop(toCast(FunctionReference.class))
.map(fun -> fun.getName())
Expand All @@ -98,6 +97,27 @@ private static Opt<MultiType> resolveInArrayNeedle(StringLiteralExpression lit,
.map(str -> funcCtx.findExprType(str).getEl());
}

// array_intersect($cmdTypes, ['redisplayPnr', 'itinerary', 'airItinerary', 'storeKeepPnr', 'changeArea', ''])
private static Opt<MultiType> resolveArrayIntersect(StringLiteralExpression lit, FuncCtx funcCtx)
{
return opt(lit)
.map(literal -> literal.getParent()) // array value
.map(literal -> literal.getParent())
.fop(toCast(ArrayCreationExpression.class))
.fop(arr -> opt(arr.getParent())
.fop(toCast(ParameterList.class))
.flt(lst -> opt(lst.getParent())
.fop(toCast(FunctionReference.class))
.map(fun -> fun.getName())
.flt(nme -> nme.equals("array_intersect")).has())
.fap(lst -> L(lst.getParameters()))
.flt(par -> !arr.isEquivalentTo(par))
.fop(toCast(PhpExpression.class))
.map(str -> funcCtx.findExprType(str).getEl())
.fap(mt -> mt.types)
.wap(types -> types.size() == 0 ? opt(null) : opt(new MultiType(types))));
}

private MultiType resolve(StringLiteralExpression lit, boolean isAutoPopup)
{
SearchContext search = new SearchContext()
Expand All @@ -107,7 +127,8 @@ private MultiType resolve(StringLiteralExpression lit, boolean isAutoPopup)
return Opt.fst(list(
resolveEqExpr(lit, funcCtx),
resolveInArrayHaystack(lit, funcCtx),
resolveInArrayNeedle(lit, funcCtx)
resolveInArrayNeedle(lit, funcCtx),
resolveArrayIntersect(lit, funcCtx)
)).def(MultiType.INVALID_PSI);
}

Expand Down
3 changes: 2 additions & 1 deletion src/org/klesun/lang/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,10 @@ public <Tnew> Tnew wap(F<L<T>, Tnew> wrapper)
/**
* "fch" stands for For Each
*/
public void fch(C<T> f)
public L<T> fch(C<T> f)
{
fch((el, i) -> f.accept(el));
return this;
}

public void fch(C2<T, Integer> f)
Expand Down
3 changes: 3 additions & 0 deletions tests/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,9 @@ private static function testInArray()
}
if (in_array('', $types)) {

}
if (array_intersect([''], $types, ['ASD'])) {

}
}

Expand Down

0 comments on commit 7ab23fa

Please sign in to comment.