Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Config] Use better typehint in PHP Configuration #44166

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
Oct 24, 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
95 changes: 53 additions & 42 deletions 95 src/Symfony/Component/Config/Builder/ConfigBuilderGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Config\Definition\ArrayNode;
use Symfony\Component\Config\Definition\BaseNode;
use Symfony\Component\Config\Definition\BooleanNode;
use Symfony\Component\Config\Definition\Builder\ExprBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\EnumNode;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
Expand Down Expand Up @@ -141,8 +142,9 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
$node->getName(),
$this->getType($childClass->getFqcn(), $hasNormalizationClosures)
);
$nodeTypes = $this->getParameterTypes($node);
$body = $hasNormalizationClosures ? '
COMMENTpublic function NAME(mixed $value = []): CLASS|static
COMMENTpublic function NAME(PARAM_TYPE $value = []): CLASS|static
{
if (!\is_array($value)) {
$this->_usedProperties[\'PROPERTY\'] = true;
Expand Down Expand Up @@ -172,7 +174,12 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
return $this->PROPERTY;
}';
$class->addUse(InvalidConfigurationException::class);
$class->addMethod($node->getName(), $body, ['COMMENT' => $comment, 'PROPERTY' => $property->getName(), 'CLASS' => $childClass->getFqcn()]);
$class->addMethod($node->getName(), $body, [
'COMMENT' => $comment,
'PROPERTY' => $property->getName(),
'CLASS' => $childClass->getFqcn(),
'PARAM_TYPE' => \in_array('mixed', $nodeTypes, true) ? 'mixed' : implode('|', $nodeTypes),
]);

$this->buildNode($node, $childClass, $this->getSubNamespace($childClass));
}
Expand Down Expand Up @@ -209,19 +216,21 @@ private function handlePrototypedArrayNode(PrototypedArrayNode $node, ClassBuild
$methodName = $name;
$hasNormalizationClosures = $this->hasNormalizationClosures($node) || $this->hasNormalizationClosures($prototype);

$parameterType = $this->getParameterType($prototype);
if (null !== $parameterType || $prototype instanceof ScalarNode) {
$nodeParameterTypes = $this->getParameterTypes($node);
$prototypeParameterTypes = $this->getParameterTypes($prototype);
if (!$prototype instanceof ArrayNode || ($prototype instanceof PrototypedArrayNode && $prototype->getPrototype() instanceof ScalarNode)) {
$class->addUse(ParamConfigurator::class);
$property = $class->addProperty($node->getName());
if (null === $key = $node->getKeyAttribute()) {
// This is an array of values; don't use singular name
$nodeTypesWithoutArray = array_filter($nodeParameterTypes, static fn ($type) => 'array' !== $type);
$body = '
/**
* @param PHPDOC_TYPE $value
* @param ParamConfigurator|list<ParamConfigurator|PROTOTYPE_TYPE>EXTRA_TYPE $value
*
* @return $this
*/
public function NAME(TYPE $value): static
public function NAME(PARAM_TYPE $value): static
{
$this->_usedProperties[\'PROPERTY\'] = true;
$this->PROPERTY = $value;
Expand All @@ -231,8 +240,9 @@ public function NAME(TYPE $value): static

$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),
'PROTOTYPE_TYPE' => implode('|', $prototypeParameterTypes),
'EXTRA_TYPE' => $nodeTypesWithoutArray ? '|'.implode('|', $nodeTypesWithoutArray) : '',
'PARAM_TYPE' => \in_array('mixed', $nodeParameterTypes, true) ? 'mixed' : 'ParamConfigurator|'.implode('|', $nodeParameterTypes),
]);
} else {
$body = '
Expand All @@ -249,7 +259,7 @@ public function NAME(string $VAR, TYPE $VALUE): static

$class->addMethod($methodName, $body, [
'PROPERTY' => $property->getName(),
'TYPE' => $hasNormalizationClosures || '' === $parameterType ? 'mixed' : 'ParamConfigurator|'.$parameterType,
'TYPE' => \in_array('mixed', $prototypeParameterTypes, true) ? 'mixed' : 'ParamConfigurator|'.implode('|', $prototypeParameterTypes),
'VAR' => '' === $key ? 'key' : $key,
'VALUE' => 'value' === $key ? 'data' : 'value',
]);
Expand Down Expand Up @@ -282,7 +292,7 @@ public function NAME(string $VAR, TYPE $VALUE): static

if (null === $key = $node->getKeyAttribute()) {
$body = $hasNormalizationClosures ? '
COMMENTpublic function NAME(mixed $value = []): CLASS|static
COMMENTpublic function NAME(PARAM_TYPE $value = []): CLASS|static
{
$this->_usedProperties[\'PROPERTY\'] = true;
if (!\is_array($value)) {
Expand All @@ -299,10 +309,15 @@ public function NAME(string $VAR, TYPE $VALUE): static

return $this->PROPERTY[] = new CLASS($value);
}';
$class->addMethod($methodName, $body, ['COMMENT' => $comment, 'PROPERTY' => $property->getName(), 'CLASS' => $childClass->getFqcn()]);
$class->addMethod($methodName, $body, [
'COMMENT' => $comment,
'PROPERTY' => $property->getName(),
'CLASS' => $childClass->getFqcn(),
'PARAM_TYPE' => \in_array('mixed', $nodeParameterTypes, true) ? 'mixed' : implode('|', $nodeParameterTypes),
]);
} else {
$body = $hasNormalizationClosures ? '
COMMENTpublic function NAME(string $VAR, mixed $VALUE = []): CLASS|static
COMMENTpublic function NAME(string $VAR, PARAM_TYPE $VALUE = []): CLASS|static
{
if (!\is_array($VALUE)) {
$this->_usedProperties[\'PROPERTY\'] = true;
Expand Down Expand Up @@ -337,6 +352,7 @@ public function NAME(string $VAR, TYPE $VALUE): static
'CLASS' => $childClass->getFqcn(),
'VAR' => '' === $key ? 'key' : $key,
'VALUE' => 'value' === $key ? 'data' : 'value',
'PARAM_TYPE' => \in_array('mixed', $prototypeParameterTypes, true) ? 'mixed' : implode('|', $prototypeParameterTypes),
]);
}

Expand Down Expand Up @@ -364,35 +380,33 @@ public function NAME($value): static
$class->addMethod($node->getName(), $body, ['PROPERTY' => $property->getName(), 'COMMENT' => $comment]);
}

private function getParameterType(NodeInterface $node): ?string
private function getParameterTypes(NodeInterface $node): array
{
if ($node instanceof BooleanNode) {
return 'bool';
}

if ($node instanceof IntegerNode) {
return 'int';
}

if ($node instanceof FloatNode) {
return 'float';
}

if ($node instanceof EnumNode) {
return '';
}

if ($node instanceof PrototypedArrayNode && $node->getPrototype() instanceof ScalarNode) {
// This is just an array of variables
return 'array';
$paramTypes = [];
if ($node instanceof BaseNode) {
$types = $node->getNormalizedTypes();
if (\in_array(ExprBuilder::TYPE_ANY, $types, true)) {
$paramTypes[] = 'mixed';
}
if (\in_array(ExprBuilder::TYPE_STRING, $types, true)) {
$paramTypes[] = 'string';
}
}

if ($node instanceof VariableNode) {
// mixed
return '';
if ($node instanceof BooleanNode) {
$paramTypes[] = 'bool';
} elseif ($node instanceof IntegerNode) {
$paramTypes[] = 'int';
} elseif ($node instanceof FloatNode) {
$paramTypes[] = 'float';
} elseif ($node instanceof EnumNode) {
$paramTypes[] = 'mixed';
} elseif ($node instanceof ArrayNode) {
$paramTypes[] = 'array';
} elseif ($node instanceof VariableNode) {
$paramTypes[] = 'mixed';
}

return null;
return array_unique($paramTypes);
}

private function getComment(BaseNode $node): string
Expand All @@ -416,11 +430,8 @@ private function getComment(BaseNode $node): string
return var_export($a, true);
}, $node->getValues())))."\n";
} else {
$parameterType = $this->getParameterType($node);
if (null === $parameterType || '' === $parameterType) {
$parameterType = 'mixed';
}
$comment .= ' * @param ParamConfigurator|'.$parameterType.' $value'."\n";
$parameterTypes = $this->getParameterTypes($node);
$comment .= ' * @param ParamConfigurator|'.implode('|', $parameterTypes).' $value'."\n";
}
} else {
foreach ((array) ($node->getExample() ?? []) as $example) {
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Config/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Deprecate calling `NodeBuilder::setParent()` without any arguments
* Add a more accurate typehint in generated PHP config

6.1
---
Expand Down
21 changes: 21 additions & 0 deletions 21 src/Symfony/Component/Config/Definition/BaseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ abstract class BaseNode implements NodeInterface
protected $name;
protected $parent;
protected $normalizationClosures = [];
protected $normalizedTypes = [];
protected $finalValidationClosures = [];
protected $allowOverwrite = true;
protected $required = false;
Expand Down Expand Up @@ -212,6 +213,26 @@ public function setNormalizationClosures(array $closures)
$this->normalizationClosures = $closures;
}

/**
* Sets the list of types supported by normalization.
*
* see ExprBuilder::TYPE_* constants.
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
*/
public function setNormalizedTypes(array $types)
{
$this->normalizedTypes = $types;
}

/**
* Gets the list of types supported by normalization.
*
* see ExprBuilder::TYPE_* constants.
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
*/
public function getNormalizedTypes(): array
{
return $this->normalizedTypes;
}

/**
* Sets the closures used for final validation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ protected function createNode(): NodeInterface

if (null !== $this->normalization) {
$node->setNormalizationClosures($this->normalization->before);
$node->setNormalizedTypes($this->normalization->declaredTypes);
$node->setXmlRemappings($this->normalization->remappings);
}

Expand Down
44 changes: 30 additions & 14 deletions 44 src/Symfony/Component/Config/Definition/Builder/ExprBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@
*/
class ExprBuilder
{
public const TYPE_ANY = 'any';
public const TYPE_STRING = 'string';
public const TYPE_NULL = 'null';
public const TYPE_ARRAY = 'array';

protected $node;

public $allowedTypes;
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
public $ifPart;
public $thenPart;

Expand All @@ -37,7 +44,8 @@ public function __construct(NodeDefinition $node)
*/
public function always(\Closure $then = null): static
{
$this->ifPart = function () { return true; };
$this->ifPart = static function () { return true; };
$this->allowedTypes = self::TYPE_ANY;

if (null !== $then) {
$this->thenPart = $then;
Expand All @@ -56,10 +64,11 @@ public function always(\Closure $then = null): static
public function ifTrue(\Closure $closure = null): static
{
if (null === $closure) {
$closure = function ($v) { return true === $v; };
$closure = static function ($v) { return true === $v; };
}

$this->ifPart = $closure;
$this->allowedTypes = self::TYPE_ANY;

return $this;
}
Expand All @@ -71,7 +80,8 @@ public function ifTrue(\Closure $closure = null): static
*/
public function ifString(): static
{
$this->ifPart = function ($v) { return \is_string($v); };
$this->ifPart = static function ($v) { return \is_string($v); };
$this->allowedTypes = self::TYPE_STRING;

return $this;
}
Expand All @@ -83,7 +93,8 @@ public function ifString(): static
*/
public function ifNull(): static
{
$this->ifPart = function ($v) { return null === $v; };
$this->ifPart = static function ($v) { return null === $v; };
$this->allowedTypes = self::TYPE_NULL;

return $this;
}
Expand All @@ -95,7 +106,8 @@ public function ifNull(): static
*/
public function ifEmpty(): static
{
$this->ifPart = function ($v) { return empty($v); };
$this->ifPart = static function ($v) { return empty($v); };
$this->allowedTypes = self::TYPE_ANY;

return $this;
}
Expand All @@ -107,7 +119,8 @@ public function ifEmpty(): static
*/
public function ifArray(): static
{
$this->ifPart = function ($v) { return \is_array($v); };
$this->ifPart = static function ($v) { return \is_array($v); };
$this->allowedTypes = self::TYPE_ARRAY;

return $this;
}
Expand All @@ -119,7 +132,8 @@ public function ifArray(): static
*/
public function ifInArray(array $array): static
{
$this->ifPart = function ($v) use ($array) { return \in_array($v, $array, true); };
$this->ifPart = static function ($v) use ($array) { return \in_array($v, $array, true); };
$this->allowedTypes = self::TYPE_ANY;

return $this;
}
Expand All @@ -131,7 +145,8 @@ public function ifInArray(array $array): static
*/
public function ifNotInArray(array $array): static
{
$this->ifPart = function ($v) use ($array) { return !\in_array($v, $array, true); };
$this->ifPart = static function ($v) use ($array) { return !\in_array($v, $array, true); };
$this->allowedTypes = self::TYPE_ANY;

return $this;
}
Expand All @@ -143,8 +158,9 @@ public function ifNotInArray(array $array): static
*/
public function castToArray(): static
{
$this->ifPart = function ($v) { return !\is_array($v); };
$this->thenPart = function ($v) { return [$v]; };
$this->ifPart = static function ($v) { return !\is_array($v); };
$this->allowedTypes = self::TYPE_ANY;
$this->thenPart = static function ($v) { return [$v]; };

return $this;
}
Expand All @@ -168,7 +184,7 @@ public function then(\Closure $closure): static
*/
public function thenEmptyArray(): static
{
$this->thenPart = function () { return []; };
$this->thenPart = static function () { return []; };

return $this;
}
Expand All @@ -184,7 +200,7 @@ public function thenEmptyArray(): static
*/
public function thenInvalid(string $message): static
{
$this->thenPart = function ($v) use ($message) { throw new \InvalidArgumentException(sprintf($message, json_encode($v))); };
$this->thenPart = static function ($v) use ($message) { throw new \InvalidArgumentException(sprintf($message, json_encode($v))); };

return $this;
}
Expand All @@ -198,7 +214,7 @@ public function thenInvalid(string $message): static
*/
public function thenUnset(): static
{
$this->thenPart = function () { throw new UnsetKeyException('Unsetting key.'); };
$this->thenPart = static function () { throw new UnsetKeyException('Unsetting key.'); };

return $this;
}
Expand Down Expand Up @@ -231,7 +247,7 @@ public static function buildExpressions(array $expressions): array
if ($expr instanceof self) {
$if = $expr->ifPart;
$then = $expr->thenPart;
$expressions[$k] = function ($v) use ($if, $then) {
$expressions[$k] = static function ($v) use ($if, $then) {
return $if($v) ? $then($v) : $v;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ public function getNode(bool $forceRootNode = false): NodeInterface
}

if (null !== $this->normalization) {
$allowedTypes = [];
foreach ($this->normalization->before as $expr) {
$allowedTypes[] = $expr->allowedTypes;
}
$allowedTypes = array_unique($allowedTypes);
$this->normalization->before = ExprBuilder::buildExpressions($this->normalization->before);
$this->normalization->declaredTypes = $allowedTypes;
}

if (null !== $this->validation) {
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.