Skip to content

Commit

Permalink
more tests and fix format
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidanio committed Jul 24, 2024
1 parent 7153438 commit 918d2af
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 17 deletions.
65 changes: 65 additions & 0 deletions src/tests/exprtype/exprtype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2201,6 +2201,71 @@ function f() {
runExprTypeTest(t, &exprTypeTestParams{code: code})
}

func TestArrayTypesWithUnpack_Primitives(t *testing.T) {
code := `<?php
function f() {
// with int type
$a = [1, 2, 3];
$b = [0, ...$a];
exprtype($b, "int[]");
// with float type
$a1 = [1.1, 2.2, 3.3];
$b1 = [0.0, ...$a1];
exprtype($b1, "float[]");
// with string type
$a2 = ["a", "b", "c"];
$b2 = ["d", ...$a2];
exprtype($b2, "string[]");
// with bool type
$a3 = [true, false, true];
$b3 = [false, ...$a3];
exprtype($b3, "bool[]");
// mixed types
$a4 = [1, "a", 3.3, true];
$b4 = ["start", ...$a4];
exprtype($b4, "mixed[]");
// with two unpack
$a5 = [1, 2, 3];
$b5 = [4, 5, 6];
$c5 = [...$a5, ...$b5];
exprtype($c5, "int[]");
// with two unpack with different types
$a6 = [1, 2, 3];
$c6 = [...$a6, ...$a2];
exprtype($c6, "mixed[]");
// one unpack
$a7 = [true, false, true];
$b7 = [...$a7];
exprtype($b7, "bool[]");
// with two unpack and just type
$a8 = [1.1, 2.2, 3.3];
$b8 = [4.4, 5.5, 6.6];
$c8 = [...$a8, 7.7, ...$b8];
exprtype($c8, "float[]");
}
`
runExprTypeTest(t, &exprTypeTestParams{code: code})
}

// @see issue1214
func TestArrayTypesWithUnpackNullNegative(t *testing.T) {
linttest.SimpleNegativeTest(t, `<?php
declare(strict_types = 1);
function test() {
$special_items = null;
$_ = [...$special_items];
}`)
}

func TestPropertyTypeHints(t *testing.T) {
code := `<?php
class Too {}
Expand Down
17 changes: 0 additions & 17 deletions src/tests/regression/issue1214_test.go

This file was deleted.

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

mm := make(map[string]struct{}, m.Len())
for typ := range m.m {
//TODO: Remove this todo after creating rule for array unpacking (inspection iterable types)
if typ == "null" {
break
}
Expand All @@ -424,6 +425,7 @@ func (m Map) LazyArrayElemType() Map {
// is a more precise type.
continue
}

mm[UnwrapArrayOf(typ)] = struct{}{}
}
return Map{m: mm, flags: m.flags}
Expand Down

0 comments on commit 918d2af

Please sign in to comment.