Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Pospisil committed Dec 10, 2019
1 parent e9062d9 commit 6d020d3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 36 deletions.
55 changes: 39 additions & 16 deletions example/phpstan.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
body {
background-color: #34495e;
color: white;
font-family: sans-serif;
font-family: monospace;
font-size: 16px;
}

b {
color: lightskyblue;
}

h1 {
Expand All @@ -24,23 +29,35 @@
color: yellow;
}

table {
width: 100%;
font-size: 12px;
}

th {
text-align: left;
padding: 25px 0 10px 0;
}

td {
padding: 5px;
}

.section {
font-size: 1.4em;
}

.line {
text-align: right;
vertical-align: top;
padding: 1px 10px 0 0px;
vertical-align: middle;
}

tr:nth-child(even) {
background: #475a73
.error:nth-child(odd) {
background: #374d63
}

.regex {
color: #00000085;
}

#thanks {
Expand All @@ -52,7 +69,7 @@
<title>PHPStan analysis result</title>
</head>
<h1>PHPStan analysis result</h1>
<div class="time">2018-11-02 01:46:21</div>
<div class="time">2019-12-10 17:40:27</div>


<div>
Expand All @@ -64,29 +81,35 @@ <h1>PHPStan analysis result</h1>
</thead>
<thead>
<tr>
<th colspan="2">C:\wamp64\www\PHPStanFileOutput\tests\MessedUpClass.php (3×)</th>
<th colspan="2">C:\wamp\www\PHPStanFileOutput\tests\MessedUpClass.php (3×)</th>
</tr>
</thead>
<tr>
<tr class="error">
<td class="line">
<a href='editor://open/?file=C:\wamp64\www\PHPStanFileOutput\tests\MessedUpClass.php&line=25'>25</a>
<a href='editor://open/?file=C:\wamp\www\PHPStanFileOutput\tests\MessedUpClass.php&line=25'>25</a>
</td>
<td>
Method <b>noximo\MessedUpClass::doSOmething()</b> should return <b>string</b> but returns <b>int.</b> </td>
Method <b>noximo\MessedUpClass::doSOmething()</b> should return <b>string</b> but returns <b>int.</b> <br>
<small class="regex">- '#Method noximo\\MessedUpClass\:\:doSOmething\(\) should return string but returns int#'</small>
</td>
</tr>
<tr>
<tr class="error">
<td class="line">
<a href='editor://open/?file=C:\wamp64\www\PHPStanFileOutput\tests\MessedUpClass.php&line=23'>23</a>
<a href='editor://open/?file=C:\wamp\www\PHPStanFileOutput\tests\MessedUpClass.php&line=23'>23</a>
</td>
<td>
PHPDoc tag <b>@param</b> for parameter <b>$problem</b> with type <b>string</b> is incompatible with native type <b>int</b> </td>
PHPDoc tag <b>@param</b> for parameter <b>$problem</b> with type <b>string</b> is incompatible with native type <b>int.</b> <br>
<small class="regex">- '#PHPDoc tag @param for parameter \$problem with type string is incompatible with native type int#'</small>
</td>
</tr>
<tr>
<tr class="error">
<td class="line">
<a href='editor://open/?file=C:\wamp64\www\PHPStanFileOutput\tests\MessedUpClass.php&line=23'>23</a>
<a href='editor://open/?file=C:\wamp\www\PHPStanFileOutput\tests\MessedUpClass.php&line=23'>23</a>
</td>
<td>
PHPDoc tag <b>@return</b> with type <b>int</b> is incompatible with native type <b>string</b> </td>
PHPDoc tag <b>@return</b> with type <b>int</b> is incompatible with native type <b>string.</b> <br>
<small class="regex">- '#PHPDoc tag @return with type int is incompatible with native type string#'</small>
</td>
</tr>

</table>
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
services:
errorFormatter.fileoutput:
class: noximo\FileOutput(./example/phpstan.html, null)
factory: noximo\FileOutput(./example/phpstan.html, null)
47 changes: 28 additions & 19 deletions src/FileOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
use Nette\Utils\Strings;
use PHPStan\Command\AnalysisResult;
use PHPStan\Command\ErrorFormatter\ErrorFormatter;
use Symfony\Component\Console\Style\OutputStyle;
use PHPStan\Command\Output;
use Safe\Exceptions\DirException;
use Webmozart\PathUtil\Path;

class FileOutput implements ErrorFormatter
final class FileOutput implements ErrorFormatter
{
/** @var string */
public const ERROR = 'error';
Expand Down Expand Up @@ -53,7 +54,7 @@ class FileOutput implements ErrorFormatter

/**
* FileOutput constructor.
* @throws \Safe\Exceptions\DirException
* @throws DirException
*/
public function __construct(string $outputFile, ?ErrorFormatter $defaultFormatterClass = null, ?string $customTemplate = null)
{
Expand All @@ -78,28 +79,31 @@ public function __construct(string $outputFile, ?ErrorFormatter $defaultFormatte
* Formats the errors and outputs them to the console.
* @return int Error code.
*/
public function formatErrors(AnalysisResult $analysisResult, OutputStyle $style): int
public function formatErrors(AnalysisResult $analysisResult, Output $output): int
{
if ($this->defaultFormatter !== null) {
$this->defaultFormatter->formatErrors($analysisResult, $style);
$this->defaultFormatter->formatErrors($analysisResult, $output);
}
try {
$this->generateFile($analysisResult);
$style->writeln('Note: Analysis outputted into file ' . $this->outputFile . '.');
$output->getStyle()->note('Note: Analysis outputted into file ' . $this->outputFile . '.');
} catch (IOException $e) {
$style->error('Analysis could not be outputted into file. ' . $e->getMessage());
$output->getStyle()->error('Analysis could not be outputted into file. ' . $e->getMessage());
}

return $analysisResult->hasErrors() ? 1 : 0;
}

/**
* @param mixed[] $data
* @return string
*/
public function getTable(array $data): string
{
ob_start(function (): void {
});
ob_start(
static function (): void {
}
);
require $this->template;

$output = ob_get_clean();
Expand Down Expand Up @@ -131,9 +135,12 @@ private function generateFile(AnalysisResult $analysisResult): void
}

foreach ($output[self::FILES] as &$file) {
usort($file, function ($a, $b) {
return -1 * ($a[self::LINE] <=> $b[self::LINE]);
});
usort(
$file,
static function ($a, $b) {
return -1 * ($a[self::LINE] <=> $b[self::LINE]);
}
);
}
unset($file);
}
Expand All @@ -144,21 +151,23 @@ private function generateFile(AnalysisResult $analysisResult): void
private static function formatMessage(string $message): string
{
$words = explode(' ', $message);
$words = array_map(function ($word) {
if (Strings::match($word, '/[^a-zA-Z,.]|(string)|(bool)|(boolean)|(int)|(integer)|(float)/')) {
$word = '<b>' . $word . '</b>';
}
$words = array_map(
static function ($word) {
if (Strings::match($word, '/[^a-zA-Z,.]|(string)|(bool)|(boolean)|(int)|(integer)|(float)/')) {
$word = '<b>' . $word . '</b>';
}

return $word;
}, $words);
return $word;
},
$words
);

return implode(' ', $words);
}

/**
* @param string $message
* @return string
* @throws RegexpException
*/
private static function formatRegex(string $message): string
{
Expand Down

0 comments on commit 6d020d3

Please sign in to comment.