Skip to content

Commit

Permalink
Merge pull request #40 from olivernybroe/fix-recursive-call-non-stright
Browse files Browse the repository at this point in the history
Fix Collect on collection recursive call matching non strictly.
  • Loading branch information
olivernybroe authored Dec 20, 2020
2 parents ba1fbc4 + 72ae9cf commit 85dc2b2
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
### Removed

### Fixed
- Fixed collect on collection matching non strictly.

### Security
## [0.1.0]
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

pluginGroup = dev.nybroe.collector
pluginName_ = Collector
pluginVersion = 0.2.0.EAP.1
pluginVersion = 0.2.0.EAP.2
pluginSinceBuild = 202
pluginUntilBuild =

Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/dev/nybroe/collector/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ val MethodReference.isCollectionMethod: Boolean
val PhpReference.isCollectionType: Boolean
get() = this.type.global(this.project).isCollection(this.project)

val PhpReference.isCollectionTypeStrict: Boolean
get() = this.type.global(this.project).isCollectionStrict(this.project)

val Function.isShortArrowFunction: Boolean
get() = FunctionImpl.isShortArrowFunction(this)

Expand All @@ -52,6 +55,10 @@ fun PhpType.isCollection(project: Project): Boolean {
)
}

fun PhpType.isCollectionStrict(project: Project): Boolean {
return this.types.all { PhpType().add(it).isCollection(project) }
}

fun PhpType.isHigherOrderCollection(project: Project): Boolean {
val filteredType = this.filterMixed()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.jetbrains.php.lang.psi.elements.FunctionReference
import com.jetbrains.php.lang.psi.elements.PhpReference
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor
import dev.nybroe.collector.MyBundle
import dev.nybroe.collector.isCollectionType
import dev.nybroe.collector.isCollectionTypeStrict
import dev.nybroe.collector.isGlobalFunctionCallWithName
import dev.nybroe.collector.quickFixes.NestedCollectionQuickFix

Expand All @@ -26,7 +26,7 @@ class CollectFunctionInCollectionInspection : PhpInspection() {
return
}

if ((reference.parameters[0].reference as? PhpReference)?.isCollectionType != true) {
if ((reference.parameters[0].reference as? PhpReference)?.isCollectionTypeStrict != true) {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ internal class CollectFunctionOnCollectionInspectionTest : InspectionTest() {
fun testCollectOnMixedVariable() {
doNotMatchTest("collect_on_mixed_variable")
}

fun testCollectOnIterableVariable() {
doNotMatchTest("collect_on_iterable_variable")
}

fun testCollectOnUnionCollectoinType() {
doNotMatchTest("collect_on_union_collection_type")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ internal abstract class InspectionTest : BaseCollectTestCase() {
myFixture.enableInspections(inspection)
myFixture.testHighlighting(true, false, true, file.virtualFile)

myFixture.getAllQuickFixes().forEach { myFixture.launchAction(it) }
assertEmpty(myFixture.getAllQuickFixes())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

class IterableVariable {

}

/** @return IterableVariable[] */
function getVariable(): iterable
{
return [];
}

collect(getVariable());
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

if(rand(1, 10)<5){
$bar = [];
}else{
$bar = new \Illuminate\Support\Collection();
}
collect($bar);

0 comments on commit 85dc2b2

Please sign in to comment.