Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Enums/ExpressionAttributeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace RonasIT\Larabuilder\Enums;

enum ExpressionAttributeEnum: string
{
case IsArrayMultiline = 'is_array_multiline';
}
21 changes: 4 additions & 17 deletions src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\PropertyItem;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use PhpParser\PrettyPrinter\Standard;
use RonasIT\Larabuilder\Enums\ExpressionAttributeEnum;
use RonasIT\Larabuilder\Enums\StatementAttributeEnum;
use RonasIT\Larabuilder\Nodes\PreformattedCode;

Expand Down Expand Up @@ -41,28 +41,15 @@ protected function removeDuplicateEmptyLines(string $code): string

protected function pExpr_Array(Array_ $node): string
{
if ($this->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 : '';
Expand Down
24 changes: 12 additions & 12 deletions src/Support/NodeValueFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/Visitors/PropertyVisitors/AddArrayPropertyItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
Expand Down
7 changes: 6 additions & 1 deletion src/Visitors/PropertyVisitors/SetProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
//
Expand Down