Skip to content

Commit

Permalink
linter: fixed unboxing from null for arrays (#1215)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidanio authored Jul 25, 2024
1 parent 19e966b commit fa7b184
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/solver/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ func mixedType() map[string]struct{} {
}

// resolveType resolves function calls, method calls and global variables.
// curStaticClass is current class name (if inside the class, otherwise "")
//
// curStaticClass is current class name (if inside the class, otherwise "")
func resolveType(info *meta.Info, curStaticClass, typ string, visitedMap ResolverMap) (result map[string]struct{}) {
r := resolver{info: info, visited: visitedMap}
return r.resolveType(curStaticClass, typ)
}

// ResolveTypes resolves function calls, method calls and global variables.
// curStaticClass is current class name (if inside the class, otherwise "")
//
// curStaticClass is current class name (if inside the class, otherwise "")
func ResolveTypes(info *meta.Info, curStaticClass string, m types.Map, visitedMap ResolverMap) map[string]struct{} {
r := resolver{info: info, visited: visitedMap}
return r.resolveTypes(curStaticClass, m)
Expand Down Expand Up @@ -76,7 +78,7 @@ func (r *resolver) resolveTypeNoLateStaticBinding(class, typ string) map[string]
return result
}

if typ == "" || typ[0] >= types.WMax {
if types.IsAfterWMaxed(typ) {
return identityType(typ)
}

Expand Down
39 changes: 39 additions & 0 deletions src/tests/exprtype/exprtype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2201,6 +2201,45 @@ function f() {
runExprTypeTest(t, &exprTypeTestParams{code: code})
}

func TestArrayTypesWithUnpackPrimitives(t *testing.T) {
code := `<?php
function f() {
// with int type
$a = 1;
$b = [...$a];
exprtype($b, "mixed");
// with float type
$a1 = 1.1;
$b1 = [...$a1];
exprtype($b1, "mixed");
// with string type
$a2 = "a";
$b2 = [...$a2];
exprtype($b2, "mixed");
// with bool type
$a3 = true;
$b3 = [...$a3];
exprtype($b3, "mixed");
// null
$a4 = null;
$b4 = [...$a4];
exprtype($b4, "mixed");
// class
class Foo {}
$a5 = new Foo();
$b5 = [...$a5];
exprtype($b5, "mixed");
}
`
runExprTypeTest(t, &exprTypeTestParams{code: code})
}

func TestPropertyTypeHints(t *testing.T) {
code := `<?php
class Too {}
Expand Down
6 changes: 5 additions & 1 deletion src/types/lazy.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,12 @@ func FormatSimpleType(s string) (res string) {
return s
}

func IsAfterWMaxed(typ string) (res bool) {
return typ == "" || typ[0] >= WMax
}

func FormatType(s string) (res string) {
if s == "" || s[0] >= WMax {
if IsAfterWMaxed(s) {
return FormatSimpleType(s)
}

Expand Down
5 changes: 5 additions & 0 deletions src/types/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,11 @@ func (m Map) LazyArrayElemType() Map {

mm := make(map[string]struct{}, m.Len())
for typ := range m.m {
// TODO: https://github.com/VKCOM/noverify/issues/1227
if IsAfterWMaxed(typ) {
break
}

if typ == "empty_array" {
// If the type contains only empty_array,
// then we resolve its element as mixed.
Expand Down

0 comments on commit fa7b184

Please sign in to comment.