Skip to content

Commit

Permalink
Provide string values completion in in_array($type, [])
Browse files Browse the repository at this point in the history
  • Loading branch information
klesun committed May 13, 2018
1 parent 8921f4d commit d6883a3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
5 changes: 3 additions & 2 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.04.17.001</version>
<version>2018.05.13.002</version>
<vendor email="safronevev@gmail.com" url="http://midiana.lv/entry/phpstorm-deep-keys">Klesun</vendor>

<description><![CDATA[
Expand Down Expand Up @@ -29,8 +29,9 @@ This plugin greatly extends phpstorm's associative array completion and typing<b

<change-notes><![CDATA[
<ul>
<li>Provide string values completion in in_array($type, [''])</li>
<li>#35 Fix: no not override "Declaration" action name with "viva Denis!"</li>
<li>Add an option to remove unused use-s on save (just a simple nice feature)</li>
<li>Refactor versioned code to Java 10 var-s (I guess I'll run Code Analysis before each production and replace them back to types)</li>
</ul>
]]>
</change-notes>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
import com.intellij.psi.PsiElement;
import com.intellij.util.ProcessingContext;
import com.jetbrains.php.PhpIcons;
import com.jetbrains.php.lang.psi.elements.PhpExpression;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import com.jetbrains.php.lang.psi.elements.*;
import com.jetbrains.php.lang.psi.elements.impl.BinaryExpressionImpl;
import com.jetbrains.php.lang.psi.elements.impl.StringLiteralExpressionImpl;
import org.jetbrains.annotations.NotNull;
Expand All @@ -22,6 +21,7 @@
import org.klesun.deep_assoc_completion.helpers.MultiType;
import org.klesun.deep_assoc_completion.helpers.SearchContext;
import org.klesun.lang.Lang;
import org.klesun.lang.Opt;

import java.util.HashSet;
import java.util.List;
Expand All @@ -46,22 +46,52 @@ private static Lang.L<LookupElement> makeOptions(MultiType mt)
return mt.getStringValues().map(strVal -> makeLookupBase(strVal, "string"));
}

private MultiType resolve(StringLiteralExpression lit, boolean isAutoPopup)
/** $type === '' */
private static Opt<MultiType> resolveEqExpr(StringLiteralExpression lit, FuncCtx funcCtx)
{
var search = new SearchContext()
.setDepth(DeepKeysPvdr.getMaxDepth(isAutoPopup));
var funcCtx = new FuncCtx(search);

return opt(lit)
.map(literal -> literal.getParent()) // BinaryExpressionImpl
.fop(toCast(BinaryExpressionImpl.class))
.fop(bin -> opt(bin.getOperation())
.flt(op -> op.getText().equals("==") || op.getText().equals("===")
|| op.getText().equals("!=") || op.getText().equals("!=="))
|| op.getText().equals("!=") || op.getText().equals("!=="))
.map(op -> bin.getLeftOperand())
.fop(toCast(PhpExpression.class))
.map(exp -> funcCtx.findExprType(exp)))
.def(MultiType.INVALID_PSI);
;
}

/** in_array($type, ['']) */
private static Opt<MultiType> resolveInArray(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 -> L(lst.getParameters()).gat(1)
.flt(arg -> arg.isEquivalentTo(arr)).has()
)
.flt(lst -> opt(lst.getParent())
.fop(toCast(FunctionReference.class))
.map(fun -> fun.getName())
.flt(nme -> nme.equals("in_array")).has())
.fop(lst -> L(lst.getParameters()).gat(0))
.fop(toCast(PhpExpression.class))
.map(str -> funcCtx.findExprType(str)));
}

private MultiType resolve(StringLiteralExpression lit, boolean isAutoPopup)
{
var search = new SearchContext()
.setDepth(DeepKeysPvdr.getMaxDepth(isAutoPopup));
var funcCtx = new FuncCtx(search);

return Opt.fst(list(
resolveEqExpr(lit, funcCtx),
resolveInArray(lit, funcCtx)
)).def(MultiType.INVALID_PSI);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public DeepKeysCbtr()
new UsedKeysPvdr()
);
// string literal after `==` like in `$writeSsrRecords[0]['type'] === ''`
// or in_array('', $types) or in_array($type, ['AIR', ''])
// should suggest possible values of 'type'
this.extend(
CompletionType.BASIC,
Expand Down
12 changes: 12 additions & 0 deletions tests/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,18 @@ private static function testLaravelModelCtorParams()
// not implemented follow
//============================

private static function testInArray()
{
$i = rand(0, 3);
$type = ['AIR', 'CAR', 'HOTEL', 'RAIL'][$i];
if ($type === '') {

}
if (in_array($type, ['CAR', ''])) {

}
}

private static function makeCoolOutfit($materials)
{
return [
Expand Down

0 comments on commit d6883a3

Please sign in to comment.