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

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
NameDescription
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
TypeDescription
string

file

The file in which the snippet is found.

Returns
TypeDescription
string

fqn

The Snippet fully-qualified name.

Returns
TypeDescription
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
TypeDescription
int

content

The snippet content.

Returns
TypeDescription
string

index

The Snippet Index

Returns
TypeDescription
int

name

The snippet name

Returns
TypeDescription
string

invoke

Eval the snippet and return the result.

Parameter
NameDescription
returnVar mixed
Returns
TypeDescription
mixed

addLocal

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

Parameters
NameDescription
name string

The variable name

value mixed

The variable value

Returns
TypeDescription
void

addUse

Add a use statement for a class.

Parameter
NameDescription
name string

The class name to import.

Returns
TypeDescription
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
NameDescription
line int

The line number (0-indexed) to replace.

content string

The PHP code to inject.

Returns
TypeDescription
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
NameDescription
line int

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

content string

The PHP code to inject.

Returns
TypeDescription
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
NameDescription
old string

The string to be replaced.

new string

The new string to insert.

Returns
TypeDescription
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
NameDescription
pattern string

The regex pattern to search for.

new string

The new string to insert.

Returns
TypeDescription
void

jsonSerialize

Serialize to json

Returns
TypeDescription
array