Skip to content

Commit 5d2ae88

Browse files
committed
Merge branch '7.4' into 8.0
* 7.4: Add `TEST_GENERATE_FIXTURES=1` to generate fixtures in tests [Config] Fix array shape for `canBeEnabled` / `canBeDisabled` [DoctrineBridge] Remove unnecassary check [JsonStreamer] Add synthetic properties support Resolved Conflicts: - src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php - src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php - src/Symfony/Component/JsonStreamer/Tests/JsonStreamReaderTest.php
2 parents d185488 + 742d742 commit 5d2ae88

30 files changed

+357
-117
lines changed

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<env name="COUCHBASE_HOST" value="localhost" />
2828
<env name="COUCHBASE_USER" value="Administrator" />
2929
<env name="COUCHBASE_PASS" value="111111@" />
30+
<env name="TEST_GENERATE_FIXTURES" value="0" />
3031
</php>
3132

3233
<testsuites>

src/Symfony/Bridge/Doctrine/Form/DoctrineOrmTypeGuesser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function guessRequired(string $class, string $property): ?ValueGuess
124124
public function guessMaxLength(string $class, string $property): ?ValueGuess
125125
{
126126
$ret = $this->getMetadata($class);
127-
if ($ret && isset($ret[0]->fieldMappings[$property]) && !$ret[0]->hasAssociation($property)) {
127+
if ($ret && isset($ret[0]->fieldMappings[$property])) {
128128
$mapping = $ret[0]->getFieldMapping($property);
129129

130130
$length = $mapping instanceof FieldMapping ? $mapping->length : ($mapping['length'] ?? null);
@@ -144,7 +144,7 @@ public function guessMaxLength(string $class, string $property): ?ValueGuess
144144
public function guessPattern(string $class, string $property): ?ValueGuess
145145
{
146146
$ret = $this->getMetadata($class);
147-
if ($ret && isset($ret[0]->fieldMappings[$property]) && !$ret[0]->hasAssociation($property)) {
147+
if ($ret && isset($ret[0]->fieldMappings[$property])) {
148148
if (\in_array($ret[0]->getTypeOfField($property), [Types::DECIMAL, Types::FLOAT], true)) {
149149
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
150150
}

src/Symfony/Component/Config/Builder/ArrayShapeGenerator.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private static function doGeneratePhpDoc(NodeInterface $node, int $nestingLevel
5252
$isHashmap = (bool) $node->getKeyAttribute();
5353
$arrayType = ($isHashmap ? 'array<string, ' : 'list<').self::doGeneratePhpDoc($node->getPrototype(), 1 + $nestingLevel).'>';
5454

55-
return $node->hasDefaultValue() && null === $node->getDefaultValue() ? $arrayType.'|null' : $arrayType;
55+
return implode('|', [$arrayType, ...self::getNormalizedTypes($node, ['array', 'any'])]);
5656
}
5757

5858
if (!($children = $node->getChildren()) && !$node->getParent() instanceof PrototypedArrayNode) {
@@ -81,7 +81,7 @@ private static function doGeneratePhpDoc(NodeInterface $node, int $nestingLevel
8181

8282
$arrayShape = $arrayShape.str_repeat(' ', $nestingLevel - 1).'}';
8383

84-
return $node->hasDefaultValue() && null === $node->getDefaultValue() ? $arrayShape.'|null' : $arrayShape;
84+
return implode('|', [$arrayShape, ...self::getNormalizedTypes($node, ['array', 'any'])]);
8585
}
8686

8787
private static function dumpNodeKey(NodeInterface $node): string
@@ -130,4 +130,20 @@ private static function generateInlinePhpDocForNode(BaseNode $node): string
130130

131131
return rtrim(preg_replace('/\s+/', ' ', $comment));
132132
}
133+
134+
/** @return list<string> */
135+
private static function getNormalizedTypes(BaseNode $node, array $excluded = []): array
136+
{
137+
$types = array_diff($node->getNormalizedTypes(), $excluded);
138+
139+
if ($node->hasDefaultValue() && null === $node->getDefaultValue()) {
140+
$types[] = 'null';
141+
}
142+
143+
$types = array_unique($types);
144+
145+
sort($types);
146+
147+
return $types;
148+
}
133149
}

src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,14 @@ public function canBeDisabled(?string $info = null): static
388388
->treatFalseLike(['enabled' => false])
389389
->treatTrueLike(['enabled' => true])
390390
->treatNullLike(['enabled' => true])
391+
->beforeNormalization()
392+
->ifArray()
393+
->then(static function ($v) {
394+
$v['enabled'] ??= true;
395+
396+
return $v;
397+
})
398+
->end()
391399
->children()
392400
->booleanNode('enabled')
393401
->defaultTrue()

src/Symfony/Component/Config/Tests/Builder/ArrayShapeGeneratorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Config\Builder\ArrayShapeGenerator;
1717
use Symfony\Component\Config\Definition\ArrayNode;
1818
use Symfony\Component\Config\Definition\BooleanNode;
19+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
1920
use Symfony\Component\Config\Definition\EnumNode;
2021
use Symfony\Component\Config\Definition\FloatNode;
2122
use Symfony\Component\Config\Definition\IntegerNode;
@@ -147,6 +148,30 @@ public function testPhpDocShapeMultiLevel()
147148
$this->assertStringMatchesFormat('array{%Achild?: array<%s>,%A}', ArrayShapeGenerator::generate($root));
148149
}
149150

151+
public function testCanBeEnabled()
152+
{
153+
$root = new ArrayNodeDefinition('root');
154+
$root->canBeEnabled();
155+
156+
$this->assertSame(<<<'CODE'
157+
array{ // Default: {"enabled":false}
158+
* enabled?: bool, // Default: false
159+
* }|bool
160+
CODE, ArrayShapeGenerator::generate($root->getNode()));
161+
}
162+
163+
public function testCanBeDisabled()
164+
{
165+
$root = new ArrayNodeDefinition('root');
166+
$root->canBeDisabled();
167+
168+
$this->assertSame(<<<'CODE'
169+
array{ // Default: {"enabled":true}
170+
* enabled?: bool, // Default: true
171+
* }|bool
172+
CODE, ArrayShapeGenerator::generate($root->getNode()));
173+
}
174+
150175
#[DataProvider('provideQuotedNodes')]
151176
public function testPhpdocQuoteNodeName(NodeInterface $node, string $expected)
152177
{

src/Symfony/Component/Config/Tests/Builder/Fixtures/ArrayValues/Symfony/Config/ArrayValues/ErrorPagesConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function withTrace($value): static
4444
* @param array{ // Default: {"enabled":false}
4545
* enabled?: bool, // Default: false
4646
* with_trace?: bool,
47-
* } $config
47+
* }|bool $config
4848
*/
4949
public function __construct(array $config = [])
5050
{

src/Symfony/Component/Config/Tests/Builder/Fixtures/ArrayValues/Symfony/Config/ArrayValues/TransportsConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function dsn($value): static
2929
/**
3030
* @param array{
3131
* dsn?: scalar|null,
32-
* } $config
32+
* }|string $config
3333
*/
3434
public function __construct(array $config = [])
3535
{

src/Symfony/Component/Config/Tests/Builder/Fixtures/ArrayValues/Symfony/Config/ArrayValuesConfig.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ public function getExtensionAlias(): string
8181
* @param array{
8282
* transports?: array<string, array{
8383
* dsn?: scalar|null,
84-
* }>,
84+
* }|string>,
8585
* error_pages?: array{ // Default: {"enabled":false}
8686
* enabled?: bool, // Default: false
8787
* with_trace?: bool,
88-
* },
88+
* }|bool,
8989
* } $config
9090
*/
9191
public function __construct(array $config = [])

src/Symfony/Component/Config/Tests/Builder/Fixtures/ScalarNormalizedTypes/Symfony/Config/ScalarNormalizedTypesConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function getExtensionAlias(): string
151151
/**
152152
* @param array{
153153
* simple_array?: list<scalar|null>,
154-
* keyed_array?: array<string, list<scalar|null>>,
154+
* keyed_array?: array<string, list<scalar|null>|string>,
155155
* object?: array{ // Default: {"enabled":null}
156156
* enabled?: bool|null, // Default: null
157157
* date_format?: scalar|null,

src/Symfony/Component/Config/Tests/Builder/GeneratedConfigTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ public function testConfig(string $name, string $alias)
8686
$expectedOutput = include $basePath.$name.'.output.php';
8787
$expectedCode = $basePath.$name;
8888

89-
// to regenerate snapshot files, uncomment these lines
90-
// (new Filesystem())->remove($expectedCode);
91-
// $this->generateConfigBuilder('Symfony\\Component\\Config\\Tests\\Builder\\Fixtures\\'.$name, $expectedCode);
92-
// $this->markTestIncomplete('Re-comment the line above and relaunch the tests');
89+
if ($_ENV['TEST_GENERATE_FIXTURES'] ?? false) {
90+
(new Filesystem())->remove($expectedCode);
91+
$this->generateConfigBuilder('Symfony\\Component\\Config\\Tests\\Builder\\Fixtures\\'.$name, $expectedCode);
92+
$this->markTestIncomplete('TEST_GENERATE_FIXTURES is set');
93+
}
9394

9495
$this->generateConfigBuilder('Symfony\\Component\\Config\\Tests\\Builder\\Fixtures\\'.$name, $outputDir);
9596

0 commit comments

Comments
 (0)