Skip to content

Commit 8b93efc

Browse files
authored
Support tomasvotruba/type-coverage 0.2.x format (#135)
1 parent 0201dec commit 8b93efc

File tree

5 files changed

+70
-7
lines changed

5 files changed

+70
-7
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"symfony/var-dumper": "^5.3",
2424
"thecodingmachine/phpstan-safe-rule": "^1.2",
2525
"tomasvotruba/cognitive-complexity": "^0.1.1",
26-
"tomasvotruba/type-coverage": "^0.1.0",
26+
"tomasvotruba/type-coverage": "^0.2.0",
2727
"tomasvotruba/unused-public": "^0.3.2"
2828
},
2929
"config": {

lib/BaselineAnalyzer.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ final class BaselineAnalyzer
1616
* @api
1717
* @var string
1818
*/
19-
public const PROPERTY_TYPE_DEClARATION_SEA_LEVEL_MESSAGE = 'Out of %d possible property types, only %d %% actually have it. Add more property types to get over %d %%';
19+
public const PROPERTY_TYPE_DEClARATION_SEA_LEVEL_MESSAGE = 'Out of %d possible property types, only %d - %.1f %% actually have it. Add more property types to get over %d %%';
2020
/**
2121
* @api
2222
* @var string
2323
*/
24-
public const PARAM_TYPE_DEClARATION_SEA_LEVEL_MESSAGE = 'Out of %d possible param types, only %d %% actually have it. Add more param types to get over %d %%';
24+
public const PARAM_TYPE_DEClARATION_SEA_LEVEL_MESSAGE = 'Out of %d possible param types, only %d - %.1f %% actually have it. Add more param types to get over %d %%';
2525
/**
2626
* @api
2727
* @var string
2828
*/
29-
public const RETURN_TYPE_DEClARATION_SEA_LEVEL_MESSAGE = 'Out of %d possible return types, only %d %% actually have it. Add more return types to get over %d %%';
29+
public const RETURN_TYPE_DEClARATION_SEA_LEVEL_MESSAGE = 'Out of %d possible return types, only %d - %.1f %% actually have it. Add more return types to get over %d %%';
3030

3131
/**
3232
* @var Baseline
@@ -118,23 +118,47 @@ private function countUnusedSymbols(BaselineError $baselineError): int
118118

119119
private function checkSeaLevels(AnalyzerResult $result, BaselineError $baselineError): void
120120
{
121-
if (sscanf($baselineError->unwrapMessage(), self::PROPERTY_TYPE_DEClARATION_SEA_LEVEL_MESSAGE, $absoluteCount, $coveragePercent, $goalPercent) >= 2) {
121+
if (
122+
sscanf(
123+
$baselineError->unwrapMessage(),
124+
$this->printfToScanfFormat(self::PROPERTY_TYPE_DEClARATION_SEA_LEVEL_MESSAGE),
125+
$absoluteCountMin, $coveragePercent, $goalPercent) >= 2
126+
) {
122127
if (!is_int($coveragePercent) || $coveragePercent < 0 || $coveragePercent > 100) {
123128
throw new \LogicException('Invalid property coveragePercent: '. $coveragePercent);
124129
}
125130
$result->propertyTypeCoverage = $coveragePercent;
126131
}
127-
if (sscanf($baselineError->unwrapMessage(), self::PARAM_TYPE_DEClARATION_SEA_LEVEL_MESSAGE, $absoluteCount, $coveragePercent, $goalPercent) >= 2) {
132+
133+
if (
134+
sscanf(
135+
$baselineError->unwrapMessage(),
136+
$this->printfToScanfFormat(self::PARAM_TYPE_DEClARATION_SEA_LEVEL_MESSAGE),
137+
$absoluteCountMin, $coveragePercent, $goalPercent) >= 2
138+
) {
128139
if (!is_int($coveragePercent) || $coveragePercent < 0 || $coveragePercent > 100) {
129140
throw new \LogicException('Invalid parameter coveragePercent: '. $coveragePercent);
130141
}
131142
$result->paramTypeCoverage = $coveragePercent;
132143
}
133-
if (sscanf($baselineError->unwrapMessage(), self::RETURN_TYPE_DEClARATION_SEA_LEVEL_MESSAGE, $absoluteCount, $coveragePercent, $goalPercent) >= 2) {
144+
145+
if (
146+
sscanf(
147+
$baselineError->unwrapMessage(),
148+
$this->printfToScanfFormat(self::RETURN_TYPE_DEClARATION_SEA_LEVEL_MESSAGE),
149+
$absoluteCountMin, $coveragePercent, $goalPercent) >= 2
150+
) {
134151
if (!is_int($coveragePercent) || $coveragePercent < 0 || $coveragePercent > 100) {
135152
throw new \LogicException('Invalid return coveragePercent: '. $coveragePercent);
136153
}
137154
$result->returnTypeCoverage = $coveragePercent;
138155
}
139156
}
157+
158+
private function printfToScanfFormat(string $format): string {
159+
// we don't need the float value, therefore simply ignore it, to make the format parseable by sscanf
160+
// see https://github.com/php/php-src/issues/12126
161+
// additionally this makes the output format of tomasvotruba/type-coverage 0.2.* compatible with tomasvotruba/type-coverage 0.1.*
162+
return str_replace('%.1f', '', $format);
163+
}
140164
}

tests/BaselineAnalyzerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ function testAnonymousVariables():void
133133

134134
function testSeaLevels():void
135135
{
136+
# output format of tomasvotruba/type-coverage 0.1.*
136137
$analyzer = new BaselineAnalyzer(Baseline::forFile(__DIR__ . '/fixtures/sea-level.neon'));
137138
$result = $analyzer->analyze();
138139

@@ -148,6 +149,24 @@ function testSeaLevels():void
148149
$this->assertSame(0, $result->unusedSymbols);
149150
}
150151

152+
function testSeaLevelsV02():void
153+
{
154+
# output format of tomasvotruba/type-coverage 0.2.*
155+
$analyzer = new BaselineAnalyzer(Baseline::forFile(__DIR__ . '/fixtures/sea-level-0_2.neon'));
156+
$result = $analyzer->analyze();
157+
158+
$this->assertSame(6, $result->overallErrors);
159+
$this->assertSame(0, $result->classesComplexity);
160+
$this->assertSame(0, $result->deprecations);
161+
$this->assertSame(0, $result->invalidPhpdocs);
162+
$this->assertSame(0, $result->unknownTypes);
163+
$this->assertSame(0, $result->anonymousVariables);
164+
$this->assertSame(1, $result->propertyTypeCoverage);
165+
$this->assertSame(27, $result->paramTypeCoverage);
166+
$this->assertSame(4, $result->returnTypeCoverage);
167+
$this->assertSame(0, $result->unusedSymbols);
168+
}
169+
151170
public function testSymplifyCompat(): void {
152171
$this->assertSame(
153172
BaselineAnalyzer::CLASS_COMPLEXITY_ERROR_MESSAGE,

tests/fixtures/sea-level-0_2.neon

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# output format of tomasvotruba/type-coverage 0.2.*
2+
3+
parameters:
4+
ignoreErrors:
5+
-
6+
message: "#^Out of 1 possible property types, only 1 \\- 36\\.0 %% actually have it\\. Add more property types to get over 99 %%$#"
7+
count: 2
8+
path: N/A
9+
10+
-
11+
message: "#^Out of 22 possible return types, only 4 \\- 36\\.0 %% actually have it\\. Add more return types to get over 99 %%$#"
12+
count: 2
13+
path: N/A
14+
15+
-
16+
message: "#^Out of 33 possible param types, only 27 \\- 36\\.0 %% actually have it\\. Add more param types to get over 99 %%$#"
17+
count: 2
18+
path: N/A

tests/fixtures/sea-level.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# output format of tomasvotruba/type-coverage 0.1.*
2+
13
parameters:
24
ignoreErrors:
35
-

0 commit comments

Comments
 (0)