Skip to content

Commit 39ae4d7

Browse files
committed
Merge remote-tracking branch 'origin/master' into create-add-item-to-return-array
2 parents 2d7ffe6 + e682823 commit 39ae4d7

13 files changed

Lines changed: 91 additions & 185 deletions

File tree

.github/actions/php-laravel-setup/action.yml

Lines changed: 0 additions & 34 deletions
This file was deleted.

.github/workflows/run-tests-with-coverage.yml

Lines changed: 0 additions & 75 deletions
This file was deleted.

.github/workflows/tests.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [ "master" ]
6+
pull_request:
7+
branches: [ "master" ]
8+
9+
jobs:
10+
run-tests:
11+
uses: ronasit/github-actions/.github/workflows/tests.yml@main

README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,16 @@ new PHPFileBuilder(app_path('Http/Controllers/UserController.php'))
7878
$service->delete($id);
7979
return response()->noContent();
8080
',
81-
params: new MethodParams(
82-
new MethodParam(name: 'request', type: 'DeleteRequest'),
83-
new MethodParam(name: 'service', type: 'Service'),
84-
new MethodParam(name: 'id', type: 'int'),
81+
params: new MethodParamsList(
82+
new MethodParamDTO(name: 'request', type: 'DeleteRequest'),
83+
new MethodParamDTO(name: 'service', type: 'UserService'),
84+
new MethodParamDTO(name: 'id', type: 'int'),
8585
),
8686
returnType: 'Response',
8787
)
8888
->save();
8989
```
9090

91-
Each `MethodParam` accepts: `name`, `type` (e.g. `'int'`, `'?string'`, `'MyClass'`), `default` (`DefaultValue::None` to omit), `variadic`, `byRef`.
92-
93-
`addMethod` also supports `static: true` and `returnsByRef: true` (for methods that return by reference, e.g. `public function &items(): array`).
94-
9591
## Special Laravel structure builders
9692

9793
### Bootstrap app

src/Builders/PHPFileBuilder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
use PhpParser\Error;
66
use PhpParser\NodeVisitor\CloningVisitor;
77
use PhpParser\ParserFactory;
8+
use RonasIT\Larabuilder\DTO\MethodParamsList;
89
use RonasIT\Larabuilder\Enums\AccessModifierEnum;
910
use RonasIT\Larabuilder\Enums\DefaultValue;
1011
use RonasIT\Larabuilder\Enums\InsertPositionEnum;
1112
use RonasIT\Larabuilder\Exceptions\InvalidPHPFileException;
1213
use RonasIT\Larabuilder\NodeTraverser;
1314
use RonasIT\Larabuilder\Printer;
14-
use RonasIT\Larabuilder\ValueOptions\MethodParams;
1515
use RonasIT\Larabuilder\Visitors\AddImports;
1616
use RonasIT\Larabuilder\Visitors\AddTraits;
1717
use RonasIT\Larabuilder\Visitors\MethodVisitors\AddItemToReturnArray;
@@ -85,13 +85,13 @@ public function addTraits(array $traits): self
8585
public function addMethod(
8686
string $name,
8787
string $code,
88-
MethodParams $params = new MethodParams(),
88+
MethodParamsList $params = new MethodParamsList(),
8989
?string $returnType = null,
9090
AccessModifierEnum $accessModifier = AccessModifierEnum::Public,
91-
bool $static = false,
92-
bool $returnsByRef = false,
91+
bool $isStatic = false,
92+
bool $isReturnsByRef = false,
9393
): self {
94-
$this->traverser->addVisitor(new AddMethod($name, $code, $params, $returnType, $accessModifier, $static, $returnsByRef));
94+
$this->traverser->addVisitor(new AddMethod($name, $code, $params, $returnType, $accessModifier, $isStatic, $isReturnsByRef));
9595

9696
return $this;
9797
}

src/DTO/MethodParamDTO.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace RonasIT\Larabuilder\DTO;
4+
5+
use RonasIT\Larabuilder\Enums\DefaultValue;
6+
7+
readonly class MethodParamDTO
8+
{
9+
public function __construct(
10+
public string $name,
11+
public ?string $type = null,
12+
public mixed $default = DefaultValue::None,
13+
public bool $isVariadic = false,
14+
public bool $isReference = false,
15+
) {
16+
}
17+
}

src/DTO/MethodParamsList.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace RonasIT\Larabuilder\DTO;
4+
5+
use Illuminate\Contracts\Support\Arrayable;
6+
7+
/** @implements Arrayable<int, MethodParamDTO> */
8+
class MethodParamsList implements Arrayable
9+
{
10+
protected array $params;
11+
12+
public function __construct(MethodParamDTO ...$params)
13+
{
14+
$this->params = $params;
15+
}
16+
17+
public function toArray(): array
18+
{
19+
return $this->params;
20+
}
21+
}

src/ValueOptions/MethodParam.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/ValueOptions/MethodParams.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Visitors/MethodVisitors/AddMethod.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@
99
use PhpParser\Node\Param;
1010
use PhpParser\Node\Stmt\ClassMethod;
1111
use RonasIT\Larabuilder\Contracts\InsertNodeContract;
12+
use RonasIT\Larabuilder\DTO\MethodParamDTO;
13+
use RonasIT\Larabuilder\DTO\MethodParamsList;
1214
use RonasIT\Larabuilder\Enums\AccessModifierEnum;
1315
use RonasIT\Larabuilder\Enums\DefaultValue;
1416
use RonasIT\Larabuilder\Exceptions\NodeAlreadyExistsException;
1517
use RonasIT\Larabuilder\Nodes\PreformattedCode;
16-
use RonasIT\Larabuilder\ValueOptions\MethodParam;
17-
use RonasIT\Larabuilder\ValueOptions\MethodParams;
1818

19-
class AddMethod extends AbstractMethodVisitor implements InsertNodeContract
19+
class AddMethod extends BaseMethodVisitor implements InsertNodeContract
2020
{
2121
protected PreformattedCode $code;
2222

2323
public function __construct(
2424
protected string $name,
2525
string $code,
26-
protected MethodParams $params,
26+
protected MethodParamsList $paramsList,
2727
protected ?string $returnType = null,
28-
protected ?AccessModifierEnum $accessModifier = null,
29-
protected bool $static = false,
30-
protected bool $returnsByRef = false,
28+
protected AccessModifierEnum $accessModifier = AccessModifierEnum::Public,
29+
protected bool $isStatic = false,
30+
protected bool $isReturnsByRef = false,
3131
) {
3232
$this->code = new PreformattedCode($code);
3333
}
@@ -45,29 +45,29 @@ protected function modify(Node $node): Node
4545

4646
public function getInsertableNode(): Node
4747
{
48-
$flags = ($this->accessModifier ?? AccessModifierEnum::Public)->value;
48+
$flags = $this->accessModifier->value;
4949

50-
if ($this->static) {
50+
if ($this->isStatic) {
5151
$flags |= Modifiers::STATIC;
5252
}
5353

5454
return new ClassMethod($this->name, [
5555
'flags' => $flags,
56-
'byRef' => $this->returnsByRef,
56+
'byRef' => $this->isReturnsByRef,
5757
'params' => $this->buildParams(),
58-
'returnType' => $this->returnType !== null ? BuilderHelpers::normalizeType($this->returnType) : null,
58+
'returnType' => (!is_null($this->returnType)) ? BuilderHelpers::normalizeType($this->returnType) : null,
5959
'stmts' => [$this->code],
6060
]);
6161
}
6262

6363
protected function buildParams(): array
6464
{
65-
return array_map(fn (MethodParam $param) => new Param(
65+
return array_map(fn (MethodParamDTO $param) => new Param(
6666
var: new Variable($param->name),
67-
default: $param->default !== DefaultValue::None ? BuilderHelpers::normalizeValue($param->default) : null,
68-
type: $param->type !== null ? BuilderHelpers::normalizeType($param->type) : null,
69-
byRef: $param->byRef,
70-
variadic: $param->variadic,
71-
), $this->params->params);
67+
default: ($param->default !== DefaultValue::None) ? BuilderHelpers::normalizeValue($param->default) : null,
68+
type: (!is_null($param->type)) ? BuilderHelpers::normalizeType($param->type) : null,
69+
byRef: $param->isReference,
70+
variadic: $param->isVariadic,
71+
), $this->paramsList->toArray());
7272
}
7373
}

0 commit comments

Comments
 (0)