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

allow documenting promoted properties with @var #6764

Merged
merged 5 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -14,6 +14,7 @@
use Psalm\Exception\IncorrectDocblockException;
use Psalm\Internal\Algebra\FormulaGenerator;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\CommentAnalyzer;
use Psalm\Internal\Analyzer\NamespaceAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\SimpleTypeInferer;
use Psalm\Internal\Scanner\FileScanner;
Expand All @@ -32,6 +33,7 @@
use Psalm\Storage\MethodStorage;
use Psalm\Storage\PropertyStorage;
use Psalm\Type;
use UnexpectedValueException;

use function array_pop;
use function count;
Expand Down Expand Up @@ -404,7 +406,7 @@ public function start(PhpParser\Node\FunctionLike $stmt, bool $fake_method = fal
if ($parser_return_type) {
$original_type = $parser_return_type;
if ($original_type instanceof PhpParser\Node\IntersectionType) {
throw new \UnexpectedValueException('Intersection types not yet supported');
throw new UnexpectedValueException('Intersection types not yet supported');
}
/** @var Identifier|Name|NullableType|UnionType $original_type */

Expand Down Expand Up @@ -585,9 +587,39 @@ public function start(PhpParser\Node\FunctionLike $stmt, bool $fake_method = fal
continue;
}

$doc_comment = $param->getDocComment();
$var_comment_type = null;
if ($doc_comment) {
$var_comments = CommentAnalyzer::getTypeFromComment(
$doc_comment,
$this->file_scanner,
$this->aliases,
$this->existing_function_template_types ?: [],
$this->type_aliases
);

$var_comment = array_pop($var_comments);

if ($var_comment !== null) {
$var_comment_type = $var_comment->type;
}
}

//both way to document type were used
if ($param_storage->type && $var_comment_type) {
throw new UnexpectedValueException(
'Both param and property type provided for ' . $param_storage->name
);
}

//no docblock type was provided for param but we have one for property
if ($param_storage->type === null && $var_comment_type) {
$param_storage->type = $var_comment_type;
}

$property_storage = $classlike_storage->properties[$param_storage->name] = new PropertyStorage();
$property_storage->is_static = false;
$property_storage->type = $param_storage->type;
$property_storage->type = $param_storage->type ?? $var_comment_type;
$property_storage->signature_type = $param_storage->signature_type;
$property_storage->signature_type_location = $param_storage->signature_type_location;
$property_storage->type_location = $param_storage->type_location;
Expand Down Expand Up @@ -762,7 +794,7 @@ private function getTranslatedFunctionParam(

if ($param_typehint) {
if ($param_typehint instanceof PhpParser\Node\IntersectionType) {
throw new \UnexpectedValueException('Intersection types not yet supported');
throw new UnexpectedValueException('Intersection types not yet supported');
}
/** @var Identifier|Name|NullableType|UnionType $param_typehint */

Expand All @@ -786,7 +818,7 @@ private function getTranslatedFunctionParam(
$is_optional = $param->default !== null;

if ($param->var instanceof PhpParser\Node\Expr\Error || !is_string($param->var->name)) {
throw new \UnexpectedValueException('Not expecting param name to be non-string');
throw new UnexpectedValueException('Not expecting param name to be non-string');
}

$default_type = null;
Expand Down Expand Up @@ -1074,7 +1106,7 @@ private function createStorageForFunctionLike(
}
}
} else {
throw new \UnexpectedValueException('Unrecognized functionlike');
throw new UnexpectedValueException('Unrecognized functionlike');
}

return [
Expand Down
69 changes: 69 additions & 0 deletions tests/AnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Psalm\Config;
use Psalm\Context;
use UnexpectedValueException;

use const DIRECTORY_SEPARATOR;

Expand Down Expand Up @@ -198,6 +199,28 @@ function foo($arr = false) {}'
$this->analyzeFile('somefile.php', new Context());
}

public function testPromotedPropertyDuplicateDoc(): void
{
$this->expectException(UnexpectedValueException::class);
weirdan marked this conversation as resolved.
Show resolved Hide resolved
$this->expectExceptionMessage('Both param and property type provided for id');

$this->addFile(
'somefile.php',
'<?php
final class UserRole
{
/** @psalm-param string $id */
public function __construct(
/** @psalm-var stdClass */
protected $id
) {
}
}'
);

$this->analyzeFile('somefile.php', new Context());
}

public function testInvalidParamDefaultButAllowedInConfig(): void
{
Config::getInstance()->add_param_default_to_docblock_type = true;
Expand Down Expand Up @@ -1303,6 +1326,22 @@ class A extends Vendor
$_b = new A();
echo (string)($a->getConfig()[0]??"");'
],
'promotedPropertiesDocumentationEitherForParamOrForProperty' => [
'<?php
final class UserRole
{
/** @psalm-param stdClass $id */
public function __construct(
protected $id,
/** @psalm-var stdClass */
protected $id2
) {
}
}

new UserRole(new stdClass(), new stdClass());
'
],
];
}

Expand Down Expand Up @@ -1882,6 +1921,36 @@ function f(): string {
",
'error_message' => 'InvalidDocblock',
],
'promotedPropertiesDocumentationFailsWhenSendingBadTypeAgainstParam' => [
'<?php
final class UserRole
{
/** @psalm-param stdClass $id */
public function __construct(
protected $id
) {
}

}
new UserRole("a");
',
'error_message' => 'InvalidArgument',
],
'promotedPropertiesDocumentationFailsWhenSendingBadTypeAgainstProperty' => [
'<?php
final class UserRole
{
public function __construct(
/** @psalm-var stdClass */
protected $id2
) {
}
}

new UserRole("a");
',
'error_message' => 'InvalidArgument',
],
];
}
}