8000 [Config] Fix PHP Configuration type hints & tests by HypeMC · Pull Request #46374 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Config] Fix PHP Configuration type hints & tests #46374

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 1 commit into from
May 17, 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.
8000 Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Config] Fix PHP Configuration type hints & tests
  • Loading branch information
HypeMC committed May 17, 2022
commit 9d38089d6ddadca4da178b59df8ab134f135e060
38 changes: 28 additions & 10 deletions src/Symfony/Component/Config/Builder/ConfigBuilderGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
/**
* @return CLASS|$this
*/
public function NAME($value = [])
public function NAME(mixed $value = []): CLASS|static
{
if (!\is_array($value)) {
$this->_usedProperties[\'PROPERTY\'] = true;
Expand Down Expand Up @@ -198,14 +198,19 @@ public function NAME(mixed $valueDEFAULT): static

return $this;
}';
$class->addMethod($node->getName(), $body, ['PROPERTY' => $property->getName(), 'COMMENT' => $comment, 'DEFAULT' => $node->hasDefaultValue() ? ' = '.var_export($node->getDefaultValue(), true) : '']);
$class->addMethod($node->getName(), $body, [
'PROPERTY' => $property->getName(),
'COMMENT' => $comment,
'DEFAULT' => $node->hasDefaultValue() ? ' = '.var_export($node->getDefaultValue(), true) : '',
]);
}

private function handlePrototypedArrayNode(PrototypedArrayNode $node, ClassBuilder $class, string $namespace): void
{
$name = $this->getSingularName($node);
$prototype = $node->getPrototype();
$methodName = $name;
$hasNormalizationClosures = $this->hasNormalizationClosures($node) || $this->hasNormalizationClosures($prototype);

$parameterType = $this->getParameterType($prototype);
if (null !== $parameterType || $prototype instanceof ScalarNode) {
Expand All @@ -215,19 +220,23 @@ private function handlePrototypedArrayNode(PrototypedArrayNode $node, ClassBuild
// This is an array of values; don't use singular name
$body = '
/**
* @param ParamConfigurator|list<ParamConfigurator|TYPE> $value
* @param PHPDOC_TYPE $value
*
* @return $this
*/
public function NAME(ParamConfigurator|array $value): static
public function NAME(TYPE $value): static
{
$this->_usedProperties[\'PROPERTY\'] = true;
$this->PROPERTY = $value;

return $this;
}';

$class->addMethod($node->getName(), $body, ['PROPERTY' => $property->getName(), 'TYPE' => '' === $parameterType ? 'mixed' : $parameterType]);
$class->addMethod($node->getName(), $body, [
'PROPERTY' => $property->getName(),
'TYPE' => $hasNormalizationClosures ? 'mixed' : 'ParamConfigurator|array',
'PHPDOC_TYPE' => $hasNormalizationClosures ? 'mixed' : sprintf('ParamConfigurator|list<ParamConfigurator|%s>', '' === $parameterType ? 'mixed' : $parameterType),
]);
} else {
$body = '
/**
Expand All @@ -241,7 +250,12 @@ public function NAME(string $VAR, TYPE $VALUE): static
return $this;
}';

$class->addMethod($methodName, $body, ['PROPERTY' => $property->getName(), 'TYPE' => '' === $parameterType ? 'mixed' : 'ParamConfigurator|'.$parameterType, 'VAR' => '' === $key ? 'key' : $key, 'VALUE' => 'value' === $key ? 'data' : 'value']);
$class->addMethod($methodName, $body, [
'PROPERTY' => $property->getName(),
'TYPE' => $hasNormalizationClosures || '' === $parameterType ? 'mixed' : 'ParamConfigurator|'.$parameterType,
'VAR' => '' === $key ? 'key' : $key,
'VALUE' => 'value' === $key ? 'data' : 'value',
]);
}

return;
Expand All @@ -254,7 +268,6 @@ public function NAME(string $VAR, TYPE $VALUE): static
$class->addRequire($childClass);
$this->classes[] = $childClass;

$hasNormalizationClosures = $this->hasNormalizationClosures($node) || $this->hasNormalizationClosures($prototype);
$property = $class->addProperty(
$node->getName(),
$this->getType($childClass->getFqcn().'[]', $hasNormalizationClosures)
Expand All @@ -265,7 +278,7 @@ public function NAME(string $VAR, TYPE $VALUE): static
/**
* @return CLASS|$this
*/
public function NAME($value = [])
public function NAME(mixed $value = []): CLASS|static
{
$this->_usedProperties[\'PROPERTY\'] = true;
if (!\is_array($value)) {
Expand All @@ -288,7 +301,7 @@ public function NAME(array $value = []): CLASS
/**
* @return CLASS|$this
*/
public function NAME(string $VAR, $VALUE = [])
public function NAME(string $VAR, mixed $VALUE = []): CLASS|static
{
if (!\is_array($VALUE)) {
$this->_usedProperties[\'PROPERTY\'] = true;
Expand Down Expand Up @@ -318,7 +331,12 @@ public function NAME(string $VAR, array $VALUE = []): CLASS
return $this->PROPERTY[$VAR];
}';
$class->addUse(InvalidConfigurationException::class);
$class->addMethod($methodName, $body, ['PROPERTY' => $property->getName(), 'CLASS' => $childClass->getFqcn(), 'VAR' => '' === $key ? 'key' : $key, 'VALUE' => 'value' === $key ? 'data' : 'value']);
$class->addMethod($methodName, $body, [
'PROPERTY' => $property->getName(),
'CLASS' => $childClass->getFqcn(),
'VAR' => '' === $key ? 'key' : $key,
'VALUE' => 'value' === $key ? 'data' : 'value',
]);
}

$this->buildNode($prototype, $childClass, $namespace.'\\'.$childClass->getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class KeyedListObjectConfig
* @param ParamConfigurator|bool $value
* @return $this
*/
public function enabled($value): self
public function enabled($value): static
{
$this->_usedProperties['enabled'] = true;
$this->enabled = $value;
Expand All @@ -28,10 +28,11 @@ public function enabled($value): self
}

/**
* @param ParamConfigurator|list<mixed|ParamConfigurator> $value
* @param ParamConfigurator|list<ParamConfigurator|mixed> $value
*
* @return $this
*/
public function settings($value): self
public function settings(ParamConfigurator|array $value): static
{
$this->_usedProperties['settings'] = true;
$this->settings = $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ListObjectConfig
* @param ParamConfigurator|mixed $value
* @return $this
*/
public function name($value): self
public function name($value): static
{
$this->_usedProperties['name'] = true;
$this->name = $value;
Expand All @@ -28,10 +28,11 @@ public function name($value): self
}

/**
* @param ParamConfigurator|list<mixed|ParamConfigurator> $value
* @param ParamConfigurator|list<ParamConfigurator|mixed> $value
*
* @return $this
*/
public function data($value): self
public function data(ParamConfigurator|array $value): static
{
$this->_usedProperties['data'] = true;
$this->data = $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class NestedListObjectConfig
* @param ParamConfigurator|mixed $value
* @return $this
*/
public function name($value): self
public function name($value): static
{
$this->_usedProperties['name'] = true;
$this->name = $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class NestedObjectConfig
* @param ParamConfigurator|bool $value
* @return $this
*/
public function enabled($value): self
public function enabled($value): static
{
$this->_usedProperties['enabled'] = true;
$this->enabled = $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class NestedConfig
/**
* @return \Symfony\Config\ScalarNormalizedTypes\Nested\NestedObjectConfig|$this
*/
public function nestedObject($value = [])
public function nestedObject(mixed $value = []): \Symfony\Config\ScalarNormalizedTypes\Nested\NestedObjectConfig|static
{
if (!\is_array($value)) {
$this->_usedProperties['nestedObject'] = true;
Expand All @@ -41,7 +41,7 @@ public function nestedObject($value = [])
/**
* @return \Symfony\Config\ScalarNormalizedTypes\Nested\NestedListObjectConfig|$this
*/
public function nestedListObject($value = [])
public function nestedListObject(mixed $value = []): \Symfony\Config\ScalarNormalizedTypes\Nested\NestedListObjectConfig|static
{
$this->_usedProperties['nestedListObject'] = true;
if (!\is_array($value)) {
Expand Down
< F438 tr data-hunk="fd581995fbecfeb52921d07810ff20e517ef3a2889caa898019e24a36b2609da" class="show-top-border">
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ObjectConfig
* @param ParamConfigurator|bool $value
* @return $this
*/
public function enabled($value): self
public function enabled($value): static
{
$this->_usedProperties['enabled'] = true;
$this->enabled = $value;
Expand All @@ -33,7 +33,7 @@ public function enabled($value): self
* @param ParamConfigurator|mixed $value
* @return $this
*/
public function dateFormat($value): self
public function dateFormat($value): static
{
$this->_usedProperties['dateFormat'] = true;
$this->dateFormat = $value;
Expand All @@ -46,7 +46,7 @@ public function dateFormat($value): self
* @param ParamConfigurator|bool $value
* @return $this
*/
public function removeUsedContextFields($value): self
public function removeUsedContextFields($value): static
{
$this->_usedProperties['removeUsedContextFields'] = true;
$this->removeUsedContextFields = $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ class ScalarNormalizedTypesConfig implements \Symfony\Component\Config\Builder\C
private $_usedProperties = [];

/**
* @param ParamConfigurator|list<mixed|ParamConfigurator> $value
* @param mixed $value
*
* @return $this
*/
public function simpleArray($value): self
public function simpleArray(mixed $value): static
{
$this->_usedProperties['simpleArray'] = true;
$this->simpleArray = $value;
Expand All @@ -36,10 +37,9 @@ public function simpleArray($value): self
}

/**
* @param ParamConfigurator|array $value
* @return $this
*/
public function keyedArray(string $name, $value): self
public function keyedArray(string $name, mixed $value): static
{
$this->_usedProperties['keyedArray'] = true;
$this->keyedArray[$name] = $value;
Expand All @@ -50,7 +50,7 @@ public function keyedArray(string $name, $value): self
/**
* @return \Symfony\Config\ScalarNormalizedTypes\ObjectConfig|$this
*/
public function object($value = [])
public function object(mixed $value = []): \Symfony\Config\ScalarNormalizedTypes\ObjectConfig|static
{
if (!\is_array($value)) {
$this->_usedProperties['object'] = true;
Expand All @@ -72,7 +72,7 @@ public function object($value = [])
/**
* @return \Symfony\Config\ScalarNormalizedTypes\ListObjectConfig|$this
*/
public function listObject($value = [])
public function listObject(mixed $value = []): \Symfony\Config\ScalarNormalizedTypes\ListObjectConfig|static
{
$this->_usedProperties['listObject'] = true;
if (!\is_array($value)) {
Expand All @@ -87,7 +87,7 @@ public function listObject($value = [])
/**
* @return \Symfony\Config\ScalarNormalizedTypes\KeyedListObjectConfig|$this
*/
public function keyedListObject(string $class, $value = [])
public function keyedListObject(string $class, mixed $value = []): \Symfony\Config\ScalarNormalizedTypes\KeyedListObjectConfig|static
{
if (!\is_array($value)) {
$this->_usedProperties['keyedListObject'] = true;
Expand Down
0