2021-02-24 16:14:04 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2021-02-24 16:14:04 +01:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase as BaseTestCase;
|
2021-12-03 20:11:20 +01:00
|
|
|
use PhpParser\Comment\Doc;
|
2022-06-11 15:07:24 +02:00
|
|
|
use PhpParser\Node\Scalar\String_;
|
|
|
|
use Psalm\CodeLocation;
|
2021-05-04 04:28:17 +02:00
|
|
|
use Psalm\Exception\IncorrectDocblockException;
|
2022-06-11 15:07:24 +02:00
|
|
|
use Psalm\Internal\Analyzer\FileAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2021-02-24 16:14:04 +01:00
|
|
|
use Psalm\Internal\PhpVisitor\Reflector\FunctionLikeDocblockParser;
|
2022-06-11 15:07:24 +02:00
|
|
|
use Psalm\Internal\Provider\FakeFileProvider;
|
|
|
|
use Psalm\Internal\Provider\Providers;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Internal\RuntimeCaches;
|
2022-06-11 15:07:24 +02:00
|
|
|
use Psalm\Tests\Internal\Provider\FakeParserCacheProvider;
|
2021-02-24 16:14:04 +01:00
|
|
|
|
|
|
|
class FunctionLikeDocblockParserTest extends BaseTestCase
|
|
|
|
{
|
2022-12-16 19:58:47 +01:00
|
|
|
public string $test_cased_function_id = 'hello_world';
|
2022-06-11 15:07:24 +02:00
|
|
|
|
2022-12-16 19:58:47 +01:00
|
|
|
public CodeLocation $test_code_location;
|
2022-06-11 15:07:24 +02:00
|
|
|
|
2021-02-24 16:14:04 +01:00
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
RuntimeCaches::clearAll();
|
2022-06-11 15:07:24 +02:00
|
|
|
|
|
|
|
$file_provider = new FakeFileProvider();
|
|
|
|
|
|
|
|
$providers = new Providers(
|
|
|
|
$file_provider,
|
2022-12-18 17:15:15 +01:00
|
|
|
new FakeParserCacheProvider(),
|
2022-06-11 15:07:24 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$test_config = new TestConfig();
|
|
|
|
|
|
|
|
$project_analyzer = new ProjectAnalyzer(
|
|
|
|
$test_config,
|
2022-12-18 17:15:15 +01:00
|
|
|
$providers,
|
2022-06-11 15:07:24 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$file_analyzer = new FileAnalyzer($project_analyzer, 'none/none.php', 'none.php');
|
|
|
|
|
|
|
|
$stmt = new String_('randomString');
|
|
|
|
$this->test_code_location = new CodeLocation($file_analyzer, $stmt);
|
2021-02-24 16:14:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDocblockDescription(): void
|
|
|
|
{
|
|
|
|
$doc = '/**
|
|
|
|
* Some Description
|
|
|
|
*
|
|
|
|
* @param string $bli
|
|
|
|
* @param int $bla
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
';
|
2021-12-03 20:11:20 +01:00
|
|
|
$php_parser_doc = new Doc($doc);
|
2022-06-11 15:07:24 +02:00
|
|
|
$function_docblock = FunctionLikeDocblockParser::parse(
|
|
|
|
$php_parser_doc,
|
|
|
|
$this->test_code_location,
|
2022-12-18 17:15:15 +01:00
|
|
|
$this->test_cased_function_id,
|
2022-06-11 15:07:24 +02:00
|
|
|
);
|
2021-02-24 16:14:04 +01:00
|
|
|
|
|
|
|
$this->assertSame('Some Description', $function_docblock->description);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDocblockParamDescription(): void
|
|
|
|
{
|
|
|
|
$doc = '/**
|
|
|
|
* Some Description
|
|
|
|
*
|
|
|
|
* @param string $bli The BLI tag to iterate over.
|
|
|
|
* @param int $bla The blah tags
|
|
|
|
* that has a very long multiline description.
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
';
|
2021-12-03 20:11:20 +01:00
|
|
|
$php_parser_doc = new Doc($doc);
|
2022-06-11 15:07:24 +02:00
|
|
|
$function_docblock = FunctionLikeDocblockParser::parse(
|
|
|
|
$php_parser_doc,
|
|
|
|
$this->test_code_location,
|
2022-12-18 17:15:15 +01:00
|
|
|
$this->test_cased_function_id,
|
2022-06-11 15:07:24 +02:00
|
|
|
);
|
2021-02-24 16:14:04 +01:00
|
|
|
|
|
|
|
$this->assertTrue(isset($function_docblock->params[0]['description']));
|
|
|
|
$this->assertSame('The BLI tag to iterate over.', $function_docblock->params[0]['description']);
|
|
|
|
|
|
|
|
$this->assertTrue(isset($function_docblock->params[1]['description']));
|
|
|
|
$this->assertSame('The blah tags that has a very long multiline description.', $function_docblock->params[1]['description']);
|
|
|
|
}
|
2021-05-04 04:28:17 +02:00
|
|
|
|
|
|
|
public function testMisplacedVariableOnNextLine(): void
|
|
|
|
{
|
|
|
|
$doc = '/**
|
|
|
|
* @param
|
|
|
|
* $p
|
|
|
|
*/';
|
2021-12-03 20:11:20 +01:00
|
|
|
$php_parser_doc = new Doc($doc);
|
2021-05-04 04:28:17 +02:00
|
|
|
$this->expectException(IncorrectDocblockException::class);
|
|
|
|
$this->expectExceptionMessage('Misplaced variable');
|
2022-06-11 15:07:24 +02:00
|
|
|
FunctionLikeDocblockParser::parse(
|
|
|
|
$php_parser_doc,
|
|
|
|
$this->test_code_location,
|
2022-12-18 17:15:15 +01:00
|
|
|
$this->test_cased_function_id,
|
2022-06-11 15:07:24 +02:00
|
|
|
);
|
2021-05-04 04:28:17 +02:00
|
|
|
}
|
2021-05-06 03:47:01 +02:00
|
|
|
|
|
|
|
public function testPreferPsalmPrefixedAnnotationsOverPhpstanOnes(): void
|
|
|
|
{
|
|
|
|
$doc = '/**
|
|
|
|
* @psalm-template T of string
|
|
|
|
* @phpstan-template T of int
|
|
|
|
*/
|
|
|
|
';
|
2021-12-03 20:11:20 +01:00
|
|
|
$php_parser_doc = new Doc($doc);
|
2022-06-11 15:07:24 +02:00
|
|
|
$function_docblock = FunctionLikeDocblockParser::parse(
|
|
|
|
$php_parser_doc,
|
|
|
|
$this->test_code_location,
|
2022-12-18 17:15:15 +01:00
|
|
|
$this->test_cased_function_id,
|
2022-06-11 15:07:24 +02:00
|
|
|
);
|
2021-05-06 03:47:01 +02:00
|
|
|
$this->assertSame([['T', 'of', 'string', false]], $function_docblock->templates);
|
|
|
|
}
|
2021-05-28 15:47:39 +02:00
|
|
|
|
|
|
|
public function testReturnsUnexpectedTags(): void
|
|
|
|
{
|
|
|
|
$doc = '/**
|
|
|
|
* @psalm-import-type abcd
|
|
|
|
* @var int $p
|
2022-11-17 00:14:13 +01:00
|
|
|
* @psalm-consistent-constructor
|
2021-05-28 15:47:39 +02:00
|
|
|
*/
|
|
|
|
';
|
2021-12-03 20:11:20 +01:00
|
|
|
$php_parser_doc = new Doc($doc, 0);
|
2022-06-11 15:07:24 +02:00
|
|
|
$function_docblock = FunctionLikeDocblockParser::parse(
|
|
|
|
$php_parser_doc,
|
|
|
|
$this->test_code_location,
|
2022-12-18 17:15:15 +01:00
|
|
|
$this->test_cased_function_id,
|
2022-06-11 15:07:24 +02:00
|
|
|
);
|
2021-05-28 15:47:39 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
'psalm-import-type' => ['lines' => [1]],
|
|
|
|
'var' => ['lines' => [2], 'suggested_replacement' => 'param'],
|
2022-11-17 00:14:13 +01:00
|
|
|
'psalm-consistent-constructor' => [
|
|
|
|
'lines' => [3],
|
2022-12-18 17:15:15 +01:00
|
|
|
'suggested_replacement' => 'psalm-consistent-constructor on a class level',
|
|
|
|
],
|
2021-05-28 15:47:39 +02:00
|
|
|
],
|
2022-12-18 17:15:15 +01:00
|
|
|
$function_docblock->unexpected_tags,
|
2021-05-28 15:47:39 +02:00
|
|
|
);
|
|
|
|
}
|
2021-02-24 16:14:04 +01:00
|
|
|
}
|