|
3 | 3 | * Coverage Threshold Checker |
4 | 4 | * |
5 | 5 | * Simple script to check if code coverage meets the minimum threshold. |
| 6 | + * This is a CLI script and does not require WordPress escaping functions. |
6 | 7 | * |
7 | 8 | * @package Simple_WP_Optimizer |
8 | 9 | * @subpackage Tests |
9 | 10 | * @since 1.5.12 |
10 | 11 | */ |
11 | 12 |
|
| 13 | +// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped |
| 14 | +// phpcs:disable WordPress.WP.AlternativeFunctions.parse_url_parse_url |
| 15 | +// phpcs:disable WordPress.WP.AlternativeFunctions.strip_tags_strip_tags |
| 16 | + |
| 17 | +<?php |
| 18 | +/** |
| 19 | + * Coverage Threshold Checker |
| 20 | + * |
| 21 | + * Simple script to check if code coverage meets the minimum threshold. |
| 22 | + * This is a CLI script and does not require WordPress escaping functions. |
| 23 | + * |
| 24 | + * @package Simple_WP_Optimizer |
| 25 | + * @subpackage Tests |
| 26 | + * @since 1.5.12 |
| 27 | + */ |
| 28 | + |
| 29 | +// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped |
| 30 | +// phpcs:disable WordPress.WP.AlternativeFunctions.parse_url_parse_url |
| 31 | +// phpcs:disable WordPress.WP.AlternativeFunctions.strip_tags_strip_tags |
| 32 | +// phpcs:disable WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_error_reporting |
| 33 | + |
12 | 34 | /** |
13 | 35 | * Check coverage threshold from PHPUnit clover XML output |
14 | 36 | * |
|
18 | 40 | */ |
19 | 41 | function check_coverage_threshold( $clover_file = 'coverage.xml', $threshold = 80.0 ) { |
20 | 42 | if ( ! file_exists( $clover_file ) ) { |
21 | | - echo "Coverage file not found: {$clover_file}\n"; |
| 43 | + fwrite( STDERR, sprintf( "Coverage file not found: %s\n", $clover_file ) ); |
22 | 44 | return false; |
23 | 45 | } |
24 | 46 |
|
25 | 47 | $xml = simplexml_load_file( $clover_file ); |
26 | 48 | if ( ! $xml ) { |
27 | | - echo "Failed to parse coverage XML file\n"; |
| 49 | + fwrite( STDERR, "Failed to parse coverage XML file\n" ); |
28 | 50 | return false; |
29 | 51 | } |
30 | 52 |
|
31 | 53 | $metrics = $xml->project->metrics; |
32 | 54 | if ( ! $metrics ) { |
33 | | - echo "No metrics found in coverage file\n"; |
| 55 | + fwrite( STDERR, "No metrics found in coverage file\n" ); |
34 | 56 | return false; |
35 | 57 | } |
36 | 58 |
|
37 | 59 | $covered_statements = (float) $metrics['coveredstatements']; |
38 | 60 | $total_statements = (float) $metrics['statements']; |
39 | 61 |
|
40 | 62 | if ( $total_statements === 0.0 ) { |
41 | | - echo "No statements found to check coverage\n"; |
| 63 | + fwrite( STDERR, "No statements found to check coverage\n" ); |
42 | 64 | return false; |
43 | 65 | } |
44 | 66 |
|
45 | 67 | $coverage_percentage = ( $covered_statements / $total_statements ) * 100; |
46 | 68 |
|
47 | | - printf( "Code Coverage: %.2f%% (%d/%d statements)\n", |
48 | | - $coverage_percentage, |
49 | | - (int) $covered_statements, |
50 | | - (int) $total_statements |
| 69 | + fwrite( |
| 70 | + STDOUT, |
| 71 | + sprintf( |
| 72 | + "Code Coverage: %.2f%% (%d/%d statements)\n", |
| 73 | + $coverage_percentage, |
| 74 | + (int) $covered_statements, |
| 75 | + (int) $total_statements |
| 76 | + ) |
51 | 77 | ); |
52 | 78 |
|
53 | 79 | if ( $coverage_percentage >= $threshold ) { |
54 | | - printf( "✅ Coverage meets threshold (%.2f%% >= %.2f%%)\n", $coverage_percentage, $threshold ); |
| 80 | + fwrite( |
| 81 | + STDOUT, |
| 82 | + sprintf( "✅ Coverage meets threshold (%.2f%% >= %.2f%%)\n", $coverage_percentage, $threshold ) |
| 83 | + ); |
55 | 84 | return true; |
56 | 85 | } else { |
57 | | - printf( "❌ Coverage below threshold (%.2f%% < %.2f%%)\n", $coverage_percentage, $threshold ); |
| 86 | + fwrite( |
| 87 | + STDERR, |
| 88 | + sprintf( "❌ Coverage below threshold (%.2f%% < %.2f%%)\n", $coverage_percentage, $threshold ) |
| 89 | + ); |
58 | 90 | return false; |
59 | 91 | } |
60 | 92 | } |
|
0 commit comments