generated from JetBrains/intellij-platform-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from olivernybroe/feat-array-map
`array_map` to collection
- Loading branch information
Showing
26 changed files
with
222 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
# Default detekt configuration: | ||
# https://github.com/detekt/detekt/blob/master/detekt-core/src/main/resources/default-detekt-config.yml | ||
# https://github.com/detekt/detekt/blob/master/detekt-cli/src/main/resources/default-detekt-config.yml | ||
|
||
formatting: | ||
Indentation: | ||
continuationIndentSize: 8 | ||
ParameterListWrapping: | ||
indentSize: 8 | ||
active: true | ||
style: | ||
ReturnCount: | ||
max: 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package dev.nybroe.collector | ||
|
||
import com.jetbrains.php.lang.psi.elements.FunctionReference | ||
|
||
fun FunctionReference.isGlobalFunctionCallWithName(name: String): Boolean { | ||
return this.name == name | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/kotlin/dev/nybroe/collector/inspections/ArrayMapToCollectionInspection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package dev.nybroe.collector.inspections | ||
|
||
import com.intellij.codeInspection.ProblemHighlightType | ||
import com.intellij.codeInspection.ProblemsHolder | ||
import com.intellij.psi.PsiElementVisitor | ||
import com.jetbrains.php.lang.inspections.PhpInspection | ||
import com.jetbrains.php.lang.psi.elements.FunctionReference | ||
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor | ||
import dev.nybroe.collector.MyBundle | ||
import dev.nybroe.collector.isGlobalFunctionCallWithName | ||
import dev.nybroe.collector.quickFixes.ArrayMapToCollectionQuickFix | ||
|
||
class ArrayMapToCollectionInspection : PhpInspection() { | ||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { | ||
return object : PhpElementVisitor() { | ||
override fun visitPhpFunctionCall(reference: FunctionReference) { | ||
if (!reference.isGlobalFunctionCallWithName("array_map")) { | ||
return | ||
} | ||
|
||
holder.registerProblem( | ||
reference, | ||
MyBundle.message("arrayMapToCollectionDescription"), | ||
ProblemHighlightType.INFORMATION, | ||
ArrayMapToCollectionQuickFix() | ||
) | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/dev/nybroe/collector/quickFixes/ArrayMapToCollectionQuickFix.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package dev.nybroe.collector.quickFixes | ||
|
||
import com.intellij.codeInspection.LocalQuickFix | ||
import com.intellij.codeInspection.ProblemDescriptor | ||
import com.intellij.openapi.project.Project | ||
import com.jetbrains.php.lang.psi.PhpPsiElementFactory | ||
import com.jetbrains.php.lang.psi.elements.FunctionReference | ||
|
||
class ArrayMapToCollectionQuickFix : LocalQuickFix { | ||
companion object { | ||
const val QUICK_FIX_NAME = "Refactor 'array_map' to collection" | ||
} | ||
|
||
override fun getFamilyName(): String { | ||
return QUICK_FIX_NAME | ||
} | ||
|
||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) { | ||
val arrayMap = descriptor.psiElement | ||
|
||
if (arrayMap !is FunctionReference) { | ||
return | ||
} | ||
|
||
val parameters = arrayMap.parameters | ||
|
||
// Expects two parameters, a function and the data. | ||
if (parameters.count() != 2) { | ||
return | ||
} | ||
|
||
descriptor.psiElement.replace( | ||
PhpPsiElementFactory.createMethodReference( | ||
project, | ||
"collect(${parameters[1].text})->map(${parameters[0].text})->all()" | ||
) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/resources/inspectionDescriptions/ArrayMapToCollectionInspection.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
<html lang="en"> | ||
<body> | ||
'array_map' used instead of 'Collection' | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/test/kotlin/dev/nybroe/collector/inspections/ArrayMapToCollectionInspectionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package dev.nybroe.collector.inspections | ||
|
||
import com.intellij.codeInspection.InspectionProfileEntry | ||
import dev.nybroe.collector.quickFixes.ArrayMapToCollectionQuickFix | ||
|
||
internal class ArrayMapToCollectionInspectionTest : InspectionTest() { | ||
override fun defaultInspection(): InspectionProfileEntry = ArrayMapToCollectionInspection() | ||
override fun defaultAction(): String = ArrayMapToCollectionQuickFix.QUICK_FIX_NAME | ||
override fun getTestDataPath(): String = "src/test/resources/inspections/ArrayMapToCollectionInspection" | ||
|
||
fun testCallbackAndVariable() { | ||
doTest("array_map-callback-and-variable") | ||
} | ||
|
||
fun testCallbackAndArray() { | ||
doTest("array_map-callback-and-array") | ||
} | ||
|
||
fun testInNamespace() { | ||
doTest("array_map-in-namespace") | ||
} | ||
|
||
fun testNamespacedArrayMap() { | ||
doTest("array_map-in-namespace") | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/test/kotlin/dev/nybroe/collector/inspections/ForeachToCollectionInspectionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package dev.nybroe.collector.inspections | ||
|
||
import com.intellij.codeInspection.InspectionProfileEntry | ||
import dev.nybroe.collector.quickFixes.ForeachToCollectionQuickFix | ||
|
||
internal class ForeachToCollectionInspectionTest : InspectionTest() { | ||
override fun defaultInspection(): InspectionProfileEntry = ForeachToCollectionInspection() | ||
override fun defaultAction(): String = ForeachToCollectionQuickFix.QUICK_FIX_NAME | ||
override fun getTestDataPath(): String = "src/test/resources/inspections/ForeachToCollectionInspection" | ||
|
||
fun testForeachValueOnly() { | ||
doTest("foreach-value-only") | ||
} | ||
|
||
fun testForeachKeyValue() { | ||
doTest("foreach-key-value") | ||
} | ||
|
||
fun testForeachOuterScopeVariable() { | ||
doTest("foreach-outer-scope-variable") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...sources/inspections/ArrayMapToCollectionInspection/array_map-callback-and-array.after.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
$b = collect([1, 2, 3, 4, 5])->map(function ($n) { | ||
return ($n * $n * $n); | ||
})->all(); |
5 changes: 5 additions & 0 deletions
5
...est/resources/inspections/ArrayMapToCollectionInspection/array_map-callback-and-array.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
$b = <caret>array_map(function($n) { | ||
return ($n * $n * $n); | ||
}, [1, 2, 3, 4, 5]); |
6 changes: 6 additions & 0 deletions
6
...rces/inspections/ArrayMapToCollectionInspection/array_map-callback-and-variable.after.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
$a = [1, 2, 3, 4, 5]; | ||
$b = collect($a)->map(function ($n) { | ||
return ($n * $n * $n); | ||
})->all(); |
6 changes: 6 additions & 0 deletions
6
.../resources/inspections/ArrayMapToCollectionInspection/array_map-callback-and-variable.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
$a = [1, 2, 3, 4, 5]; | ||
$b = <caret>array_map(function($n) { | ||
return ($n * $n * $n); | ||
}, $a); |
8 changes: 8 additions & 0 deletions
8
...est/resources/inspections/ArrayMapToCollectionInspection/array_map-in-namespace.after.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace MyAwesomeName\Good; | ||
|
||
$a = [1, 2, 3, 4, 5]; | ||
$b = collect($a)->map(function ($n) { | ||
return ($n * $n * $n); | ||
})->all(); |
8 changes: 8 additions & 0 deletions
8
src/test/resources/inspections/ArrayMapToCollectionInspection/array_map-in-namespace.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace MyAwesomeName\Good; | ||
|
||
$a = [1, 2, 3, 4, 5]; | ||
$b = <caret>array_map(function($n) { | ||
return ($n * $n * $n); | ||
}, $a); |
8 changes: 8 additions & 0 deletions
8
src/test/resources/inspections/ArrayMapToCollectionInspection/namespaced_array_map.after.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace MyAwesomeName\Good; | ||
|
||
$a = [1, 2, 3, 4, 5]; | ||
$b = <caret>\array_map(function($n) { | ||
return ($n * $n * $n); | ||
}, $a); |
8 changes: 8 additions & 0 deletions
8
src/test/resources/inspections/ArrayMapToCollectionInspection/namespaced_array_map.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace MyAwesomeName\Good; | ||
|
||
$a = [1, 2, 3, 4, 5]; | ||
$b = collect($a)->map(function ($n) { | ||
return ($n * $n * $n); | ||
})->all(); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.