Skip to content

Commit

Permalink
Fixed closure to arrow function on collection variables
Browse files Browse the repository at this point in the history
  • Loading branch information
olivernybroe committed Nov 24, 2020
1 parent d72de9c commit 38bc901
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 17 deletions.
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
# collections-intellij Changelog

## [Unreleased]
### Added
### Added

### Changed

### Deprecated
### Deprecated

### Removed
### Removed

### Fixed
### Fixed
- Fixed closure to arrow function not working on collections variables

### Security

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.0.1-EAP.11
pluginVersion = 0.0.1-EAP.12
pluginSinceBuild = 202
pluginUntilBuild =

Expand Down
6 changes: 0 additions & 6 deletions src/main/kotlin/dev/nybroe/collector/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ val Method.isCollectionMethod: Boolean
val MethodReference.isCollectionMethod: Boolean
get() = (this.resolve() as? Method)?.isCollectionMethod ?: false

val Function.returnsCollection: Boolean
get() = this.type.global(this.project).isCollection(this.project)

val FunctionReference.returnsCollection: Boolean
get() = (this.resolve() as? Function)?.returnsCollection ?: false

val PhpReference.isCollectionType: Boolean
get() = this.type.global(this.project).isCollection(this.project)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import com.jetbrains.php.lang.inspections.PhpInspection
import com.jetbrains.php.lang.lexer.PhpTokenTypes
import com.jetbrains.php.lang.psi.PhpPsiUtil
import com.jetbrains.php.lang.psi.elements.Function
import com.jetbrains.php.lang.psi.elements.FunctionReference
import com.jetbrains.php.lang.psi.elements.GroupStatement
import com.jetbrains.php.lang.psi.elements.MethodReference
import com.jetbrains.php.lang.psi.elements.ParameterList
import com.jetbrains.php.lang.psi.elements.PhpUseList
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor
import dev.nybroe.collector.MyBundle
import dev.nybroe.collector.isCollectionMethod
import dev.nybroe.collector.isShortArrowFunction
import dev.nybroe.collector.quickFixes.ClosureToArrowFunctionQuickFix
import dev.nybroe.collector.returnsCollection

class ClosureToArrowFunctionInspection : PhpInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
Expand Down Expand Up @@ -46,10 +46,10 @@ class ClosureToArrowFunctionInspection : PhpInspection() {

// And closure is inside a collection.
val methodReference = PhpPsiUtil
.getParentByCondition<ParameterList>(closure, ParameterList.INSTANCEOF)?.parent ?: return
val functionReference = PhpPsiUtil
.getChildByCondition<FunctionReference>(methodReference, FunctionReference.INSTANCEOF) ?: return
if (!functionReference.returnsCollection) {
.getParentByCondition<ParameterList>(closure, ParameterList.INSTANCEOF)
?.parent as? MethodReference ?: return

if (!methodReference.isCollectionMethod) {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ internal class ClosureToArrowFunctionInspectionTest : InspectionTest() {
PhpProjectConfigurationFacade.getInstance(project).languageLevel = PhpLanguageLevel.PHP740
doNotMatchTest("function-call-to-arrow-function-not-in-collection")
}

fun testEachFunctionCallToArrowFromVariable() {
PhpProjectConfigurationFacade.getInstance(project).languageLevel = PhpLanguageLevel.PHP740
doTest("each-function-call-to-arrow-function-from-variable")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

$array = collect([
'one',
'two',
'tree'
]);

$array->each(fn($item) => doAction($item));
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$array = collect([
'one',
'two',
'tree'
]);

$array->each(<weak_warning descr="Closure can be converted to arrow function">function ($item) {
doAction($item);
}</weak_warning>);

0 comments on commit 38bc901

Please sign in to comment.