Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ $ git clone ...

$ phpstan-baseline-analyze *phpstan-baseline.neon --json > now.json

# To use baseline file modification time as the analysis date instead of "now"
# Best used together with `git restore-mtime *phpstan-baseline.neon`
$ phpstan-baseline-analyze *phpstan-baseline.neon --json --mtime > now.json

$ git checkout `git rev-list -n 1 --before="1 week ago" HEAD`
$ phpstan-baseline-analyze '*phpstan-baseline.neon' --json > 1-week-ago.json

Expand All @@ -92,6 +96,8 @@ $ phpstan-baseline-analyze '*phpstan-baseline.neon' --json > 2-weeks-ago.json
$ php phpstan-baseline-graph '*.json' > result.html
```

See [git-tools](https://github.com/MestreLion/git-tools) for details on `git restore-mtime`

![PHPStan baseline analysis graph](https://github.com/staabm/phpstan-baseline-analysis/assets/120441/ea5abe25-21e8-43f2-9118-0967a75517c6)


Expand Down
7 changes: 6 additions & 1 deletion bin/phpstan-baseline-analyze.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@


$format = ResultPrinter::FORMAT_TEXT;
$useFileMtime = false;
if (in_array('--json', $argv)) {
$format = ResultPrinter::FORMAT_JSON;
}

$exitCode = $app->start($argv[1], $format);
if (in_array('--mtime', $argv)) {
$useFileMtime = true;
}

$exitCode = $app->start($argv[1], $format, $useFileMtime);
exit($exitCode);
3 changes: 2 additions & 1 deletion lib/AnalyzeApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class AnalyzeApplication
*
* @api
*/
public function start(string $glob, string $format): int
public function start(string $glob, string $format, bool $useFileMtime = false): int
{
$printer = new ResultPrinter();
$baselines = BaselineFinder::forGlob($glob);
Expand All @@ -36,6 +36,7 @@ public function start(string $glob, string $format): int
$isLast = $i == $numBaselines - 1;

$analyzer = new BaselineAnalyzer($baseline);
$analyzer->useFileMtime($useFileMtime);
$result = $analyzer->analyze();

if ($format == ResultPrinter::FORMAT_JSON) {
Expand Down
14 changes: 12 additions & 2 deletions lib/BaselineAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace staabm\PHPStanBaselineAnalysis;

use Safe\DateTimeImmutable;
use function Safe\preg_match;
use function Safe\filemtime;

final class BaselineAnalyzer
{
Expand Down Expand Up @@ -32,16 +32,26 @@
* @var Baseline
*/
private $baseline;
private bool $fileMtime;

public function __construct(Baseline $baseline)
{
$this->baseline = $baseline;
}

public function useFileMtime(bool $useFileMtime)

Check failure on line 42 in lib/BaselineAnalyzer.php

View workflow job for this annotation

GitHub Actions / phpstan static code analysis (ubuntu-latest, 7.4)

Method staabm\PHPStanBaselineAnalysis\BaselineAnalyzer::useFileMtime() has no return type specified.

Check failure on line 42 in lib/BaselineAnalyzer.php

View workflow job for this annotation

GitHub Actions / phpstan static code analysis (ubuntu-latest, 7.4)

Out of 55 possible return types, only 54 - 98.1 % actually have it. Add more return types to get over 100 %

Check failure on line 42 in lib/BaselineAnalyzer.php

View workflow job for this annotation

GitHub Actions / phpstan static code analysis (ubuntu-latest, 8.0)

Method staabm\PHPStanBaselineAnalysis\BaselineAnalyzer::useFileMtime() has no return type specified.

Check failure on line 42 in lib/BaselineAnalyzer.php

View workflow job for this annotation

GitHub Actions / phpstan static code analysis (ubuntu-latest, 8.0)

Out of 55 possible return types, only 54 - 98.1 % actually have it. Add more return types to get over 100 %

Check failure on line 42 in lib/BaselineAnalyzer.php

View workflow job for this annotation

GitHub Actions / phpstan static code analysis (ubuntu-latest, 8.1)

Method staabm\PHPStanBaselineAnalysis\BaselineAnalyzer::useFileMtime() has no return type specified.

Check failure on line 42 in lib/BaselineAnalyzer.php

View workflow job for this annotation

GitHub Actions / phpstan static code analysis (ubuntu-latest, 8.1)

Out of 55 possible return types, only 54 - 98.1 % actually have it. Add more return types to get over 100 %

Check failure on line 42 in lib/BaselineAnalyzer.php

View workflow job for this annotation

GitHub Actions / phpstan static code analysis (ubuntu-latest, 8.2)

Method staabm\PHPStanBaselineAnalysis\BaselineAnalyzer::useFileMtime() has no return type specified.

Check failure on line 42 in lib/BaselineAnalyzer.php

View workflow job for this annotation

GitHub Actions / phpstan static code analysis (ubuntu-latest, 8.2)

Out of 55 possible return types, only 54 - 98.1 % actually have it. Add more return types to get over 100 %
{
$this->fileMtime = $useFileMtime;
}

public function analyze(): AnalyzerResult
{
$result = new AnalyzerResult();
$result->referenceDate = new DateTimeImmutable();
if ($this->fileMtime) {
$result->referenceDate = DateTimeImmutable::createFromFormat("U", (string)filemtime($this->baseline->getFilePath()));
} else {
$result->referenceDate = new DateTimeImmutable();
}

/**
* @var BaselineError $baselineError
Expand Down
1 change: 1 addition & 0 deletions lib/GraphTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
}

foreach ($dataByDates as $baselinePath => $dataByDate) {
ksort($dataByDate);

Check failure on line 106 in lib/GraphTemplate.php

View workflow job for this annotation

GitHub Actions / phpstan static code analysis (ubuntu-latest, 7.4)

Function ksort is unsafe to use. It can return FALSE instead of throwing an exception. Please add 'use function Safe\ksort;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library.
foreach ($dataByDate as $date => $data) {
$dates[$baselinePath][] = 'new Date(' . $date . ' * 1000).toLocaleDateString("de-DE")';
$splines[$baselinePath][0]['data'][] = $data[0];
Expand Down
37 changes: 36 additions & 1 deletion tests/AnalyzeApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace staabm\PHPStanBaselineAnalysis\Tests;

use Safe\DateTimeImmutable;
use staabm\PHPStanBaselineAnalysis\AnalyzeApplication;
use staabm\PHPStanBaselineAnalysis\ResultPrinter;

use function Safe\filemtime;
use function Safe\json_encode;
use function Safe\ob_start;

class AnalyzeApplicationTest extends BaseTestCase
{
function testTextPrinting():void
function testTextPrintingWithCurrentTime():void
{
$app = new AnalyzeApplication();

Expand All @@ -33,6 +37,37 @@ function testTextPrinting():void
Native-Return-Type-Coverage: 4
Unused-Symbols: 3

PHP;

$this->assertSame($expected, $rendered);
$this->assertSame(0, $exitCode);
}

function testTextPrintingWithFileModificationTime():void
{
$app = new AnalyzeApplication();

ob_start();
$exitCode = $app->start(__DIR__ . '/fixtures/all-in.neon', ResultPrinter::FORMAT_TEXT, true);
$rendered = ob_get_clean();

$rendered = str_replace(__DIR__, '', $rendered);

$expectedDate = DateTimeImmutable::createFromFormat("U", (string) filemtime(__DIR__ . '/fixtures/all-in.neon'))->format(ResultPrinter::DATE_FORMAT);
$expected = <<<PHP
Analyzing /fixtures/all-in.neon
Date: {$expectedDate}
Overall-Errors: 41
Classes-Cognitive-Complexity: 70
Deprecations: 2
Invalid-Phpdocs: 5
Unknown-Types: 1
Anonymous-Variables: 4
Native-Property-Type-Coverage: 1
Native-Param-Type-Coverage: 27
Native-Return-Type-Coverage: 4
Unused-Symbols: 3

PHP;

$this->assertSame($expected, $rendered);
Expand Down
Loading