-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve return types for array_fill_keys and array_combine
- Loading branch information
Showing
6 changed files
with
257 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace ArrayCombinePHP7; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo | ||
{ | ||
/** @phpstan-return 'foo' */ | ||
public function __toString(): string | ||
{ | ||
return 'foo'; | ||
} | ||
} | ||
|
||
class Bar | ||
{ | ||
public function __toString(): string | ||
{ | ||
return 'bar'; | ||
} | ||
} | ||
|
||
class Baz {} | ||
|
||
function withBoolKey(): void | ||
{ | ||
$a = [true, 'red', 'yellow']; | ||
$b = ['avocado', 'apple', 'banana']; | ||
|
||
assertType("array{1: 'avocado', red: 'apple', yellow: 'banana'}", array_combine($a, $b)); | ||
|
||
$c = [false, 'red', 'yellow']; | ||
$d = ['avocado', 'apple', 'banana']; | ||
|
||
assertType("array{: 'avocado', red: 'apple', yellow: 'banana'}", array_combine($c, $d)); | ||
} | ||
|
||
function withFloatKey(): void | ||
{ | ||
$a = [1.5, 'red', 'yellow']; | ||
$b = ['avocado', 'apple', 'banana']; | ||
|
||
assertType("array{1.5: 'avocado', red: 'apple', yellow: 'banana'}", array_combine($a, $b)); | ||
} | ||
|
||
function withIntegerKey(): void | ||
{ | ||
$a = [1, 2, 3]; | ||
$b = ['avocado', 'apple', 'banana']; | ||
|
||
assertType("array{1: 'avocado', 2: 'apple', 3: 'banana'}", array_combine($a, $b)); | ||
} | ||
|
||
function withNumericStringKey(): void | ||
{ | ||
$a = ["1", "2", "3"]; | ||
$b = ['avocado', 'apple', 'banana']; | ||
|
||
assertType("array{1: 'avocado', 2: 'apple', 3: 'banana'}", array_combine($a, $b)); | ||
} | ||
|
||
function withObjectKey() : void | ||
{ | ||
$a = [new Foo, 'red', 'yellow']; | ||
$b = ['avocado', 'apple', 'banana']; | ||
|
||
assertType("array{foo: 'avocado', red: 'apple', yellow: 'banana'}", array_combine($a, $b)); | ||
assertType("non-empty-array<string, 'apple'|'avocado'|'banana'>|false", array_combine([new Bar, 'red', 'yellow'], $b)); | ||
assertType("*NEVER*", array_combine([new Baz, 'red', 'yellow'], $b)); | ||
} | ||
|
||
/** | ||
* @param non-empty-array<int, 'foo'|'bar'|'baz'> $a | ||
* @param non-empty-array<int, 'apple'|'avocado'|'banana'> $b | ||
*/ | ||
function withNonEmptyArray(array $a, array $b): void | ||
{ | ||
assertType("non-empty-array<'bar'|'baz'|'foo', 'apple'|'avocado'|'banana'>", array_combine($a, $b)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace ArrayCombinePHP8; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo | ||
{ | ||
/** @phpstan-return 'foo' */ | ||
public function __toString(): string | ||
{ | ||
return 'foo'; | ||
} | ||
} | ||
|
||
class Bar | ||
{ | ||
public function __toString(): string | ||
{ | ||
return 'bar'; | ||
} | ||
} | ||
|
||
class Baz {} | ||
|
||
function withBoolKey(): void | ||
{ | ||
$a = [true, 'red', 'yellow']; | ||
$b = ['avocado', 'apple', 'banana']; | ||
|
||
assertType("array{1: 'avocado', red: 'apple', yellow: 'banana'}", array_combine($a, $b)); | ||
|
||
$c = [false, 'red', 'yellow']; | ||
$d = ['avocado', 'apple', 'banana']; | ||
|
||
assertType("array{: 'avocado', red: 'apple', yellow: 'banana'}", array_combine($c, $d)); | ||
} | ||
|
||
function withFloatKey(): void | ||
{ | ||
$a = [1.5, 'red', 'yellow']; | ||
$b = ['avocado', 'apple', 'banana']; | ||
|
||
assertType("array{1.5: 'avocado', red: 'apple', yellow: 'banana'}", array_combine($a, $b)); | ||
} | ||
|
||
function withIntegerKey(): void | ||
{ | ||
$a = [1, 2, 3]; | ||
$b = ['avocado', 'apple', 'banana']; | ||
|
||
assertType("array{1: 'avocado', 2: 'apple', 3: 'banana'}", array_combine($a, $b)); | ||
} | ||
|
||
function withNumericStringKey(): void | ||
{ | ||
$a = ["1", "2", "3"]; | ||
$b = ['avocado', 'apple', 'banana']; | ||
|
||
assertType("array{1: 'avocado', 2: 'apple', 3: 'banana'}", array_combine($a, $b)); | ||
} | ||
|
||
function withObjectKey() : void | ||
{ | ||
$a = [new Foo, 'red', 'yellow']; | ||
$b = ['avocado', 'apple', 'banana']; | ||
|
||
assertType("array{foo: 'avocado', red: 'apple', yellow: 'banana'}", array_combine($a, $b)); | ||
assertType("non-empty-array<string, 'apple'|'avocado'|'banana'>", array_combine([new Bar, 'red', 'yellow'], $b)); | ||
assertType("*NEVER*", array_combine([new Baz, 'red', 'yellow'], $b)); | ||
} | ||
|
||
/** | ||
* @param non-empty-array<int, 'foo'|'bar'|'baz'> $a | ||
* @param non-empty-array<int, 'apple'|'avocado'|'banana'> $b | ||
*/ | ||
function withNonEmptyArray(array $a, array $b): void | ||
{ | ||
assertType("non-empty-array<'bar'|'baz'|'foo', 'apple'|'avocado'|'banana'>", array_combine($a, $b)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace ArrayFillKeys; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo | ||
{ | ||
/** @phpstan-return 'foo' */ | ||
public function __toString(): string | ||
{ | ||
return 'foo'; | ||
} | ||
} | ||
|
||
class Bar | ||
{ | ||
public function __toString(): string | ||
{ | ||
return 'bar'; | ||
} | ||
} | ||
|
||
class Baz {} | ||
|
||
function withBoolKey() : array | ||
{ | ||
assertType("array{1: 'b'}", array_fill_keys([true], 'b')); | ||
assertType("array{: 'b'}", array_fill_keys([false], 'b')); | ||
} | ||
|
||
function withFloatKey() : array | ||
{ | ||
assertType("array{1.5: 'b'}", array_fill_keys([1.5], 'b')); | ||
} | ||
|
||
function withIntegerKey() : array | ||
{ | ||
assertType("array{99: 'b'}", array_fill_keys([99], 'b')); | ||
} | ||
|
||
function withNumericStringKey() : array | ||
{ | ||
assertType("array{999: 'b'}", array_fill_keys(["999"], 'b')); | ||
} | ||
|
||
function withObjectKey() : array | ||
{ | ||
assertType("array{foo: 'b'}", array_fill_keys([new Foo()], 'b')); | ||
assertType("non-empty-array<string, 'b'>", array_fill_keys([new Bar()], 'b')); | ||
assertType("*NEVER*", array_fill_keys([new Baz()], 'b')); | ||
} |