Skip to content

Function names can be reused across multiple PHP snippets #230

New issue

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

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

Already on GitHub? # to your account

Open
georgestephanis opened this issue Mar 21, 2025 · 0 comments
Open

Function names can be reused across multiple PHP snippets #230

georgestephanis opened this issue Mar 21, 2025 · 0 comments
Labels

Comments

@georgestephanis
Copy link

Current behavior

This behavior can cause fatal errors on site front-ends.

Image

Expected behavior

It would be useful if the PHP Validator's check for duplicate identifiers would keep an index of other defined functions in other snippets, and if someone attempts to redeclare an existing function name, prompt the user to change it.

/**
* Check whether a particular identifier has been used previously.
*
* @param string $type Which type of identifier this is. Supports T_FUNCTION, T_CLASS and T_INTERFACE.
* @param string $identifier The name of the identifier itself.
*
* @return bool true if the identifier is not unique.
*/
private function check_duplicate_identifier( string $type, string $identifier ): bool {
if ( ! isset( $this->defined_identifiers[ $type ] ) ) {
switch ( $type ) {
case T_FUNCTION:
$defined_functions = get_defined_functions();
$this->defined_identifiers[ T_FUNCTION ] = array_merge( $defined_functions['internal'], $defined_functions['user'] );
break;
case T_CLASS:
$this->defined_identifiers[ T_CLASS ] = get_declared_classes();
break;
case T_INTERFACE:
$this->defined_identifiers[ T_INTERFACE ] = get_declared_interfaces();
break;
default:
return false;
}
}
$duplicate = in_array( $identifier, $this->defined_identifiers[ $type ], true );
array_unshift( $this->defined_identifiers[ $type ], $identifier );
return $duplicate && ! ( isset( $this->exceptions[ $type ] ) && in_array( $identifier, $this->exceptions[ $type ], true ) );
}

Alternately, if the function names declared in each snippet could be stored in meta for the snippet, with function_exists() checks for each before it is eval'd, to avoid fataling a site?

Steps to reproduce

To reproduce, activate these two (simple) test snippets and view the front-end of the site.

test-fn-1.code-snippets.json
test-fn-2.code-snippets.json

WordPress version

6.7.2

Code Snippets version

3.6.8

Code Snippets license

Core (free)

Anything else?

No response

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant