Skip to content

Commit

Permalink
Fixed some smaller issues
Browse files Browse the repository at this point in the history
- closure to arrow function with if statements
- type procider and internal assertion errors
  • Loading branch information
Oliver Nybroe committed Jan 21, 2021
1 parent efaf265 commit fb0c29d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
9 changes: 2 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,21 @@ class HigherOrderTypeProvider : PhpTypeProvider4 {
project: Project
): MutableCollection<out PhpNamedElement>? {
// 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$array = [
'one',
'two',
'tree'
];

collect($array)->map(function ($string) {
if ($string) {
return $string;
}
});

0 comments on commit fb0c29d

Please sign in to comment.