Skip to content

Commit 6ab093e

Browse files
authored
chore: better PHP types (#4886)
1 parent 690656d commit 6ab093e

File tree

401 files changed

+753
-1551
lines changed

Some content is hidden

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

401 files changed

+753
-1551
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"justinrainbow/json-schema": "^5.2.1",
5454
"phpdocumentor/reflection-docblock": "^3.0 || ^4.0 || ^5.1",
5555
"phpdocumentor/type-resolver": "^0.3 || ^0.4 || ^1.4",
56+
"phpspec/prophecy-phpunit": "^2.0",
5657
"phpstan/extension-installer": "^1.1",
5758
"phpstan/phpstan": "^1.1",
5859
"phpstan/phpstan-doctrine": "^1.0",

src/Api/IdentifiersExtractor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function __construct(ResourceMetadataCollectionFactoryInterface $resource
4747
*
4848
* TODO: 3.0 identifiers should be stringable?
4949
*/
50-
public function getIdentifiersFromItem($item, Operation $operation = null, array $context = []): array
50+
public function getIdentifiersFromItem(object $item, Operation $operation = null, array $context = []): array
5151
{
5252
$identifiers = [];
5353

@@ -84,7 +84,7 @@ public function getIdentifiersFromItem($item, Operation $operation = null, array
8484
/**
8585
* Gets the value of the given class property.
8686
*/
87-
private function getIdentifierValue($item, string $class, string $property, string $parameterName)
87+
private function getIdentifierValue(object $item, string $class, string $property, string $parameterName): float|bool|int|string
8888
{
8989
if ($item instanceof $class) {
9090
return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, $property), $parameterName);
@@ -124,10 +124,10 @@ private function getIdentifierValue($item, string $class, string $property, stri
124124
*
125125
* @param mixed|\Stringable $identifierValue
126126
*/
127-
private function resolveIdentifierValue($identifierValue, string $parameterName)
127+
private function resolveIdentifierValue(mixed $identifierValue, string $parameterName): float|bool|int|string
128128
{
129129
if (null === $identifierValue) {
130-
throw new RuntimeException('No identifier value found, did you forgot to persist the entity?');
130+
throw new RuntimeException('No identifier value found, did you forget to persist the entity?');
131131
}
132132

133133
if (\is_scalar($identifierValue)) {

src/Api/IdentifiersExtractorInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ interface IdentifiersExtractorInterface
2626
/**
2727
* Finds identifiers from an Item (object).
2828
*
29-
* @param object $item
30-
*
3129
* @throws RuntimeException
3230
*/
33-
public function getIdentifiersFromItem($item, ?Operation $operation = null, array $context = []): array;
31+
public function getIdentifiersFromItem(object $item, ?Operation $operation = null, array $context = []): array;
3432
}

src/Api/IriConverterInterface.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ interface IriConverterInterface
3030
*
3131
* @throws InvalidArgumentException
3232
* @throws ItemNotFoundException
33-
*
34-
* @return object
3533
*/
36-
public function getResourceFromIri(string $iri, array $context = [], ?Operation $operation = null);
34+
public function getResourceFromIri(string $iri, array $context = [], ?Operation $operation = null): object;
3735

3836
/**
3937
* Gets the IRI associated with the given item.
@@ -43,5 +41,5 @@ public function getResourceFromIri(string $iri, array $context = [], ?Operation
4341
* @throws InvalidArgumentException
4442
* @throws RuntimeException
4543
*/
46-
public function getIriFromResource($resource, int $referenceType = UrlGeneratorInterface::ABS_PATH, ?Operation $operation = null, array $context = []): ?string;
44+
public function getIriFromResource(object|string $resource, int $referenceType = UrlGeneratorInterface::ABS_PATH, ?Operation $operation = null, array $context = []): ?string;
4745
}

src/Api/QueryParameterValidator/QueryParameterValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class QueryParameterValidator
3333
{
3434
use FilterLocatorTrait;
3535

36-
private $validators;
36+
private array $validators;
3737

3838
public function __construct(ContainerInterface $filterLocator)
3939
{

src/Api/ResourceClassResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(private readonly ResourceNameCollectionFactoryInterf
3636
/**
3737
* {@inheritdoc}
3838
*/
39-
public function getResourceClass($value, string $resourceClass = null, bool $strict = false): string
39+
public function getResourceClass(mixed $value, string $resourceClass = null, bool $strict = false): string
4040
{
4141
if ($strict && null === $resourceClass) {
4242
throw new InvalidArgumentException('Strict checking is only possible when resource class is specified.');

src/Api/ResourceClassResolverInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ interface ResourceClassResolverInterface
3030
*
3131
* @throws InvalidArgumentException
3232
*/
33-
public function getResourceClass($value, string $resourceClass = null, bool $strict = false): string;
33+
public function getResourceClass(mixed $value, string $resourceClass = null, bool $strict = false): string;
3434

3535
/**
3636
* Is the given class a resource class?

src/Api/UriVariableTransformer/DateTimeUriVariableTransformer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct()
2727
$this->dateTimeNormalizer = new DateTimeNormalizer();
2828
}
2929

30-
public function transform($value, array $types, array $context = [])
30+
public function transform(mixed $value, array $types, array $context = []): \DateTimeInterface
3131
{
3232
try {
3333
return $this->dateTimeNormalizer->denormalize($value, $types[0], null, $context);
@@ -36,7 +36,7 @@ public function transform($value, array $types, array $context = [])
3636
}
3737
}
3838

39-
public function supportsTransformation($value, array $types, array $context = []): bool
39+
public function supportsTransformation(mixed $value, array $types, array $context = []): bool
4040
{
4141
return $this->dateTimeNormalizer->supportsDenormalization($value, $types[0]);
4242
}

src/Api/UriVariableTransformer/IntegerUriVariableTransformer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
final class IntegerUriVariableTransformer implements UriVariableTransformerInterface
2020
{
21-
public function transform($value, array $types, array $context = [])
21+
public function transform(mixed $value, array $types, array $context = []): int
2222
{
2323
return (int) $value;
2424
}
2525

26-
public function supportsTransformation($value, array $types, array $context = []): bool
26+
public function supportsTransformation(mixed $value, array $types, array $context = []): bool
2727
{
2828
return Type::BUILTIN_TYPE_INT === $types[0] && \is_string($value);
2929
}

src/Api/UriVariableTransformerInterface.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@
1818
interface UriVariableTransformerInterface
1919
{
2020
/**
21-
* Denormalizes data back into an object of the given class.
21+
* Transforms the value of a URI variable (identifier) to its type.
2222
*
23-
* @param mixed $value The uri variable value to transform
24-
* @param array $types The guessed type behind the uri variable
23+
* @param mixed $value The URI variable value to transform
24+
* @param array $types The guessed type behind the URI variable
2525
* @param array $context Options available to the transformer
2626
*
27-
* @throws InvalidUriVariableException Occurs when the uriVariable could not be transformed
27+
* @throws InvalidUriVariableException Occurs when the URI variable could not be transformed
2828
*/
29-
public function transform($value, array $types, array $context = []);
29+
public function transform(mixed $value, array $types, array $context = []);
3030

3131
/**
32-
* Checks whether the given class is supported for denormalization by this normalizer.
32+
* Checks whether the value of a URI variable can be transformed to its type by this transformer.
3333
*
34-
* @param mixed $value The uri variable value to transform
35-
* @param array $types The types to which the data should be transformed
34+
* @param mixed $value The URI variable value to transform
35+
* @param array $types The types to which the URI variable value should be transformed
3636
* @param array $context Options available to the transformer
3737
*/
38-
public function supportsTransformation($value, array $types, array $context = []): bool;
38+
public function supportsTransformation(mixed $value, array $types, array $context = []): bool;
3939
}

0 commit comments

Comments
 (0)