Google Cloud PHP shared dependency, providing functionality useful to all components. Client - Class Snippet (1.59.0)

Reference documentation and code samples for the Google Cloud PHP shared dependency, providing functionality useful to all components. Client class Snippet.

Represents a single code snippet

Namespace

Google \ Cloud \ Core \ Testing \ Snippet \ Parser

Methods

__construct

Create a snippet

Parameters
Name Description
identifier string

The snippet ID

config array

The snippet config

identifier

A unique identifier for the snippet.

This identifier is deterministic and will remain constant unless the snippet is modified or moved.

Returns
Type Description
string

file

The file in which the snippet is found.

Returns
Type Description
string

fqn

The Snippet fully-qualified name.

Returns
Type Description
string

line

The line number where the snippet's method or class is declared.

Note that this is NOT the line number where the snippet is declared. It indicates the method or class which the snippet annotates.

Returns
Type Description
int

content

The snippet content.

Returns
Type Description
string

index

The Snippet Index

Returns
Type Description
int

name

The snippet name

Returns
Type Description
string

invoke

Eval the snippet and return the result.

Parameter
Name Description
returnVar mixed
Returns
Type Description
mixed

addLocal

Add a local variable to make available in the snippet execution scope.

Parameters
Name Description
name string

The variable name

value mixed

The variable value

Returns
Type Description
void

addUse

Add a use statement for a class.

Parameter
Name Description
name string

The class name to import.

Returns
Type Description
void

setLine

Replace a line with new code.

Hopefully this is obvious, but be careful using this, and only use it when no other feasible options present themselves. It's pretty easy to make your test useless when you're overwriting the thing you are trying to test.

This is provided for cases when a snippet relies on a global, or on something else which can not be overridden or mocked.

Parameters
Name Description
line int

The line number (0-indexed) to replace.

content string

The PHP code to inject.

Returns
Type Description
void

insertAfterLine

Inject new code after a given line.

Hopefully this is obvious, but be careful using this, and only use it when no other feasible options present themselves. It's pretty easy to make your test useless when you're modifying the thing you are trying to test.

This is provided for cases when a snippet relies on a global, or on something else which can not be overridden or mocked.

Parameters
Name Description
line int

The line number (0-indexed) to write in after.

content string

The PHP code to inject.

Returns
Type Description
void

replace

Replace a string in the snippet with a new one.

Hopefully this is obvious, but be careful using this, and only use it when no other feasible options present themselves. It's pretty easy to make your test useless when you're modifying the thing you are trying to test.

This is provided for cases when a snippet relies on a global, or on something else which can not be overridden or mocked.

Parameters
Name Description
old string

The string to be replaced.

new string

The new string to insert.

Returns
Type Description
void

regexReplace

Find something with a regex and replace it.

Hopefully this is obvious, but be careful using this, and only use it when no other feasible options present themselves. It's pretty easy to make your test useless when you're modifying the thing you are trying to test.

This is provided for cases when a snippet relies on a global, or on something else which can not be overridden or mocked.

Parameters
Name Description
pattern string

The regex pattern to search for.

new string

The new string to insert.

Returns
Type Description
void

jsonSerialize

Serialize to json

Returns
Type Description
array