1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Handle comments in area where return type should be

This commit is contained in:
Matthew Brown 2018-01-07 13:06:56 -05:00
parent 29b09f9633
commit 77b6b397fc
2 changed files with 53 additions and 3 deletions

View File

@ -133,26 +133,64 @@ class FunctionDocblockManipulator
/** @var array<int, string> */
$chars = str_split($function_code_after_bracket);
foreach ($chars as $i => $char) {
$in_single_line_comment = $in_multi_line_comment = false;
for ($i = 0; $i < count($chars); $i++) {
$char = $chars[$i];
switch ($char) {
case PHP_EOL:
$in_single_line_comment = false;
continue;
case ':':
if ($in_multi_line_comment || $in_single_line_comment) {
continue;
}
continue;
case '/':
// @todo handle comments in this area
throw new \UnexpectedValueException('Not expecting comments where return types should live');
if ($in_multi_line_comment || $in_single_line_comment) {
continue;
}
if ($chars[$i + 1] === '*') {
$in_multi_line_comment = true;
$i++;
}
case '*':
if ($in_single_line_comment) {
continue;
}
if ($chars[$i + 1] === '/') {
$in_multi_line_comment = false;
$i++;
}
case '{':
if ($in_multi_line_comment || $in_single_line_comment) {
continue;
}
break 2;
case '?':
if ($in_multi_line_comment || $in_single_line_comment) {
continue;
}
$this->return_typehint_start = $i + $end_bracket_position + 1;
break;
}
if ($in_multi_line_comment || $in_single_line_comment) {
continue;
}
if (preg_match('/\w/', $char)) {
if ($this->return_typehint_start === null) {
$this->return_typehint_start = $i + $end_bracket_position + 1;

View File

@ -187,6 +187,18 @@ class FileManipulationTest extends TestCase
'7.1',
['MissingReturnType'],
],
'addMissingStringReturnTypeWithComment71' => [
'<?php
function foo() /** : ?string */ {
return rand(0, 1) ? "hello" : null;
}',
'<?php
function foo() : ?string /** : ?string */ {
return rand(0, 1) ? "hello" : null;
}',
'7.1',
['MissingReturnType'],
],
'addMissingStringArrayReturnType56' => [
'<?php
function foo() {