diff --git a/src/Enums/ExpressionAttributeEnum.php b/src/Enums/ExpressionAttributeEnum.php new file mode 100644 index 0000000..32ca234 --- /dev/null +++ b/src/Enums/ExpressionAttributeEnum.php @@ -0,0 +1,8 @@ +hasParentOfType($node, PropertyItem::class)) { + $isMultiline = $node->getAttribute(ExpressionAttributeEnum::IsArrayMultiline->value, false); + + if ($isMultiline) { return '[' . $this->pCommaSeparatedMultiline($node->items, true) . $this->nl . ']'; } return parent::pExpr_Array($node); } - protected function hasParentOfType(Node $node, string $type): bool - { - $parent = $node->getAttribute(StatementAttributeEnum::Parent->value); - - while ($parent !== null) { - if ($parent instanceof $type) { - return true; - } - - $parent = $parent->getAttribute(StatementAttributeEnum::Parent->value); - } - - return false; - } - protected function pStmt_Property(Property $node): string { $newLine = ($this->shouldAddNewlineBeforeIfTypeDiffers($node, Property::class)) ? $this->nl : ''; diff --git a/src/Support/NodeValueFactory.php b/src/Support/NodeValueFactory.php index 405ebff..dd90c87 100644 --- a/src/Support/NodeValueFactory.php +++ b/src/Support/NodeValueFactory.php @@ -14,37 +14,37 @@ class NodeValueFactory { - public static function make(mixed $value): NodeValueDTO + public static function make(mixed $value, array $attributes = []): NodeValueDTO { $type = get_debug_type($value); $node = match ($type) { - 'int' => new Int_($value), - 'array' => static::makeArrayValue($value), - 'string' => new String_($value), - 'float' => new Float_($value), - 'bool' => static::makeBoolValue($value), - 'null' => new ConstFetch(new Name('null')), + 'int' => new Int_($value, $attributes), + 'array' => static::makeArrayValue($value, $attributes), + 'string' => new String_($value, $attributes), + 'float' => new Float_($value, $attributes), + 'bool' => static::makeBoolValue($value, $attributes), + 'null' => new ConstFetch(new Name('null'), $attributes), }; return new NodeValueDTO($node, new Identifier($type)); } - protected static function makeBoolValue(bool $value): ConstFetch + protected static function makeBoolValue(bool $value, array $attributes): ConstFetch { $name = new Name($value ? 'true' : 'false'); - return new ConstFetch($name); + return new ConstFetch($name, $attributes); } - protected static function makeArrayValue(array $values): Array_ + protected static function makeArrayValue(array $values, array $attributes): Array_ { $items = []; foreach ($values as $key => $val) { - $items[] = new ArrayItem(static::make($val)->node, static::make($key)->node); + $items[] = new ArrayItem(static::make($val, $attributes)->node, static::make($key)->node); } - return new Array_($items); + return new Array_($items, $attributes); } } diff --git a/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php b/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php index ea3ca9f..a1b4b62 100644 --- a/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php +++ b/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php @@ -14,6 +14,7 @@ use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Nop; +use RonasIT\Larabuilder\Enums\ExpressionAttributeEnum; use RonasIT\Larabuilder\Enums\InsertPositionEnum; class AddMiddlewarePrependToGroup extends AbstractAppBootstrapVisitor @@ -125,7 +126,9 @@ protected function buildPrependToGroupCall(): Expression protected function buildMiddlewareArg(array $middlewares): Arg { - return new Arg(new Array_($middlewares)); + return new Arg(new Array_($middlewares, [ + ExpressionAttributeEnum::IsArrayMultiline->value => true, + ])); } protected function getMiddlewareList(): array diff --git a/src/Visitors/PropertyVisitors/AddArrayPropertyItem.php b/src/Visitors/PropertyVisitors/AddArrayPropertyItem.php index aab7085..fc1d0bf 100644 --- a/src/Visitors/PropertyVisitors/AddArrayPropertyItem.php +++ b/src/Visitors/PropertyVisitors/AddArrayPropertyItem.php @@ -8,6 +8,7 @@ use PhpParser\Node\Identifier; use PhpParser\Node\PropertyItem; use PhpParser\Node\Stmt\Property; +use RonasIT\Larabuilder\Enums\ExpressionAttributeEnum; use RonasIT\Larabuilder\Exceptions\UnexpectedPropertyTypeException; class AddArrayPropertyItem extends SetProperty @@ -21,7 +22,9 @@ public function __construct( parent::__construct($name, $value); $this->arrayItem = new ArrayItem($this->property->node); - $arrayNode = new Array_([$this->arrayItem]); + $arrayNode = new Array_([$this->arrayItem], [ + ExpressionAttributeEnum::IsArrayMultiline->value => true, + ]); $this->propertyItem = new PropertyItem($this->name, $arrayNode); $this->typeIdentifier = new Identifier('array'); diff --git a/src/Visitors/PropertyVisitors/SetProperty.php b/src/Visitors/PropertyVisitors/SetProperty.php index d8d1343..040be68 100644 --- a/src/Visitors/PropertyVisitors/SetProperty.php +++ b/src/Visitors/PropertyVisitors/SetProperty.php @@ -9,6 +9,7 @@ use RonasIT\Larabuilder\Contracts\InsertNodeContract; use RonasIT\Larabuilder\DTO\NodeValueDTO; use RonasIT\Larabuilder\Enums\AccessModifierEnum; +use RonasIT\Larabuilder\Enums\ExpressionAttributeEnum; use RonasIT\Larabuilder\Support\NodeValueFactory; class SetProperty extends AbstractPropertyVisitor implements InsertNodeContract @@ -24,7 +25,11 @@ public function __construct( ) { parent::__construct($name); - $this->property = NodeValueFactory::make($value); + $attributes = is_array($value) + ? [ExpressionAttributeEnum::IsArrayMultiline->value => true] + : []; + + $this->property = NodeValueFactory::make($value, $attributes); $this->propertyItem = new PropertyItem($this->name, $this->property->node); $this->typeIdentifier = $this->property->typeNode; diff --git a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group.php b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group.php index 9dd5260..7f2c60d 100644 --- a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group.php +++ b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group.php @@ -12,8 +12,14 @@ health: '/up', ) ->withMiddleware(function (Middleware $middleware): void { - $middleware->prependToGroup('api', ['throttle:60,10', FakeClass::class]); - $middleware->prependToGroup('web', ['throttle:10,10', FakeClass::class]); + $middleware->prependToGroup('api', [ + 'throttle:60,10', + FakeClass::class, + ]); + $middleware->prependToGroup('web', [ + 'throttle:10,10', + FakeClass::class, + ]); }) ->withExceptions(function (Exceptions $exceptions): void { // diff --git a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group_as_string.php b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group_as_string.php index bd7306b..820332e 100644 --- a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group_as_string.php +++ b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group_as_string.php @@ -11,7 +11,10 @@ health: '/up', ) ->withMiddleware(function (Middleware $middleware): void { - $middleware->prependToGroup('api', ['throttle:60,10', 'some_middleware']); + $middleware->prependToGroup('api', [ + 'throttle:60,10', + 'some_middleware', + ]); }) ->withExceptions(function (Exceptions $exceptions): void { // diff --git a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group_as_string_set_class.php b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group_as_string_set_class.php index bed591c..39e6ac4 100644 --- a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group_as_string_set_class.php +++ b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group_as_string_set_class.php @@ -11,7 +11,10 @@ health: '/up', ) ->withMiddleware(function (Middleware $middleware): void { - $middleware->prependToGroup('api', [\Illuminate\Auth\Middleware\Authenticate::class, 'some_middleware']); + $middleware->prependToGroup('api', [ + \Illuminate\Auth\Middleware\Authenticate::class, + 'some_middleware', + ]); }) ->withExceptions(function (Exceptions $exceptions): void { // diff --git a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_without_changed_prepend_group.php b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_without_changed_prepend_group.php index 0052a0e..691f34f 100644 --- a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_without_changed_prepend_group.php +++ b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_without_changed_prepend_group.php @@ -12,7 +12,10 @@ health: '/up', ) ->withMiddleware(function (Middleware $middleware): void { - $middleware->prependToGroup('api', ['throttle:60,10', Authenticate::class]); + $middleware->prependToGroup('api', [ + 'throttle:60,10', + Authenticate::class, + ]); }) ->withExceptions(function (Exceptions $exceptions): void { //