Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #143

Merged
merged 30 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
70c6593
add "filter"-param to phpstan-baseline-analyze
fbecker-complex Oct 12, 2023
db47ad9
revert changes
fbecker-complex Oct 12, 2023
0a6c402
filter errors by keys
fbecker-complex Oct 26, 2023
edf938c
print results
fbecker-complex Oct 26, 2023
ed08edb
fix phpstan errors
fbecker-complex Oct 26, 2023
83bdc04
correct format for baseline
fbecker-complex Oct 26, 2023
c4cd7b8
add first test case
fbecker-complex Oct 26, 2023
d6dbfb6
test printing of filter result
fbecker-complex Oct 26, 2023
329b789
typo
staabm Oct 30, 2023
9c18034
fix bad merge
staabm Oct 30, 2023
785c309
refactor
staabm Oct 30, 2023
d983081
another test
staabm Oct 30, 2023
01749e4
fix
staabm Oct 30, 2023
3d205e4
simplify getFilterKeyForString()
staabm Oct 30, 2023
c3fb18e
Update phpstan-baseline-analyze.php
staabm Oct 30, 2023
0e8f202
remove inline phpdoc
staabm Oct 30, 2023
c81e862
use is*Error() methods
staabm Oct 30, 2023
0c0f30e
Delete bin/phpstan-baseline-filter
staabm Oct 30, 2023
1af2f3c
Delete bin/phpstan-baseline-filter.php
staabm Oct 30, 2023
bf67152
Discard changes to lib/Baseline.php
staabm Oct 30, 2023
7f38b97
Delete lib/FilterApplication.php
staabm Oct 30, 2023
3a3f127
Delete tests/FilterApplicationTest.php
staabm Oct 30, 2023
a38f4cd
Delete lib/ResultPrinter.php
staabm Oct 30, 2023
694c36d
Update BaselineError.php
staabm Oct 30, 2023
978b9c2
Update BaselineAnalyzer.php
staabm Oct 30, 2023
813e8c6
Discard changes to lib/ResultPrinter.php
staabm Oct 30, 2023
1a3d2ae
Update BaselineError.php
staabm Oct 30, 2023
bc1ef52
Merge branch 'extr' of https://github.com/staabm/phpstan-baseline-ana…
staabm Oct 30, 2023
3279e65
Update Baseline.php
staabm Oct 30, 2023
0658520
Update BaselineError.php
staabm Oct 30, 2023
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
5 changes: 3 additions & 2 deletions bin/phpstan-baseline-analyze.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use staabm\PHPStanBaselineAnalysis\ResultPrinter;
use function Safe\ini_set;

// Finding composer
Expand Down Expand Up @@ -33,9 +34,9 @@
}


$format = \staabm\PHPStanBaselineAnalysis\ResultPrinter::FORMAT_TEXT;
$format = ResultPrinter::FORMAT_TEXT;
if (in_array('--json', $argv)) {
$format = \staabm\PHPStanBaselineAnalysis\ResultPrinter::FORMAT_JSON;
$format = ResultPrinter::FORMAT_JSON;
}

$exitCode = $app->start($argv[1], $format);
Expand Down
8 changes: 4 additions & 4 deletions lib/Baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public function getIgnoreErrors(): Iterator {
$ignoreErrors = $parameters['ignoreErrors'];

foreach($ignoreErrors as $error) {
$baselineError = new BaselineError();
$baselineError->message = $error['message'];
$baselineError->count = $error['count'];
yield $baselineError;
yield new BaselineError(
$error['count'],
$error['message'],
);
}
}

Expand Down
22 changes: 5 additions & 17 deletions lib/BaselineAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,7 @@ public function analyze(): AnalyzerResult

private function countDeprecations(BaselineError $baselineError): int
{
return
str_contains($baselineError->message, ' deprecated class ')
|| str_contains($baselineError->message, ' deprecated method ')
|| str_contains($baselineError->message, ' deprecated function ')
|| str_contains($baselineError->message, ' deprecated property ')
? $baselineError->count
: 0;
return $baselineError->isDeprecationError() ? $baselineError->count : 0;
}

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

private function countInvalidPhpdocs(BaselineError $baselineError): int
{
return str_contains($baselineError->message, 'PHPDoc tag ')
return $baselineError->isInvalidPhpDocError()
? $baselineError->count
: 0;
}

private function countUnknownTypes(BaselineError $baselineError): int
{
$notFoundCount = preg_match('/Instantiated class .+ not found/', $baselineError->message, $matches) === 1
? $baselineError->count
: 0;

$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')
return $baselineError->isUnknownTypeError()
? $baselineError->count
: 0;

return $notFoundCount + $unknownCount;
}

private function countAnonymousVariables(BaselineError $baselineError): int
{
return str_contains($baselineError->message, 'Anonymous variable')
return $baselineError->isAnonymousVariableError()
? $baselineError->count
: 0;
}

private function countUnusedSymbols(BaselineError $baselineError): int
{
return str_ends_with($baselineError->message, 'is never used$#')
return $baselineError->isUnusedSymbolError()
? $baselineError->count
: 0;
}
Expand Down
54 changes: 44 additions & 10 deletions lib/BaselineError.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,62 @@

namespace staabm\PHPStanBaselineAnalysis;

use function Safe\preg_match;

/**
* @immutable
*/
final class BaselineError
{
/**
* @var int
*/
public $count;
public int $count;

/**
* @var string
*/
public $message;
private string $message;

public function __construct(int $count, string $message)
{
$this->count = $count;
$this->message = $message;
}

/**
* Returns the baseline error message, without regex delimiters.
* Note: the message may still contain escaped regex meta characters.
*
* @return string
*/
public function unwrapMessage(): string {
$msg = $this->message;
$msg = str_replace(['\\-', '\\.', '%%'], ['-', '.', '%'], $msg);
$msg = trim($msg, '#^$');
return $msg;
}

public function isDeprecationError(): bool
{
return str_contains($this->message, ' deprecated class ')
|| str_contains($this->message, ' deprecated method ')
|| str_contains($this->message, ' deprecated function ')
|| str_contains($this->message, ' deprecated property ');
}

public function isInvalidPhpDocError(): bool
{
return str_contains($this->message, 'PHPDoc tag ');
}

public function isUnknownTypeError(): bool
{
return preg_match('/Instantiated class .+ not found/', $this->message, $matches) === 1
|| str_contains($this->message, 'on an unknown class')
|| str_contains($this->message, 'has invalid type unknown')
|| str_contains($this->message, 'unknown_type as its type');
}

public function isAnonymousVariableError(): bool
{
return str_contains($this->message, 'Anonymous variable');
}

public function isUnusedSymbolError(): bool
{
return str_ends_with($this->message, 'is never used$#');
}
}