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

Detect parse errors #237

Merged
merged 4 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Binary file modified qit
Binary file not shown.
36 changes: 23 additions & 13 deletions src/src/LocalTests/PrepareQMLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function extract_fatal_errors_from_debug_file( string $file_path ): array
if ( $handle ) {

while ( ( $line = fgets( $handle ) ) !== false ) { // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
if ( str_contains( $line, 'PHP Fatal error:' ) ) {
if ( str_contains( $line, 'PHP Fatal error:' ) || str_contains( $line, 'PHP Parse error:' ) ) {
$lines[] = $line;
}
}
Expand All @@ -114,35 +114,45 @@ public function extract_fatal_errors_from_debug_file( string $file_path ): array
public function extract_error_info( array $lines ): array {
$fatal_errors = [];
foreach ( $lines as $line ) {
preg_match( '/PHP Fatal error: (.*?) in (.*?) on line (\d+)/', $line, $matches1 );
// First pattern: `on line X`.
preg_match(
'/PHP (Fatal|Parse) error: (.*?) in (.*?) on line (\d+)/',
$line,
$matches1
);

if (
isset( $matches1[1] ) &&
isset( $matches1[2] ) &&
isset( $matches1[3] )
isset( $matches1[3] ) &&
isset( $matches1[4] )
) {
$fatal_errors[] = [
'message' => $matches1[1],
'file' => str_replace( '/var/www/html/', '', $matches1[2] ),
'line' => $matches1[3],
'message' => $matches1[2],
'file' => str_replace( '/var/www/html/', '', $matches1[3] ),
'line' => $matches1[4],
];

continue;
}

preg_match( '/PHP Fatal error: (.*?) in (.*?):(\d+)/', $line, $matches2 );
// Second pattern: `in path:line`.
preg_match(
'/PHP (Fatal|Parse) error: (.*?) in (.*?):(\d+)/',
$line,
$matches2
);

if (
isset( $matches2[1] ) &&
isset( $matches2[2] ) &&
isset( $matches2[3] )
isset( $matches2[3] ) &&
isset( $matches2[4] )
) {
$fatal_errors[] = [
'message' => $matches2[1],
'file' => str_replace( '/var/www/html/', '', $matches2[2] ),
'line' => $matches2[3],
'message' => $matches2[2],
'file' => str_replace( '/var/www/html/', '', $matches2[3] ),
'line' => $matches2[4],
];

continue;
}

Expand Down
23 changes: 23 additions & 0 deletions src/tests/LocalTests/PrepareQMLogTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace QIT_CLI_Tests\LocalTests;

use QIT_CLI\App;
use QIT_CLI\LocalTests\PrepareQMLog;
use QIT_CLI_Tests\QITTestCase;
use Spatie\Snapshots\MatchesSnapshots;

class PrepareQMLogTest extends QITTestCase {
use MatchesSnapshots;

public function test_parses_fatal_and_parse_errors() {
$debug_log = <<<'TXT'
[10-Jan-2025 17:12:43 UTC] PHP Parse error: syntax error, unexpected '|', expecting variable (T_VARIABLE) in /var/www/html/wp-content/plugins/foo-slug/vendor/phpunit/phpunit/src/Framework/Assert/Functions.php on line 83
[10-Jan-2025 17:12:43 UTC] PHP Fatal error: Exception thrown without a stack frame in Unknown on line 0
[10-Jan-2025 17:52:22 UTC] PHP Fatal error: Uncaught Exception: Foo in /var/www/html/wp-content/plugins/foo-slug/foo-slug.php:29
TXT;
$sut = App::make( PrepareQMLog::class );

$this->assertMatchesJsonSnapshot( $sut->extract_error_info( explode( "\n", $debug_log ) ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"message": " syntax error, unexpected '|', expecting variable (T_VARIABLE)",
"file": "wp-content\/plugins\/foo-slug\/vendor\/phpunit\/phpunit\/src\/Framework\/Assert\/Functions.php",
"line": "83"
},
{
"message": " Exception thrown without a stack frame",
"file": "Unknown",
"line": "0"
},
{
"message": " Uncaught Exception: Foo",
"file": "wp-content\/plugins\/foo-slug\/foo-slug.php",
"line": "29"
}
]
Loading