Skip to content

Commit a64f3b3

Browse files
Refactor (#143)
Co-authored-by: fbecker-complex <[email protected]>
1 parent cef6971 commit a64f3b3

File tree

4 files changed

+56
-33
lines changed

4 files changed

+56
-33
lines changed

bin/phpstan-baseline-analyze.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use staabm\PHPStanBaselineAnalysis\ResultPrinter;
34
use function Safe\ini_set;
45

56
// Finding composer
@@ -33,9 +34,9 @@
3334
}
3435

3536

36-
$format = \staabm\PHPStanBaselineAnalysis\ResultPrinter::FORMAT_TEXT;
37+
$format = ResultPrinter::FORMAT_TEXT;
3738
if (in_array('--json', $argv)) {
38-
$format = \staabm\PHPStanBaselineAnalysis\ResultPrinter::FORMAT_JSON;
39+
$format = ResultPrinter::FORMAT_JSON;
3940
}
4041

4142
$exitCode = $app->start($argv[1], $format);

lib/Baseline.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ public function getIgnoreErrors(): Iterator {
5858
$ignoreErrors = $parameters['ignoreErrors'];
5959

6060
foreach($ignoreErrors as $error) {
61-
$baselineError = new BaselineError();
62-
$baselineError->message = $error['message'];
63-
$baselineError->count = $error['count'];
64-
yield $baselineError;
61+
yield new BaselineError(
62+
$error['count'],
63+
$error['message'],
64+
);
6565
}
6666
}
6767

lib/BaselineAnalyzer.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,7 @@ public function analyze(): AnalyzerResult
6565

6666
private function countDeprecations(BaselineError $baselineError): int
6767
{
68-
return
69-
str_contains($baselineError->message, ' deprecated class ')
70-
|| str_contains($baselineError->message, ' deprecated method ')
71-
|| str_contains($baselineError->message, ' deprecated function ')
72-
|| str_contains($baselineError->message, ' deprecated property ')
73-
? $baselineError->count
74-
: 0;
68+
return $baselineError->isDeprecationError() ? $baselineError->count : 0;
7569
}
7670

7771
private function countClassesComplexity(BaselineError $baselineError): int
@@ -84,34 +78,28 @@ private function countClassesComplexity(BaselineError $baselineError): int
8478

8579
private function countInvalidPhpdocs(BaselineError $baselineError): int
8680
{
87-
return str_contains($baselineError->message, 'PHPDoc tag ')
81+
return $baselineError->isInvalidPhpDocError()
8882
? $baselineError->count
8983
: 0;
9084
}
9185

9286
private function countUnknownTypes(BaselineError $baselineError): int
9387
{
94-
$notFoundCount = preg_match('/Instantiated class .+ not found/', $baselineError->message, $matches) === 1
95-
? $baselineError->count
96-
: 0;
97-
98-
$unknownCount = str_contains($baselineError->message, 'on an unknown class') || str_contains($baselineError->message, 'has invalid type unknown') || str_contains($baselineError->message, 'unknown_type as its type')
88+
return $baselineError->isUnknownTypeError()
9989
? $baselineError->count
10090
: 0;
101-
102-
return $notFoundCount + $unknownCount;
10391
}
10492

10593
private function countAnonymousVariables(BaselineError $baselineError): int
10694
{
107-
return str_contains($baselineError->message, 'Anonymous variable')
95+
return $baselineError->isAnonymousVariableError()
10896
? $baselineError->count
10997
: 0;
11098
}
11199

112100
private function countUnusedSymbols(BaselineError $baselineError): int
113101
{
114-
return str_ends_with($baselineError->message, 'is never used$#')
102+
return $baselineError->isUnusedSymbolError()
115103
? $baselineError->count
116104
: 0;
117105
}

lib/BaselineError.php

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,62 @@
22

33
namespace staabm\PHPStanBaselineAnalysis;
44

5+
use function Safe\preg_match;
6+
7+
/**
8+
* @immutable
9+
*/
510
final class BaselineError
611
{
7-
/**
8-
* @var int
9-
*/
10-
public $count;
12+
public int $count;
1113

12-
/**
13-
* @var string
14-
*/
15-
public $message;
14+
private string $message;
15+
16+
public function __construct(int $count, string $message)
17+
{
18+
$this->count = $count;
19+
$this->message = $message;
20+
}
1621

1722
/**
1823
* Returns the baseline error message, without regex delimiters.
1924
* Note: the message may still contain escaped regex meta characters.
20-
*
21-
* @return string
2225
*/
2326
public function unwrapMessage(): string {
2427
$msg = $this->message;
2528
$msg = str_replace(['\\-', '\\.', '%%'], ['-', '.', '%'], $msg);
2629
$msg = trim($msg, '#^$');
2730
return $msg;
2831
}
32+
33+
public function isDeprecationError(): bool
34+
{
35+
return str_contains($this->message, ' deprecated class ')
36+
|| str_contains($this->message, ' deprecated method ')
37+
|| str_contains($this->message, ' deprecated function ')
38+
|| str_contains($this->message, ' deprecated property ');
39+
}
40+
41+
public function isInvalidPhpDocError(): bool
42+
{
43+
return str_contains($this->message, 'PHPDoc tag ');
44+
}
45+
46+
public function isUnknownTypeError(): bool
47+
{
48+
return preg_match('/Instantiated class .+ not found/', $this->message, $matches) === 1
49+
|| str_contains($this->message, 'on an unknown class')
50+
|| str_contains($this->message, 'has invalid type unknown')
51+
|| str_contains($this->message, 'unknown_type as its type');
52+
}
53+
54+
public function isAnonymousVariableError(): bool
55+
{
56+
return str_contains($this->message, 'Anonymous variable');
57+
}
58+
59+
public function isUnusedSymbolError(): bool
60+
{
61+
return str_ends_with($this->message, 'is never used$#');
62+
}
2963
}

0 commit comments

Comments
 (0)