Skip to content

Commit f99cc0a

Browse files
authored
Merge pull request api-platform#3851 from dunglas/feat/php8-attributes
feat: add an ApiResource PHP 8 attribute
2 parents a2f719c + 8a5494d commit f99cc0a

19 files changed

+492
-424
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
/tests/Fixtures/app/var/
1212
/tests/Fixtures/app/public/bundles/
1313
/vendor/
14+
/Dockerfile

.php_cs.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ return PhpCsFixer\Config::create()
2929
'@PHPUnit60Migration:risky' => true,
3030
'@Symfony' => true,
3131
'@Symfony:risky' => true,
32+
'single_line_comment_style' => false, // Temporary fix for compatibility with PHP 8 attributes, see https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/5284
3233
'align_multiline_comment' => [
3334
'comment_type' => 'phpdocs_like',
3435
],

phpstan.neon.dist

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ parameters:
2121
- tests/Bridge/NelmioApiDoc/*
2222
- src/Bridge/FosUser/*
2323
# BC layer
24+
- tests/Annotation/ApiResourceTest.php
25+
- tests/Annotation/ApiPropertyTest.php
26+
- tests/Metadata/Resource/Factory/AnnotationResourceMetadataFactoryTest.php
2427
- tests/Fixtures/TestBundle/BrowserKit/Client.php
2528
# The Symfony Configuration API isn't good enough to be analysed
2629
- src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php
@@ -101,6 +104,13 @@ parameters:
101104
message: '#Property Doctrine\\ORM\\Mapping\\ClassMetadataInfo::\$fieldMappings \(array.*\)>\) does not accept array\(.*\)\.#'
102105
path: tests/Bridge/Doctrine/Orm/Metadata/Property/DoctrineOrmPropertyMetadataFactoryTest.php
103106

107+
# Expected, due to PHP 8 attributes
108+
- '#ReflectionProperty::getAttributes\(\)#'
109+
- '#ReflectionMethod::getAttributes\(\)#'
110+
- '#ReflectionClass<mixed>::getAttributes\(\)#'
111+
- '#Constructor of class ApiPlatform\\Core\\Annotation\\ApiResource has an unused parameter#'
112+
- '#Constructor of class ApiPlatform\\Core\\Annotation\\ApiProperty has an unused parameter#'
113+
104114
# Expected, due to optional interfaces
105115
- '#Method ApiPlatform\\Core\\Bridge\\Doctrine\\Orm\\Extension\\QueryCollectionExtensionInterface::applyToCollection\(\) invoked with 5 parameters, 3-4 required\.#'
106116
- '#Method ApiPlatform\\Core\\Bridge\\Doctrine\\Orm\\Extension\\QueryResult(Item|Collection)ExtensionInterface::getResult\(\) invoked with 4 parameters, 1 required\.#'

src/Annotation/ApiProperty.php

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@
3333
* @Attribute("swaggerContext", type="array")
3434
* )
3535
*/
36+
#[\Attribute(\Attribute::TARGET_PROPERTY|\Attribute::TARGET_METHOD)]
3637
final class ApiProperty
3738
{
3839
use AttributesHydratorTrait;
3940

41+
/**
42+
* @var array<string, array>
43+
*/
44+
private static $deprecatedAttributes = [];
45+
4046
/**
4147
* @var string
4248
*/
@@ -88,66 +94,63 @@ final class ApiProperty
8894
public $example;
8995

9096
/**
91-
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
92-
*
93-
* @var string
94-
*/
95-
private $deprecationReason;
96-
97-
/**
98-
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
99-
*
100-
* @var bool
101-
*/
102-
private $fetchable;
103-
104-
/**
105-
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
106-
*
107-
* @var bool
108-
*/
109-
private $fetchEager;
110-
111-
/**
112-
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
97+
* @param string $description
98+
* @param bool $readable
99+
* @param bool $writable
100+
* @param bool $readableLink
101+
* @param bool $writableLink
102+
* @param bool $required
103+
* @param string $iri
104+
* @param bool $identifier
105+
* @param string|int|float|bool|array $default
106+
* @param string|int|float|bool|array|null $example
107+
* @param string $deprecationReason
108+
* @param bool $fetchable
109+
* @param bool $fetchEager
110+
* @param array $jsonldContext
111+
* @param array $openapiContext
112+
* @param bool $push
113+
* @param string $security
114+
* @param array $swaggerContext
113115
*
114-
* @var array
115-
*/
116-
private $jsonldContext;
117-
118-
/**
119-
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
120-
*
121-
* @var array
122-
*/
123-
private $openapiContext;
124-
125-
/**
126-
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
127-
*
128-
* @var bool
129-
*/
130-
private $push;
131-
132-
/**
133-
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
134-
*
135-
* @var string
136-
*/
137-
private $security;
138-
139-
/**
140-
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
141-
*
142-
* @var array
143-
*/
144-
private $swaggerContext;
145-
146-
/**
147116
* @throws InvalidArgumentException
148117
*/
149-
public function __construct(array $values = [])
150-
{
151-
$this->hydrateAttributes($values);
118+
public function __construct(
119+
$description = null,
120+
?bool $readable = null,
121+
?bool $writable = null,
122+
?bool $readableLink = null,
123+
?bool $writableLink = null,
124+
?bool $required = null,
125+
?string $iri = null,
126+
?bool $identifier = null,
127+
$default = null,
128+
$example = null,
129+
130+
// attributes
131+
?array $attributes = null,
132+
?string $deprecationReason = null,
133+
?bool $fetchable = null,
134+
?bool $fetchEager = null,
135+
?array $jsonldContext = null,
136+
?array $openapiContext = null,
137+
?bool $push = null,
138+
?string $security = null,
139+
?array $swaggerContext = null
140+
) {
141+
if (!\is_array($description)) { // @phpstan-ignore-line Doctrine annotations support
142+
[$publicProperties, $configurableAttributes] = self::getConfigMetadata();
143+
144+
foreach ($publicProperties as $prop => $_) {
145+
$this->{$prop} = ${$prop};
146+
}
147+
148+
$description = [];
149+
foreach ($configurableAttributes as $attribute => $_) {
150+
$description[$attribute] = ${$attribute};
151+
}
152+
}
153+
154+
$this->hydrateAttributes($description);
152155
}
153156
}

0 commit comments

Comments
 (0)