Skip to content

Commit

Permalink
Unify array shape description with PHPDoc type syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 13, 2021
1 parent 2b3ee60 commit 51d7431
Show file tree
Hide file tree
Showing 78 changed files with 443 additions and 401 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ parameters:
path: src/Rules/Api/ApiTraitUseRule.php

-
message: "#^Binary operation \"\\+\" between array\\(class\\-string\\<TNodeType of PhpParser\\\\Node\\>\\) and array\\<string, class\\-string\\>\\|false results in an error\\.$#"
message: "#^Binary operation \"\\+\" between array{class\\-string\\<TNodeType of PhpParser\\\\Node\\>} and array\\<string, class\\-string\\>\\|false results in an error\\.$#"
count: 1
path: src/Rules/Registry.php

Expand Down
13 changes: 11 additions & 2 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,16 @@ public function describe(VerbosityLevel $level): string
$exportValuesOnly = false;
}

$items[] = sprintf('%s%s => %s', $isOptional ? '?' : '', var_export($keyType->getValue(), true), $valueType->describe($level));
$keyDescription = $keyType->getValue();
if (is_string($keyDescription)) {
if (strpos($keyDescription, '"') !== false) {
$keyDescription = sprintf('\'%s\'', $keyDescription);
} elseif (strpos($keyDescription, '\'') !== false) {
$keyDescription = sprintf('"%s"', $keyDescription);
}
}

$items[] = sprintf('%s%s: %s', $keyDescription, $isOptional ? '?' : '', $valueType->describe($level));
$values[] = $valueType->describe($level);
}

Expand All @@ -740,7 +749,7 @@ public function describe(VerbosityLevel $level): string
}

return sprintf(
'array(%s%s)',
'array{%s%s}',
implode(', ', $exportValuesOnly ? $values : $items),
$append
);
Expand Down
Loading

0 comments on commit 51d7431

Please sign in to comment.