diff --git a/src/Printer.php b/src/Printer.php index f487f3e..15e09db 100644 --- a/src/Printer.php +++ b/src/Printer.php @@ -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 diff --git a/src/Support/NodeInserter.php b/src/Support/NodeInserter.php index feffe93..90ddae8 100644 --- a/src/Support/NodeInserter.php +++ b/src/Support/NodeInserter.php @@ -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; @@ -24,6 +25,7 @@ class NodeInserter Trait_::class, Enum_::class, TraitUse::class, + EnumCase::class, ClassConst::class, Property::class, ClassMethod::class, diff --git a/tests/PHPFileBuilderTest.php b/tests/PHPFileBuilderTest.php index b302fa4..df2e5f3 100644 --- a/tests/PHPFileBuilderTest.php +++ b/tests/PHPFileBuilderTest.php @@ -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', @@ -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', @@ -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) @@ -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."); @@ -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', @@ -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'); diff --git a/tests/Support/OriginStructures/class_with_properties.php b/tests/Support/OriginStructures/class_with_properties.php index e656bbc..3163d1e 100644 --- a/tests/Support/OriginStructures/class_with_properties.php +++ b/tests/Support/OriginStructures/class_with_properties.php @@ -29,15 +29,4 @@ class SomeClass extends Some 'string1', ]; public array $notArray = []; - - public function __construct() - { - if ($boolProperty) { - $nullProperty = null; - } - } - - public function someMethod(): void - { - } } diff --git a/tests/fixtures/PHPFileBuilderTest/class_with_properties_unchanged.php b/tests/Support/OriginStructures/class_with_properties_and_method.php similarity index 100% rename from tests/fixtures/PHPFileBuilderTest/class_with_properties_unchanged.php rename to tests/Support/OriginStructures/class_with_properties_and_method.php diff --git a/tests/Support/OriginStructures/enum_without_method.php b/tests/Support/OriginStructures/enum_without_method.php new file mode 100644 index 0000000..7ffb810 --- /dev/null +++ b/tests/Support/OriginStructures/enum_without_method.php @@ -0,0 +1,13 @@ +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 { diff --git a/tests/fixtures/PHPFileBuilderTest/class_with_array_properties.php b/tests/fixtures/PHPFileBuilderTest/class_with_array_properties.php index 0fe55f3..84366f9 100644 --- a/tests/fixtures/PHPFileBuilderTest/class_with_array_properties.php +++ b/tests/fixtures/PHPFileBuilderTest/class_with_array_properties.php @@ -43,15 +43,4 @@ class SomeClass extends Some public array $bool = [ true, ]; - - public function __construct() - { - if ($boolProperty) { - $nullProperty = null; - } - } - - public function someMethod(): void - { - } } diff --git a/tests/fixtures/PHPFileBuilderTest/class_with_array_properties_removed.php b/tests/fixtures/PHPFileBuilderTest/class_with_array_properties_removed.php index 4dfca03..9d5b208 100644 --- a/tests/fixtures/PHPFileBuilderTest/class_with_array_properties_removed.php +++ b/tests/fixtures/PHPFileBuilderTest/class_with_array_properties_removed.php @@ -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 - { - } } diff --git a/tests/fixtures/PHPFileBuilderTest/class_with_properties.php b/tests/fixtures/PHPFileBuilderTest/class_with_properties.php index 1a8101c..d5b9e67 100644 --- a/tests/fixtures/PHPFileBuilderTest/class_with_properties.php +++ b/tests/fixtures/PHPFileBuilderTest/class_with_properties.php @@ -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 - { - } } diff --git a/tests/fixtures/PHPFileBuilderTest/class_with_properties_and_method_unchanged.php b/tests/fixtures/PHPFileBuilderTest/class_with_properties_and_method_unchanged.php new file mode 100644 index 0000000..e656bbc --- /dev/null +++ b/tests/fixtures/PHPFileBuilderTest/class_with_properties_and_method_unchanged.php @@ -0,0 +1,43 @@ + '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 + { + } +} diff --git a/tests/fixtures/PHPFileBuilderTest/enum_without_method_with_added_method.php b/tests/fixtures/PHPFileBuilderTest/enum_without_method_with_added_method.php new file mode 100644 index 0000000..d834bb7 --- /dev/null +++ b/tests/fixtures/PHPFileBuilderTest/enum_without_method_with_added_method.php @@ -0,0 +1,20 @@ +