1+ <?php
2+
3+ namespace staabm \PHPStanBaselineAnalysis ;
4+
5+ use \Iterator ;
6+
7+ final class TrendApplication
8+ {
9+ const EXIT_IMPROVED = 0 ;
10+ const EXIT_STEADY = 1 ;
11+ const EXIT_WORSE = 2 ;
12+
13+ /**
14+ * @throws \Safe\Exceptions\FilesystemException
15+ * @throws \Safe\Exceptions\JsonException
16+ *
17+ * @return self::EXIT_*
18+ */
19+ public function start (string $ referenceFilePath , string $ comparingFilePath ): int
20+ {
21+ $ exitCode = self ::EXIT_IMPROVED ;
22+
23+ $ reference = $ this ->decodeFile ($ referenceFilePath );
24+ $ comparing = $ this ->decodeFile ($ comparingFilePath );
25+
26+ foreach ($ reference as $ baselinePath => $ result ) {
27+ echo 'Analyzing Trend for ' . $ baselinePath . "\n" ;
28+
29+ if (isset ($ comparing [$ baselinePath ])) {
30+ if ($ comparing [$ baselinePath ]->overallComplexity > $ result ->overallComplexity ) {
31+ printf (' %s: %d -> %d => worse ' , ResultPrinter::KEY_OVERALL_CLASS_COMPLEXITY , $ result ->overallComplexity , $ comparing [$ baselinePath ]->overallComplexity );
32+
33+ $ exitCode = max ($ exitCode , self ::EXIT_WORSE );
34+ } elseif ($ comparing [$ baselinePath ]->overallComplexity < $ result ->overallComplexity ) {
35+ printf (' %s: %d -> %d => improved ' , ResultPrinter::KEY_OVERALL_CLASS_COMPLEXITY , $ result ->overallComplexity , $ comparing [$ baselinePath ]->overallComplexity );
36+
37+ $ exitCode = max ($ exitCode , self ::EXIT_IMPROVED );
38+ } else {
39+ printf (' %s: %d -> %d => good ' , ResultPrinter::KEY_OVERALL_CLASS_COMPLEXITY , $ result ->overallComplexity , $ comparing [$ baselinePath ]->overallComplexity );
40+
41+ $ exitCode = max ($ exitCode , self ::EXIT_STEADY );
42+ }
43+ }
44+ }
45+
46+ return $ exitCode ;
47+ }
48+
49+ public function help (): void
50+ {
51+ printf ('USAGE: phpstan-baseline-trend <reference-result.json> <comparing-result.json> ' );
52+ }
53+
54+ /**
55+ * @return array<string, AnalyzerResult>
56+ * @throws \Safe\Exceptions\FilesystemException
57+ * @throws \Safe\Exceptions\JsonException
58+ */
59+ private function decodeFile (string $ filePath ): array
60+ {
61+ $ content = \Safe \file_get_contents ($ filePath );
62+ $ json = \Safe \json_decode ($ content , true );
63+
64+ if (!is_array ($ json )) {
65+ throw new \RuntimeException ('Expecting array, got ' . gettype ($ json ));
66+ }
67+
68+ $ decoded = [];
69+ foreach ($ json as $ data ) {
70+
71+ if (!is_array ($ data )) {
72+ throw new \RuntimeException ('Expecting array, got ' . gettype ($ data ));
73+ }
74+
75+ foreach ($ data as $ baselinePath => $ resultArray ) {
76+
77+ if (!is_string ($ baselinePath )) {
78+ throw new \RuntimeException ('Expecting string, got ' . gettype ($ baselinePath ));
79+ }
80+ if (!is_array ($ resultArray )) {
81+ throw new \RuntimeException ('Expecting string, got ' . gettype ($ resultArray ));
82+ }
83+
84+ $ result = new AnalyzerResult ();
85+ $ result ->overallComplexity = $ resultArray [ResultPrinter::KEY_OVERALL_CLASS_COMPLEXITY ];
86+
87+ $ decoded [$ baselinePath ] = $ result ;
88+ }
89+ }
90+
91+ return $ decoded ;
92+ }
93+ }
0 commit comments