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

WIP: Introduce @psalm-use to import types #2924

Closed
wants to merge 1 commit into from
Closed
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
Introduce @psalm-use to import types
That is a proposal of how we could import types defined in other
classes, so we don't need to repeat the types over and over again.
That could be really interesting for composition of types as well.

/** @psalm-type TPhone = array{phone: string} */
class Phone {
    /** @psalm-return TPhone */
    toArray(): array {}
}

/** @psalm-type TName = array{name: string} */
class Name {
    /** @psalm-return TName */
    toArray(): array {}
}

/**
 * @psalm-use TPhone from Phone
 * @psalm-use TName from Name
 *
 * @psalm-type TUserArray = TPhone&TName
 */
class User {
}

Signed-off-by: Jefersson Nathan <malukenho.dev@gmail.com>
  • Loading branch information
malukenho committed Mar 6, 2020
commit 1a7b9695549fe5fceb9d045dce0aa623e6bf8f92
2 changes: 1 addition & 1 deletion src/Psalm/DocComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public static function parsePreservingLength(\PhpParser\Comment\Doc $docblock)
$special_key,
[
'return', 'param', 'template', 'var', 'type',
'template-covariant', 'property', 'method',
'template-covariant', 'property', 'method', 'use',
'assert', 'assert-if-true', 'assert-if-false', 'suppress',
'ignore-nullable-return', 'override-property-visibility',
'override-method-visibility', 'seal-properties', 'seal-methods',
Expand Down
15 changes: 15 additions & 0 deletions src/Psalm/Internal/Analyzer/CommentAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use PhpParser;
use Psalm\Aliases;
use Psalm\Codebase;
use Psalm\DocComment;
use Psalm\Exception\DocblockParseException;
use Psalm\Exception\IncorrectDocblockException;
Expand All @@ -11,8 +12,11 @@
use Psalm\Internal\Scanner\ClassLikeDocblockComment;
use Psalm\Internal\Scanner\FunctionDocblockComment;
use Psalm\Internal\Scanner\VarDocblockComment;
use Psalm\Internal\Taint\Source;
use Psalm\Internal\Type\ParseTree;
use Psalm\Type;
use function array_map;
use function array_values;
use function trim;
use function substr_count;
use function strlen;
Expand All @@ -22,6 +26,7 @@
use function count;
use function reset;
use function preg_split;
use function var_dump;
use const PREG_SPLIT_DELIM_CAPTURE;
use const PREG_SPLIT_NO_EMPTY;
use function array_shift;
Expand Down Expand Up @@ -241,11 +246,21 @@ public static function arrayToDocblocks(
*/
public static function getTypeAliasesFromComment(
PhpParser\Comment\Doc $comment,
Codebase $codebase,
Aliases $aliases,
array $type_aliases = null
) {
$parsed_docblock = DocComment::parsePreservingLength($comment);

if (isset($parsed_docblock['specials']['psalm-use'])) {

// @todo Is it possible to get the Aliases form another class using {@see $codebase}?
return [
// @todo faking the data
'TData' => [['array', 4], ['{', 1], ['name', 4], [':', 1], ['string', 5], ['}', 1]]
];
}

if (!isset($parsed_docblock['specials']['psalm-type'])) {
return [];
}
Expand Down
1 change: 1 addition & 0 deletions src/Psalm/Internal/Visitor/ReflectorVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public function enterNode(PhpParser\Node $node)
try {
$type_alias_tokens = CommentAnalyzer::getTypeAliasesFromComment(
$comment,
$this->codebase,
$this->aliases,
$this->type_aliases
);
Expand Down
48 changes: 48 additions & 0 deletions tests/AnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,54 @@ function getCollection(): Generator
$this->analyzeFile('somefile.php', new Context());
}

/**
* @return void
*/
public function testReferenceTypeAliasFromAnotherScopeArray()
{
$this->expectException(\Psalm\Exception\CodeException::class);
$this->expectExceptionMessage(
'Argument 1 of Boo\Baz::doIt expects array{name: string}, array{age: int(12)} provided'
);

$this->addFile(
'definition.php',
'<?php
namespace Foo {
/**
* @psalm-type TData = array{name: string}
*/
class Bar {}
}'
);

$this->addFile(
'import.php',
'<?php
namespace Boo {
class Baz {
/**
* @psalm-use TData from \Foo\Bar
* @psalm-param TData $array
*/
public static function doIt(array $array) : void {}
}
}'
);

$this->addFile(
'client.php',
'<?php
namespace Bar {
\Boo\Baz::doIt(["age" => 12]);
}'
);

$this->analyzeFile('definition.php', new Context());
$this->analyzeFile('import.php', new Context());
$this->analyzeFile('client.php', new Context());
}

/**
* @return void
*/
Expand Down