Skip to content

Commit

Permalink
minor #2125 add type declarations (xabbuh)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.0-dev branch.

Discussion
----------

add type declarations

Commits
-------

d71f1fc add type declarations where possible
  • Loading branch information
xabbuh committed Mar 18, 2020
2 parents 991fcca + d71f1fc commit fc2666e
Show file tree
Hide file tree
Showing 75 changed files with 276 additions and 629 deletions.
13 changes: 3 additions & 10 deletions Context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ final class Context
private $attributes = array();
private $version;
private $groups;
private $maxDepth;
private $isMaxDepthEnabled;
private $serializeNull;

Expand Down Expand Up @@ -56,20 +55,14 @@ public function getAttributes(): array
return $this->attributes;
}

/**
* Sets the normalization version.
*/
public function setVersion(string $version): self
{
$this->version = $version;

return $this;
}

/**
* @return string|int|null
*/
public function getVersion()
public function getVersion(): ?string
{
return $this->version;
}
Expand Down Expand Up @@ -125,7 +118,7 @@ public function setGroups(array $groups = null): self
return $this;
}

public function enableMaxDepth()
public function enableMaxDepth(): self
{
$this->isMaxDepthEnabled = true;

Expand Down Expand Up @@ -173,7 +166,7 @@ public function getExclusionStrategies(): array
*
* Notice: This method only applies to the JMS serializer adapter.
*/
public function addExclusionStrategy(ExclusionStrategyInterface $exclusionStrategy)
public function addExclusionStrategy(ExclusionStrategyInterface $exclusionStrategy): void
{
$this->exclusionStrategies[] = $exclusionStrategy;
}
Expand Down
14 changes: 3 additions & 11 deletions Controller/ControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,25 @@ protected function getViewHandler()
}

/**
* @param int $statusCode
*
* @return View
*/
protected function view($data = null, $statusCode = null, array $headers = [])
protected function view($data = null, ?int $statusCode = null, array $headers = [])
{
return View::create($data, $statusCode, $headers);
}

/**
* @param string $url
* @param int $statusCode
*
* @return View
*/
protected function redirectView($url, $statusCode = Response::HTTP_FOUND, array $headers = [])
protected function redirectView(string $url, int $statusCode = Response::HTTP_FOUND, array $headers = [])
{
return View::createRedirect($url, $statusCode, $headers);
}

/**
* @param string $route
* @param int $statusCode
*
* @return View
*/
protected function routeRedirectView($route, array $parameters = [], $statusCode = Response::HTTP_CREATED, array $headers = [])
protected function routeRedirectView(string $route, array $parameters = [], int $statusCode = Response::HTTP_CREATED, array $headers = [])
{
return View::createRouteRedirect($route, $parameters, $statusCode, $headers);
}
Expand Down
8 changes: 2 additions & 6 deletions Controller/ExceptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;

/**
* Custom ExceptionController that uses the view layer and supports HTTP response status code mapping.
Expand All @@ -38,12 +37,9 @@ public function __construct(
$this->showException = $showException;
}

/**
* @param \Throwable $exception
*/
public function showAction(Request $request, $exception, DebugLoggerInterface $logger = null)
public function showAction(Request $request, \Throwable $exception): Response
{
$currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));
$this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));

$view = new View($exception, $this->getStatusCode($exception), $exception instanceof HttpExceptionInterface ? $exception->getHeaders() : []);

Expand Down
4 changes: 2 additions & 2 deletions Decoder/ContainerDecoderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public function __construct(ContainerInterface $container, array $decoders)
/**
* {@inheritdoc}
*/
public function supports($format)
public function supports(string $format): bool
{
return isset($this->decoders[$format]);
}

/**
* {@inheritdoc}
*/
public function getDecoder($format)
public function getDecoder(string $format): DecoderInterface
{
if (!$this->supports($format)) {
throw new \InvalidArgumentException(sprintf("Format '%s' is not supported by ContainerDecoderProvider.", $format));
Expand Down
4 changes: 1 addition & 3 deletions Decoder/DecoderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ interface DecoderInterface
/**
* Decodes a string into PHP data.
*
* @param string $data
*
* @return mixed False in case the content could not be decoded
*/
public function decode($data);
public function decode(string $data);
}
8 changes: 2 additions & 6 deletions Decoder/DecoderProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,14 @@ interface DecoderProviderInterface
/**
* Checks if a certain format is supported.
*
* @param string $format
*
* @return bool
*/
public function supports($format);
public function supports(string $format);

/**
* Provides decoders, possibly lazily.
*
* @param string $format
*
* @return DecoderInterface
*/
public function getDecoder($format);
public function getDecoder(string $format);
}
2 changes: 1 addition & 1 deletion Decoder/JsonDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class JsonDecoder implements DecoderInterface
/**
* {@inheritdoc}
*/
public function decode($data)
public function decode(string $data)
{
return @json_decode($data, true);
}
Expand Down
4 changes: 2 additions & 2 deletions Decoder/JsonToFormDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class JsonToFormDecoder implements DecoderInterface
/**
* Makes data decoded from JSON application/x-www-form-encoded compliant.
*/
private function xWwwFormEncodedLike(array &$data)
private function xWwwFormEncodedLike(array &$data): void
{
foreach ($data as $key => &$value) {
if (is_array($value)) {
Expand All @@ -43,7 +43,7 @@ private function xWwwFormEncodedLike(array &$data)
/**
* {@inheritdoc}
*/
public function decode($data)
public function decode(string $data)
{
$decodedData = @json_decode($data, true);
if ($decodedData) {
Expand Down
2 changes: 1 addition & 1 deletion Decoder/XmlDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct()
/**
* {@inheritdoc}
*/
public function decode($data)
public function decode(string $data)
{
try {
return $this->encoder->decode($data, 'xml');
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Compiler/ConfigurationCheckPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
final class ConfigurationCheckPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
if ($container->has('fos_rest.converter.request_body') && !($container->has('sensio_framework_extra.converter.listener') || $container->has(ParamConverterListener::class))) {
throw new \RuntimeException('You need to enable the parameter converter listeners in SensioFrameworkExtraBundle when using the FOSRestBundle RequestBodyParamConverter');
Expand Down
6 changes: 3 additions & 3 deletions DependencyInjection/Compiler/FormatListenerRulesPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
final class FormatListenerRulesPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition('fos_rest.format_listener')) {
return;
Expand Down Expand Up @@ -57,7 +57,7 @@ public function process(ContainerBuilder $container)
$container->setParameter('fos_rest.format_listener.rules', null);
}

private function addRule(array $rule, ContainerBuilder $container)
private function addRule(array $rule, ContainerBuilder $container): void
{
$matcher = $this->createRequestMatcher(
$container,
Expand All @@ -76,7 +76,7 @@ private function addRule(array $rule, ContainerBuilder $container)
->addMethodCall('add', [$matcher, $rule]);
}

private function createRequestMatcher(ContainerBuilder $container, $path = null, $host = null, $methods = null, array $attributes = array())
private function createRequestMatcher(ContainerBuilder $container, ?string $path = null, ?string $host = null, ?array $methods = null, array $attributes = array()): Reference
{
$arguments = [$path, $host, $methods, null, $attributes];
$serialized = serialize($arguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
class HandlerRegistryDecorationPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
if (!$container->has('fos_rest.serializer.jms_handler_registry')) {
return;
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Compiler/JMSFormErrorHandlerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
final class JMSFormErrorHandlerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
if (!$container->has('jms_serializer.form_error_handler')) {
return;
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Compiler/JMSHandlersPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
final class JMSHandlersPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
if ($container->has('jms_serializer.handler_registry')) {
// the public alias prevents the handler registry definition from being removed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
final class SerializerConfigurationPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
if ($container->has('fos_rest.serializer')) {
$class = $container->getParameterBag()->resolveValue(
Expand Down
17 changes: 6 additions & 11 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
*/
final class Configuration implements ConfigurationInterface
{
/**
* Default debug mode value.
*
* @var bool
*/
private $debug;

public function __construct(bool $debug)
Expand Down Expand Up @@ -188,7 +183,7 @@ public function getConfigTreeBuilder(): TreeBuilder
return $treeBuilder;
}

private function addViewSection(ArrayNodeDefinition $rootNode)
private function addViewSection(ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
Expand Down Expand Up @@ -274,7 +269,7 @@ private function addViewSection(ArrayNodeDefinition $rootNode)
->end();
}

private function addBodyListenerSection(ArrayNodeDefinition $rootNode)
private function addBodyListenerSection(ArrayNodeDefinition $rootNode): void
{
$decodersDefaultValue = ['json' => 'fos_rest.decoder.json'];
if (class_exists(XmlEncoder::class)) {
Expand Down Expand Up @@ -315,7 +310,7 @@ private function addBodyListenerSection(ArrayNodeDefinition $rootNode)
->end();
}

private function addFormatListenerSection(ArrayNodeDefinition $rootNode)
private function addFormatListenerSection(ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
Expand Down Expand Up @@ -369,7 +364,7 @@ private function addFormatListenerSection(ArrayNodeDefinition $rootNode)
->end();
}

private function addVersioningSection(ArrayNodeDefinition $rootNode)
private function addVersioningSection(ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
Expand Down Expand Up @@ -419,7 +414,7 @@ private function addVersioningSection(ArrayNodeDefinition $rootNode)
->end();
}

private function addExceptionSection(ArrayNodeDefinition $rootNode)
private function addExceptionSection(ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
Expand Down Expand Up @@ -486,7 +481,7 @@ private function addExceptionSection(ArrayNodeDefinition $rootNode)
->end();
}

private function testExceptionExists(string $exception)
private function testExceptionExists(string $exception): void
{
if (!is_subclass_of($exception, \Exception::class) && !is_a($exception, \Exception::class, true)) {
throw new InvalidConfigurationException(sprintf('FOSRestBundle exception mapper: Could not load class "%s" or the class does not extend from "%s". Most probably this is a configuration problem.', $exception, \Exception::class));
Expand Down
Loading

0 comments on commit fc2666e

Please sign in to comment.