Skip to content

Basic utility methods

quintenmbusiness edited this page Nov 28, 2024 · 1 revision

Table of Contents

  1. Overview
  2. Methods

Overview

The BasicGenerationHelper class provides utility methods for generating PHP code using abstract syntax tree (AST) nodes. It simplifies the creation of PHP methods, properties, arrays, arguments, and PHPDoc comments.


Methods

return(Expr $expr): Return_

Generates a return statement for a given expression.

  • Parameters:
    • Expr $expr: The expression to return.
  • Returns:
    • Return_: An AST node representing the return statement.

addVisibility(Method|Property $stmt, string $visibility = 'public'): Method|Property

Sets the visibility of a method or property (e.g., public, protected, private).

  • Parameters:
    • Method|Property $stmt: The method or property to update.
    • string $visibility: The visibility to apply (e.g., public, protected, private, static, abstract, final).
  • Returns:
    • Method|Property: The updated method or property.

createDocComment(Method|Property $method, array $params = [], ?string $returnType = null, ?string $throws = null, ?string $deprecated = null, array $additional = []): Method|Property

Adds a PHPDoc comment to a method or property.

  • Parameters:
    • Method|Property $method: The method or property to document.
    • array $params: List of parameters with their types and names.
    • ?string $returnType: The return type for the method.
    • ?string $throws: Exception type the method might throw.
    • ?string $deprecated: Marks the method or property as deprecated with an optional message.
    • array $additional: Additional PHPDoc tags (e.g., author).
  • Returns:
    • Method|Property: The updated method or property.

createArguments(array $args): array

Creates an array of AST arguments for a method call.

  • Parameters:
    • array $args: List of arguments for the method call.
  • Returns:
    • array: AST nodes representing the arguments.

createArray(array $items): Array_

Generates an AST node representing a PHP array.

  • Parameters:
    • array $items: Array items to convert.
  • Returns:
    • Array_: An AST array node.

convertToAstNode(mixed $value): Array_|LNumber|String_|ConstFetch|Variable

Converts a PHP value to an AST node.

  • Parameters:
    • mixed $value: The value to convert.
  • Returns:
    • An AST node representing the value.

isValidDefault(?string $type, mixed $default): bool

Validates whether a given value matches the specified type.

  • Parameters:
    • ?string $type: The expected type (int, string, bool, float, array).
    • mixed $default: The value to validate.
  • Returns:
    • bool: Whether the value is valid for the type.

generateFile(Class_|Interface_|Namespace_ $definition, ?string $namespace, string $outputPath): void

Generates a PHP file from a class, interface, or namespace definition.

  • Parameters:
    • Class_|Interface_|Namespace_ $definition: The class, interface, or namespace to generate.
    • ?string $namespace: The namespace to use (if not already included in the definition).
    • string $outputPath: Path to save the generated file.
  • Returns:
    • void: Writes the generated file to the specified path.