Skip to content

Commit 870b448

Browse files
authored
patch: drop DoctrineAnnotations (#677)
* patch: drop DoctrineAnnotations BC: AnnotationReader constructor changed and it's methods stops triggering exceptions TestControllerWithInvalidParameterAnnotation deleted as `for` is ignored when `HideParameter` used as parameter attribute Also attribute exceptions and classes polished Fixes: #675 * fix: AnnotationReader condition * test: improve coverage * fix: drop parameters check Currently, each parameter attribute is assigned to method's parameter instead of method, makes this check useless * fix: code style * fix: code review * docs: updated with annotations removal * fix: static analysis
1 parent bcda966 commit 870b448

File tree

194 files changed

+1431
-5193
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

194 files changed

+1431
-5193
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"require": {
1313
"php": ">=8.1",
1414
"ext-json": "*",
15-
"doctrine/annotations": "^1.14 || ^2.0",
1615
"composer/package-versions-deprecated": "^1.8",
1716
"phpdocumentor/reflection-docblock": "^4.3 || ^5.0",
1817
"phpdocumentor/type-resolver": "^1.4",

src/AnnotationReader.php

Lines changed: 57 additions & 214 deletions
Large diffs are not rendered by default.

src/Annotations/Autowire.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,36 @@
1111
use function ltrim;
1212

1313
/**
14-
* Use this annotation to autowire a service from the container into a given parameter of a field/query/mutation.
15-
*
16-
* @Annotation
17-
* @Target({"METHOD", "ANNOTATION"})
18-
* @Attributes({
19-
* @Attribute("for", type = "string"),
20-
* @Attribute("identifier", type = "string")
21-
* })
14+
* Use this attribute to autowire a service from the container into a given parameter of a field/query/mutation.
2215
*/
2316
#[Attribute(Attribute::TARGET_PARAMETER)]
2417
class Autowire implements ParameterAnnotationInterface
2518
{
26-
/** @var string|null */
27-
private $for;
28-
/** @var string|null */
29-
private $identifier;
30-
31-
/** @param array<string, mixed>|string $identifier */
32-
public function __construct(array|string $identifier = [])
19+
private string|null $for = null;
20+
private string|null $identifier = null;
21+
22+
/** @param array<string, mixed>|string $params */
23+
public function __construct(
24+
array|string $params = [],
25+
string|null $for = null,
26+
string|null $identifier = null,
27+
)
3328
{
34-
$values = $identifier;
29+
$values = $params;
3530
if (is_string($values)) {
3631
$this->identifier = $values;
3732
} else {
38-
$this->identifier = $values['identifier'] ?? $values['value'] ?? null;
39-
if (isset($values['for'])) {
40-
$this->for = ltrim($values['for'], '$');
33+
$this->identifier = $identifier ?? $values['identifier'] ?? $values['value'] ?? null;
34+
if (isset($values['for']) || $for !== null) {
35+
$this->for = ltrim($for ?? $values['for'], '$');
4136
}
4237
}
4338
}
4439

4540
public function getTarget(): string
4641
{
4742
if ($this->for === null) {
48-
throw new BadMethodCallException('The @Autowire annotation must be passed a target. For instance: "@Autowire(for="$myService")"');
43+
throw new BadMethodCallException('The #[Autowire] attribute must be passed a target. For instance: "#[Autowire(for: "$myService")]"');
4944
}
5045
return $this->for;
5146
}

src/Annotations/Decorate.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,13 @@
1010
use function is_string;
1111

1212
/**
13-
* Methods with this annotation are decorating an input type when the input type is resolved.
13+
* Methods with this attribute are decorating an input type when the input type is resolved.
1414
* This is meant to be used only when the input type is provided by a third-party library and you want to modify it.
15-
*
16-
* @Annotation
17-
* @Target({"METHOD"})
18-
* @Attributes({
19-
* @Attribute("inputTypeName", type = "string"),
20-
* })
2115
*/
2216
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
2317
class Decorate
2418
{
25-
/** @var string */
26-
private $inputTypeName;
19+
private string $inputTypeName;
2720

2821
/**
2922
* @param array<string, mixed>|string $inputTypeName
@@ -36,7 +29,7 @@ public function __construct(array|string $inputTypeName = [])
3629
if (is_string($values)) {
3730
$this->inputTypeName = $values;
3831
} elseif (! isset($values['value']) && ! isset($values['inputTypeName'])) {
39-
throw new BadMethodCallException('The @Decorate annotation must be passed an input type. For instance: "@Decorate("MyInputType")"');
32+
throw new BadMethodCallException('The #[Decorate] attribute must be passed an input type. For instance: "#[Decorate("MyInputType")]"');
4033
} else {
4134
$this->inputTypeName = $values['value'] ?? $values['inputTypeName'];
4235
}

src/Annotations/Exceptions/ClassNotFoundException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ public static function couldNotFindClass(string $className): self
1717

1818
public static function wrapException(self $e, string $className): self
1919
{
20-
return new self($e->getMessage() . " defined in @Type annotation of class '" . $className . "'");
20+
return new self($e->getMessage() . " defined in #[Type] attribute of class '" . $className . "'");
2121
}
2222

2323
public static function wrapExceptionForExtendTag(self $e, string $className): self
2424
{
25-
return new self($e->getMessage() . " defined in @ExtendType annotation of class '" . $className . "'");
25+
return new self($e->getMessage() . " defined in #[ExtendType] attribute of class '" . $className . "'");
2626
}
2727
}

src/Annotations/Exceptions/InvalidParameterException.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@
1111

1212
class InvalidParameterException extends BadMethodCallException
1313
{
14-
public static function parameterNotFound(string $parameter, string $annotationClass, ReflectionMethod $reflectionMethod): self
15-
{
16-
return new self(sprintf('Parameter "%s" declared in annotation "%s" of method "%s::%s()" does not exist.', $parameter, $annotationClass, $reflectionMethod->getDeclaringClass()->getName(), $reflectionMethod->getName()));
17-
}
18-
1914
public static function parameterNotFoundFromSourceField(string $parameter, string $annotationClass, ReflectionMethod $reflectionMethod): self
2015
{
21-
return new self(sprintf('Could not find parameter "%s" declared in annotation "%s". This annotation is itself declared in a SourceField annotation targeting resolver "%s::%s()".', $parameter, $annotationClass, $reflectionMethod->getDeclaringClass()->getName(), $reflectionMethod->getName()));
16+
return new self(sprintf('Could not find parameter "%s" declared in annotation "%s". This annotation is itself declared in a SourceField attribute targeting resolver "%s::%s()".', $parameter, $annotationClass, $reflectionMethod->getDeclaringClass()->getName(), $reflectionMethod->getName()));
2217
}
2318
}

src/Annotations/ExtendType.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,7 @@
1313
use function ltrim;
1414

1515
/**
16-
* The ExtendType annotation must be put in a GraphQL type class docblock and is used to add additional fields to the underlying PHP class.
17-
*
18-
* @Annotation
19-
* @Target({"CLASS"})
20-
* @Attributes({
21-
* @Attribute("class", type = "string"),
22-
* @Attribute("name", type = "string"),
23-
* })
16+
* The ExtendType attribute must be put in a GraphQL type class docblock and is used to add additional fields to the underlying PHP class.
2417
*/
2518
#[Attribute(Attribute::TARGET_CLASS)]
2619
class ExtendType
@@ -41,7 +34,7 @@ public function __construct(array $attributes = [], string|null $class = null, s
4134
$this->name = $name ?? $attributes['name'] ?? null;
4235
$this->class = $className;
4336
if (! $this->class && ! $this->name) {
44-
throw new BadMethodCallException('In annotation @ExtendType, missing one of the compulsory parameter "class" or "name".');
37+
throw new BadMethodCallException('In attribute #[ExtendType], missing one of the compulsory parameter "class" or "name".');
4538
}
4639
}
4740

src/Annotations/Factory.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@
99

1010
/**
1111
* Factories are methods used to declare GraphQL input types.
12-
*
13-
* @Annotation
14-
* @Target({"METHOD"})
15-
* @Attributes({
16-
* @Attribute("name", type = "string"),
17-
* @Attribute("default", type = "bool")
18-
* })
1912
*/
2013
#[Attribute(Attribute::TARGET_METHOD)]
2114
class Factory
@@ -33,7 +26,7 @@ public function __construct(array $attributes = [], string|null $name = null, bo
3326
$this->default = $default ?? $attributes['default'] ?? ! isset($attributes['name']);
3427

3528
if ($this->name === null && $this->default === false) {
36-
throw new GraphQLRuntimeException('A @Factory that has "default=false" attribute must be given a name (i.e. add a name="FooBarInput" attribute).');
29+
throw new GraphQLRuntimeException('A #[Factory] that has "default=false" attribute must be given a name (i.e. add a name="FooBarInput" attribute).');
3730
}
3831
}
3932

src/Annotations/FailWith.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@
1010
use function array_key_exists;
1111
use function is_array;
1212

13-
/**
14-
* @Annotation
15-
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
16-
* @Attributes({
17-
* @Attribute("value", type = "mixed"),
18-
* @Attribute("mode", type = "string")
19-
* })
20-
*/
2113
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD)]
2214
class FailWith implements MiddlewareAnnotationInterface
2315
{
@@ -35,8 +27,10 @@ public function __construct(mixed $values = [], mixed $value = '__fail__with__ma
3527
$this->value = $value;
3628
} elseif (is_array($values) && array_key_exists('value', $values)) {
3729
$this->value = $values['value'];
30+
} elseif (! is_array($values)) {
31+
$this->value = $values;
3832
} else {
39-
throw new BadMethodCallException('The @FailWith annotation must be passed a defaultValue. For instance: "@FailWith(null)"');
33+
throw new BadMethodCallException('The #[FailWith] attribute must be passed a defaultValue. For instance: "#[FailWith(null)]"');
4034
}
4135
}
4236

src/Annotations/Field.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@
1010

1111
use const E_USER_DEPRECATED;
1212

13-
/**
14-
* @Annotation
15-
* @Target({"PROPERTY", "METHOD"})
16-
* @Attributes({
17-
* @Attribute("name", type = "string"),
18-
* @Attribute("outputType", type = "string"),
19-
* @Attribute("prefetchMethod", type = "string"),
20-
* @Attribute("for", type = "string[]"),
21-
* @Attribute("description", type = "string"),
22-
* @Attribute("inputType", type = "string"),
23-
* })
24-
*/
2513
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
2614
class Field extends AbstractRequest
2715
{

0 commit comments

Comments
 (0)