Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
olivernybroe committed Oct 25, 2020
1 parent c3ade39 commit a5e2412
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 34 deletions.
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
25 changes: 1 addition & 24 deletions src/main/kotlin/dev/nybroe/collector/Util.kt
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
package dev.nybroe.collector

import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.PsiElement
import com.jetbrains.php.lang.PhpLangUtil
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.StringLiteralExpression
import com.jetbrains.php.lang.psi.elements.Variable

fun FunctionReference.isGlobalFunctionCallWithName(name: String): Boolean {
return PhpLangUtil.isGlobalNamespaceFQN(this.namespaceName) && StringUtil.equals(this.name, name)
return this.name == name
}


fun PsiElement.canBeCalledAsLambda(): Boolean {
return this is Variable || this is StringLiteralExpression
}


fun PsiElement.resolveFunction(): Function? {
if (this.canBeCalledAsLambda()) {
val references = this.references
if (references.size == 1) {
return references[0].resolve() as? Function
}
}
return PhpPsiUtil.getChildByCondition<PsiElement>(this, Function.INSTANCEOF) as Function?
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ class ArrayMapToCollectionInspection : PhpInspection() {
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.jetbrains.php.lang.psi.elements.FunctionReference

class ArrayMapToCollectionQuickFix : LocalQuickFix {
companion object {
const val QUICK_FIX_NAME = "Refactor array_map to collection"
const val QUICK_FIX_NAME = "Refactor 'array_map' to collection"
}

override fun getFamilyName(): String {
Expand All @@ -30,10 +30,10 @@ class ArrayMapToCollectionQuickFix : LocalQuickFix {
}

descriptor.psiElement.replace(
PhpPsiElementFactory.createStatement(
PhpPsiElementFactory.createMethodReference(
project,
"collect(${parameters[1].text})->map(${parameters[0].text})->all()"
)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ internal class ArrayMapToCollectionInspectionTest : InspectionTest() {
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,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 a5e2412

Please sign in to comment.