@@ -472,24 +472,26 @@ function test_snippet_code( Snippet $snippet ) {
472472 $ snippet ->code_error = null ;
473473
474474 if ( 'php ' !== $ snippet ->type ) {
475- error_log ( "Code Snippets DEBUG: Skipping validation for non-PHP snippet type: {$ snippet ->type }" );
476475 return ;
477476 }
478477
479- error_log ( "Code Snippets DEBUG: Running Validator on snippet ID {$ snippet ->id }" );
480478 $ validator = new Validator ( $ snippet ->code );
481479 $ result = $ validator ->validate ();
482480
483481 if ( $ result ) {
484- error_log ( "Code Snippets DEBUG: Validator found error: " . $ result ['message ' ] );
485482 $ snippet ->code_error = [ $ result ['message ' ], $ result ['line ' ] ];
486- } else {
487- error_log ( "Code Snippets DEBUG: Validator passed, running execute_snippet " );
488483 }
489484
490485 if ( ! $ snippet ->code_error ) {
491- error_log ( "Code Snippets DEBUG: Calling execute_snippet for redeclaration check " );
492- $ result = execute_snippet ( $ snippet ->code , $ snippet ->id , true );
486+ // First check for conflicts with other active snippets
487+ $ conflict_error = detect_function_redeclaration_with_active_snippets ( $ snippet ->code , $ snippet ->id );
488+ if ( $ conflict_error ) {
489+ $ snippet ->code_error = [
490+ ucfirst ( rtrim ( $ conflict_error ->message , '. ' ) ) . '. ' ,
491+ $ conflict_error ->line ,
492+ ];
493+ } else {
494+ $ result = execute_snippet ( $ snippet ->code , $ snippet ->id , true );
493495
494496 if ( $ result instanceof ParseError ) {
495497 $ snippet ->code_error = [
@@ -507,6 +509,7 @@ function test_snippet_code( Snippet $snippet ) {
507509 $ result ->line ,
508510 ];
509511 }
512+ }
510513 }
511514}
512515
@@ -669,6 +672,55 @@ function execute_snippet( string $code, int $id = 0, bool $force = false ) {
669672}
670673
671674
675+ /**
676+ * Check for function redeclaration conflicts with other active snippets
677+ *
678+ * @param string $code The code to check
679+ * @param int $current_snippet_id The ID of the current snippet being validated
680+ * @return object|null Error object if redeclaration detected, null otherwise
681+ */
682+ function detect_function_redeclaration_with_active_snippets ( string $ code , int $ current_snippet_id ) {
683+ // Extract function names from the current code
684+ preg_match_all ( '/function\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(/ ' , $ code , $ matches );
685+
686+ if ( empty ( $ matches [1 ] ) ) {
687+ return null ; // No functions found
688+ }
689+
690+ $ current_functions = $ matches [1 ];
691+
692+ // Get all active snippets except the current one
693+ $ active_snippets = get_snippets ( [], null );
694+ $ active_snippets = array_filter ( $ active_snippets , function ( $ snippet ) use ( $ current_snippet_id ) {
695+ return $ snippet ->active && $ snippet ->id !== $ current_snippet_id && 'php ' === $ snippet ->type ;
696+ });
697+
698+ // Check each active snippet for function conflicts
699+ foreach ( $ active_snippets as $ snippet ) {
700+ // Extract function names from the active snippet
701+ preg_match_all ( '/function\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(/ ' , $ snippet ->code , $ active_matches );
702+
703+ if ( ! empty ( $ active_matches [1 ] ) ) {
704+ $ active_functions = $ active_matches [1 ];
705+
706+ // Check for conflicts
707+ $ conflicts = array_intersect ( $ current_functions , $ active_functions );
708+ if ( ! empty ( $ conflicts ) ) {
709+ $ conflict_function = reset ( $ conflicts );
710+
711+ $ error = new \stdClass ();
712+ $ error ->type = 'fatal_error ' ;
713+ $ error ->message = "Cannot redeclare {$ conflict_function }() (already declared in snippet ID {$ snippet ->id }) " ;
714+ $ error ->line = 1 ;
715+ $ error ->file = '' ;
716+ return $ error ;
717+ }
718+ }
719+ }
720+
721+ return null ; // No conflicts detected
722+ }
723+
672724/**
673725 * Retrieve a single snippets from the database using its cloud ID.
674726 *
0 commit comments