Description
What needs to be done?
Implement the addItemToReturnArray(string $methodName, string $value, string $key = DefaultValue::None) method that adds or updates an element in the array returned by a class/trait/enum method.
Both $value and $key accept raw PHP code strings parsed via PreformattedCode, which allows arbitrary expressions beyond primitives. This is necessary because real-world array elements in notification channels can use class constants and enum values as both keys and values:
public function viaQueues(): array
{
return [
ExpoChannel::class => QueueEnum::PushNotifications,
];
}
Behavior:
- Finds the method by
$methodName and locates the return statement inside it. - If the
return expression is not an array literal — throws UnexpectedReturnTypeException . - If the method contains multiple
return statements — throws MultipleReturnStatementsException . - If
$key is not provided (DefaultValue::None) — appends the value as an indexed element. - If the key already exists in the return array — overwrites its value.
- If the key does not exist — appends a new
key => value element at the end. - If the method is not found — throws
NodeNotExistException . - Works with
Class_, Trait_, Enum_ . - If the file does not contain a
class/trait/enum — throws InvalidStructureTypeException .
Example usage:
new PHPFileBuilder($file)
->addItemToReturnArray('casts', 'datetime', 'email_verified_at')
->addItemToReturnArray('casts', 'RoleEnum::class', 'role')
->addItemToReturnArray('viaQueues', 'QueueEnum::PushNotifications', 'ExpoChannel::class')
->addItemToReturnArray('getAvailableRelations', 'logo')
->save();
- The new functionality must be covered with tests.
Expected outcome
What is the expected result?
- A new visitor
AddItemToReturnArray src/Visitors/MethodVisitors/AddItemToReturnArray.php . - A new exception
UnexpectedReturnTypeException in src/Exceptions/UnexpectedReturnTypeException.php . - A new exception
MultipleReturnStatementsException in src/Exceptions/MultipleReturnStatementsException.php . - A public method
addItemToReturnArray(string $methodName, mixed $value, mixed $key = DefaultValue::None): self` added to PHPFileBuilder . - Both
$value and $key are parsed as raw PHP code strings via PreformattedCode . - Existing keys are overwritten; new keys are appended; elements without a key are appended as indexed items.
- Formatting of the rest of the file is preserved.
Verification scenarios
How can this be tested?
1. Adding a new keyed element — new key => value at the end of the return array.
2. Adding an element without a key — value is appended as an indexed element.
3. Overwriting an existing key — value is updated in-place, no duplicate key is added.
4. Return is not an array literal (e.g. return $variable) — UnexpectedReturnTypeException is thrown.
5. Method does not exist — NodeNotExistException is thrown.
6. Works with trait — same behavior as with a class.
7. Works with enum — same behavior as with a class.
8. Chained calls — ->addItemToReturnArray('casts', 'b', 'a')->addItemToReturnArray('casts', 'd', 'c') applies both changes.
9. Invalid structure — applying to a file without a class/trait/enum throws InvalidStructureTypeException .
Description
What needs to be done?
Implement the
addItemToReturnArray(string $methodName, string $value, string $key = DefaultValue::None)method that adds or updates an element in the array returned by a class/trait/enum method.Both
$valueand$keyaccept raw PHP code strings parsed viaPreformattedCode, which allows arbitrary expressions beyond primitives. This is necessary because real-world array elements in notification channels can use class constants and enum values as both keys and values:Behavior:
$methodNameand locates thereturnstatement inside it.returnexpression is not an array literal — throwsUnexpectedReturnTypeException.returnstatements — throwsMultipleReturnStatementsException.$keyis not provided (DefaultValue::None) — appends the value as an indexed element.key => valueelement at the end.NodeNotExistException.Class_,Trait_,Enum_.class/trait/enum— throwsInvalidStructureTypeException.Example usage:
Expected outcome
What is the expected result?
AddItemToReturnArraysrc/Visitors/MethodVisitors/AddItemToReturnArray.php.UnexpectedReturnTypeExceptioninsrc/Exceptions/UnexpectedReturnTypeException.php.MultipleReturnStatementsExceptioninsrc/Exceptions/MultipleReturnStatementsException.php.addItemToReturnArray(string $methodName, mixed $value, mixed $key = DefaultValue::None): self`added toPHPFileBuilder.$valueand$keyare parsed as raw PHP code strings viaPreformattedCode.Verification scenarios
How can this be tested?
1. Adding a new keyed element — new
key => valueat the end of the return array.2. Adding an element without a key — value is appended as an indexed element.
3. Overwriting an existing key — value is updated in-place, no duplicate key is added.
4. Return is not an array literal (e.g.
return $variable) —UnexpectedReturnTypeExceptionis thrown.5. Method does not exist —
NodeNotExistExceptionis thrown.6. Works with trait — same behavior as with a class.
7. Works with enum — same behavior as with a class.
8. Chained calls —
->addItemToReturnArray('casts', 'b', 'a')->addItemToReturnArray('casts', 'd', 'c')applies both changes.9. Invalid structure — applying to a file without a
class/trait/enumthrowsInvalidStructureTypeException.