Skip to content
Permalink
Browse files

Allow @Property to contain union types

  • Loading branch information...
muglug committed Jun 14, 2019
1 parent c42269e commit 7a6395490cadb00f94979ff9a2749cdffade26d6
Showing with 19 additions and 16 deletions.
  1. +19 −16 src/Psalm/Internal/Analyzer/CommentAnalyzer.php
@@ -895,7 +895,7 @@ public static function extractClassLikeDocblockInfo(\PhpParser\Node $node, PhpPa
/**
* @param ClassLikeDocblockComment $info
* @param array<string, array<int, string>> $specials
* @param string $property_tag ('property', 'psalm-property', 'property-read', or 'property-write')
* @param 'property'|'psalm-property'|'property-read'|'property-write' $property_tag
*
* @throws DocblockParseException
*
@@ -912,35 +912,38 @@ protected static function addMagicPropertyToInfo(
foreach ($magic_property_comments as $offset => $property) {
$line_parts = self::splitDocLine($property);
if (count($line_parts) === 1 && $line_parts[0][0] === '$') {
array_unshift($line_parts, 'mixed');
if (count($line_parts) === 1 && isset($line_parts[0][0]) && $line_parts[0][0] === '$') {
continue;
}
if (count($line_parts) > 1) {
if (preg_match('/^' . self::TYPE_REGEX . '$/', $line_parts[0])
&& !preg_match('/\[[^\]]+\]/', $line_parts[0])
&& preg_match('/^(\.\.\.)?&?\$[A-Za-z0-9_]+,?$/', $line_parts[1])
&& !strpos($line_parts[0], '::')
if (preg_match('/^&?\$[A-Za-z0-9_]+,?$/', $line_parts[1])
&& $line_parts[0][0] !== '{'
) {
if ($line_parts[1][0] === '&') {
$line_parts[1] = substr($line_parts[1], 1);
}
$line_parts[1] = str_replace('&', '', $line_parts[1]);
$line_parts[1] = preg_replace('/,$/', '', $line_parts[1]);
$start = $offset + $comment->getFilePos();
$end = $start + strlen($line_parts[0]);
$line_parts[0] = str_replace("\n", '', preg_replace('@^[ \t]*\*@m', '', $line_parts[0]));
if ($line_parts[0][0] === '$' && !preg_match('/^\$this(\||$)/', $line_parts[0])) {
if ($line_parts[0] === ''
|| ($line_parts[0][0] === '$'
&& !preg_match('/^\$this(\||$)/', $line_parts[0]))
) {
throw new IncorrectDocblockException('Misplaced variable');
}
$line_parts[1] = preg_replace('/,$/', '', $line_parts[1]);
$info->properties[] = [
'name' => $line_parts[1],
'name' => trim($line_parts[1]),
'type' => $line_parts[0],
'line_number' => $comment->getLine() + substr_count($comment->getText(), "\n", 0, $offset),
'tag' => $property_tag,
'start' => $start,
'end' => $end,
];
} else {
throw new DocblockParseException('Badly-formatted @property');
}
} else {
throw new DocblockParseException('Badly-formatted @property');

0 comments on commit 7a63954

Please sign in to comment.
You can’t perform that action at this time.