Skip to content

Commit

Permalink
Provide type to idea when var is assignment from an array key
Browse files Browse the repository at this point in the history
  • Loading branch information
klesun committed Sep 16, 2017
1 parent 516d93a commit 450c6a4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
10 changes: 5 additions & 5 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<vendor email="safronevev@gmail.com" url="http://midiana.lv/entry/phpstorm-deep-keys">Klesun</vendor>

<description><![CDATA[
This plugin greatly extends phpstorm's associative array completion and typing<br/>
<ul>
<li>Associative array auto-completion inferred from other functions (in other files)</li>
<li>Go To Declaration</li>
<li>Specify array keys and reference functions in phpdoc</li>
<li>Preserve type info of object placed in an associative array</li>
<li>Associative array auto-completion inferred from other functions (in other files).</li>
<li>Go To Declaration.</li>
<li>Specify array keys and reference functions in phpdoc.</li>
<li>Preserve type info of object placed in an associative array.</li>
</ul>
This plugin greatly extends phpstorm's associative array completion and typing<br/>
<br clear="all"/>
<img style="width: 250px; height: auto;" src="https://user-images.githubusercontent.com/5202330/30349574-179c80e2-981c-11e7-9783-c7b9e6076c5c.png"/>
Expand Down
8 changes: 7 additions & 1 deletion src/org/klesun/deep_keys/entry/AssocTypePvdr.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.jetbrains.php.lang.psi.elements.PhpExpression;
import com.jetbrains.php.lang.psi.elements.PhpNamedElement;
import com.jetbrains.php.lang.psi.elements.impl.ArrayAccessExpressionImpl;
import com.jetbrains.php.lang.psi.elements.impl.AssignmentExpressionImpl;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider3;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -38,9 +39,14 @@ public PhpType getType(PsiElement psi)
boolean isFieldAcc = PlatformPatterns.psiElement(PhpElementTypes.ARRAY_ACCESS_EXPRESSION)
.withParent(PlatformPatterns.psiElement(PhpElementTypes.FIELD_REFERENCE))
.accepts(psi);
boolean isAssVal = Tls.cast(ArrayAccessExpressionImpl.class, psi)
.map(acc -> acc.getParent())
.fap(toCast(AssignmentExpressionImpl.class))
.map(ass -> psi.isEquivalentTo(ass.getValue()))
.def(false);

// we will calculate type only for method or property access
if (!isMethCall && !isFieldAcc){
if (!isMethCall && !isFieldAcc && !isAssVal){
return null;
}

Expand Down
16 changes: 16 additions & 0 deletions tests/src/DeepTest/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,22 @@ public static function provideObjectInAKey()
return $list;
}

public function provideAssignObjectFromArray()
{
$list = [];

$rpcResult = [
'success' => true,
'result' => new PersonStorage(),
];
$stor = $rpcResult['result'];
$rpcResult['result'] = [];
$stor->mainPerson[''];
$list[] = [$stor->mainPerson, ['name' => [], 'age' => []]];

return $list;
}

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

0 comments on commit 450c6a4

Please sign in to comment.