2018-01-29 05:41:11 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\FileAnalyzer;
|
2018-01-29 05:41:11 +01:00
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class DocumentationTest extends TestCase
|
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
/** @var \Psalm\Internal\Analyzer\ProjectAnalyzer */
|
2018-11-11 18:01:14 +01:00
|
|
|
protected $project_analyzer;
|
2018-01-29 05:41:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, array<int, string>>
|
|
|
|
*/
|
|
|
|
private static function getCodeBlocksFromDocs()
|
|
|
|
{
|
|
|
|
$issue_file = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'docs' . DIRECTORY_SEPARATOR . 'issues.md';
|
|
|
|
|
|
|
|
if (!file_exists($issue_file)) {
|
|
|
|
throw new \UnexpectedValueException('docs not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_contents = file_get_contents($issue_file);
|
|
|
|
|
|
|
|
if (!$file_contents) {
|
|
|
|
throw new \UnexpectedValueException('Docs are empty');
|
|
|
|
}
|
|
|
|
|
2018-04-13 01:42:24 +02:00
|
|
|
$file_lines = explode("\n", $file_contents);
|
2018-01-29 05:41:11 +01:00
|
|
|
|
|
|
|
$issue_code = [];
|
|
|
|
|
|
|
|
$current_issue = null;
|
|
|
|
|
|
|
|
for ($i = 0, $j = count($file_lines); $i < $j; ++$i) {
|
|
|
|
$current_line = $file_lines[$i];
|
|
|
|
|
|
|
|
if (substr($current_line, 0, 4) === '### ') {
|
|
|
|
$current_issue = trim(substr($current_line, 4));
|
|
|
|
++$i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (substr($current_line, 0, 6) === '```php' && $current_issue) {
|
|
|
|
$current_block = '';
|
|
|
|
++$i;
|
|
|
|
|
|
|
|
do {
|
2018-04-13 01:42:24 +02:00
|
|
|
$current_block .= $file_lines[$i] . "\n";
|
2018-01-29 05:41:11 +01:00
|
|
|
++$i;
|
|
|
|
} while (substr($file_lines[$i], 0, 3) !== '```' && $i < $j);
|
|
|
|
|
2018-02-22 00:59:31 +01:00
|
|
|
$issue_code[(string) $current_issue][] = trim($current_block);
|
2018-01-29 05:41:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $issue_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
FileAnalyzer::clearCache();
|
2018-11-12 16:46:55 +01:00
|
|
|
\Psalm\Internal\FileManipulation\FunctionDocblockManipulator::clearCache();
|
2018-01-29 05:41:11 +01:00
|
|
|
|
|
|
|
$this->file_provider = new Provider\FakeFileProvider();
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = new \Psalm\Internal\Analyzer\ProjectAnalyzer(
|
2018-01-29 05:41:11 +01:00
|
|
|
new TestConfig(),
|
2018-11-06 03:57:36 +01:00
|
|
|
new \Psalm\Internal\Provider\Providers(
|
2018-09-28 22:18:45 +02:00
|
|
|
$this->file_provider,
|
|
|
|
new Provider\FakeParserCacheProvider()
|
|
|
|
)
|
2018-01-29 05:41:11 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-29 06:39:21 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAllIssuesCovered()
|
|
|
|
{
|
|
|
|
$all_issues = ConfigTest::getAllIssues();
|
|
|
|
sort($all_issues);
|
|
|
|
|
|
|
|
$code_blocks = self::getCodeBlocksFromDocs();
|
|
|
|
|
|
|
|
// these cannot have code
|
|
|
|
$code_blocks['UnrecognizedExpression'] = true;
|
|
|
|
$code_blocks['UnrecognizedStatement'] = true;
|
|
|
|
|
|
|
|
$documented_issues = array_keys($code_blocks);
|
|
|
|
sort($documented_issues);
|
|
|
|
|
2018-04-13 01:42:24 +02:00
|
|
|
$this->assertSame(implode("\n", $all_issues), implode("\n", $documented_issues));
|
2018-01-29 06:39:21 +01:00
|
|
|
}
|
|
|
|
|
2018-01-29 05:41:11 +01:00
|
|
|
/**
|
2018-11-06 03:57:36 +01:00
|
|
|
* @dataProvider providerInvalidCodeParse
|
2018-01-29 05:41:11 +01:00
|
|
|
* @small
|
|
|
|
*
|
|
|
|
* @param string $code
|
|
|
|
* @param string $error_message
|
|
|
|
* @param array<string> $error_levels
|
|
|
|
* @param bool $check_references
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInvalidCode($code, $error_message, $error_levels = [], $check_references = false)
|
|
|
|
{
|
2018-07-13 23:44:50 +02:00
|
|
|
if (strpos($this->getTestName(), 'SKIPPED-') !== false) {
|
2018-01-29 05:41:11 +01:00
|
|
|
$this->markTestSkipped();
|
|
|
|
}
|
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
if ($check_references) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer->getCodebase()->reportUnusedCode();
|
2018-02-04 00:52:35 +01:00
|
|
|
}
|
2018-01-29 05:41:11 +01:00
|
|
|
|
|
|
|
foreach ($error_levels as $error_level) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer->getCodebase()->config->setCustomErrorLevel($error_level, Config::REPORT_SUPPRESS);
|
2018-01-29 05:41:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->expectException('\Psalm\Exception\CodeException');
|
|
|
|
$this->expectExceptionMessageRegexp('/\b' . preg_quote($error_message, '/') . '\b/');
|
|
|
|
|
|
|
|
$file_path = self::$src_dir_path . 'somefile.php';
|
|
|
|
|
|
|
|
$this->addFile($file_path, $code);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
$context->collect_references = $check_references;
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, $context);
|
|
|
|
|
|
|
|
if ($check_references) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer->getCodebase()->classlikes->checkClassReferences();
|
2018-01-29 05:41:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerInvalidCodeParse()
|
2018-01-29 05:41:11 +01:00
|
|
|
{
|
|
|
|
$invalid_code_data = [];
|
|
|
|
|
|
|
|
foreach (self::getCodeBlocksFromDocs() as $issue_name => $blocks) {
|
|
|
|
switch ($issue_name) {
|
2018-06-22 07:13:49 +02:00
|
|
|
case 'MissingThrowsDocblock':
|
|
|
|
continue 2;
|
|
|
|
|
2018-03-06 18:19:50 +01:00
|
|
|
case 'InvalidStringClass':
|
|
|
|
continue 2;
|
|
|
|
|
2018-09-18 23:08:32 +02:00
|
|
|
case 'ForbiddenEcho':
|
|
|
|
continue 2;
|
|
|
|
|
2018-01-29 05:41:11 +01:00
|
|
|
case 'InvalidFalsableReturnType':
|
|
|
|
$ignored_issues = ['FalsableReturnStatement'];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'InvalidNullableReturnType':
|
|
|
|
$ignored_issues = ['NullableReturnStatement'];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'InvalidReturnType':
|
|
|
|
$ignored_issues = ['InvalidReturnStatement'];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'MixedInferredReturnType':
|
|
|
|
$ignored_issues = ['MixedReturnStatement'];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'MixedStringOffsetAssignment':
|
|
|
|
$ignored_issues = ['MixedAssignment'];
|
|
|
|
break;
|
|
|
|
|
2018-05-08 22:34:08 +02:00
|
|
|
case 'ParadoxicalCondition':
|
|
|
|
$ignored_issues = ['MissingParamType'];
|
|
|
|
break;
|
|
|
|
|
2018-01-29 05:41:11 +01:00
|
|
|
case 'UnusedClass':
|
|
|
|
case 'UnusedMethod':
|
|
|
|
$ignored_issues = ['UnusedVariable'];
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$ignored_issues = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$invalid_code_data[$issue_name] = [
|
2018-04-13 01:42:24 +02:00
|
|
|
'<?php' . "\n" . $blocks[0],
|
2018-01-29 05:41:11 +01:00
|
|
|
$issue_name,
|
|
|
|
$ignored_issues,
|
|
|
|
strpos($issue_name, 'Unused') !== false || strpos($issue_name, 'Unevaluated') !== false,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $invalid_code_data;
|
|
|
|
}
|
|
|
|
}
|