-
Notifications
You must be signed in to change notification settings - Fork 696
Expand file tree
/
Copy pathFunctionLikeDocblockParserTest.php
More file actions
166 lines (146 loc) · 4.84 KB
/
FunctionLikeDocblockParserTest.php
File metadata and controls
166 lines (146 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
declare(strict_types=1);
namespace Psalm\Tests;
use Override;
use PHPUnit\Framework\TestCase as BaseTestCase;
use PhpParser\Comment\Doc;
use PhpParser\Node\Scalar\String_;
use Psalm\CodeLocation;
use Psalm\Exception\IncorrectDocblockException;
use Psalm\Internal\Analyzer\FileAnalyzer;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\PhpVisitor\Reflector\FunctionLikeDocblockParser;
use Psalm\Internal\Provider\FakeFileProvider;
use Psalm\Internal\Provider\Providers;
use Psalm\Internal\RuntimeCaches;
use Psalm\Tests\Internal\Provider\FakeParserCacheProvider;
final class FunctionLikeDocblockParserTest extends BaseTestCase
{
public string $test_cased_function_id = 'hello_world';
public CodeLocation $test_code_location;
#[Override]
public function setUp(): void
{
RuntimeCaches::clearAll();
$file_provider = new FakeFileProvider();
$providers = new Providers(
$file_provider,
new FakeParserCacheProvider(),
);
$test_config = new TestConfig();
$project_analyzer = new ProjectAnalyzer(
$test_config,
$providers,
);
$file_analyzer = new FileAnalyzer($project_analyzer, 'none/none.php', 'none.php');
$stmt = new String_('randomString');
$this->test_code_location = new CodeLocation($file_analyzer, $stmt);
}
public function testDocblockDescription(): void
{
$doc = '/**
* Some Description
*
* @param string $bli
* @param int $bla
*
* @throws \Exception
*
* @return bool
*/
';
$php_parser_doc = new Doc($doc);
$function_docblock = FunctionLikeDocblockParser::parse(
ProjectAnalyzer::getInstance()->getCodebase(),
$php_parser_doc,
$this->test_code_location,
$this->test_cased_function_id,
);
$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
*/
';
$php_parser_doc = new Doc($doc);
$function_docblock = FunctionLikeDocblockParser::parse(
ProjectAnalyzer::getInstance()->getCodebase(),
$php_parser_doc,
$this->test_code_location,
$this->test_cased_function_id,
);
$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']);
}
public function testMisplacedVariableOnNextLine(): void
{
$doc = '/**
* @param
* $p
*/';
$php_parser_doc = new Doc($doc);
$this->expectException(IncorrectDocblockException::class);
$this->expectExceptionMessage('Misplaced variable');
FunctionLikeDocblockParser::parse(
ProjectAnalyzer::getInstance()->getCodebase(),
$php_parser_doc,
$this->test_code_location,
$this->test_cased_function_id,
);
}
public function testPreferPsalmPrefixedAnnotationsOverPhpstanOnes(): void
{
$doc = '/**
* @psalm-template T of string
* @phpstan-template T of int
*/
';
$php_parser_doc = new Doc($doc);
$function_docblock = FunctionLikeDocblockParser::parse(
ProjectAnalyzer::getInstance()->getCodebase(),
$php_parser_doc,
$this->test_code_location,
$this->test_cased_function_id,
);
$this->assertSame([['T', 'of', 'string', false]], $function_docblock->templates);
}
public function testReturnsUnexpectedTags(): void
{
$doc = '/**
* @psalm-import-type abcd
* @var int $p
* @psalm-consistent-constructor
*/
';
$php_parser_doc = new Doc($doc, 0);
$function_docblock = FunctionLikeDocblockParser::parse(
ProjectAnalyzer::getInstance()->getCodebase(),
$php_parser_doc,
$this->test_code_location,
$this->test_cased_function_id,
);
$this->assertEquals(
[
'psalm-import-type' => ['lines' => [1]],
'var' => ['lines' => [2], 'suggested_replacement' => 'param'],
'psalm-consistent-constructor' => [
'lines' => [3],
'suggested_replacement' => 'psalm-consistent-constructor on a class level',
],
],
$function_docblock->unexpected_tags,
);
}
}