Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

null safety check for instances when calling functions #62

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions src/main/kotlin/com/vk/kphpstorm/exphptype/ExPhpTypeInstance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ class ExPhpTypeInstance(val fqn: String) : ExPhpType {
override fun toString() = fqn

override fun toHumanReadable(expr: PhpPsiElement) =
if (fqn.startsWith('\\')) PhpCodeInsightUtil.createQualifiedName(PhpCodeInsightUtil.findScopeForUseOperator(expr)!!, fqn)
else fqn
if (fqn.startsWith('\\')) PhpCodeInsightUtil.createQualifiedName(
PhpCodeInsightUtil.findScopeForUseOperator(expr)!!,
fqn
)
else fqn

override fun toPhpType(): PhpType {
return PhpType().add(fqn)
Expand All @@ -28,11 +31,43 @@ class ExPhpTypeInstance(val fqn: String) : ExPhpType {
override fun instantiateTemplate(nameMap: Map<String, ExPhpType>): ExPhpType {
return nameMap[fqn] ?: this
}
private fun canBeAssigned(l: ExPhpTypeInstance, r: ExPhpTypeInstance) = with(ExPhpType.Companion) {
when {
l === ExPhpTypeInstance(KphpPrimitiveTypes.OBJECT) -> r === ExPhpTypeInstance(KphpPrimitiveTypes.OBJECT)
else -> false // not supposed to happen
}
}

override fun isAssignableFrom(rhs: ExPhpType, project: Project): Boolean = when (rhs) {
is ExPhpTypeAny -> true
is ExPhpTypePipe -> rhs.isAssignableTo(this, project)
is ExPhpTypeNullable -> isAssignableFrom(rhs.inner, project)
is ExPhpTypeNullable -> {
/* if(this === ExPhpType.OBJECT){

}
rhs === ExPhpType.OBJECT || rhs === ExPhpType.NULL*/
/*ExPhpType.OBJECT
val OBJECT = ExPhpTypeInstance(KphpPrimitiveTypes.OBJECT)
rhs === OBJECT || rhs === ExPhpType.NULL*/
/*if(this as ExPhpTypeNullable && rhs is ExPhpTypeNullable){
false
}*/
false
/*val t = canBeAssigned(this, ExPhpTypeInstance(KphpPrimitiveTypes.NULL))
if( this === ExPhpTypeInstance(KphpPrimitiveTypes.OBJECT)){
rhs === ExPhpTypeInstance(KphpPrimitiveTypes.NULL)
}else{
isAssignableFrom(rhs.inner, project)
}*/
/* canBeAssigned(this, ExPhpTypeInstance(KphpPrimitiveTypes.NULL))
val t = this.fqn
if (rhs.toString() != fqn) {
false
} else {
isAssignableFrom(rhs.inner, project)
}*/
}

is ExPhpTypePrimitive -> rhs === ExPhpType.NULL || rhs === ExPhpType.OBJECT

// rhs can be assigned if: rhs == lhs or rhs is child of lhs (no matter, lhs is interface or class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ExPhpTypeNullable(val inner: ExPhpType) : ExPhpType {
is ExPhpTypePipe -> rhs.isAssignableTo(this, project)
is ExPhpTypeNullable -> inner.isAssignableFrom(rhs.inner, project)
is ExPhpTypePrimitive -> rhs === ExPhpType.NULL || inner.isAssignableFrom(rhs, project)
is ExPhpTypeInstance -> false
else -> inner.isAssignableFrom(rhs, project)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class KphpParameterTypeMismatchInspection : PhpInspection() {
val callType = PsiToExPhpType.getTypeOfExpr(callParam, project) ?: continue
val argType = PsiToExPhpType.getArgumentDeclaredType(fArg, project) ?: continue

val test = !argType.isAssignableFrom(callType, project)
if (!argType.isAssignableFrom(callType, project) && needsReporting(f)) {
holder.registerProblem(callParam, "Can't pass '${callType.toHumanReadable(call)}' to '${argType.toHumanReadable(call)}' \$${fArg.name}", ProblemHighlightType.GENERIC_ERROR_OR_WARNING)
}
Expand Down
Loading