Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for phpstan import-type and type annotations #5648

Merged
merged 3 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use function count;
use function reset;
use function preg_split;
use function array_merge;
use function array_shift;
use function implode;
use function substr;
Expand Down Expand Up @@ -239,15 +240,18 @@ public static function parse(
}
}

if (isset($parsed_docblock->tags['psalm-import-type'])) {
foreach ($parsed_docblock->tags['psalm-import-type'] as $offset => $imported_type_entry) {
$info->imported_types[] = [
'line_number' => $comment->getStartLine() + substr_count($comment->getText(), "\n", 0, $offset),
'start_offset' => $comment->getStartFilePos() + $offset,
'end_offset' => $comment->getStartFilePos() + $offset + strlen($imported_type_entry),
'parts' => CommentAnalyzer::splitDocLine($imported_type_entry) ?: []
];
}
$imported_types = array_merge(
$parsed_docblock->tags['phpstan-import-type'] ?? [],
$parsed_docblock->tags['psalm-import-type'] ?? []
);

foreach ($imported_types as $offset => $imported_type_entry) {
$info->imported_types[] = [
'line_number' => $comment->getStartLine() + substr_count($comment->getText(), "\n", 0, $offset),
'start_offset' => $comment->getStartFilePos() + $offset,
'end_offset' => $comment->getStartFilePos() + $offset + strlen($imported_type_entry),
'parts' => CommentAnalyzer::splitDocLine($imported_type_entry) ?: []
];
}

if (isset($parsed_docblock->combined_tags['method'])) {
Expand Down
10 changes: 8 additions & 2 deletions src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Psalm\Internal\Analyzer\NamespaceAnalyzer;
use Psalm\Internal\Scanner\ClassLikeDocblockComment;
use function array_merge;
use function array_pop;
use function count;
use function explode;
Expand Down Expand Up @@ -1538,12 +1539,17 @@ public static function getTypeAliasesFromComment(
): array {
$parsed_docblock = DocComment::parsePreservingLength($comment);

if (!isset($parsed_docblock->tags['psalm-type'])) {
if (!isset($parsed_docblock->tags['psalm-type']) && !isset($parsed_docblock->tags['phpstan-type'])) {
return [];
}

$type_alias_comment_lines = array_merge(
$parsed_docblock->tags['phpstan-type'] ?? [],
$parsed_docblock->tags['psalm-type'] ?? []
);

return self::getTypeAliasesFromCommentLines(
$parsed_docblock->tags['psalm-type'],
$type_alias_comment_lines,
$aliases,
$type_aliases,
$self_fqcln
Expand Down
32 changes: 32 additions & 0 deletions tests/TypeAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,38 @@ class B {}
*/
class C extends A {}

$instance = new C("hello");
$output = $instance->value;',
[
'$output' => 'string',
],
],
'importedTypeWithPhpstanAnnotation' => [
'<?php
/** @template T */
abstract class A {
/** @var T */
public $value;

/** @param T $value */
public function __construct($value) {
$this->value = $value;
}
}

/**
* @phpstan-type Foo=string
*/
class B {}

/**
* @phpstan-import-type Foo from B
* @phpstan-type Baz=Foo
*
* @extends A<Baz>
*/
class C extends A {}

$instance = new C("hello");
$output = $instance->value;',
[
Expand Down