Skip to content

Commit

Permalink
Infer keys from mysqli_result
Browse files Browse the repository at this point in the history
  • Loading branch information
klesun-itn committed Jan 11, 2019
1 parent b3a438b commit 36bcca2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
8 changes: 2 additions & 6 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin>
<id>lv.midiana.misc.phpstorm-plugins.deep-keys</id>
<name>deep-assoc-completion</name>
<version>2018.01.08.001</version>
<version>2018.01.11.001</version>
<vendor email="arturklesun@gmail.com" url="http://midiana.lv/entry/deep-assoc-completion">klesun</vendor>

<description><![CDATA[
Expand Down Expand Up @@ -37,11 +37,7 @@ This plugin greatly extends phpstorm's typing with associative array support<br/

<change-notes><![CDATA[
<ul>
<li>Add file_put_contents/preg_*/simplexml_load_string/preg_last_error constant completion</li>
<li>Fix: do not _Ctrl + B_ to key decl if caret is on it already. Fixes a nasty thing that you would not get GoTo of a magic method if it is used as a key</li>
<li>Add constant arg completion to curl_setopt_array/json_encode/str_pad/pcntl_signal and few more small functions</li>
<li>Suggest class constants in string === value completion (Not just it's actual string value)</li>
<li>Add completion of keys in stream_context_create() params</li>
<li>Infer keys from mysqli_result (and, same as PDO, 'SELECT * FROM abc' will work as well if you connect DB to IDEA)</li>
</ul>
]]>
</change-notes>
Expand Down
15 changes: 14 additions & 1 deletion src/org/klesun/deep_assoc_completion/resolvers/MethCallRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,25 @@ private It<DeepType> findBuiltInRetType(Method meth, IExprCtx argCtx, MethodRefe
getBindVars(strType).fch(type.pdoBindVars::add);
});
types = It(list(type));
} else if (clsNme.equals("PDOStatement") && meth.getName().equals("fetch")) {
} else if (clsNme.equals("PDOStatement") && meth.getName().equals("fetch")
|| clsNme.equals("mysqli_result") && meth.getName().equals("fetch_assoc")
) {
It<DeepType> pdoTypes = opt(methCall.getClassReference())
.fop(toCast(PhpExpression.class))
.fap(obj -> ctx.findExprType(obj))
.fap(t -> t.pdoFetchTypes);
types = It(pdoTypes);
} else if (clsNme.equals("mysqli") && meth.getName().equals("query")) {
MemIt<DeepType> rowTypes = argCtx.func().getArg(0).fap(mt -> mt.types)
.map(strType -> parseSqlSelect(strType, meth.getProject())).mem();
types = It.cnc(
som(new DeepType(methCall).btw(t -> {
// it's not a PDO, but nah
rowTypes.fch((rowt, i) -> t.pdoFetchTypes.add(rowt));
})),
// since PHP 5.4 mysqli_result can also be iterated
som(Mt.getInArraySt(It(rowTypes), methCall))
);
}
It<DeepType> modelRowTypes = getModelRowType(methCall, meth);
It<DeepType> modelRowArrTypes = !modelRowTypes.has() ? It.non() :
Expand Down
7 changes: 7 additions & 0 deletions src/org/klesun/deep_assoc_completion/structures/DeepType.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,11 @@ public Mt mt()
{
return new Mt(list(this));
}

/** a handy method to do stuff in same expression */
public DeepType btw(C<DeepType> addData)
{
addData.accept(this);
return this;
}
}
25 changes: 25 additions & 0 deletions tests/src/DeepTest/ExactKeysUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,31 @@ public function provideReturnInRequire($params)
];
}

public function provideMysqliIterator()
{
$rows = [];
$mysqli = new \mysqli("10.128.128.150", "stasadm", "G0a4wa&", "rbstools");
$result = $mysqli->query("SELECT id, iata_code, name FROM airports limit 20;");
foreach ($result as $row) {
$rows[] = $row;
}
$rows[0][''];
return [
[$rows[0], ['id', 'iata_code', 'name']],
];
}

public function provideMysqliFetchAssoc()
{
$mysqli = new \mysqli("10.128.128.150", "stasadm", "G0a4wa&", "rbstools");
$result = $mysqli->query("SELECT id, iata_code, name FROM airports limit 20;");
$row = $result->fetch_assoc();
$row[''];
return [
[$row, ['id', 'iata_code', 'name']],
];
}

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

0 comments on commit 36bcca2

Please sign in to comment.