Skip to content

Constants in defined() calls are incorrectly prefixed with namespace #10

@frugan-dev

Description

@frugan-dev

Description

wpify/scoper is incorrectly adding namespace prefixes to constant names inside defined() function calls. PHP constants do not support namespaces, so this breaks the functionality.

Current Behavior

// Before scoping
if (defined('WP_DEBUG_LOG') && WP_DEBUG_LOG) {
    // ...
}

// After scoping (incorrect)
if (defined('My\Prefix\WP_DEBUG_LOG') && WP_DEBUG_LOG) {
    // ...
}

The defined('My\Prefix\WP_DEBUG_LOG') call will always return false because the actual constant is named WP_DEBUG_LOG, not My\Prefix\WP_DEBUG_LOG.

Expected Behavior

Constant names inside defined() calls should remain unchanged:

// After scoping (correct)
if (defined('WP_DEBUG_LOG') && WP_DEBUG_LOG) {
    // ...
}

Workaround

Add this patcher to scoper.custom.php:

function customize_php_scoper_config(array $config): array {
    $config['patchers'][] = static function (string $filePath, string $prefix, string $content): string {
        return str_replace("defined('{$prefix}\\", "defined('", $content);
    };
    
    return $config;
}

Environment

  • wpify/scoper: 3.2
  • PHP: 8.2

composer.json

{
  "require": {
    "php": ">=8.2"
  },
  "require-dev": {
    "wpify/scoper": "^3.2"
  },
  "config": {
    "allow-plugins": {
      "wpify/scoper": true
    },
    "optimize-autoloader": true,
    "platform": {
      "php": "8.2.9999"
    }
  },
  "extra": {
    "wpify-scoper": {
      "prefix": "My\\Prefix"
    }
  }
}

composer-deps.json

{
  "require": {
    "inpsyde/wonolog": "2.x-dev",
    "thecodingmachine/safe": "^3.3",
    "wecodemore/wordpress-early-hook": "^1.1.0"
  },
  "config": {
    "platform": {
      "php": "8.2.9999"
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions