1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PHPStan\Command\ErrorFormatter; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use PHPStan\Analyser\Error; |
9
|
|
|
use PHPStan\Command\AnalysisResult; |
10
|
|
|
use PHPStan\Command\ErrorsConsoleStyle; |
11
|
|
|
use PHPStan\ShouldNotHappenException; |
12
|
|
|
use PHPStan\Testing\TestCase; |
13
|
|
|
use PHPStanVendor\Symfony\Component\Console\Input\StringInput; |
14
|
|
|
use PHPStanVendor\Symfony\Component\Console\Output\StreamOutput; |
15
|
|
|
|
16
|
|
|
abstract class TestBaseFormatter extends TestCase |
17
|
|
|
{ |
18
|
|
|
const DIRECTORY_PATH = '/data/folder/with space/and unicode 😃/project'; |
19
|
|
|
|
20
|
|
|
/** @var StreamOutput */ |
21
|
|
|
private $outputStream; |
22
|
|
|
|
23
|
|
|
/** @var ErrorsConsoleStyle */ |
24
|
|
|
private $errorConsoleStyle; |
25
|
|
|
|
26
|
|
|
protected function setUp() |
27
|
|
|
{ |
28
|
|
|
parent::setUp(); |
29
|
|
|
|
30
|
|
|
$resource = fopen('php://memory', 'w', false); |
31
|
|
|
if ($resource === false) { |
32
|
|
|
throw new ShouldNotHappenException(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->outputStream = new StreamOutput($resource); |
36
|
|
|
|
37
|
|
|
$this->errorConsoleStyle = new ErrorsConsoleStyle(new StringInput(''), $this->outputStream); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function getOutputContent(): string |
41
|
|
|
{ |
42
|
|
|
rewind($this->outputStream->getStream()); |
43
|
|
|
|
44
|
|
|
$contents = stream_get_contents($this->outputStream->getStream()); |
45
|
|
|
if ($contents === false) { |
46
|
|
|
throw new ShouldNotHappenException(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this->rtrimMultiline($contents); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function getErrorConsoleStyle(): ErrorsConsoleStyle |
53
|
|
|
{ |
54
|
|
|
return $this->errorConsoleStyle; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function getAnalysisResult(int $numFileErrors, int $numGenericErrors): AnalysisResult |
58
|
|
|
{ |
59
|
|
|
if ($numFileErrors > 4 || $numFileErrors < 0 || $numGenericErrors > 2 || $numGenericErrors < 0) { |
60
|
|
|
throw new Exception(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$fileErrors = array_slice( |
64
|
|
|
[ |
65
|
|
|
new Error('Foo', self::DIRECTORY_PATH.'/folder with unicode 😃/file name with "spaces" and unicode 😃.php', 4), |
66
|
|
|
new Error('Foo', self::DIRECTORY_PATH.'/foo.php', 1), |
67
|
|
|
new Error('Bar', self::DIRECTORY_PATH.'/foo.php', 5), |
68
|
|
|
new Error('Bar', self::DIRECTORY_PATH.'/folder with unicode 😃/file name with "spaces" and unicode 😃.php', 2), |
69
|
|
|
], |
70
|
|
|
0, |
71
|
|
|
$numFileErrors |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$genericErrors = array_slice( |
75
|
|
|
[ |
76
|
|
|
'first generic error', |
77
|
|
|
'second generic error', |
78
|
|
|
], |
79
|
|
|
0, |
80
|
|
|
$numGenericErrors |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
return new AnalysisResult( |
84
|
|
|
$fileErrors, |
85
|
|
|
$genericErrors, |
86
|
|
|
false, |
87
|
|
|
self::DIRECTORY_PATH |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function rtrimMultiline(string $output): string |
92
|
|
|
{ |
93
|
|
|
$result = array_map( |
94
|
|
|
static function (string $line): string { |
95
|
|
|
return rtrim($line, " \r\n"); |
96
|
|
|
}, |
97
|
|
|
explode("\n", $output) |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
return implode("\n", $result); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|