Skip to content

Commit e342b26

Browse files
committed
📦 Migrate to attributes - since support for Doctrine annotations dropped
1 parent b296280 commit e342b26

File tree

9 files changed

+51
-105
lines changed

9 files changed

+51
-105
lines changed

src/Types/SymfonyUserInterfaceType.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@
99
use Symfony\Component\Security\Core\User\UserInterface;
1010
use TheCodingMachine\GraphQLite\FieldNotFoundException;
1111

12-
/**
13-
* @Type(class=UserInterface::class)
14-
*/
12+
#[Type(class: UserInterface::class)]
1513
class SymfonyUserInterfaceType
1614
{
17-
/**
18-
* @Field
19-
*/
15+
#[Field]
2016
public function getUserName(UserInterface $user): string
2117
{
2218
// @phpstan-ignore-next-line Forward Compatibility for Symfony >=5.3
@@ -33,9 +29,9 @@ public function getUserName(UserInterface $user): string
3329
}
3430

3531
/**
36-
* @Field()
3732
* @return string[]
3833
*/
34+
#[Field]
3935
public function getRoles(UserInterface $user): array
4036
{
4137
$roles = [];

tests/Fixtures/Controller/TestGraphqlController.php

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,116 +20,89 @@
2020

2121
class TestGraphqlController
2222
{
23-
24-
/**
25-
* @Query()
26-
*/
23+
#[Query]
2724
public function test(string $foo): string
2825
{
2926
return 'echo ' .$foo;
3027
}
3128

3229
/**
33-
* @Query()
3430
* @return Product[]
3531
*/
32+
#[Query]
3633
public function products(): array
3734
{
3835
return [
3936
new Product('Mouf', 9999)
4037
];
4138
}
4239

43-
/**
44-
* @Query()
45-
*/
40+
#[Query]
4641
public function contact(): Contact
4742
{
4843
return new Contact('Mouf');
4944
}
5045

51-
/**
52-
* @Mutation()
53-
*/
46+
#[Mutation]
5447
public function saveProduct(Product $product): Product
5548
{
5649
return $product;
5750
}
5851

5952
/**
60-
* @Query()
6153
* @return Contact[]
6254
*/
55+
#[Query]
6356
public function contacts(): ArrayResult
6457
{
6558
return new ArrayResult([new Contact('Mouf')]);
6659
}
6760

68-
/**
69-
* @Query()
70-
* @return string
71-
*/
61+
#[Query]
7262
public function triggerException(int $code = 0): string
7363
{
7464
throw new MyException('Boom', $code);
7565
}
7666

77-
/**
78-
* @Query()
79-
* @return string
80-
*/
67+
#[Query]
8168
public function triggerAggregateException(): string
8269
{
8370
$exception1 = new GraphQLException('foo', 401);
8471
$exception2 = new GraphQLException('bar', 404, null, ['field' => 'baz', 'category' => 'MyCat']);
8572
throw new GraphQLAggregateException([$exception1, $exception2]);
8673
}
8774

88-
/**
89-
* @Query()
90-
* @Logged()
91-
* @FailWith(null)
92-
* @return string
93-
*/
75+
#[Query]
76+
#[Logged]
77+
#[FailWith(null)]
9478
public function loggedQuery(): string
9579
{
9680
return 'foo';
9781
}
9882

99-
/**
100-
* @Query()
101-
* @Right("ROLE_ADMIN")
102-
* @FailWith(null)
103-
* @return string
104-
*/
83+
#[Query]
84+
#[Right('ROLE_ADMIN')]
85+
#[FailWith(null)]
10586
public function withAdminRight(): string
10687
{
10788
return 'foo';
10889
}
10990

110-
/**
111-
* @Query()
112-
* @Right("ROLE_USER")
113-
* @FailWith(null)
114-
* @return string
115-
*/
91+
#[Query]
92+
#[Right('ROLE_USER')]
93+
#[FailWith(null)]
11694
public function withUserRight(): string
11795
{
11896
return 'foo';
11997
}
12098

121-
/**
122-
* @Query()
123-
* @return string
124-
*/
99+
#[Query]
125100
public function getUri(Request $request): string
126101
{
127102
return $request->getPathInfo();
128103
}
129104

130-
/**
131-
* @Query
132-
*/
105+
#[Query]
133106
#[Assertion(for: 'email', constraint: new Assert\Email())]
134107
public function findByMail(string $email = 'a@a.com'): string
135108
{

tests/Fixtures/Entities/BadClass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
22

3+
if (class_exists('\BadClass')) {
4+
// workaround for https://github.com/alekitto/class-finder/issues/24
5+
return;
6+
}
37

48
// The namespace for this class is broken. It must not impact Symfony.
59
class BadClass

tests/Fixtures/Entities/Contact.php

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
<?php
22

3-
43
namespace TheCodingMachine\GraphQLite\Bundle\Tests\Fixtures\Entities;
54

6-
75
use stdClass;
86
use TheCodingMachine\GraphQLite\Annotations\Field;
97
use TheCodingMachine\GraphQLite\Annotations\Type;
108
use TheCodingMachine\GraphQLite\Bundle\Tests\Fixtures\Controller\TestGraphqlController;
119
use TheCodingMachine\GraphQLite\Annotations\Autowire;
1210

13-
/**
14-
* @Type()
15-
*/
11+
#[Type]
1612
class Contact
1713
{
1814
/**
@@ -25,51 +21,45 @@ public function __construct(string $name)
2521
$this->name = $name;
2622
}
2723

28-
/**
29-
* @Field(name="name")
30-
*/
24+
#[Field(name: 'name')]
3125
public function getName(): string
3226
{
3327
return $this->name;
3428
}
3529

36-
/**
37-
* @Field()
38-
* @Autowire(for="$testService")
39-
* @Autowire(for="$someService", identifier="someService")
40-
* @Autowire(for="$someAlias", identifier="someAlias")
41-
* @return string
42-
*/
43-
public function injectService(TestGraphqlController $testService = null, stdClass $someService = null, stdClass $someAlias = null): string
44-
{
30+
#[Field]
31+
public function injectService(
32+
#[Autowire]
33+
TestGraphqlController $testService = null,
34+
#[Autowire(identifier: 'someService')]
35+
stdClass $someService = null,
36+
#[Autowire(identifier: 'someAlias')]
37+
stdClass $someAlias = null,
38+
): string {
4539
if (!$testService instanceof TestGraphqlController || $someService === null || $someAlias === null) {
4640
return 'KO';
4741
}
4842
return 'OK';
4943
}
5044

51-
/**
52-
* @Field(prefetchMethod="prefetchData")
53-
*/
45+
#[Field(prefetchMethod: 'prefetchData')]
5446
public function injectServicePrefetch($prefetchData): string
5547
{
5648
return $prefetchData;
5749
}
5850

59-
/**
60-
* @Autowire(for="$someOtherService", identifier="someOtherService")
61-
*/
62-
public function prefetchData(iterable $iterable, stdClass $someOtherService = null)
63-
{
51+
public function prefetchData(
52+
iterable $iterable,
53+
#[Autowire(identifier: 'someOtherService')]
54+
stdClass $someOtherService = null,
55+
) {
6456
if ($someOtherService === null) {
6557
return 'KO';
6658
}
6759
return 'OK';
6860
}
6961

70-
/**
71-
* @Field()
72-
*/
62+
#[Field]
7363
public function getManager(): ?Contact
7464
{
7565
return null;

tests/Fixtures/Entities/Product.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace TheCodingMachine\GraphQLite\Bundle\Tests\Fixtures\Entities;
54

6-
75
class Product
86
{
97
/**

tests/Fixtures/Types/ContactType.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@
99
use TheCodingMachine\GraphQLite\Bundle\Tests\Fixtures\Entities\Contact;
1010

1111

12-
/**
13-
* @ExtendType(class=Contact::class)
14-
*/
12+
#[ExtendType(class: Contact::class)]
1513
class ContactType
1614
{
17-
/**
18-
* @Field()
19-
*/
15+
#[Field]
2016
public function uppercaseName(Contact $contact): string
2117
{
2218
return strtoupper($contact->getName());

tests/Fixtures/Types/ProductFactory.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99

1010
class ProductFactory
1111
{
12-
13-
/**
14-
* @Factory()
15-
*/
12+
#[Factory]
1613
public function buildProduct(string $name, float $price): Product
1714
{
1815
return new Product($name, $price);

tests/Fixtures/Types/ProductType.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@
1010
use TheCodingMachine\GraphQLite\Bundle\Tests\Fixtures\Entities\Product;
1111

1212

13-
/**
14-
* @Type(class=Product::class)
15-
* @SourceField(name="name")
16-
* @SourceField(name="price")
17-
*/
13+
#[Type(class: Product::class)]
14+
#[SourceField(name: 'name')]
15+
#[SourceField(name: 'price')]
1816
class ProductType
1917
{
20-
/**
21-
* @Field()
22-
*/
18+
#[Field]
2319
public function getSeller(Product $product): ?Contact
2420
{
2521
return null;

tests/NoSecurityBundleFixtures/Controller/EchoController.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
<?php
22

3-
43
namespace TheCodingMachine\GraphQLite\Bundle\Tests\NoSecurityBundleFixtures\Controller;
54

6-
75
use TheCodingMachine\GraphQLite\Annotations\Query;
86

97
class EchoController
108
{
11-
/**
12-
* @Query()
13-
*/
9+
#[Query]
1410
public function echoMsg(string $message): string {
1511
return $message;
1612
}

0 commit comments

Comments
 (0)