2018-10-30 15:32:20 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm;
|
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
use function array_filter;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function array_intersect;
|
|
|
|
use function array_map;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function array_merge;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function array_reduce;
|
|
|
|
use function explode;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function get_loaded_extensions;
|
|
|
|
use function implode;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function ksort;
|
|
|
|
use const LIBXML_NOBLANKS;
|
|
|
|
use function min;
|
|
|
|
use const PHP_VERSION;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function phpversion;
|
2019-07-05 21:40:46 +02:00
|
|
|
use function preg_replace_callback;
|
2019-07-05 22:24:00 +02:00
|
|
|
use Psalm\Internal\Provider\FileProvider;
|
2019-07-05 22:30:51 +02:00
|
|
|
use RuntimeException;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function str_replace;
|
|
|
|
use function strpos;
|
|
|
|
use function usort;
|
2018-10-30 15:32:20 +01:00
|
|
|
|
|
|
|
class ErrorBaseline
|
|
|
|
{
|
2019-02-09 12:13:58 +01:00
|
|
|
/**
|
|
|
|
* @param array<string,array<string,array{o:int, s:array<int, string>}>> $existingIssues
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
2019-02-09 12:13:58 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public static function countTotalIssues(array $existingIssues)
|
|
|
|
{
|
|
|
|
$totalIssues = 0;
|
|
|
|
|
|
|
|
foreach ($existingIssues as $existingIssue) {
|
|
|
|
$totalIssues += array_reduce(
|
|
|
|
$existingIssue,
|
|
|
|
function (int $carry, array $existingIssue): int {
|
|
|
|
return $carry + (int)$existingIssue['o'];
|
|
|
|
},
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $totalIssues;
|
|
|
|
}
|
|
|
|
|
2018-10-30 15:32:20 +01:00
|
|
|
/**
|
|
|
|
* @param FileProvider $fileProvider
|
|
|
|
* @param string $baselineFile
|
2018-11-09 06:46:13 +01:00
|
|
|
* @param array<array{file_name: string, type: string, severity: string, selected_text: string}> $issues
|
2018-10-30 15:32:20 +01:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-07-11 16:41:44 +02:00
|
|
|
public static function create(
|
|
|
|
FileProvider $fileProvider,
|
|
|
|
string $baselineFile,
|
|
|
|
array $issues,
|
|
|
|
bool $include_php_versions
|
|
|
|
) {
|
2018-10-30 15:32:20 +01:00
|
|
|
$groupedIssues = self::countIssueTypesByFile($issues);
|
|
|
|
|
2019-07-11 16:41:44 +02:00
|
|
|
self::writeToFile($fileProvider, $baselineFile, $groupedIssues, $include_php_versions);
|
2018-10-30 15:32:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FileProvider $fileProvider
|
|
|
|
* @param string $baselineFile
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
2018-10-30 15:32:20 +01:00
|
|
|
* @throws Exception\ConfigException
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
|
|
|
* @return array<string,array<string,array{o:int, s:array<int, string>}>>
|
2018-10-30 15:32:20 +01:00
|
|
|
*/
|
|
|
|
public static function read(FileProvider $fileProvider, string $baselineFile): array
|
|
|
|
{
|
|
|
|
if (!$fileProvider->fileExists($baselineFile)) {
|
2019-06-28 16:49:57 +02:00
|
|
|
throw new Exception\ConfigException("{$baselineFile} does not exist or is not readable");
|
2018-10-30 15:32:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$xmlSource = $fileProvider->getContents($baselineFile);
|
|
|
|
|
|
|
|
$baselineDoc = new \DOMDocument();
|
|
|
|
$baselineDoc->loadXML($xmlSource, LIBXML_NOBLANKS);
|
|
|
|
|
|
|
|
/** @var \DOMNodeList $filesElement */
|
|
|
|
$filesElement = $baselineDoc->getElementsByTagName('files');
|
|
|
|
|
|
|
|
if ($filesElement->length === 0) {
|
|
|
|
throw new Exception\ConfigException('Baseline file does not contain <files>');
|
|
|
|
}
|
|
|
|
|
|
|
|
$files = [];
|
|
|
|
|
|
|
|
/** @var \DOMElement $filesElement */
|
|
|
|
$filesElement = $filesElement[0];
|
|
|
|
|
|
|
|
/** @var \DOMElement $file */
|
|
|
|
foreach ($filesElement->getElementsByTagName('file') as $file) {
|
|
|
|
$fileName = $file->getAttribute('src');
|
|
|
|
|
2019-01-10 06:10:09 +01:00
|
|
|
$fileName = str_replace('\\', '/', $fileName);
|
|
|
|
|
2018-10-30 15:32:20 +01:00
|
|
|
$files[$fileName] = [];
|
|
|
|
|
|
|
|
/** @var \DOMElement $issue */
|
|
|
|
foreach ($file->childNodes as $issue) {
|
|
|
|
$issueType = $issue->tagName;
|
|
|
|
|
2018-11-09 06:46:13 +01:00
|
|
|
$files[$fileName][$issueType] = [
|
|
|
|
'o' => (int)$issue->getAttribute('occurrences'),
|
|
|
|
's' => [],
|
|
|
|
];
|
|
|
|
$codeSamples = $issue->getElementsByTagName('code');
|
|
|
|
|
|
|
|
/** @var \DOMElement $codeSample */
|
|
|
|
foreach ($codeSamples as $codeSample) {
|
|
|
|
$files[$fileName][$issueType]['s'][] = (string) $codeSample->textContent;
|
|
|
|
}
|
2018-10-30 15:32:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FileProvider $fileProvider
|
|
|
|
* @param string $baselineFile
|
2018-11-09 06:46:13 +01:00
|
|
|
* @param array<array{file_name: string, type: string, severity: string, selected_text: string}> $issues
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
2018-10-30 15:32:20 +01:00
|
|
|
* @throws Exception\ConfigException
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
|
|
|
* @return array<string,array<string,array{o:int, s:array<int, string>}>>
|
2018-10-30 15:32:20 +01:00
|
|
|
*/
|
2019-07-11 16:41:44 +02:00
|
|
|
public static function update(
|
|
|
|
FileProvider $fileProvider,
|
|
|
|
string $baselineFile,
|
|
|
|
array $issues,
|
|
|
|
bool $include_php_versions
|
|
|
|
) {
|
2018-10-30 15:32:20 +01:00
|
|
|
$existingIssues = self::read($fileProvider, $baselineFile);
|
|
|
|
$newIssues = self::countIssueTypesByFile($issues);
|
|
|
|
|
|
|
|
foreach ($existingIssues as $file => &$existingIssuesCount) {
|
|
|
|
if (!isset($newIssues[$file])) {
|
|
|
|
unset($existingIssues[$file]);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-09 14:43:17 +01:00
|
|
|
foreach ($existingIssuesCount as $issueType => $existingIssueType) {
|
2018-10-30 15:32:20 +01:00
|
|
|
if (!isset($newIssues[$file][$issueType])) {
|
|
|
|
unset($existingIssuesCount[$issueType]);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-09 14:43:17 +01:00
|
|
|
$existingIssuesCount[$issueType]['o'] = min(
|
|
|
|
$existingIssueType['o'],
|
|
|
|
$newIssues[$file][$issueType]['o']
|
|
|
|
);
|
2018-11-09 06:46:13 +01:00
|
|
|
$existingIssuesCount[$issueType]['s'] = array_intersect(
|
2018-11-09 14:43:17 +01:00
|
|
|
$existingIssueType['s'],
|
2018-11-09 06:46:13 +01:00
|
|
|
$newIssues[$file][$issueType]['s']
|
|
|
|
);
|
2018-10-30 15:32:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$groupedIssues = array_filter($existingIssues);
|
|
|
|
|
2019-07-11 16:41:44 +02:00
|
|
|
self::writeToFile($fileProvider, $baselineFile, $groupedIssues, $include_php_versions);
|
2018-10-30 15:32:20 +01:00
|
|
|
|
|
|
|
return $groupedIssues;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-09 06:46:13 +01:00
|
|
|
* @param array<array{file_name: string, type: string, severity: string, selected_text: string}> $issues
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
2018-11-09 06:46:13 +01:00
|
|
|
* @return array<string,array<string,array{o:int, s:array<int, string>}>>
|
2018-10-30 15:32:20 +01:00
|
|
|
*/
|
|
|
|
private static function countIssueTypesByFile(array $issues): array
|
|
|
|
{
|
|
|
|
$groupedIssues = array_reduce(
|
|
|
|
$issues,
|
|
|
|
/**
|
2018-11-09 06:46:13 +01:00
|
|
|
* @param array<string,array<string,array{o:int, s:array<int, string>}>> $carry
|
|
|
|
* @param array{type: string, file_name: string, severity: string, selected_text: string} $issue
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
2018-11-09 06:46:13 +01:00
|
|
|
* @return array<string,array<string,array{o:int, s:array<int, string>}>>
|
2018-10-30 15:32:20 +01:00
|
|
|
*/
|
|
|
|
function (array $carry, array $issue): array {
|
|
|
|
if ($issue['severity'] !== Config::REPORT_ERROR) {
|
|
|
|
return $carry;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fileName = $issue['file_name'];
|
2019-06-09 16:30:14 +02:00
|
|
|
$fileName = str_replace('\\', '/', $fileName);
|
2018-10-30 15:32:20 +01:00
|
|
|
$issueType = $issue['type'];
|
|
|
|
|
|
|
|
if (!isset($carry[$fileName])) {
|
|
|
|
$carry[$fileName] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($carry[$fileName][$issueType])) {
|
2018-11-09 06:46:13 +01:00
|
|
|
$carry[$fileName][$issueType] = ['o' => 0, 's' => []];
|
2018-10-30 15:32:20 +01:00
|
|
|
}
|
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
++$carry[$fileName][$issueType]['o'];
|
2018-11-09 06:46:13 +01:00
|
|
|
|
|
|
|
if (!strpos($issue['selected_text'], "\n")) {
|
|
|
|
$carry[$fileName][$issueType]['s'][] = $issue['selected_text'];
|
|
|
|
}
|
2018-10-30 15:32:20 +01:00
|
|
|
|
|
|
|
return $carry;
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Sort files first
|
|
|
|
ksort($groupedIssues);
|
|
|
|
|
|
|
|
foreach ($groupedIssues as &$issues) {
|
|
|
|
ksort($issues);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $groupedIssues;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FileProvider $fileProvider
|
|
|
|
* @param string $baselineFile
|
2018-11-09 06:46:13 +01:00
|
|
|
* @param array<string,array<string,array{o:int, s:array<int, string>}>> $groupedIssues
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
2018-10-30 15:32:20 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private static function writeToFile(
|
|
|
|
FileProvider $fileProvider,
|
|
|
|
string $baselineFile,
|
2019-07-11 16:41:44 +02:00
|
|
|
array $groupedIssues,
|
|
|
|
bool $include_php_versions
|
2018-10-30 15:32:20 +01:00
|
|
|
) {
|
|
|
|
$baselineDoc = new \DOMDocument('1.0', 'UTF-8');
|
|
|
|
$filesNode = $baselineDoc->createElement('files');
|
2019-05-24 14:32:14 +02:00
|
|
|
$filesNode->setAttribute('psalm-version', PSALM_VERSION);
|
2018-10-30 15:32:20 +01:00
|
|
|
|
2019-07-11 16:41:44 +02:00
|
|
|
if ($include_php_versions) {
|
|
|
|
$extensions = array_merge(get_loaded_extensions(), get_loaded_extensions(true));
|
|
|
|
|
|
|
|
usort($extensions, 'strnatcasecmp');
|
|
|
|
|
|
|
|
$filesNode->setAttribute('php-version', implode(';' . "\n\t", array_merge(
|
|
|
|
[
|
|
|
|
('php:' . PHP_VERSION),
|
|
|
|
],
|
|
|
|
array_map(
|
|
|
|
function (string $extension) : string {
|
|
|
|
return $extension . ':' . phpversion($extension);
|
|
|
|
},
|
|
|
|
$extensions
|
|
|
|
)
|
|
|
|
)));
|
|
|
|
}
|
2019-06-17 22:43:54 +02:00
|
|
|
|
2018-10-30 15:32:20 +01:00
|
|
|
foreach ($groupedIssues as $file => $issueTypes) {
|
|
|
|
$fileNode = $baselineDoc->createElement('file');
|
2019-04-11 17:02:46 +02:00
|
|
|
|
2018-10-30 15:32:20 +01:00
|
|
|
$fileNode->setAttribute('src', $file);
|
|
|
|
|
2018-11-09 14:43:17 +01:00
|
|
|
foreach ($issueTypes as $issueType => $existingIssueType) {
|
2018-10-30 15:32:20 +01:00
|
|
|
$issueNode = $baselineDoc->createElement($issueType);
|
2019-04-11 17:02:46 +02:00
|
|
|
|
2018-11-09 14:43:17 +01:00
|
|
|
$issueNode->setAttribute('occurrences', (string)$existingIssueType['o']);
|
|
|
|
foreach ($existingIssueType['s'] as $selection) {
|
2018-11-09 06:46:13 +01:00
|
|
|
$codeNode = $baselineDoc->createElement('code');
|
2019-04-11 17:02:46 +02:00
|
|
|
|
2018-11-09 06:46:13 +01:00
|
|
|
$codeNode->textContent = $selection;
|
|
|
|
$issueNode->appendChild($codeNode);
|
|
|
|
}
|
2018-10-30 15:32:20 +01:00
|
|
|
$fileNode->appendChild($issueNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
$filesNode->appendChild($fileNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
$baselineDoc->appendChild($filesNode);
|
|
|
|
$baselineDoc->formatOutput = true;
|
|
|
|
|
2019-07-05 21:40:46 +02:00
|
|
|
$xml = preg_replace_callback(
|
|
|
|
'/<files (psalm-version="[^"]+") (?:php-version="(.+)">\n)/',
|
|
|
|
/**
|
|
|
|
* @param array<int, string> $matches
|
|
|
|
*/
|
|
|
|
function (array $matches) : string {
|
|
|
|
return
|
|
|
|
'<files' .
|
|
|
|
"\n " .
|
|
|
|
$matches[1] .
|
|
|
|
"\n" .
|
|
|
|
' php-version="' .
|
|
|
|
"\n " .
|
|
|
|
implode("\n ", explode(' 	', $matches[2])) .
|
|
|
|
"\n" .
|
|
|
|
' "' .
|
|
|
|
"\n" .
|
|
|
|
'>' .
|
|
|
|
"\n";
|
|
|
|
},
|
|
|
|
$baselineDoc->saveXML()
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($xml === null) {
|
|
|
|
throw new RuntimeException('Failed to reformat opening attributes!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$fileProvider->setContents($baselineFile, $xml);
|
2018-10-30 15:32:20 +01:00
|
|
|
}
|
|
|
|
}
|