Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add an ApiResource PHP 8 attribute #3851

Merged
merged 3 commits into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
/tests/Fixtures/app/var/
/tests/Fixtures/app/public/bundles/
/vendor/
/Dockerfile
1 change: 1 addition & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ return PhpCsFixer\Config::create()
'@PHPUnit60Migration:risky' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
'single_line_comment_style' => false, // Temporary fix for compatibility with PHP 8 attributes, see https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/5284
'align_multiline_comment' => [
'comment_type' => 'phpdocs_like',
],
Expand Down
10 changes: 10 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ parameters:
- tests/Bridge/NelmioApiDoc/*
- src/Bridge/FosUser/*
# BC layer
- tests/Annotation/ApiResourceTest.php
- tests/Annotation/ApiPropertyTest.php
- tests/Metadata/Resource/Factory/AnnotationResourceMetadataFactoryTest.php
- tests/Fixtures/TestBundle/BrowserKit/Client.php
# The Symfony Configuration API isn't good enough to be analysed
- src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php
Expand Down Expand Up @@ -101,6 +104,13 @@ parameters:
message: '#Property Doctrine\\ORM\\Mapping\\ClassMetadataInfo::\$fieldMappings \(array.*\)>\) does not accept array\(.*\)\.#'
path: tests/Bridge/Doctrine/Orm/Metadata/Property/DoctrineOrmPropertyMetadataFactoryTest.php

# Expected, due to PHP 8 attributes
- '#ReflectionProperty::getAttributes\(\)#'
- '#ReflectionMethod::getAttributes\(\)#'
- '#ReflectionClass<mixed>::getAttributes\(\)#'
- '#Constructor of class ApiPlatform\\Core\\Annotation\\ApiResource has an unused parameter#'
- '#Constructor of class ApiPlatform\\Core\\Annotation\\ApiProperty has an unused parameter#'

# Expected, due to optional interfaces
- '#Method ApiPlatform\\Core\\Bridge\\Doctrine\\Orm\\Extension\\QueryCollectionExtensionInterface::applyToCollection\(\) invoked with 5 parameters, 3-4 required\.#'
- '#Method ApiPlatform\\Core\\Bridge\\Doctrine\\Orm\\Extension\\QueryResult(Item|Collection)ExtensionInterface::getResult\(\) invoked with 4 parameters, 1 required\.#'
Expand Down
119 changes: 61 additions & 58 deletions src/Annotation/ApiProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@
* @Attribute("swaggerContext", type="array")
* )
*/
#[\Attribute(\Attribute::TARGET_PROPERTY|\Attribute::TARGET_METHOD)]
final class ApiProperty
{
use AttributesHydratorTrait;

/**
* @var array<string, array>
*/
private static $deprecatedAttributes = [];

/**
* @var string
*/
Expand Down Expand Up @@ -88,66 +94,63 @@ final class ApiProperty
public $example;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var string
*/
private $deprecationReason;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var bool
*/
private $fetchable;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var bool
*/
private $fetchEager;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
* @param string $description
* @param bool $readable
* @param bool $writable
* @param bool $readableLink
* @param bool $writableLink
* @param bool $required
* @param string $iri
* @param bool $identifier
* @param string|int|float|bool|array $default
* @param string|int|float|bool|array|null $example
* @param string $deprecationReason
* @param bool $fetchable
* @param bool $fetchEager
* @param array $jsonldContext
* @param array $openapiContext
* @param bool $push
* @param string $security
* @param array $swaggerContext
*
* @var array
*/
private $jsonldContext;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var array
*/
private $openapiContext;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var bool
*/
private $push;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var string
*/
private $security;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var array
*/
private $swaggerContext;

/**
* @throws InvalidArgumentException
*/
public function __construct(array $values = [])
{
$this->hydrateAttributes($values);
public function __construct(
$description = null,
?bool $readable = null,
?bool $writable = null,
?bool $readableLink = null,
?bool $writableLink = null,
?bool $required = null,
?string $iri = null,
?bool $identifier = null,
$default = null,
$example = null,

// attributes
?array $attributes = null,
?string $deprecationReason = null,
?bool $fetchable = null,
?bool $fetchEager = null,
?array $jsonldContext = null,
?array $openapiContext = null,
?bool $push = null,
?string $security = null,
?array $swaggerContext = null
) {
if (!\is_array($description)) { // @phpstan-ignore-line Doctrine annotations support
[$publicProperties, $configurableAttributes] = self::getConfigMetadata();

foreach ($publicProperties as $prop => $_) {
$this->{$prop} = ${$prop};
}

$description = [];
foreach ($configurableAttributes as $attribute => $_) {
$description[$attribute] = ${$attribute};
}
}

$this->hydrateAttributes($description);
}
}
Loading