Skip to content

Commit

Permalink
Merge pull request #34 from olivernybroe/fix-type-checking
Browse files Browse the repository at this point in the history
Rework type checking so it works for extended classes also
  • Loading branch information
olivernybroe authored Nov 24, 2020
2 parents ebfdb0c + 674c0e1 commit 1b7223b
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 18 deletions.
10 changes: 1 addition & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,9 @@
# collections-intellij Changelog

## [Unreleased]
### Added

### Changed
- Redefine how we determine if a call is a collection call

### Deprecated

### Removed

### Fixed

### Security
## [0.0.1-EAP.10]
### Added
- Added `where(...)->first()` to `firstWhere` refactoring ([#31](https://github.com/olivernybroe/collector-intellij/pull/31))
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

pluginGroup = dev.nybroe.collector
pluginName_ = Collector
pluginVersion = 0.0.1-EAP.10
pluginVersion = 0.0.1-EAP.11
pluginSinceBuild = 202
pluginUntilBuild =

Expand Down
19 changes: 11 additions & 8 deletions src/main/kotlin/dev/nybroe/collector/Util.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package dev.nybroe.collector

import com.intellij.openapi.project.Project
import com.jetbrains.php.PhpIndex
import com.jetbrains.php.lang.psi.elements.Function
import com.jetbrains.php.lang.psi.elements.FunctionReference
import com.jetbrains.php.lang.psi.elements.Method
import com.jetbrains.php.lang.psi.elements.MethodReference
import com.jetbrains.php.lang.psi.elements.PhpClass
import com.jetbrains.php.lang.psi.elements.PhpReference
import com.jetbrains.php.lang.psi.elements.impl.FunctionImpl
import com.jetbrains.php.lang.psi.resolve.types.PhpType
Expand All @@ -18,26 +19,28 @@ private val collectionClasses = listOf(
"\\Illuminate\\Support\\Traits\\EnumeratesValues",
)

val PhpClass.isCollectionClass: Boolean
get() = collectionClasses.contains(this.fqn)
private val collectionType = PhpType().apply {
collectionClasses.forEach { this.add(it) }
}

val Method.isCollectionMethod: Boolean
get() = this.containingClass?.isCollectionClass ?: false
get() = this.containingClass?.type?.isCollection(this.project) ?: false

val MethodReference.isCollectionMethod: Boolean
get() = (this.resolve() as? Method)?.isCollectionMethod ?: false

val Function.returnsCollection: Boolean
get() = this.type.global(this.project).isCollection
get() = this.type.global(this.project).isCollection(this.project)

val FunctionReference.returnsCollection: Boolean
get() = (this.resolve() as? Function)?.returnsCollection ?: false

val PhpReference.isCollectionType: Boolean
get() = this.type.global(this.project).isCollection
get() = this.type.global(this.project).isCollection(this.project)

val Function.isShortArrowFunction: Boolean
get() = FunctionImpl.isShortArrowFunction(this)

val PhpType.isCollection: Boolean
get() = this.types.contains("\\Illuminate\\Support\\Collection")
fun PhpType.isCollection(project: Project): Boolean {
return collectionType.isConvertibleFrom(this, PhpIndex.getInstance(project))
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ internal class CollectFunctionOnCollectionInspectionTest : InspectionTest() {
fun testCollectInCollect() {
doTest("collect_in_collect")
}

fun testCollectOnEloquentCollectVariable() {
doTest("collect_on_eloquent_collect_variable")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

$variable = new \Illuminate\Database\Eloquent\Collection([]);

$sum = $variable->sum();
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

$variable = new \Illuminate\Database\Eloquent\Collection([]);

$sum = <warning descr="Recursive call on collection">collect($variable)</warning>->sum();
16 changes: 16 additions & 0 deletions src/test/resources/stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

namespace Illuminate\Support {
class Collection {
/**
* Create a new collection.
*
* @param mixed $items
*/
public function __construct($items = [])
{
}

/**
* @return Collection
*/
Expand Down Expand Up @@ -85,6 +94,13 @@ public function where($key, $operator, $value = null)
}
}

namespace Illuminate\Database\Eloquent {
class Collection extends \Illuminate\Support\Collection {

}
}


/**
* @param $array
* @return \Illuminate\Support\Collection
Expand Down

0 comments on commit 1b7223b

Please sign in to comment.