Skip to content

Commit

Permalink
Fix #4349 - improve types for suggested unions
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Oct 16, 2020
1 parent 76986eb commit be1cd52
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 36 deletions.
Expand Up @@ -305,7 +305,7 @@ public static function verifyReturnType(

if (IssueBuffer::accepts(
new MissingClosureReturnType(
'Closure does not have a return type, expecting ' . $inferred_return_type,
'Closure does not have a return type, expecting ' . $inferred_return_type->getId(),
new CodeLocation($function_like_analyzer, $function, null, true)
),
$suppressed_issues,
Expand Down Expand Up @@ -345,7 +345,7 @@ public static function verifyReturnType(
if (IssueBuffer::accepts(
new MissingReturnType(
'Method ' . $cased_method_id . ' does not have a return type' .
(!$inferred_return_type->hasMixed() ? ', expecting ' . $inferred_return_type : ''),
(!$inferred_return_type->hasMixed() ? ', expecting ' . $inferred_return_type->getId() : ''),
new CodeLocation($function_like_analyzer, $function->name, null, true)
),
$suppressed_issues,
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic/TLiteralInt.php
Expand Up @@ -44,6 +44,6 @@ public function toNamespacedString(
?string $this_class,
bool $use_phpdoc_format
): string {
return 'int';
return $use_phpdoc_format ? 'int' : $this->value;
}
}
18 changes: 18 additions & 0 deletions src/Psalm/Type/Atomic/TPositiveInt.php
Expand Up @@ -8,11 +8,29 @@ public function getId(bool $nested = false): string
return 'positive-int';
}

public function __toString(): string
{
return 'positive-int';
}

/**
* @return false
*/
public function canBeFullyExpressedInPhp(): bool
{
return false;
}

/**
* @param array<string> $aliased_classes
*
*/
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
): string {
return 'positive-int';
}
}
34 changes: 11 additions & 23 deletions src/Psalm/Type/Union.php
Expand Up @@ -402,40 +402,28 @@ public function toNamespacedString(
?string $this_class,
bool $use_phpdoc_format
): string {
$printed_int = false;
$printed_float = false;
$printed_string = false;

$types = [];

$multi_ints = count($this->literal_int_types) > 1;
$multi_strings = count($this->literal_string_types) > 1;
$multi_floats = count($this->literal_float_types) > 1;

foreach ($this->types as $type) {
$type_string = $type->toNamespacedString($namespace, $aliased_classes, $this_class, $use_phpdoc_format);

if ($type instanceof TLiteralFloat && $type_string === 'float') {
if ($printed_float) {
continue;
}

$printed_float = true;
} elseif ($type instanceof TLiteralString && $type_string === 'string') {
if ($printed_string) {
continue;
}

$printed_string = true;
} elseif ($type instanceof TLiteralInt && $type_string === 'int') {
if ($printed_int) {
continue;
}

$printed_int = true;
if ($type instanceof TLiteralInt && !$multi_ints) {
$type_string = 'int';
} elseif ($type instanceof TLiteralFloat && !$multi_floats) {
$type_string = 'float';
} elseif ($type instanceof TLiteralString && !$multi_strings) {
$type_string = 'string';
}

$types[] = $type_string;
}

sort($types);
return implode('|', $types);
return implode('|', array_unique($types));
}

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/BinaryOperationTest.php
Expand Up @@ -174,9 +174,9 @@ function foo(string $s) : int {
'assertions' => [
'$a' => 'int',
'$b' => 'int',
'$c' => 'int',
'$d' => 'int',
'$e' => 'int',
'$c' => 'positive-int',
'$d' => 'positive-int',
'$e' => 'positive-int',
'$f' => 'string',
],
],
Expand All @@ -187,8 +187,8 @@ function foo(string $s) : int {
$c = (true xor false);
$d = (false xor false);',
'assertions' => [
'$a' => 'int',
'$b' => 'int',
'$a' => 'positive-int',
'$b' => 'positive-int',
'$c' => 'bool',
'$d' => 'bool',
],
Expand Down Expand Up @@ -220,7 +220,7 @@ function foo(string $s) : int {
$b = 4 ^ 5;',
'assertions' => [
'$a' => 'string',
'$b' => 'int',
'$b' => 'positive-int',
],
],
'bitwiseNot' => [
Expand Down
2 changes: 1 addition & 1 deletion tests/JsonOutputTest.php
Expand Up @@ -98,7 +98,7 @@ function fooFoo(Badger\Bodger $a): Badger\Bodger {
function fooFoo() {
return "hello";
}',
'message' => 'Method fooFoo does not have a return type, expecting string',
'message' => 'Method fooFoo does not have a return type, expecting string(hello)',
'line' => 2,
'error' => 'fooFoo',
],
Expand Down
4 changes: 2 additions & 2 deletions tests/Php56Test.php
Expand Up @@ -49,8 +49,8 @@ public function f($a = self::ONE + self::THREE) {
$c4 = (new C)->four;',
'assertions' => [
'$c1' => 'int',
'$c2' => 'int',
'$c3' => 'int',
'$c2' => 'positive-int',
'$c3' => 'positive-int',
'$c1_3rd' => 'float|int',
'$c_sentence' => 'string',
'$cf' => 'int',
Expand Down
2 changes: 1 addition & 1 deletion tests/TypeReconciliation/TypeTest.php
Expand Up @@ -863,7 +863,7 @@ function fooFoo(A $a) {
$a = 0;
$b = $a++;',
'assertions' => [
'$a' => 'int',
'$a' => 'positive-int',
],
],
'typedValueAssertion' => [
Expand Down

0 comments on commit be1cd52

Please sign in to comment.