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: 4 additions & 4 deletions src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,23 @@ protected function shouldAddNewlineBeforeIfTypeDiffers(Node $node, string $type)

protected function pStmt_Expression(Expression $node): string
{
$newLine = ($this->shouldAddNewlineBeforeIfTypeSame($node, Expression::class)) ? $this->nl : '';
$newLine = ($this->shouldAddNewlineBeforeIfPreviousNodeExists($node)) ? $this->nl : '';

return $newLine . parent::pStmt_Expression($node);
}

protected function pStmt_ClassMethod(ClassMethod $node): string
{
$newLine = ($this->shouldAddNewlineBeforeIfTypeSame($node, ClassMethod::class)) ? $this->nl : '';
$newLine = ($this->shouldAddNewlineBeforeIfPreviousNodeExists($node)) ? $this->nl : '';

return $newLine . parent::pStmt_ClassMethod($node);
}

protected function shouldAddNewlineBeforeIfTypeSame(Node $node, string $type): bool
protected function shouldAddNewlineBeforeIfPreviousNodeExists(Node $node): bool
{
$previousNode = $node->getAttribute(StatementAttributeEnum::Previous->value);

return $previousNode !== null && $previousNode instanceof $type;
return $previousNode !== null;
}

protected function pStmt_PreformattedCode(PreformattedCode $node): string
Expand Down
2 changes: 2 additions & 0 deletions src/Support/NodeInserter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Enum_;
use PhpParser\Node\Stmt\EnumCase;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Property;
Expand All @@ -24,6 +25,7 @@ class NodeInserter
Trait_::class,
Enum_::class,
TraitUse::class,
EnumCase::class,
ClassConst::class,
Property::class,
ClassMethod::class,
Expand Down
35 changes: 29 additions & 6 deletions tests/PHPFileBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public function testAddTraitsNotClassTraitEnum(): void

public function testInsertCodeToMethodToTheEndPosition(): void
{
$file = $this->generateOriginalStructurePath('class_with_properties.php');
$file = $this->generateOriginalStructurePath('class_with_properties_and_method.php');

$this->mockNativeFunction(
'RonasIT\Larabuilder\Builders',
Expand All @@ -387,7 +387,7 @@ public function testInsertCodeToMethodToTheEndPosition(): void

public function testInsertCodeToMethodToTheStartPosition(): void
{
$file = $this->generateOriginalStructurePath('class_with_properties.php');
$file = $this->generateOriginalStructurePath('class_with_properties_and_method.php');

$this->mockNativeFunction(
'RonasIT\Larabuilder\Builders',
Expand Down Expand Up @@ -440,11 +440,11 @@ public function testInsertCodeToMethodNotExists(): void

public function testInsertCodeToMethodEmptyString(): void
{
$file = $this->generateOriginalStructurePath('class_with_properties.php');
$file = $this->generateOriginalStructurePath('class_with_properties_and_method.php');

$this->mockNativeFunction(
'RonasIT\Larabuilder\Builders',
$this->callFilePutContent($file, 'class_with_properties_unchanged.php'),
$this->callFilePutContent($file, 'class_with_properties_and_method_unchanged.php'),
);

new PHPFileBuilder($file)
Expand Down Expand Up @@ -476,7 +476,7 @@ public function testInsertCodeToMethodNotClassTraitEnum(): void

public function testInsertCodeToMethodWhenMethodNotExist(): void
{
$file = $this->generateOriginalStructurePath('class_with_properties.php');
$file = $this->generateOriginalStructurePath('class_with_properties_and_method.php');

$this->assertExceptionThrew(NodeNotExistException::class, "Method 'noMethod' does not exist.");

Expand Down Expand Up @@ -596,7 +596,7 @@ public function testAddMethodAlreadyExists(): void

public function testAddStaticMethod(): void
{
$file = $this->generateOriginalStructurePath('class.php');
$file = $this->generateOriginalStructurePath('class_with_properties.php');

$this->mockNativeFunction(
'RonasIT\Larabuilder\Builders',
Expand Down Expand Up @@ -650,6 +650,29 @@ public function testAddMethodToEnum(): void
->save();
}

public function testAddMethodToEnumWithoutMethod(): void
{
$file = $this->generateOriginalStructurePath('enum_without_method.php');

$this->mockNativeFunction(
'RonasIT\Larabuilder\Builders',
$this->callFilePutContent($file, 'enum_without_method_with_added_method.php'),
);

new PHPFileBuilder($file)
->addMethod(
name: 'updatableStatuses',
code: '
return [
self::Paid,
];
',
returnType: 'array',
isStatic: true,
)
->save();
}

public function testAddMethodToTrait(): void
{
$file = $this->generateOriginalStructurePath('trait.php');
Expand Down
11 changes: 0 additions & 11 deletions tests/Support/OriginStructures/class_with_properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,4 @@ class SomeClass extends Some
'string1',
];
public array $notArray = [];

public function __construct()
{
if ($boolProperty) {
$nullProperty = null;
}
}

public function someMethod(): void
{
}
}
13 changes: 13 additions & 0 deletions tests/Support/OriginStructures/enum_without_method.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Enums;

use RonasIT\Support\Traits\EnumTrait;

enum StatusEnum: string
{
use EnumTrait;

case Paid = 'paid';
case Error = 'error';
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,33 @@

namespace RonasIT\Larabuilder\Tests\Support;

use RonasIT\Larabuilder\Tests\Support\FirstClass;
use Some\SomeTrait;
use RonasIT\Support\Traits\FirstTrait;
use Some;

/**
* Test
*/
class SomeClass implements Test, Some
class SomeClass extends Some
{
use FirstTrait, SecondTrait;

public function __construct()
{
}

public function someMethod()
{
$a = 1;
$b = 2;

if ($a === $b) return true;

// Save the user model to the database
$user->save();

$config = ['status' => true, 'version' => 1];

$db->table('users')->where('id', 1)->first();

Arr::map($arr, fn ($value) => str_replace('0', '1', $value));
}
use SomeTrait;

public const STATUS_ACTIVE = 'active';
public const STATUS_ACTIVE1 = 'active';

public string $stringProperty = 'some value';
public bool $boolProperty = false;
public array $arrayProperty = ['element' => 'value'];
public int $intProperty;
public float $floatProperty;
public $nullProperty = null;
protected array $tags = ['one', 'two', 3, true, 5.5, 78.4];
protected array $fillable = [
'name',
'email',
];
public array $newMultiArrayProperty = [
'arrayProperty' => [0 => 1, 1 => 'string', 2 => true],
'arrayProperty2' => [1, 2, 3],
'arrayProperty3' => ['key1' => 5, 'key2' => 3.67, 'key3' => false, 'key4' => 'test', 'key5' => [10, true, 'foo'], 'key6' => null],
'string1',
];
public array $notArray = [];

public static function create(): static
{
Expand Down
11 changes: 0 additions & 11 deletions tests/fixtures/PHPFileBuilderTest/class_with_array_properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,4 @@ class SomeClass extends Some
public array $bool = [
true,
];

public function __construct()
{
if ($boolProperty) {
$nullProperty = null;
}
}

public function someMethod(): void
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,4 @@ class SomeClass extends Some
'arrayProperty2' => [1, 2, 3],
];
public array $notArray = [];

public function __construct()
{
if ($boolProperty) {
$nullProperty = null;
}
}

public function someMethod(): void
{
}
}
11 changes: 0 additions & 11 deletions tests/fixtures/PHPFileBuilderTest/class_with_properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,4 @@ class SomeClass extends Some
];
public array $notArray = [];
public string $newString = 'some string';

public function __construct()
{
if ($boolProperty) {
$nullProperty = null;
}
}

public function someMethod(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace RonasIT\Larabuilder\Tests\Support;

use Some;

class SomeClass extends Some
{
use SomeTrait;

public const STATUS_ACTIVE = 'active';
public const STATUS_ACTIVE1 = 'active';

public string $stringProperty = 'some value';
public bool $boolProperty = false;
public array $arrayProperty = ['element' => 'value'];
public int $intProperty;
public float $floatProperty;
public $nullProperty = null;
protected array $tags = ['one', 'two', 3, true, 5.5, 78.4];
protected array $fillable = [
'name',
'email',
];
public array $newMultiArrayProperty = [
'arrayProperty' => [0 => 1, 1 => 'string', 2 => true],
'arrayProperty2' => [1, 2, 3],
'arrayProperty3' => ['key1' => 5, 'key2' => 3.67, 'key3' => false, 'key4' => 'test', 'key5' => [10, true, 'foo'], 'key6' => null],
'string1',
];
public array $notArray = [];

public function __construct()
{
if ($boolProperty) {
$nullProperty = null;
}
}

public function someMethod(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Enums;

use RonasIT\Support\Traits\EnumTrait;

enum StatusEnum: string
{
use EnumTrait;

case Paid = 'paid';
case Error = 'error';

public static function updatableStatuses(): array
{
return [
self::Paid,
];
}
}