8000 Show deprecated options definition on debug:form command · symfony/symfony@b2cf002 · GitHub
[go: up one dir, main page]

Skip to content

Commit b2cf002

Browse files
committed
Show deprecated options definition on debug:form command
1 parent a31f4aa commit b2cf002

15 files changed

+307
-44
lines changed

src/Symfony/Component/Form/Command/DebugCommand.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ protected function configure()
5858
->setDefinition(array(
5959
new InputArgument('class', InputArgument::OPTIONAL, 'The form type class'),
6060
new InputArgument('option', InputArgument::OPTIONAL, 'The form type option'),
61+
new InputOption('show-deprecated', null, InputOption::VALUE_NONE, 'Used to show deprecated options in form types'),
6162
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt or json)', 'txt'),
6263
))
6364
->setDescription('Displays form type information')
@@ -75,6 +76,11 @@ protected function configure()
7576
7677
<info>php %command.full_name% ChoiceType choice_value</info>
7778
79+
Use the <info>--show-deprecated</info> option to display form types with deprecated options or the deprecated options of the given form type:
80+
81+
<info>php %command.full_name% --show-deprecated</info>
82+
<info>php %command.full_name% ChoiceType --show-deprecated</info>
83+
7884
The command displays the definition of the given option name.
7985
8086
<info>php %command.full_name% --format=json</info>
@@ -96,6 +102,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
96102
$object = null;
97103
$options['core_types'] = $this->getCoreTypes();
98104
$options['service_types'] = array_values(array_diff($this->types, $options['core_types']));
105+
if ($input->getOption('show-deprecated')) {
106+
$options['core_types'] = $this->filterTypesByDeprecated($options['core_types']);
107+
$options['service_types'] = $this->filterTypesByDeprecated($options['service_types']);
108+
}
99109
$options['extensions'] = $this->extensions;
100110
$options['guessers'] = $this->guessers;
101111
foreach ($options as $k => $list) {
@@ -114,7 +124,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
114124
$message = sprintf('Option "%s" is not defined in "%s".', $option, \get_class($resolvedType->getInnerType()));
115125

116126
if ($alternatives = $this->findAlternatives($option, $object->getDefinedOptions())) {
117-
if (1 == \count($alternatives)) {
127+
if (1 === \count($alternatives)) {
118128
$message .= "\n\nDid you mean this?\n ";
119129
} else {
120130
$message .= "\n\nDid you mean one of these?\n ";
@@ -134,6 +144,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
134144

135145
$helper = new DescriptorHelper();
136146
$options['format'] = $input->getOption('format');
147+
$options['show_deprecated'] = $input->getOption('show-deprecated');
137148
$helper->describe($io, $object, $options);
138149
}
139150

@@ -152,7 +163,7 @@ private function getFqcnTypeClass(InputInterface $input, SymfonyStyle $io, $shor
152163

153164
$allTypes = array_merge($this->getCoreTypes(), $this->types);
154165
if ($alternatives = $this->findAlternatives($shortClassName, $allTypes)) {
155-
if (1 == \count($alternatives)) {
166+
if (1 === \count($alternatives)) {
156167
$message .= "\n\nDid you mean this?\n ";
157168
} else {
158169
$message .= "\n\nDid you mean one of these?\n ";
@@ -184,6 +195,22 @@ private function getCoreTypes()
184195
return $coreTypes;
185196
}
186197

198+
private function filterTypesByDeprecated(array $types): array
199+
{
200+
$typesWithDeprecatedOptions = array();
201+
foreach ($types as $class) {
202+
$optionsResolver = $this->formRegistry->getType($class)->getOptionsResolver();
203+
foreach ($optionsResolver->getDefinedOptions() as $option) {
204+
if ($optionsResolver->isDeprecated($option)) {
205+
$typesWithDeprecatedOptions[] = $class;
206+
break;
207+
}
208+
}
209+
}
210+
211+
return $typesWithDeprecatedOptions;
212+
}
213+
187214
private function findAlternatives($name, array $collection)
188215
{
189216
$alternatives = array();

src/Symfony/Component/Form/Console/Descriptor/Descriptor.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ protected function collectOptions(ResolvedFormTypeInterface $type)
108108

109109
protected function getOptionDefinition(OptionsResolver $optionsResolver, $option)
110110
{
111-
$definition = array('required' => $optionsResolver->isRequired($option));
111+
$definition = array(
112+
'required' => $optionsResolver->isRequired($option),
113+
'deprecated' => $optionsResolver->isDeprecated($option),
114+
);
112115

113116
$introspector = new OptionsResolverIntrospector($optionsResolver);
114117

@@ -118,6 +121,7 @@ protected function getOptionDefinition(OptionsResolver $optionsResolver, $option
118121
'allowedTypes' => 'getAllowedTypes',
119122
'allowedValues' => 'getAllowedValues',
120123
'normalizer' => 'getNormalizer',
124+
'deprecationMessage' => 'getDeprecationMessage',
121125
);
122126

123127
foreach ($map as $key => $method) {
@@ -128,9 +132,41 @@ protected function getOptionDefinition(OptionsResolver $optionsResolver, $option
128132
}
129133
}
130134

135+
if (isset($definition['deprecationMessage']) && \is_string($definition['deprecationMessage'])) {
136+
$definition['deprecationMessage'] = strtr($definition['deprecationMessage'], array('%name%' => $option));
137+
}
138+
131139
return $definition;
132140
}
133141

142+
protected function filterOptionsByDeprecated(ResolvedFormTypeInterface $type)
143+
{
144+
$deprecatedOptions = array();
145+
$resolver = $type->getOptionsResolver();
146+
foreach ($resolver->getDefinedOptions() as $option) {
147+
if ($resolver->isDeprecated($option)) {
148+
$deprecatedOptions[] = $option;
149+
}
150+
}
151+
152+
$filterByDeprecated = function (array $options) use ($deprecatedOptions) {
153+
foreach ($options as $class => $opts) {
154+
if ($deprecated = array_intersect($deprecatedOptions, $opts)) {
155+
$options[$class] = $deprecated;
156+
} else {
157+
unset($options[$class]);
158+
}
159+
}
160+
161+
return $options;
162+
};
163+
164+
$this->ownOptions = array_intersect($deprecatedOptions, $this->ownOptions);
165+
$this->overriddenOptions = $filterByDeprecated($this->overriddenOptions);
166+
$this->parentOptions = $filterByDeprecated($this->parentOptions);
167+
$this->extensionOptions = $filterByDeprecated($this->extensionOptions);
168+
}
169+
134170
private function getParentOptionsResolver(ResolvedFormTypeInterface $type)
135171
{
136172
$this->parents[$class = \get_class($type->getInnerType())] = array();

src/Symfony/Component/Form/Console/Descriptor/JsonDescriptor.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ protected function describeDefaults(array $options)
2525
{
2626
$data['builtin_form_types'] = $options['core_types'];
2727
$data['service_form_types'] = $options['service_types'];
28-
$data['type_extensions'] = $options['extensions'];
29-
$data['type_guessers'] = $options['guessers'];
28+
if (!$options['show_deprecated']) {
29+
$data['type_extensions'] = $options['extensions'];
30+
$data['type_guessers'] = $options['guessers'];
31+
}
3032

3133
$this->writeData($data, $options);
3234
}
@@ -35,6 +37,10 @@ protected function describeResolvedFormType(ResolvedFormTypeInterface $resolvedF
3537
{
3638
$this->collectOptions($resolvedFormType);
3739

40+
if ($options['show_deprecated']) {
41+
$this->filterOptionsByDeprecated($resolvedFormType);
42+
}
43+
3844
$formOptions = array(
3945
'own' => $this->ownOptions,
4046
'overridden' => $this->overriddenOptions,
@@ -59,7 +65,14 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio
5965
{
6066
$definition = $this->getOptionDefinition($optionsResolver, $options['option']);
6167

62-
$map = array(
68+
$map = array();
69+
if ($definition['deprecated']) {
70+
$map['deprecated'] = 'deprecated';
71+
if (\is_string($definition['deprecationMessage'])) {
72+
$map['deprecation_message'] = 'deprecationMessage';
73+
}
74+
}
75+
$map += array(
6376
'required' => 'required',
6477
'default' => 'default',
6578
'allowed_types' => 'allowedTypes',
@@ -81,7 +94,7 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio
8194

8295
private function writeData(array $data, array $options)
8396
{
84-
$flags = isset($options['json_encoding']) ? $options['json_encoding'] : 0;
97+
$flags = $options['json_encoding'] ?? 0;
8598
$this->output->write(json_encode($data, $flags | JSON_PRETTY_PRINT)."\n");
8699
}
87100

src/Symfony/Component/Form/Console/Descriptor/TextDescriptor.php

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,40 @@ class TextDescriptor extends Descriptor
2727
{
2828
protected function describeDefaults(array $options)
2929
{
30-
$this->output->section('Built-in form types (Symfony\Component\Form\Extension\Core\Type)');
31-
$shortClassNames = array_map(function ($fqcn) { return \array_slice(explode('\\', $fqcn), -1)[0]; }, $options['core_types']);
32-
for ($i = 0; $i * 5 < \count($shortClassNames); ++$i) {
33-
$this->output->writeln(' '.implode(', ', \array_slice($shortClassNames, $i * 5, 5)));
30+
if ($options['core_types']) {
31+
$this->output->section('Built-in form types (Symfony\Component\Form\Extension\Core\Type)');
32+
$shortClassNames = array_map(function ($fqcn) { return \array_slice(explode('\\', $fqcn), -1)[0]; }, $options['core_types']);
33+
for ($i = 0, $loopsMax = \count($shortClassNames); $i * 5 < $loopsMax; ++$i) {
34+
$this->output->writeln(' '.implode(', ', \array_slice($shortClassNames, $i * 5, 5)));
35+
}
3436
}
3537

36-
$this->output->section('Service form types');
37-
$this->output->listing($options['service_types']);
38+
if ($options['service_types']) {
39+
$this->output->section('Service form types');
40+
$this->output->listing($options['service_types']);
41+
}
3842

39-
$this->output->section('Type extensions');
40-
$this->output->listing($options['extensions']);
43+
if (!$options['show_deprecated']) {
44+
if ($options['extensions']) {
45+
$this->output->section('Type extensions');
46+
$this->output->listing($options['extensions']);
47+
}
4148

42-
$this->output->section('Type guessers');
43-
$this->output->listing($options['guessers']);
49+
if ($options['guessers']) {
50+
$this->output->section('Type guessers');
51+
$this->output->listing($options['guessers']);
52+
}
53+
}
4454
}
4555

4656
protected function describeResolvedFormType(ResolvedFormTypeInterface $resolvedFormType, array $options = array())
4757
{
4858
$this->collectOptions($resolvedFormType);
4959

60+
if ($options['show_deprecated']) {
61+
$this->filterOptionsByDeprecated($resolvedFormType);
62+
}
63+
5064
$formOptions = $this->normalizeAndSortOptionsColumns(array_filter(array(
5165
'own' => $this->ownOptions,
5266
'overridden' => $this->overriddenOptions,
@@ -62,29 +76,12 @@ protected function describeResolvedFormType(ResolvedFormTypeInterface $resolvedF
6276
'extension' => 'Extension options',
6377
), $formOptions);
6478

65-
$tableRows = array();
66-
$count = \count(max($formOptions));
67-
for ($i = 0; $i < $count; ++$i) {
68-
$cells = array();
69-
foreach (array_keys($tableHeaders) as $group) {
70-
if (isset($formOptions[$group][$i])) {
71-
$option = $formOptions[$group][$i];
72-
73-
if (\is_string($option) && \in_array($option, $this->requiredOptions)) {
74-
$option .= ' <info>(required)</info>';
75-
}
79+
$this->output->title(sprintf('%s (Block prefix: "%s")', \get_class($resolvedFormType->getInnerType()), $resolvedFormType->getInnerType()->getBlockPrefix()));
7680

77-
$cells[] = $option;
78-
} else {
79-
$cells[] = null;
80-
}
81-
}
82-
$tableRows[] = $cells;
81+
if ($formOptions) {
82+
$this->output->table($tableHeaders, $this->buildTableRows($tableHeaders, $formOptions));
8383
}
8484

85-
$this->output->title(sprintf('%s (Block prefix: "%s")', \get_class($resolvedFormType->getInnerType()), $resolvedFormType->getInnerType()->getBlockPrefix()));
86-
$this->output->table($tableHeaders, $tableRows);
87-
8885
if ($this->parents) {
8986
$this->output->section('Parent types');
9087
$this->output->listing($this->parents);
@@ -101,7 +98,14 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio
10198
$definition = $this->getOptionDefinition($optionsResolver, $options['option']);
10299

103100
$dump = $this->getDumpFunction();
104-
$map = array(
101+
$map = array();
102+
if ($definition['deprecated']) {
103+
$map = array(
104+
'Deprecated' => 'deprecated',
105+
'Deprecation message' => 'deprecationMessage',
106+
);
107+
}
108+
$map += array(
105109
'Required' => 'required',
106110
'Default' => 'default',
107111
'Allowed types' => 'allowedTypes',
@@ -124,6 +128,25 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio
124128
$this->output->table(array(), $rows);
125129
}
126130

131+
private function buildTableRows(array $headers, array $options): array
132+
{
133+
$tableRows = array();
134+
$count = \count(max($options));
135+
for ($i = 0; $i < $count; ++$i) {
136+
$cells = array();
137+
foreach (array_keys($headers) as $group) {
138+
$option = $options[$group][$i] ?? null;
139+
if (\is_string($option) && \in_array($option, $this->requiredOptions, true)) {
140+
$option .= ' <info>(required)</info>';
141+
}
142+
$cells[] = $option;
143+
}
144+
$tableRows[] = $cells;
145+
}
146+
147+
return $tableRows;
148+
}
149+
127150
private function normalizeAndSortOptionsColumns(array $options)
128151
{
129152
foreach ($options as $group => $opts) {

src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
use Symfony\Component\Console\Exception\InvalidArgumentException;
1717
use Symfony\Component\Console\Tester\CommandTester;
1818
use Symfony\Component\Form\Command\DebugCommand;
19+
use Symfony\Component\Form\Extension\Core\Type\TextType;
1920
use Symfony\Component\Form\FormRegistry;
2021
use Symfony\Component\Form\ResolvedFormTypeFactory;
22+
use Symfony\Component\Form\Tests\Console\Descriptor\FooType;
2123

2224
class DebugCommandTest extends TestCase
2325
{
@@ -30,6 +32,24 @@ public function testDebugDefaults()
3032
$this->assertContains('Built-in form types', $tester->getDisplay());
3133
}
3234

35+
public function testDebugDeprecatedDefaults()
36+
{
37+
$tester = $this->createCommandTester(array('Symfony\Component\Form\Tests\Console\Descriptor'), array(TextType::class, FooType::class));
38+
$ret = $tester->execute(array('--show-deprecated' => true), array('decorated' => false));
39+
40+
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
41+
$this->assertSame(<<<TXT
42+
43+
Service form types
44+
------------------
45+
46+
* Symfony\Component\Form\Tests\Console\Descriptor\FooType
47+
48+
49+
TXT
50+
, $tester->getDisplay(true));
51+
}
52+
3353
public function testDebugSingleFormType()
3454
{
3555
$tester = $this->createCommandTester();
@@ -117,10 +137,10 @@ public function testDebugInvalidFormType()
117137
$this->createCommandTester()->execute(array('class' => 'test'));
118138
}
119139

120-
private function createCommandTester(array $namespaces = null)
140+
private function createCommandTester(array $namespaces = array('Symfony\Component\Form\Extension\Core\Type'), array $types = array())
121141
{
122142
$formRegistry = new FormRegistry(array(), new ResolvedFormTypeFactory());
123-
$command = null === $namespaces ? new DebugCommand($formRegistry) : new DebugCommand($formRegistry, $namespaces);
143+
$command = new DebugCommand($formRegistry, $namespaces, $types);
124144
$application = new Application();
125145
$application->add($command);
126146

0 commit comments

Comments
 (0)
0