From fb0c29db67e4ae6bbb320763a60fc57eaa2c68c5 Mon Sep 17 00:00:00 2001 From: Oliver Nybroe Date: Thu, 21 Jan 2021 14:02:28 +0100 Subject: [PATCH] Fixed some smaller issues - closure to arrow function with if statements - type procider and internal assertion errors --- CHANGELOG.md | 9 ++------ .../ClosureToArrowFunctionInspection.kt | 7 ++++++ .../types/HigherOrderTypeProvider.kt | 22 ++++++++++++------- .../ClosureToArrowFunctionInspectionTest.kt | 5 +++++ .../if-closure-to-arrow-function.php | 13 +++++++++++ 5 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 src/test/resources/inspections/ClosureToArrowFunctionInspection/nonMatches/if-closure-to-arrow-function.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fccd93..f33e72b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,22 +12,17 @@ ### Removed ### Fixed +- Fixed closure to arrow function on if statements +- Fixed higher order type provider assertion error ### Security ## [0.3.0] ### Added - Type provider for higher order collection methods and properties -### Changed - -### Deprecated - -### Removed - ### Fixed - Fixed collect on collection matching non strictly. -### Security ## [0.1.0] ### Changed - Tagged first stable release. diff --git a/src/main/kotlin/dev/nybroe/collector/inspections/ClosureToArrowFunctionInspection.kt b/src/main/kotlin/dev/nybroe/collector/inspections/ClosureToArrowFunctionInspection.kt index 8862f1e..40135f2 100644 --- a/src/main/kotlin/dev/nybroe/collector/inspections/ClosureToArrowFunctionInspection.kt +++ b/src/main/kotlin/dev/nybroe/collector/inspections/ClosureToArrowFunctionInspection.kt @@ -8,9 +8,11 @@ import com.intellij.psi.util.elementType import com.jetbrains.php.config.PhpLanguageFeature import com.jetbrains.php.lang.inspections.PhpInspection import com.jetbrains.php.lang.lexer.PhpTokenTypes +import com.jetbrains.php.lang.parser.parsing.statements.IfStatement import com.jetbrains.php.lang.psi.PhpPsiUtil import com.jetbrains.php.lang.psi.elements.Function import com.jetbrains.php.lang.psi.elements.GroupStatement +import com.jetbrains.php.lang.psi.elements.If import com.jetbrains.php.lang.psi.elements.MethodReference import com.jetbrains.php.lang.psi.elements.ParameterList import com.jetbrains.php.lang.psi.elements.PhpUseList @@ -65,6 +67,11 @@ class ClosureToArrowFunctionInspection : PhpInspection() { return } + // And cannot be an if statement. + if (body.statements[0] is If) { + return + } + holder.registerProblem( closure, MyBundle.message("closureToArrowFunctionDescription"), diff --git a/src/main/kotlin/dev/nybroe/collector/types/HigherOrderTypeProvider.kt b/src/main/kotlin/dev/nybroe/collector/types/HigherOrderTypeProvider.kt index 0ea67bf..771262a 100644 --- a/src/main/kotlin/dev/nybroe/collector/types/HigherOrderTypeProvider.kt +++ b/src/main/kotlin/dev/nybroe/collector/types/HigherOrderTypeProvider.kt @@ -53,15 +53,21 @@ class HigherOrderTypeProvider : PhpTypeProvider4 { project: Project ): MutableCollection? { // Decode the expression into a php type - val type = PhpIndex.getInstance(project).completeType( - project, - PhpType().apply { - expression.split('|').filter { it.length != 1 }.forEach { it -> - this.add(it) - } - }, + val type = try { + PhpIndex.getInstance(project).completeType( + project, + PhpType().apply { + expression.split('|').filter { it.length != 1 }.forEach { + this.add(it) + } + }, + null + ) + } catch (e: AssertionError) { null - ) + } + + if (type === null) return null if (!type.isHigherOrderCollection(project)) return null diff --git a/src/test/kotlin/dev/nybroe/collector/inspections/ClosureToArrowFunctionInspectionTest.kt b/src/test/kotlin/dev/nybroe/collector/inspections/ClosureToArrowFunctionInspectionTest.kt index d1e747e..ff432cd 100644 --- a/src/test/kotlin/dev/nybroe/collector/inspections/ClosureToArrowFunctionInspectionTest.kt +++ b/src/test/kotlin/dev/nybroe/collector/inspections/ClosureToArrowFunctionInspectionTest.kt @@ -48,4 +48,9 @@ internal class ClosureToArrowFunctionInspectionTest : InspectionTest() { PhpProjectConfigurationFacade.getInstance(project).languageLevel = PhpLanguageLevel.PHP740 doTest("each-function-call-to-arrow-function-from-variable") } + + fun testClosureWithIfStatementCannotBeChanged() { + PhpProjectConfigurationFacade.getInstance(project).languageLevel = PhpLanguageLevel.PHP740 + doNotMatchTest("if-closure-to-arrow-function") + } } diff --git a/src/test/resources/inspections/ClosureToArrowFunctionInspection/nonMatches/if-closure-to-arrow-function.php b/src/test/resources/inspections/ClosureToArrowFunctionInspection/nonMatches/if-closure-to-arrow-function.php new file mode 100644 index 0000000..a1e63a9 --- /dev/null +++ b/src/test/resources/inspections/ClosureToArrowFunctionInspection/nonMatches/if-closure-to-arrow-function.php @@ -0,0 +1,13 @@ +map(function ($string) { + if ($string) { + return $string; + } +}); \ No newline at end of file