Skip to content
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
7 changes: 2 additions & 5 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
->setRules([
'@DoctrineAnnotation' => true,
'@PHP71Migration' => true,
'@PHP71Migration:risky' => true,
'@PHP70Migration:risky' => true,
'@PHPUnit60Migration:risky' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
Expand Down Expand Up @@ -76,7 +76,7 @@
],
'no_superfluous_elseif' => true,
'no_superfluous_phpdoc_tags' => [
'allow_mixed' => false,
'allow_mixed' => true, // For PHPStan
'allow_unused_params' => true,
],
'no_unset_cast' => true,
Expand All @@ -98,9 +98,6 @@
'php_unit_test_annotation' => [
'style' => 'prefix',
],
'phpdoc_add_missing_param_annotation' => [
'only_untyped' => true,
],
'phpdoc_no_alias_tag' => true,
'phpdoc_order' => true,
'phpdoc_trim_consecutive_blank_line_separation' => true,
Expand Down
62 changes: 61 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 5
level: 6
paths:
- src
- tests
Expand Down Expand Up @@ -112,3 +112,63 @@ parameters:

# Allow extra assertions in tests: https://github.com/phpstan/phpstan-strict-rules/issues/130
- '#^Call to (static )?method PHPUnit\\Framework\\Assert::.* will always evaluate to true\.$#'

# Level 6
-
identifier: missingType.iterableValue
-
identifier: missingType.generics
Comment on lines +118 to +120

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iterableValue and generics are creating a lot of error, so I prefer to fix missing param/property/return type first

-
identifier: missingType.property
paths:
- src/Doctrine/Common/Tests

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ignored all the tests folder.

- src/Doctrine/Odm/Tests
- src/Doctrine/Orm/Tests
- src/Elasticsearch/Tests
- src/Hydra/Tests
- src/JsonApi/Tests
- src/JsonSchema/Tests
- src/Metadata/Tests
- src/Serializer/Tests
- src/Symfony/Tests
- tests
-
identifier: missingType.parameter
paths:
- src/Doctrine/Common/Tests
- src/Doctrine/Odm/Tests
- src/Doctrine/Orm/Tests
- src/Elasticsearch/Tests
- src/GraphQl/Tests
- src/Hydra/Tests
- src/JsonApi/Tests
- src/JsonSchema/Tests
- src/Metadata/Tests
- src/OpenApi/Tests
- src/Serializer/Tests
- src/Symfony/Bundle/Test
- src/Symfony/Tests
- tests
- src # TODO

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter is ~150 error, I'll do it in the next PR.

-
identifier: missingType.return
paths:
- src/Doctrine/Common/Tests
- src/Doctrine/Odm/Tests
- src/Doctrine/Orm/Tests
- src/Elasticsearch/Tests
- src/GraphQl/Tests
- src/Hydra/Tests
- src/JsonApi/Tests
- src/JsonSchema/Tests
- src/Metadata/Tests
- src/OpenApi/Tests
- src/Serializer/Tests
- src/Symfony/Tests
- tests
-
identifier: argument.templateType
paths:
- src/Symfony/Bundle/Test
- tests
- src # TODO

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's around 50 error, I'll try in another PR.

2 changes: 1 addition & 1 deletion src/GraphQl/Resolver/Factory/ResolverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function __invoke(?string $resourceClass = null, ?string $rootClass = nul
};
}

private function resolve(?array $source, array $args, ResolveInfo $info, ?string $rootClass = null, ?Operation $operation = null, mixed $body = null)
private function resolve(?array $source, array $args, ResolveInfo $info, ?string $rootClass = null, ?Operation $operation = null, mixed $body = null): mixed
{
// Handles relay nodes
if (!$operation) {
Expand Down
2 changes: 1 addition & 1 deletion src/Hydra/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@

if (null !== $inputClass) {
$possibleValue = [];
foreach ($operation->getInputFormats() as $mimeTypes) {
foreach ($operation->getInputFormats() ?? [] as $mimeTypes) {

Check warning on line 316 in src/Hydra/Serializer/DocumentationNormalizer.php

View check run for this annotation

Codecov / codecov/patch

src/Hydra/Serializer/DocumentationNormalizer.php#L316

Added line #L316 was not covered by tests
foreach ($mimeTypes as $mimeType) {
$possibleValue[] = $mimeType;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Laravel/Controller/ApiPlatformController.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
{
$uriVariables = [];
foreach ($operation->getUriVariables() ?? [] as $parameterName => $_) {
$parameter = $request->route($parameterName);
$parameter = $request->route((string) $parameterName);

Check warning on line 106 in src/Laravel/Controller/ApiPlatformController.php

View check run for this annotation

Codecov / codecov/patch

src/Laravel/Controller/ApiPlatformController.php#L106

Added line #L106 was not covered by tests

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mhh actually this is weird, it's a hard problem though maybe we'll leave that for later ^^

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an error saying that route is called with int|string instead of string, so I casted the value.
This value is also casted on the following line.

You prefer a phpstan-ignore-line ?

if (\is_string($parameter) && ($format = $request->attributes->get('_format')) && str_contains($parameter, $format)) {
$parameter = substr($parameter, 0, \strlen($parameter) - (\strlen($format) + 1));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Laravel/Exception/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
$operation = null;
foreach ($resourceCollection as $resource) {
foreach ($resource->getOperations() as $op) {
foreach ($op->getOutputFormats() as $key => $value) {
foreach ($op->getOutputFormats() ?? [] as $key => $value) {

Check warning on line 80 in src/Laravel/Exception/ErrorHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Laravel/Exception/ErrorHandler.php#L80

Added line #L80 was not covered by tests
if ($key === $format) {
$operation = $op;
break 3;
Expand Down
18 changes: 10 additions & 8 deletions src/Metadata/ApiProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ final class ApiProperty
* @param bool|null $push https://api-platform.com/docs/core/push-relations/
* @param string|\Stringable|null $security https://api-platform.com/docs/core/security
* @param string|\Stringable|null $securityPostDenormalize https://api-platform.com/docs/core/security/#executing-access-control-rules-after-denormalization
* @param string[] $types the RDF types of this property
* @param string[] $iris
* @param LegacyType[] $builtinTypes
* @param string[]|null $types the RDF types of this property
* @param string[]|null $iris
* @param LegacyType[]|null $builtinTypes
* @param string|null $uriTemplate (experimental) whether to return the subRessource collection IRI instead of an iterable of IRI
* @param string|null $property The property name
* @param Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth|array<array-key, Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth> $serialize Serializer attributes
* @param Type $nativeType The internal PHP type
* @param Type|null $nativeType The internal PHP type
*/
public function __construct(
private ?string $description = null,
Expand Down Expand Up @@ -345,12 +345,12 @@ public function withIdentifier(bool $identifier): static
return $self;
}

public function getDefault()
public function getDefault(): mixed
{
return $this->default;
}

public function withDefault($default): static
public function withDefault(mixed $default): static
{
$self = clone $this;
$self->default = $default;
Expand Down Expand Up @@ -507,7 +507,7 @@ public function withTypes(array|string $types = []): static
/**
* deprecated since 4.2, use "getNativeType" instead.
*
* @return LegacyType[]
* @return LegacyType[]|null
*/
public function getBuiltinTypes(): ?array
{
Expand Down Expand Up @@ -587,6 +587,8 @@ public function withExtraProperties(array $extraProperties = []): static

/**
* Gets IRI of this property.
*
* @return string[]|null
*/
public function getIris()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we change the return type to ?array as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, done with c29eb43

{
Expand All @@ -609,7 +611,7 @@ public function withIris(string|array $iris): static
/**
* Whether to generate a skolem iri on anonymous resources.
*/
public function getGenId()
public function getGenId(): ?bool
{
return $this->genId;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Metadata/Extractor/XmlPropertyExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ private function buildValues(\SimpleXMLElement $resource): array
return $data;
}

private function buildArrayValue(?\SimpleXMLElement $resource, string $key, mixed $default = null)
private function buildArrayValue(?\SimpleXMLElement $resource, string $key): ?array

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default is never used

{
if (!isset($resource->{$key.'s'}->{$key})) {
return $default;
return null;
}

return (array) $resource->{$key.'s'}->{$key};
Expand Down
4 changes: 2 additions & 2 deletions src/Metadata/Extractor/YamlPropertyExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ private function buildProperties(array $resourcesYaml): void
}
}

private function buildAttribute(array $resource, string $key, mixed $default = null)
private function buildAttribute(array $resource, string $key): ?array
{
if (empty($resource[$key])) {
return $default;
return null;
}

if (!\is_array($resource[$key])) {
Expand Down
46 changes: 39 additions & 7 deletions src/Metadata/HttpOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class HttpOperation extends Operation
public const METHOD_HEAD = 'HEAD';
public const METHOD_OPTIONS = 'OPTIONS';

/** @var array<int|string, string|string[]>|null */
protected $formats;
/** @var array<int|string, string|string[]>|null */
protected $inputFormats;
/** @var array<int|string, string|string[]>|null */
protected $outputFormats;

/**
* @param string[]|null $types the RDF types of this property
* @param array<int|string, string|string[]>|string|null $formats {@see https://api-platform.com/docs/core/content-negotiation/#configuring-formats-for-a-specific-resource-or-operation}
Expand Down Expand Up @@ -90,9 +97,9 @@ public function __construct(
protected string $method = 'GET',
protected ?string $uriTemplate = null,
protected ?array $types = null,
protected $formats = null,
protected $inputFormats = null,
protected $outputFormats = null,
$formats = null,
$inputFormats = null,
$outputFormats = null,
protected $uriVariables = null,
protected ?string $routePrefix = null,
protected ?string $routeName = null,
Expand Down Expand Up @@ -214,6 +221,10 @@ public function __construct(
?bool $queryParameterValidationEnabled = null,
array $extraProperties = [],
) {
$this->formats = (null === $formats || \is_array($formats)) ? $formats : [$formats];
$this->inputFormats = (null === $inputFormats || \is_array($inputFormats)) ? $inputFormats : [$inputFormats];
$this->outputFormats = (null === $outputFormats || \is_array($outputFormats)) ? $outputFormats : [$outputFormats];

parent::__construct(
shortName: $shortName,
class: $class,
Expand Down Expand Up @@ -287,7 +298,7 @@ public function getUriTemplate(): ?string
return $this->uriTemplate;
}

public function withUriTemplate(?string $uriTemplate = null)
public function withUriTemplate(?string $uriTemplate = null): static
{
$self = clone $this;
$self->uriTemplate = $uriTemplate;
Expand All @@ -311,45 +322,66 @@ public function withTypes($types): static
return $self;
}

/**
* @return array<int|string, string|string[]>|null
*/
public function getFormats()
{
return $this->formats;
}

/**
* @param array<int|string, string|string[]>|string|null $formats
*/
public function withFormats($formats = null): static
{
$self = clone $this;
$self->formats = $formats;
$self->formats = (null === $formats || \is_array($formats)) ? $formats : [$formats];

return $self;
}

/**
* @return array<int|string, string|string[]>|null
*/
public function getInputFormats()
{
return $this->inputFormats;
}

/**
* @param array<int|string, string|string[]>|string|null $inputFormats
*/
public function withInputFormats($inputFormats = null): static
{
$self = clone $this;
$self->inputFormats = $inputFormats;
$self->inputFormats = (null === $inputFormats || \is_array($inputFormats)) ? $inputFormats : [$inputFormats];

return $self;
}

/**
* @return array<int|string, string|string[]>|null
*/
public function getOutputFormats()
{
return $this->outputFormats;
}

/**
* @param array<int|string, string|string[]>|string|null $outputFormats
*/
public function withOutputFormats($outputFormats = null): static
{
$self = clone $this;
$self->outputFormats = $outputFormats;
$self->outputFormats = (null === $outputFormats || \is_array($outputFormats)) ? $outputFormats : [$outputFormats];

return $self;
}

/**
* @return array<string, mixed>|array<int, Link>|list<string>|null
*/
public function getUriVariables()
{
return $this->uriVariables;
Expand Down
Loading
Loading