Skip to content

Improve some PHPdocs based on existing Symfony stubs in PHPstan and Psalm #47016

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

Merged
merged 3 commits into from
Aug 2, 2022
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
63 changes: 35 additions & 28 deletions .github/expected-missing-return-types.diff
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ index 165797504b..0c0922088a 100644
{
if (!static::$booted) {
diff --git a/src/Symfony/Component/BrowserKit/AbstractBrowser.php b/src/Symfony/Component/BrowserKit/AbstractBrowser.php
index ac25bdf4be..949a036abd 100644
index b27ca37529..5b80175850 100644
--- a/src/Symfony/Component/BrowserKit/AbstractBrowser.php
+++ b/src/Symfony/Component/BrowserKit/AbstractBrowser.php
@@ -408,5 +408,5 @@ abstract class AbstractBrowser
Expand Down Expand Up @@ -209,23 +209,30 @@ index 64068fcc23..f29aaf1b94 100644
{
foreach ($command->getHelperSet() as $helper) {
diff --git a/src/Symfony/Component/Console/Command/Command.php b/src/Symfony/Component/Console/Command/Command.php
index 0a3f4b7889..18c2312399 100644
index b41e691537..34de10fa70 100644
--- a/src/Symfony/Component/Console/Command/Command.php
+++ b/src/Symfony/Component/Console/Command/Command.php
@@ -188,5 +188,5 @@ class Command
@@ -189,5 +189,5 @@ class Command
* @return bool
*/
- public function isEnabled()
+ public function isEnabled(): bool
{
return true;
@@ -214,5 +214,5 @@ class Command
@@ -215,5 +215,5 @@ class Command
* @see setCode()
*/
- protected function execute(InputInterface $input, OutputInterface $output)
+ protected function execute(InputInterface $input, OutputInterface $output): int
{
throw new LogicException('You must override the execute() method in the concrete command class.');
@@ -684,5 +684,5 @@ class Command
* @throws InvalidArgumentException if the helper is not defined
*/
- public function getHelper(string $name): mixed
+ public function getHelper(string $name): HelperInterface
{
if (null === $this->helperSet) {
diff --git a/src/Symfony/Component/Console/Formatter/OutputFormatter.php b/src/Symfony/Component/Console/Formatter/OutputFormatter.php
index 3c6b0efccd..121664f15a 100644
--- a/src/Symfony/Component/Console/Formatter/OutputFormatter.php
Expand Down Expand Up @@ -283,7 +290,7 @@ index 3af991a76f..742e2508f3 100644

/**
diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AbstractRecursivePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AbstractRecursivePass.php
index 70b6c91ff5..cfced387f3 100644
index 2f1631ed30..a4b572771e 100644
--- a/src/Symfony/Component/DependencyInjection/Compiler/AbstractRecursivePass.php
+++ b/src/Symfony/Component/DependencyInjection/Compiler/AbstractRecursivePass.php
@@ -71,5 +71,5 @@ abstract class AbstractRecursivePass implements CompilerPassInterface
Expand Down Expand Up @@ -351,31 +358,31 @@ index d553203c43..1163f4b107 100644
{
$class = static::class;
diff --git a/src/Symfony/Component/DependencyInjection/Extension/ExtensionInterface.php b/src/Symfony/Component/DependencyInjection/Extension/ExtensionInterface.php
index f2373ed5ea..1eec21a938 100644
index 4f66f18073..e96d867296 100644
--- a/src/Symfony/Component/DependencyInjection/Extension/ExtensionInterface.php
+++ b/src/Symfony/Component/DependencyInjection/Extension/ExtensionInterface.php
@@ -33,5 +33,5 @@ interface ExtensionInterface
@@ -35,5 +35,5 @@ interface ExtensionInterface
* @return string
*/
- public function getNamespace();
+ public function getNamespace(): string;

/**
@@ -40,5 +40,5 @@ interface ExtensionInterface
@@ -42,5 +42,5 @@ interface ExtensionInterface
* @return string|false
*/
- public function getXsdValidationBasePath();
+ public function getXsdValidationBasePath(): string|false;

/**
@@ -49,4 +49,4 @@ interface ExtensionInterface
@@ -51,4 +51,4 @@ interface ExtensionInterface
* @return string
*/
- public function getAlias();
+ public function getAlias(): string;
}
diff --git a/src/Symfony/Component/DependencyInjection/LazyProxy/Instantiator/InstantiatorInterface.php b/src/Symfony/Component/DependencyInjection/LazyProxy/Instantiator/InstantiatorInterface.php
index a9d78115dd..8b3b420a9c 100644
index 92c4b44845..ae557d8bca 100644
--- a/src/Symfony/Component/DependencyInjection/LazyProxy/Instantiator/InstantiatorInterface.php
+++ b/src/Symfony/Component/DependencyInjection/LazyProxy/Instantiator/InstantiatorInterface.php
@@ -31,4 +31,4 @@ interface InstantiatorInterface
Expand Down Expand Up @@ -568,10 +575,10 @@ index 1cb865fd66..f6f4efe7a7 100644
+ public function getName(): string;
}
diff --git a/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php b/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
index 45ff4a006c..611259b3b6 100644
index d97064da8b..d76c73df90 100644
--- a/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
+++ b/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
@@ -448,5 +448,5 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
@@ -463,5 +463,5 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
* @return Response
*/
- protected function forward(Request $request, bool $catch = false, Response $entry = null)
Expand Down Expand Up @@ -776,7 +783,7 @@ index 6da0bcb4c8..16e9765b1d 100644
+ public function getTypes(string $class, string $property, array $context = []): ?array;
}
diff --git a/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php b/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php
index 204e9b3341..8e624e1154 100644
index 5f3a852d32..65a18bd924 100644
--- a/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php
+++ b/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php
@@ -253,5 +253,5 @@ abstract class AnnotationClassLoader implements LoaderInterface
Expand Down Expand Up @@ -808,7 +815,7 @@ index 6912f8a15b..caf18c886a 100644
+ public function getRouteCollection(): RouteCollection;
}
diff --git a/src/Symfony/Component/Security/Core/Authentication/RememberMe/TokenProviderInterface.php b/src/Symfony/Component/Security/Core/Authentication/RememberMe/TokenProviderInterface.php
index eda4730004..00cfc5b9c7 100644
index 9b32fdce31..fbbd65d8b7 100644
--- a/src/Symfony/Component/Security/Core/Authentication/RememberMe/TokenProviderInterface.php
+++ b/src/Symfony/Component/Security/Core/Authentication/RememberMe/TokenProviderInterface.php
@@ -28,5 +28,5 @@ interface TokenProviderInterface
Expand All @@ -819,11 +826,11 @@ index eda4730004..00cfc5b9c7 100644

/**
diff --git a/src/Symfony/Component/Security/Core/Authorization/Voter/VoterInterface.php b/src/Symfony/Component/Security/Core/Authorization/Voter/VoterInterface.php
index 7e401c3ff3..6b446ff376 100644
index ba52c8ea65..e879a84982 100644
--- a/src/Symfony/Component/Security/Core/Authorization/Voter/VoterInterface.php
+++ b/src/Symfony/Component/Security/Core/Authorization/Voter/VoterInterface.php
@@ -36,4 +36,4 @@ interface VoterInterface
* @return int either ACCESS_GRANTED, ACCESS_ABSTAIN, or ACCESS_DENIED
@@ -37,4 +37,4 @@ interface VoterInterface
* @psalm-return self::ACCESS_* must be transformed into @return on Symfony 7
*/
- public function vote(TokenInterface $token, mixed $subject, array $attributes);
+ public function vote(TokenInterface $token, mixed $subject, array $attributes): int;
Expand Down Expand Up @@ -889,7 +896,7 @@ index 480ea8ad6b..fa43d6a6e9 100644
+ public function getListeners(Request $request): array;
}
diff --git a/src/Symfony/Component/Serializer/Encoder/DecoderInterface.php b/src/Symfony/Component/Serializer/Encoder/DecoderInterface.php
index f38069e471..0966eb3e89 100644
index 5014b9bd51..757c76f546 100644
--- a/src/Symfony/Component/Serializer/Encoder/DecoderInterface.php
+++ b/src/Symfony/Component/Serializer/Encoder/DecoderInterface.php
@@ -35,5 +35,5 @@ interface DecoderInterface
Expand All @@ -906,7 +913,7 @@ index f38069e471..0966eb3e89 100644
+ public function supportsDecoding(string $format /* , array $context = [] */): bool;
}
diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
index 44ba45f581..3398115497 100644
index 391cdcb39c..f637687e74 100644
--- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
+++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
@@ -213,5 +213,5 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
Expand All @@ -931,7 +938,7 @@ index 44ba45f581..3398115497 100644
{
if (null !== $object = $this->extractObjectToPopulate($class, $context, self::OBJECT_TO_POPULATE)) {
diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
index 511dd1c724..c319e1839b 100644
index 714fb10e30..ea3f4428bf 100644
--- a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+++ b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
@@ -139,5 +139,5 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
Expand All @@ -948,43 +955,43 @@ index 511dd1c724..c319e1839b 100644
+ public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
if (!isset($context['cache_key'])) {
@@ -265,5 +265,5 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
@@ -230,5 +230,5 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
* {@inheritdoc}
*/
- protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes, string $format = null)
+ protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes, string $format = null): object
{
if ($this->classDiscriminatorResolver && $mapping = $this->classDiscriminatorResolver->getMappingForClass($class)) {
@@ -327,5 +327,5 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
@@ -292,5 +292,5 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
* @return string[]
*/
- abstract protected function extractAttributes(object $object, string $format = null, array $context = []);
+ abstract protected function extractAttributes(object $object, string $format = null, array $context = []): array;

/**
@@ -334,5 +334,5 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
@@ -299,5 +299,5 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
* @return mixed
*/
- abstract protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = []);
+ abstract protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = []): mixed;

/**
@@ -341,5 +341,5 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
@@ -306,5 +306,5 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
* @param array $context
*/
- public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */)
+ public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */): bool
{
return class_exists($type) || (interface_exists($type, false) && $this->classDiscriminatorResolver && null !== $this->classDiscriminatorResolver->getMappingForClass($type));
@@ -349,5 +349,5 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
@@ -314,5 +314,5 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
* {@inheritdoc}
*/
- public function denormalize(mixed $data, string $type, string $format = null, array $context = [])
+ public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
{
if (!isset($context['cache_key'])) {
diff --git a/src/Symfony/Component/Serializer/Normalizer/DenormalizerInterface.php b/src/Symfony/Component/Serializer/Normalizer/DenormalizerInterface.php
index 1c708738a1..3b6c9d5056 100644
index ae3adbfe33..3a38429cf1 100644
--- a/src/Symfony/Component/Serializer/Normalizer/DenormalizerInterface.php
+++ b/src/Symfony/Component/Serializer/Normalizer/DenormalizerInterface.php
@@ -45,5 +45,5 @@ interface DenormalizerInterface
Expand All @@ -1001,7 +1008,7 @@ index 1c708738a1..3b6c9d5056 100644
+ public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */): bool;
}
diff --git a/src/Symfony/Component/Serializer/Normalizer/NormalizerInterface.php b/src/Symfony/Component/Serializer/Normalizer/NormalizerInterface.php
index 741f19e50b..acf3be931b 100644
index 691e9c70f0..fc87f672e1 100644
--- a/src/Symfony/Component/Serializer/Normalizer/NormalizerInterface.php
+++ b/src/Symfony/Component/Serializer/Normalizer/NormalizerInterface.php
@@ -37,5 +37,5 @@ interface NormalizerInterface
Expand Down Expand Up @@ -1077,7 +1084,7 @@ index ee1d68c78f..9baaabb04c 100644
{
return self::PROPERTY_CONSTRAINT;
diff --git a/src/Symfony/Component/VarExporter/Internal/Exporter.php b/src/Symfony/Component/VarExporter/Internal/Exporter.php
index f7ef22df5c..9439e9526f 100644
index b22d6ae609..31d1a25f9d 100644
--- a/src/Symfony/Component/VarExporter/Internal/Exporter.php
+++ b/src/Symfony/Component/VarExporter/Internal/Exporter.php
@@ -36,5 +36,5 @@ class Exporter
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Helper\HelperInterface;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
Expand Down Expand Up @@ -677,6 +678,8 @@ public function getUsages(): array
/**
* Gets a helper instance by name.
*
* @return HelperInterface
*
* @throws LogicException if no HelperSet is defined
* @throws InvalidArgumentException if the helper is not defined
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Console/Command/LazyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Completion\Suggestion;
use Symfony\Component\Console\Helper\HelperInterface;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -176,7 +177,7 @@ public function getUsages(): array
return $this->getCommand()->getUsages();
}

public function getHelper(string $name): mixed
public function getHelper(string $name): HelperInterface
{
return $this->getCommand()->getHelper($name);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Console/Helper/HelperSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @implements \IteratorAggregate<string, Helper>
* @implements \IteratorAggregate<string, HelperInterface>
*/
class HelperSet implements \IteratorAggregate
{
/** @var array<string, Helper> */
/** @var array<string, HelperInterface> */
private array $helpers = [];

/**
* @param Helper[] $helpers An array of helper
* @param HelperInterface[] $helpers
*/
public function __construct(array $helpers = [])
{
Expand Down
8 changes: 5 additions & 3 deletions src/Symfony/Component/Console/Output/OutputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@ interface OutputInterface
/**
* Writes a message to the output.
*
* @param $newline Whether to add a newline
* @param $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
* @param bool $newline Whether to add a newline
* @param self::VERBOSITY_*|self::OUTPUT_* $options A bitmask of options (one of the OUTPUT or VERBOSITY constants),
* 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
*/
public function write(string|iterable $messages, bool $newline = false, int $options = 0);

/**
* Writes a message to the output and adds a newline at the end.
*
* @param $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
* @param self::VERBOSITY_*|self::OUTPUT_* $options A bitmask of options (one of the OUTPUT or VERBOSITY constants),
* 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
*/
public function writeln(string|iterable $messages, int $options = 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ interface ExtensionInterface
/**
* Loads a specific configuration.
*
* @param array<array<mixed>> $configs
*
* @throws \InvalidArgumentException When provided tag is not defined in this extension
*/
public function load(array $configs, ContainerBuilder $container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public function all(): array;
/**
* Replaces parameter placeholders (%name%) by their values.
*
* @template TValue of array<array|scalar>|scalar
*
* @param TValue $value
*
* @psalm-return (TValue is scalar ? array|scalar : array<array|scalar>)
*
* @throws ParameterNotFoundException if a placeholder references a parameter that does not exist
*/
public function resolveValue(mixed $value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ public function resolve()
/**
* Replaces parameter placeholders (%name%) by their values.
*
* @param array $resolving An array of keys that are being resolved (used internally to detect circular references)
* @template TValue of array<array|scalar>|scalar
*
* @param TValue $value
* @param array $resolving An array of keys that are being resolved (used internally to detect circular references)
*
* @return (TValue is scalar ? array|scalar : array<array|scalar>)
*
* @throws ParameterNotFoundException if a placeholder references a parameter that does not exist
* @throws ParameterCircularReferenceException if a circular reference if detected
Expand All @@ -158,8 +163,13 @@ public function resolveValue(mixed $value, array $resolving = []): mixed
{
if (\is_array($value)) {
$args = [];
foreach ($value as $k => $v) {
$args[\is_string($k) ? $this->resolveValue($k, $resolving) : $k] = $this->resolveValue($v, $resolving);
foreach ($value as $key => $v) {
$resolvedKey = \is_string($key) ? $this->resolveValue($key, $resolving) : $key;
if (!\is_scalar($resolvedKey) && !$resolvedKey instanceof \Stringable) {
throw new RuntimeException(sprintf('Array keys must be a scalar-value, but found key "%s" to resolve to type "%s".', $key, get_debug_type($resolvedKey)));
}

$args[$resolvedKey] = $this->resolveValue($v, $resolving);
}

return $args;
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/ErrorHandler/DebugClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,11 @@ private function setReturnType(string $types, string $class, string $method, str
return;
}

if (!preg_match('/^(?:\\\\?[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)+$/', $n)) {
// exclude any invalid PHP class name (e.g. `Cookie::SAMESITE_*`)
continue;
}

if (!isset($phpTypes[''])) {
$phpTypes[] = $n;
}
Expand Down
Loading