Skip to content

Commit

Permalink
chore: better PHP types (#4886)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpoulain authored Aug 11, 2022
1 parent 690656d commit 6ab093e
Show file tree
Hide file tree
Showing 401 changed files with 753 additions and 1,551 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"justinrainbow/json-schema": "^5.2.1",
"phpdocumentor/reflection-docblock": "^3.0 || ^4.0 || ^5.1",
"phpdocumentor/type-resolver": "^0.3 || ^0.4 || ^1.4",
"phpspec/prophecy-phpunit": "^2.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^1.1",
"phpstan/phpstan-doctrine": "^1.0",
Expand Down
8 changes: 4 additions & 4 deletions src/Api/IdentifiersExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct(ResourceMetadataCollectionFactoryInterface $resource
*
* TODO: 3.0 identifiers should be stringable?
*/
public function getIdentifiersFromItem($item, Operation $operation = null, array $context = []): array
public function getIdentifiersFromItem(object $item, Operation $operation = null, array $context = []): array
{
$identifiers = [];

Expand Down Expand Up @@ -84,7 +84,7 @@ public function getIdentifiersFromItem($item, Operation $operation = null, array
/**
* Gets the value of the given class property.
*/
private function getIdentifierValue($item, string $class, string $property, string $parameterName)
private function getIdentifierValue(object $item, string $class, string $property, string $parameterName): float|bool|int|string
{
if ($item instanceof $class) {
return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, $property), $parameterName);
Expand Down Expand Up @@ -124,10 +124,10 @@ private function getIdentifierValue($item, string $class, string $property, stri
*
* @param mixed|\Stringable $identifierValue
*/
private function resolveIdentifierValue($identifierValue, string $parameterName)
private function resolveIdentifierValue(mixed $identifierValue, string $parameterName): float|bool|int|string
{
if (null === $identifierValue) {
throw new RuntimeException('No identifier value found, did you forgot to persist the entity?');
throw new RuntimeException('No identifier value found, did you forget to persist the entity?');
}

if (\is_scalar($identifierValue)) {
Expand Down
4 changes: 1 addition & 3 deletions src/Api/IdentifiersExtractorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ interface IdentifiersExtractorInterface
/**
* Finds identifiers from an Item (object).
*
* @param object $item
*
* @throws RuntimeException
*/
public function getIdentifiersFromItem($item, ?Operation $operation = null, array $context = []): array;
public function getIdentifiersFromItem(object $item, ?Operation $operation = null, array $context = []): array;
}
6 changes: 2 additions & 4 deletions src/Api/IriConverterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ interface IriConverterInterface
*
* @throws InvalidArgumentException
* @throws ItemNotFoundException
*
* @return object
*/
public function getResourceFromIri(string $iri, array $context = [], ?Operation $operation = null);
public function getResourceFromIri(string $iri, array $context = [], ?Operation $operation = null): object;

/**
* Gets the IRI associated with the given item.
Expand All @@ -43,5 +41,5 @@ public function getResourceFromIri(string $iri, array $context = [], ?Operation
* @throws InvalidArgumentException
* @throws RuntimeException
*/
public function getIriFromResource($resource, int $referenceType = UrlGeneratorInterface::ABS_PATH, ?Operation $operation = null, array $context = []): ?string;
public function getIriFromResource(object|string $resource, int $referenceType = UrlGeneratorInterface::ABS_PATH, ?Operation $operation = null, array $context = []): ?string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class QueryParameterValidator
{
use FilterLocatorTrait;

private $validators;
private array $validators;

public function __construct(ContainerInterface $filterLocator)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Api/ResourceClassResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(private readonly ResourceNameCollectionFactoryInterf
/**
* {@inheritdoc}
*/
public function getResourceClass($value, string $resourceClass = null, bool $strict = false): string
public function getResourceClass(mixed $value, string $resourceClass = null, bool $strict = false): string
{
if ($strict && null === $resourceClass) {
throw new InvalidArgumentException('Strict checking is only possible when resource class is specified.');
Expand Down
2 changes: 1 addition & 1 deletion src/Api/ResourceClassResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface ResourceClassResolverInterface
*
* @throws InvalidArgumentException
*/
public function getResourceClass($value, string $resourceClass = null, bool $strict = false): string;
public function getResourceClass(mixed $value, string $resourceClass = null, bool $strict = false): string;

/**
* Is the given class a resource class?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct()
$this->dateTimeNormalizer = new DateTimeNormalizer();
}

public function transform($value, array $types, array $context = [])
public function transform(mixed $value, array $types, array $context = []): \DateTimeInterface
{
try {
return $this->dateTimeNormalizer->denormalize($value, $types[0], null, $context);
Expand All @@ -36,7 +36,7 @@ public function transform($value, array $types, array $context = [])
}
}

public function supportsTransformation($value, array $types, array $context = []): bool
public function supportsTransformation(mixed $value, array $types, array $context = []): bool
{
return $this->dateTimeNormalizer->supportsDenormalization($value, $types[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

final class IntegerUriVariableTransformer implements UriVariableTransformerInterface
{
public function transform($value, array $types, array $context = [])
public function transform(mixed $value, array $types, array $context = []): int
{
return (int) $value;
}

public function supportsTransformation($value, array $types, array $context = []): bool
public function supportsTransformation(mixed $value, array $types, array $context = []): bool
{
return Type::BUILTIN_TYPE_INT === $types[0] && \is_string($value);
}
Expand Down
18 changes: 9 additions & 9 deletions src/Api/UriVariableTransformerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
interface UriVariableTransformerInterface
{
/**
* Denormalizes data back into an object of the given class.
* Transforms the value of a URI variable (identifier) to its type.
*
* @param mixed $value The uri variable value to transform
* @param array $types The guessed type behind the uri variable
* @param mixed $value The URI variable value to transform
* @param array $types The guessed type behind the URI variable
* @param array $context Options available to the transformer
*
* @throws InvalidUriVariableException Occurs when the uriVariable could not be transformed
* @throws InvalidUriVariableException Occurs when the URI variable could not be transformed
*/
public function transform($value, array $types, array $context = []);
public function transform(mixed $value, array $types, array $context = []);

/**
* Checks whether the given class is supported for denormalization by this normalizer.
* Checks whether the value of a URI variable can be transformed to its type by this transformer.
*
* @param mixed $value The uri variable value to transform
* @param array $types The types to which the data should be transformed
* @param mixed $value The URI variable value to transform
* @param array $types The types to which the URI variable value should be transformed
* @param array $context Options available to the transformer
*/
public function supportsTransformation($value, array $types, array $context = []): bool;
public function supportsTransformation(mixed $value, array $types, array $context = []): bool;
}
2 changes: 1 addition & 1 deletion src/Api/UriVariablesConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(private readonly PropertyMetadataFactoryInterface $p
public function convert(array $uriVariables, string $class, array $context = []): array
{
$operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($class)->getOperation();
$context = $context + ['operation' => $operation];
$context += ['operation' => $operation];
$uriVariablesDefinitions = $operation->getUriVariables() ?? [];

foreach ($uriVariables as $parameterName => $value) {
Expand Down
8 changes: 4 additions & 4 deletions src/Api/UriVariablesConverterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
interface UriVariablesConverterInterface
{
/**
* Takes an array of strings representing identifiers and transform their values to the expected type.
* Takes an array of strings representing URI variables (identifiers) and transform their values to the expected type.
*
* @param array $data Identifier to convert to php values
* @param string $class The class to which the identifiers belong to
* @param array $data URI variables to convert to PHP values
* @param string $class The class to which the URI variables belong to
*
* @throws InvalidIdentifierException
*
* @return array indexed by identifiers properties with their values denormalized
* @return array Array indexed by identifiers properties with their values denormalized
*/
public function convert(array $data, string $class, array $context = []): array;
}
15 changes: 10 additions & 5 deletions src/Api/UrlGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

namespace ApiPlatform\Api;

use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;

/**
* UrlGeneratorInterface is the interface that all URL generator classes must implement.
*
Expand Down Expand Up @@ -69,11 +73,12 @@ interface UrlGeneratorInterface
*
* If there is no route with the given name, the generator must throw the RouteNotFoundException.
*
* @param string $name The name of the route
* @param mixed $parameters An array of parameters
* @param int $referenceType The type of reference to be generated (one of the constants)
* The special parameter _fragment will be used as the document fragment suffixed to the final URL.
*
* @return string The generated URL
* @throws RouteNotFoundException If the named route doesn't exist
* @throws MissingMandatoryParametersException When some parameters are missing that are mandatory for the route
* @throws InvalidParameterException When a parameter value for a placeholder is not correct because
* it does not match the requirement
*/
public function generate($name, $parameters = [], $referenceType = self::ABS_PATH);
public function generate(string $name, array $parameters = [], int $referenceType = self::ABS_PATH): string;
}
2 changes: 1 addition & 1 deletion src/Doctrine/Common/Filter/BooleanFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ abstract protected function getProperties(): ?array;

abstract protected function getLogger(): LoggerInterface;

abstract protected function normalizePropertyName($property): string;
abstract protected function normalizePropertyName(string $property): string;

/**
* Determines whether the given property refers to a boolean field.
Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/Common/Filter/DateFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getDescription(string $resourceClass): array

abstract protected function getProperties(): ?array;

abstract protected function normalizePropertyName($property): string;
abstract protected function normalizePropertyName(string $property): string;

/**
* Determines whether the given property refers to a date field.
Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/Common/Filter/ExistsFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ abstract protected function getProperties(): ?array;

abstract protected function getLogger(): LoggerInterface;

abstract protected function normalizePropertyName($property): string;
abstract protected function normalizePropertyName(string $property): string;

private function normalizeValue($value, string $property): ?bool
{
Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/Common/Filter/NumericFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ abstract protected function getProperties(): ?array;

abstract protected function getLogger(): LoggerInterface;

abstract protected function normalizePropertyName($property): string;
abstract protected function normalizePropertyName(string $property): string;

/**
* Determines whether the given property refers to a numeric field.
Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/Common/Filter/OrderFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function getDescription(string $resourceClass): array

abstract protected function getProperties(): ?array;

abstract protected function normalizePropertyName($property): string;
abstract protected function normalizePropertyName(string $property): string;

private function normalizeValue($value, string $property): ?string
{
Expand Down
6 changes: 2 additions & 4 deletions src/Doctrine/Common/Filter/RangeFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ abstract protected function getProperties(): ?array;

abstract protected function getLogger(): LoggerInterface;

abstract protected function normalizePropertyName($property): string;
abstract protected function normalizePropertyName(string $property): string;

/**
* Gets filter description.
Expand Down Expand Up @@ -123,10 +123,8 @@ private function normalizeBetweenValues(array $values): ?array

/**
* Normalize the value.
*
* @return int|float|null
*/
private function normalizeValue(string $value, string $operator)
private function normalizeValue(string $value, string $operator): float|int|null
{
if (!is_numeric($value)) {
$this->getLogger()->notice('Invalid filter ignored', [
Expand Down
10 changes: 4 additions & 6 deletions src/Doctrine/Common/Filter/SearchFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ abstract protected function getIriConverter(): IriConverterInterface;

abstract protected function getPropertyAccessor(): PropertyAccessorInterface;

abstract protected function normalizePropertyName($property): string;
abstract protected function normalizePropertyName(string $property): string;

/**
* Gets the ID from an IRI or a raw ID.
*/
protected function getIdFromValue(string $value)
protected function getIdFromValue(string $value): mixed
{
try {
$iriConverter = $this->getIriConverter();
Expand Down Expand Up @@ -156,12 +156,10 @@ protected function normalizeValues(array $values, string $property): ?array

/**
* When the field should be an integer, check that the given value is a valid one.
*
* @param mixed|null $type
*/
protected function hasValidValues(array $values, $type = null): bool
protected function hasValidValues(array $values, ?string $type = null): bool
{
foreach ($values as $key => $value) {
foreach ($values as $value) {
if (null !== $value && \in_array($type, (array) self::DOCTRINE_INTEGER_TYPE, true) && false === filter_var($value, \FILTER_VALIDATE_INT)) {
return false;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Doctrine/Common/PropertyHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace ApiPlatform\Doctrine\Common;

use Doctrine\DBAL\Types\Type;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\Mapping\ClassMetadata;

Expand Down Expand Up @@ -98,10 +97,8 @@ protected function splitPropertyParts(string $property, string $resourceClass):

/**
* Gets the Doctrine Type of a given property/resourceClass.
*
* @return Type|string|null
*/
protected function getDoctrineFieldType(string $property, string $resourceClass)
protected function getDoctrineFieldType(string $property, string $resourceClass): ?string
{
$propertyParts = $this->splitPropertyParts($property, $resourceClass);
$metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/Common/State/LinksHandlerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private function getLinks(string $resourceClass, Operation $operation, array $co
return $newLinks;
}

private function getIdentifierValue(array &$identifiers, string $name = null)
private function getIdentifierValue(array &$identifiers, string $name = null): mixed
{
if (isset($identifiers[$name])) {
$value = $identifiers[$name];
Expand Down
9 changes: 8 additions & 1 deletion src/Doctrine/Common/State/PersistProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ public function __construct(private readonly ManagerRegistry $managerRegistry)
{
}

public function process($data, Operation $operation, array $uriVariables = [], array $context = [])
/**
* @template T
*
* @param T $data
*
* @return T
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
{
if (!$manager = $this->getManager($data)) {
return $data;
Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/Common/State/RemoveProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(private readonly ManagerRegistry $managerRegistry)
{
}

public function process($data, Operation $operation, array $uriVariables = [], array $context = []): void
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): void
{
if (!$manager = $this->getManager($data)) {
return;
Expand Down
Loading

0 comments on commit 6ab093e

Please sign in to comment.