Skip to content

Commit 3cca6bc

Browse files
authored
fix(generation): simplify traits and method parameter attributes (#753)
1 parent a4b4709 commit 3cca6bc

10 files changed

+126
-0
lines changed

src/Tempest/Generation/src/SimplifiesClassNames.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,19 @@ private function simplifyClassNames(PhpFile $file): PhpFile
6767
$types[] = $attribute->getName();
6868
}
6969

70+
foreach ($class->getTraits() as $trait) {
71+
$types[] = $trait->getName();
72+
}
73+
7074
foreach ($class->getMethods() as $method) {
7175
$types[] = $method->getReturnType(true);
7276

7377
foreach ($method->getParameters() as $parameter) {
7478
$types[] = $parameter->getType(true);
79+
80+
foreach ($parameter->getAttributes() as $attribute) {
81+
$types[] = $attribute->getName();
82+
}
7583
}
7684

7785
foreach ($method->getAttributes() as $attribute) {

src/Tempest/Generation/tests/ClassManipulatorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use PHPUnit\Framework\Attributes\Test;
88
use Tempest\Generation\ClassManipulator;
9+
use Tempest\Generation\Tests\Fixtures\ClassWithMethodParameterAttributes;
10+
use Tempest\Generation\Tests\Fixtures\ClassWithTraitInAnotherNamespace;
911
use Tempest\Generation\Tests\Fixtures\CreateMigrationsTable;
1012
use Tempest\Generation\Tests\Fixtures\Database\MigrationModel;
1113
use Tempest\Generation\Tests\Fixtures\TestAttribute;
@@ -161,4 +163,20 @@ public function manipulates_classes(): void
161163

162164
$this->assertMatchesSnapshot($class->print());
163165
}
166+
167+
#[Test]
168+
public function simplifies_traits_from_another_namespace(): void
169+
{
170+
$class = new ClassManipulator(ClassWithTraitInAnotherNamespace::class);
171+
172+
$this->assertMatchesSnapshot($class->print());
173+
}
174+
175+
#[Test]
176+
public function simplifies_method_parameter_attributes(): void
177+
{
178+
$class = new ClassManipulator(ClassWithMethodParameterAttributes::class);
179+
180+
$this->assertMatchesSnapshot($class->print());
181+
}
164182
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Generation\Tests\Fixtures;
6+
7+
use Tempest\Generation\Tests\Fixtures\SampleNamespace\SampleParameterAttribute;
8+
9+
final class ClassWithMethodParameterAttributes
10+
{
11+
public function example(
12+
#[SampleParameterAttribute]
13+
string $parameter
14+
): void {
15+
}
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Generation\Tests\Fixtures;
6+
7+
use Tempest\Generation\Tests\Fixtures\SampleNamespace\ExampleTrait;
8+
9+
final class ClassWithTraitInAnotherNamespace
10+
{
11+
use ExampleTrait;
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Generation\Tests\Fixtures\SampleNamespace;
6+
7+
trait ExampleTrait
8+
{
9+
public function traitMethod(): void
10+
{
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Generation\Tests\Fixtures\SampleNamespace;
6+
7+
use Attribute;
8+
9+
#[Attribute(Attribute::TARGET_PARAMETER)]
10+
final class SampleParameterAttribute
11+
{
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Tempest\Generation\Tests\Fixtures;
4+
5+
use Tempest\Generation\Tests\Fixtures\SampleNamespace\SampleParameterAttribute;
6+
7+
final class ClassWithMethodParameterAttributes
8+
{
9+
public function example(
10+
#[SampleParameterAttribute]
11+
string $parameter,
12+
): void {
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Tempest\Generation\Tests\Fixtures;
4+
5+
use Tempest\Generation\Tests\Fixtures\SampleNamespace\SamplePropertyAttribute;
6+
7+
final class ClassWithMethodPropertyAttributes
8+
{
9+
public function example(
10+
#[SamplePropertyAttribute]
11+
string $parameter,
12+
): void {
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Tempest\Generation\Tests\Fixtures;
4+
5+
use Tempest\Generation\Tests\Fixtures\SampleNamespace\ExampleTrait;
6+
7+
final class ClassWithTraitInAnotherNamespace
8+
{
9+
use ExampleTrait;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Tempest\Generation\Tests\Fixtures;
4+
5+
use Tempest\Generation\Tests\Fixtures\SampleNamespace\ExampleTrait;
6+
7+
final class ClassWithTraitInAnotherNamespace
8+
{
9+
use ExampleTrait;
10+
}

0 commit comments

Comments
 (0)