WooCommerce Code Reference

ClassExtractor
in package

Class ClassExtractor

Provides functionality to manipulate PHP class files by replacing variables, adding prefixes, and removing strict types declarations.

This class is used to generate 'code' part for runPHP step from a template file.

Table of Contents

$class_variable_replacements  : array<string|int, mixed>
Replacements for class variables.
$file_path  : string
Path to the PHP file being processed.
$has_strict_types_declaration  : bool
Whether the file contains a strict types declaration.
$method_variable_replacements  : array<string|int, mixed>
Replacements for method variables.
$prefix  : string
PHP code to prefix to the final output.
__construct()  : mixed
Constructor.
get_code()  : string
Generates the processed PHP code with applied replacements and prefixes.
has_strict_type_declaration()  : bool
Checks if the file has a strict types declaration.
replace_class_variable()  : $this
Replaces a class variable with a new value.
replace_method_variable()  : $this
Replaces a variable inside a method with a new value.
with_wp_load()  : $this
Adds a prefix to include the WordPress wp-load.php file.
apply_class_variable_replacement()  : string
Applies a replacement to a class variable in the file content.
apply_variable_replacement()  : string
Applies a replacement to a variable in a specific method.

Properties

Methods

__construct()

Constructor.

public __construct(string $file_path) : mixed
Parameters
$file_path : string

Path to the PHP file to process.

Tags
throws
InvalidArgumentException

If the file does not exist.

Return values
mixed

get_code()

Generates the processed PHP code with applied replacements and prefixes.

public get_code() : string
Return values
stringThe modified PHP code.

has_strict_type_declaration()

Checks if the file has a strict types declaration.

public has_strict_type_declaration() : bool
Return values
boolTrue if the file has a strict types declaration, false otherwise.

replace_class_variable()

Replaces a class variable with a new value.

public replace_class_variable(string $variable_name, mixed $new_value) : $this
Parameters
$variable_name : string

Name of the class variable.

$new_value : mixed

The new value to assign to the variable.

Return values
$this

replace_method_variable()

Replaces a variable inside a method with a new value.

public replace_method_variable(string $method_name, string $variable_name, mixed $new_value) : $this
Parameters
$method_name : string

Name of the method.

$variable_name : string

Name of the variable to replace.

$new_value : mixed

The new value to assign to the variable.

Return values
$this

apply_class_variable_replacement()

Applies a replacement to a class variable in the file content.

private apply_class_variable_replacement(string $file_content, string $variable_name, mixed $new_value) : string
Parameters
$file_content : string

The content of the PHP file.

$variable_name : string

The name of the variable to replace.

$new_value : mixed

The new value for the variable.

Return values
stringThe updated file content.

apply_variable_replacement()

Applies a replacement to a variable in a specific method.

private apply_variable_replacement(string $file_content, string $method_name, string $variable_name, mixed $new_value) : string
Parameters
$file_content : string

The content of the PHP file.

$method_name : string

The name of the method containing the variable.

$variable_name : string

The name of the variable to replace.

$new_value : mixed

The new value for the variable.

Return values
stringThe updated file content.