Skip to content

Commit

Permalink
Support foreach($pairs as list($a, $b))
Browse files Browse the repository at this point in the history
  • Loading branch information
klesun committed Feb 11, 2018
1 parent 827e108 commit b5a460c
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ private L<DeepType> findBuiltInFuncCallType(FunctionReferenceImpl call)
.addType(() -> elMt, elMt.getIdeaType()));
combine.anyKeyElTypes.addAll(elMt.types);
result.add(combine);
} else if (name.equals("array_pop") || name.equals("array_shift")) {
} else if (name.equals("array_pop") || name.equals("array_shift")
|| name.equals("current") || name.equals("end") || name.equals("next")
|| name.equals("prev") || name.equals("reset")
) {
L(params).gat(0).map(p -> findPsiExprType(p).getEl().types).thn(result::addAll);
} else if (name.equals("array_merge")) {
for (PsiElement paramPsi: params) {
Expand Down
17 changes: 13 additions & 4 deletions src/org/klesun/deep_assoc_completion/resolvers/VarRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,19 @@ private Opt<S<MultiType>> assertForeachElement(PsiElement varRef)
{
return opt(varRef.getParent())
.fop(toCast(ForeachImpl.class))
.flt(fch -> opt(fch.getValue()).map(v -> v.isEquivalentTo(varRef)).def(false))
.map(fch -> fch.getArray())
.fop(toCast(PhpExpression.class))
.map(arr -> () -> ctx.findExprType(arr).getEl());
.fop(fch -> opt(fch.getArray())
.fop(toCast(PhpExpression.class))
.map(arr -> () -> {
MultiType valMt = ctx.findExprType(arr).getEl();
L<Variable> tuple = L(fch.getVariables());
if (tuple.size() > 1) {
return valMt.getKey(tuple.indexOf(varRef) + "");
} else {
// this is actually not correct since you could write list($a)
// or $list(,,$c), but IDEA does not parse list in foreach well
return valMt;
}
}));
}

private Opt<S<MultiType>> assertTupleAssignment(PsiElement varRef)
Expand Down
80 changes: 80 additions & 0 deletions tests/src/Lib/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,84 @@ public function provideDbFetch()
$list[] = [$rows['0'], ['id' => [], 'name' => [], 'profit' => []]];
return $list;
}

public function provideZip()
{
$list = [];
$grooms = [
['smoking' => 'black', 'salary' => '510090.00'],
['smoking' => 'red', 'salary' => '710090.00'],
];
$wives = [
['weddingDress' => 'expensiveOne', 'face' => 'average'],
['weddingDress' => 'cheapOne', 'face' => 'beautiful'],
];
$weddingPairs = Fp::zip([$grooms, $wives]);
$firstPair = $weddingPairs[0];
$list[] = [$firstPair[0], ['smoking' => [], 'salary' => []]];
$list[] = [$firstPair[1], ['weddingDress' => [], 'face' => []]];
return $list;
}

private static function pairsToDict(array $pairs)
{
$result = [];
foreach ($pairs as list($key, $value)) {
$result[$key] = $value;
}
return $result;
}

public function providePairsToDict()
{
$list = [];
$pairs = [
['gds', 'apollo'],
['profile', 'GENERIC'],
['user', 'RBS'],
];
$dict = static::pairsToDict($pairs);
$list[] = [$dict, ['gds' => [], 'profile' => [], 'user' => []]];
return $list;
}

private static function unpackList(array $keys, array $data)
{
$result = [];
foreach ($data as $record) {
$pairs = Fp::zip([$keys, $record]);
$result[] = static::pairsToDict($pairs);
}
return $result;
}

public static function provideTableOwnImplementation()
{
$list = [];
$profiles = [
// gds | profile | user | group | gds_profile | session_limit | idle_session_timeout |
//-----------+------------------------+--------+-----------------------+----------------------------+---------------+----------------------|
[ 'amadeus' , 'GENERIC' , 'RBS' , null , 'AMADEUS_PROD_1ASIWTUTICO' , null , null ],
[ 'apollo' , 'AIRLINE_TERMINAL' , 'AFPE' , 'apollo_terminals' , 'DynApolloProd_1O3K' , null , null ],
[ 'apollo' , 'CMS_TERMINAL' , 'RBS' , null , 'DynApolloProd_2G55' , 1000 , null ],
[ 'apollo' , 'FPE_STUDENT_TERMINAL' , 'FPE' , 'apollo_terminals' , 'DynApolloProd_1O3K' , null , null ],
[ 'apollo' , 'GRAB_FARES' , 'RBS' , 'apollo_auto_process' , 'DynApolloProd_1O3K' , null , null ],
[ 'apollo' , 'IMPORT_PNR' , 'RBS' , 'apollo_auto_process' , 'DynApolloProd_1O3K' , null , null ],
[ 'apollo' , 'QUEUE_PROCESSING' , 'RBS' , 'apollo_auto_process' , 'DynApolloProd_1O3K' , null , null ],
[ 'apollo' , 'UNCLASSIFIED' , 'RBS' , 'apollo_auto_process' , 'DynApolloProd_1O3K' , null , null ],
[ 'apollo' , 'OTA' , 'OTA' , 'apollo_auto_process' , 'DynApolloProd_1O3K' , null , null ],
[ 'apollo' , 'STAGING' , 'RBS' , null , 'DynApolloCopy_1O3K' , 200 , null ],
[ 'sabre' , 'CMS_OLD_TERMINAL' , 'FPE' , 'sabre_terminals' , 'SABRE_PROD_L3II' , null , 5*60 ],
[ 'sabre' , 'CMS_TERMINAL' , 'RBS' , 'sabre_terminals' , 'SABRE_PROD_L3II' , null , null ],
[ 'sabre' , 'FPE_STUDENT_TERMINAL' , 'FPE' , 'sabre_terminals' , 'SABRE_PROD_Z2NI' , null , 5*60 ],
[ 'sabre' , 'IMPORT_PNR' , 'RBS' , 'sabre_auto_process' , 'SABRE_PROD_L3II' , null , null ],
[ 'sabre' , 'QUEUE_PROCESSING' , 'RBS' , 'sabre_auto_process' , 'SABRE_PROD_L3II' , null , null ],
[ 'sabre' , 'OTA' , 'OTA' , 'sabre_auto_process' , 'SABRE_PROD_L3II' , null , null ],
[ 'sabre' , 'UNCLASSIFIED' , 'RBS' , 'sabre_auto_process' , 'SABRE_PROD_L3II' , null , null ],
];
$unpacked = static::unpackList(['gds', 'profile', 'user', 'group', 'gds_profile', 'session_limit', 'idle_session_timeout'], $profiles);
$row = $unpacked[0];
$list[] = [$row, ['gds' => [], 'profile' => [], 'user' => [], 'group' => [], 'gds_profile' => [], 'session_limit' => [], 'idle_session_timeout' => []]];
return $list;
}
}

0 comments on commit b5a460c

Please sign in to comment.