Skip to content

Commit

Permalink
Fix readability for object-like types when running psalter (#1708)
Browse files Browse the repository at this point in the history
* chore: update tests

* fix: better readability for object-like types

* test: add tests for nested array return type

* Improve spacing
  • Loading branch information
Kocal authored and muglug committed Jun 16, 2019
1 parent 7fc11af commit 37d719e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/Psalm/Type/Atomic/ObjectLike.php
Expand Up @@ -62,7 +62,7 @@ public function __toString()
* @return string * @return string
*/ */
function ($name, Union $type) { function ($name, Union $type) {
return $name . ($type->possibly_undefined ? '?' : '') . ':' . $type; return $name . ($type->possibly_undefined ? '?' : '') . ': ' . $type;
}, },
array_keys($this->properties), array_keys($this->properties),
$this->properties $this->properties
Expand All @@ -85,7 +85,7 @@ public function getId()
* @return string * @return string
*/ */
function ($name, Union $type) { function ($name, Union $type) {
return $name . ($type->possibly_undefined ? '?' : '') . ':' . $type->getId(); return $name . ($type->possibly_undefined ? '?' : '') . ': ' . $type->getId();
}, },
array_keys($this->properties), array_keys($this->properties),
$this->properties $this->properties
Expand Down Expand Up @@ -134,7 +134,7 @@ function (
$this_class, $this_class,
$use_phpdoc_format $use_phpdoc_format
) { ) {
return $name . ($type->possibly_undefined ? '?' : '') . ':' . $type->toNamespacedString( return $name . ($type->possibly_undefined ? '?' : '') . ': ' . $type->toNamespacedString(
$namespace, $namespace,
$aliased_classes, $aliased_classes,
$this_class, $this_class,
Expand Down
58 changes: 50 additions & 8 deletions tests/FileManipulation/ReturnTypeManipulationTest.php
Expand Up @@ -173,7 +173,7 @@ function foo() {
/** /**
* @return string[] * @return string[]
* *
* @psalm-return array{0:string} * @psalm-return array{0: string}
*/ */
function foo() { function foo() {
return ["hello"]; return ["hello"];
Expand All @@ -191,7 +191,7 @@ function foo() {
/** /**
* @return string[] * @return string[]
* *
* @psalm-return array{0:string} * @psalm-return array{0: string}
*/ */
function foo(): array { function foo(): array {
return ["hello"]; return ["hello"];
Expand All @@ -209,7 +209,7 @@ function foo() {
/** /**
* @return string[] * @return string[]
* *
* @psalm-return array{a:string, b?:string} * @psalm-return array{a: string, b?: string}
*/ */
function foo(): array { function foo(): array {
return rand(0, 1) ? ["a" => "hello"] : ["a" => "goodbye", "b" => "hello again"]; return rand(0, 1) ? ["a" => "hello"] : ["a" => "goodbye", "b" => "hello again"];
Expand All @@ -234,7 +234,7 @@ function foo() {
/** /**
* @return int[] * @return int[]
* *
* @psalm-return array{a?:int, b?:int} * @psalm-return array{a?: int, b?: int}
*/ */
function foo(): array { function foo(): array {
if (rand(0, 1)) { if (rand(0, 1)) {
Expand All @@ -250,6 +250,48 @@ function foo(): array {
['MissingReturnType'], ['MissingReturnType'],
true, true,
], ],
'addMissingObjectLikeReturnTypeWithNestedArrays' => [
'<?php
function foo() {
return [
"a" => 1,
"b" => 2,
"c" => [
"a" => 1,
"b" => 2,
"c" => [
"a" => 1,
"b" => 2,
"c" => 3,
],
],
];
}',
'<?php
/**
* @return ((int[]|int)[]|int)[]
*
* @psalm-return array{a: int, b: int, c: array{a: int, b: int, c: array{a: int, b: int, c: int}}}
*/
function foo(): array {
return [
"a" => 1,
"b" => 2,
"c" => [
"a" => 1,
"b" => 2,
"c" => [
"a" => 1,
"b" => 2,
"c" => 3,
],
],
];
}',
'7.0',
['MissingReturnType'],
true,
],
'addMissingObjectLikeReturnTypeSeparateStatements70' => [ 'addMissingObjectLikeReturnTypeSeparateStatements70' => [
'<?php '<?php
function foo() { function foo() {
Expand All @@ -267,7 +309,7 @@ function foo() {
/** /**
* @return string[] * @return string[]
* *
* @psalm-return array{a:string, b?:string} * @psalm-return array{a: string, b?: string}
*/ */
function foo(): array { function foo(): array {
if (rand(0, 1)) { if (rand(0, 1)) {
Expand Down Expand Up @@ -890,7 +932,7 @@ class D {
/** /**
* @return B\C[] * @return B\C[]
* *
* @psalm-return array{0:B\C} * @psalm-return array{0: B\C}
*/ */
public function getArrayOfC(): array { public function getArrayOfC(): array {
return [new \A\B\C]; return [new \A\B\C];
Expand Down Expand Up @@ -1241,7 +1283,7 @@ class A {
/** /**
* @return string[] * @return string[]
* *
* @psalm-return array{0:string} * @psalm-return array{0: string}
*/ */
public function foo(): ?array { public function foo(): ?array {
return ["hello"]; return ["hello"];
Expand All @@ -1267,7 +1309,7 @@ class A {
/** /**
* @return string[] * @return string[]
* *
* @psalm-return array{0:string} * @psalm-return array{0: string}
*/ */
public function foo(): array { public function foo(): array {
return ["hello"]; return ["hello"];
Expand Down

0 comments on commit 37d719e

Please sign in to comment.