1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00

Add support for PHPDoc-style type updates

This commit is contained in:
Matthew Brown 2016-11-13 20:32:09 -05:00
parent f2ce83c8e5
commit 360213df9e
6 changed files with 51 additions and 16 deletions

View File

@ -287,7 +287,7 @@ class FileChecker implements StatementsSource
$file_docblock_updates = self::$docblock_return_types[$this->short_file_name];
foreach ($file_docblock_updates as $line_number => $type) {
self::updateDocblock($file_lines, $line_number, $line_upset, $type[0], $type[1]);
self::updateDocblock($file_lines, $line_number, $line_upset, $type[0], $type[1], $type[2]);
}
file_put_contents($this->real_file_name, implode(PHP_EOL, $file_lines));
@ -908,11 +908,11 @@ class FileChecker implements StatementsSource
* @param string $new_type
* @return void
*/
public static function addDocblockReturnType($file_name, $line_number, $docblock, $new_type)
public static function addDocblockReturnType($file_name, $line_number, $docblock, $new_type, $phpdoc_type)
{
$new_type = str_replace(['<mixed, mixed>', '<empty, empty>'], '', $new_type);
self::$docblock_return_types[$file_name][$line_number] = [$docblock, $new_type];
self::$docblock_return_types[$file_name][$line_number] = [$docblock, $new_type, $phpdoc_type];
}
/**
@ -923,7 +923,7 @@ class FileChecker implements StatementsSource
* @param string $type
* @return void
*/
public static function updateDocblock(array &$file_lines, $line_number, &$line_upset, $existing_docblock, $type)
public static function updateDocblock(array &$file_lines, $line_number, &$line_upset, $existing_docblock, $type, $phpdoc_type)
{
$line_number += $line_upset;
$function_line = $file_lines[$line_number - 1];
@ -941,7 +941,12 @@ class FileChecker implements StatementsSource
$parsed_docblock['description'] = '';
}
$parsed_docblock['specials']['return'] = [$type];
$parsed_docblock['specials']['return'] = [$phpdoc_type];
if ($type !== $phpdoc_type) {
$parsed_docblock['specials']['psalm-return'] = [$type];
}
$new_docblock_lines = CommentChecker::renderDocComment($parsed_docblock, $left_padding);
$line_upset += count($new_docblock_lines) - $existing_line_count;

View File

@ -519,7 +519,8 @@ abstract class FunctionLikeChecker implements StatementsSource
$this->file_name,
$this->function->getLine(),
(string)$this->function->getDocComment(),
$inferred_return_type->toNamespacedString($this->getAliasedClassesFlipped(), $this->getFQCLN())
$inferred_return_type->toNamespacedString($this->getAliasedClassesFlipped(), $this->getFQCLN(), false),
$inferred_return_type->toNamespacedString($this->getAliasedClassesFlipped(), $this->getFQCLN(), true)
);
}
@ -591,7 +592,8 @@ abstract class FunctionLikeChecker implements StatementsSource
$this->file_name,
$this->function->getLine(),
(string)$this->function->getDocComment(),
$inferred_return_type->toNamespacedString($this->getAliasedClassesFlipped(), $this->getFQCLN())
$inferred_return_type->toNamespacedString($this->getAliasedClassesFlipped(), $this->getFQCLN(), false),
$inferred_return_type->toNamespacedString($this->getAliasedClassesFlipped(), $this->getFQCLN(), true)
);
}

View File

@ -33,9 +33,10 @@ class Atomic extends Type
/**
* @param array<string> $aliased_classes
* @param string $this_class
* @param bool $use_phpdoc_format
* @return string
*/
public function toNamespacedString(array $aliased_classes, $this_class)
public function toNamespacedString(array $aliased_classes, $this_class, $use_phpdoc_format)
{
if ($this->value === $this_class) {
$class_parts = explode('\\', $this_class);

View File

@ -42,17 +42,38 @@ class Generic extends Atomic
/**
* @param array<string> $aliased_classes
* @param string $this_class
* @param bool $use_phpdoc_format
* @return string
*/
public function toNamespacedString(array $aliased_classes, $this_class)
public function toNamespacedString(array $aliased_classes, $this_class, $use_phpdoc_format)
{
if ($use_phpdoc_format) {
if ($this->value !== 'array') {
return $this->value;
}
$value_type = $this->type_params[1];
if ($value_type->isMixed()) {
return $this->value;
}
$value_type_string = $value_type->toNamespacedString($aliased_classes, $this_class, true);
if (count($value_type->types) > 1) {
return '(' . $value_type_string . ')[]';
}
return $value_type_string . '[]';
}
return $this->value .
'<' .
implode(
', ',
array_map(
function (Union $type_param) use ($aliased_classes, $this_class) {
return $type_param->toNamespacedString($aliased_classes, $this_class);
return $type_param->toNamespacedString($aliased_classes, $this_class, false);
},
$this->type_params
)

View File

@ -47,17 +47,22 @@ class ObjectLike extends Atomic
/**
* @param array<string> $aliased_classes
* @param string $this_class
* @param bool $use_phpdoc_format
* @return string
*/
public function toNamespacedString(array $aliased_classes, $this_class)
public function toNamespacedString(array $aliased_classes, $this_class, $use_phpdoc_format)
{
if ($use_phpdoc_format) {
return $this->value;
}
return $this->value .
'{' .
implode(
', ',
array_map(
function ($name, Union $type) use ($aliased_classes, $this_class) {
return $name . ':' . $type->toNamespacedString($aliased_classes, $this_class);
function ($name, Union $type) use ($aliased_classes, $this_class, $use_phpdoc_format) {
return $name . ':' . $type->toNamespacedString($aliased_classes, $this_class, $use_phpdoc_format);
},
array_keys($this->properties),
$this->properties

View File

@ -44,15 +44,16 @@ class Union extends Type
/**
* @param array<string> $aliased_classes
* @param string $this_class
* @param bool $use_phpdoc_format
* @return string
*/
public function toNamespacedString(array $aliased_classes, $this_class)
public function toNamespacedString(array $aliased_classes, $this_class, $use_phpdoc_format)
{
return implode(
'|',
array_map(
function (Atomic $type) use ($aliased_classes, $this_class) {
return $type->toNamespacedString($aliased_classes, $this_class);
function (Atomic $type) use ($aliased_classes, $this_class, $use_phpdoc_format) {
return $type->toNamespacedString($aliased_classes, $this_class, $use_phpdoc_format);
},
$this->types
)