Skip to content

Commit

Permalink
Merge pull request #9 from olivernybroe/feat-array-map
Browse files Browse the repository at this point in the history
`array_map` to collection
  • Loading branch information
olivernybroe authored Oct 25, 2020
2 parents 0235e20 + a5e2412 commit ec5b5f1
Show file tree
Hide file tree
Showing 26 changed files with 222 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
### Added
- Initial scaffold created from [IntelliJ Platform Plugin Template](https://github.com/JetBrains/intellij-platform-plugin-template)
- Added foreach to collection refactoring
- Added array map to collection refactoring
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ tasks {
// Set the compatibility versions to 1.8
withType<JavaCompile> {
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
targetCompatibility = "11"
}
listOf("compileKotlin", "compileTestKotlin").forEach {
getByName<KotlinCompile>(it) {
Expand All @@ -83,7 +83,7 @@ tasks {
}

withType<Detekt> {
jvmTarget = "1.8"
jvmTarget = "11"
}

patchPluginXml {
Expand Down
9 changes: 5 additions & 4 deletions detekt-config.yml
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
7 changes: 7 additions & 0 deletions src/main/kotlin/dev/nybroe/collector/Util.kt
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
}
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()
)
}
}
}
}
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()"
)
)
}
}
11 changes: 11 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,16 @@
key="foreachToCollectionDisplayName"
implementationClass="dev.nybroe.collector.inspections.ForeachToCollectionInspection"
/>

<localInspection
language="PHP"
groupPath="PHP"
groupKey="collectionGroupName"
shortName="ArrayMapToCollectionInspection"
enabledByDefault="true"
bundle="messages.MyBundle"
key="arrayMapToCollectionDisplayName"
implementationClass="dev.nybroe.collector.inspections.ArrayMapToCollectionInspection"
/>
</extensions>
</idea-plugin>
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>
5 changes: 4 additions & 1 deletion src/main/resources/messages/MyBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ name=Collector
collectionGroupName=Collections

foreachToCollectionDisplayName='foreach' used instead of 'Collection'
foreachToCollectionDescription='foreach' used instead of 'Collection'
foreachToCollectionDescription='foreach' used instead of 'Collection'

arrayMapToCollectionDisplayName='array_map' used instead of 'Collection'
arrayMapToCollectionDescription='array_map' used instead of 'Collection'
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")
}
}
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")
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package dev.nybroe.collector.inspections

import com.intellij.codeInspection.InspectionProfileEntry
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import dev.nybroe.collector.quickFixes.ForeachToCollectionQuickFix
import junit.framework.TestCase

internal class ForeachStatementInspectionTest : BasePlatformTestCase() {
override fun getTestDataPath(): String {
return "src/test/resources/inspections/ForeachStatementInspection"
}
internal abstract class InspectionTest : BasePlatformTestCase() {
protected abstract fun defaultInspection(): InspectionProfileEntry
protected abstract fun defaultAction(): String

protected fun doTest(testName: String) = doTest(testName, defaultInspection(), defaultAction())

/**
* Given the name of a test file, runs comparing references inspection quick fix and tests
Expand All @@ -16,30 +17,22 @@ internal class ForeachStatementInspectionTest : BasePlatformTestCase() {
*
* @param testName The name of the test file before comparing references inspection.
*/
private fun doTest(testName: String) {
protected fun doTest(testName: String, inspection: InspectionProfileEntry, action: String) {
// Initialize the test based on the testData file
myFixture.configureByFile("$testName.php")

// Initialize the inspection and get a list of highlighted
myFixture.enableInspections(ForeachToCollectionInspection())
myFixture.enableInspections(inspection)
val highlightInfos = myFixture.doHighlighting()
TestCase.assertFalse(highlightInfos.isEmpty())

// Get the quick fix action for comparing references inspection and apply it to the file
val action = myFixture.findSingleIntention(ForeachToCollectionQuickFix.QUICK_FIX_NAME)
TestCase.assertNotNull(action)
myFixture.launchAction(action)
myFixture.findSingleIntention(action).also {
assertNotNull(it)
myFixture.launchAction(it)
}

// Verify the results
myFixture.checkResultByFile("$testName.after.php")
}

fun testForeachValueOnly() {
doTest("foreach-value-only")
}

fun testForeachKeyValue() {
doTest("foreach-key-value")
}

fun testForeachOuterScopeVariable() {
doTest("foreach-outer-scope-variable")
}
}
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();
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]);
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();
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);
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();
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);
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);
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();

0 comments on commit ec5b5f1

Please sign in to comment.