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

Skip to content

Commit 87c209d

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

15 files changed

+312
-46
lines changed

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ 3419 -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, 'Display 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')
@@ -66,15 +67,23 @@ protected function configure()
6667
6768
<info>php %command.full_name%</info>
6869
69-
The command lists all built-in types, services types, type extensions and guessers currently available.
70+
The command lists all built-in types, services types, type extensions and
71+
guessers currently available.
7072
7173
<info>php %command.full_name% Symfony\Component\Form\Extension\Core\Type\ChoiceType</info>
7274
<info>php %command.full_name% ChoiceType</info>
7375
74-
The command lists all defined options that contains the given form type, as well as their parents and type extensions.
76+
The command lists all defined options that contains the given form type,
77+
as well as their parents and type extensions.
7578
7679
<info>php %command.full_name% ChoiceType choice_value</info>
7780
81+
Use the <info>--show-deprecated</info> option to display form types with
82+
deprecated options or the deprecated options of the given form type:
83+
84+
<info>php %command.full_name% --show-deprecated</info>
85+
<info>php %command.full_name% ChoiceType --show-deprecated</info>
86+
7887
The command displays the definition of the given option name.
7988
8089
<info>php %command.full_name% --format=json</info>
@@ -96,6 +105,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
96105
$object = null;
97106
$options['core_types'] = $this->getCoreTypes();
98107
$options['service_types'] = array_values(array_diff($this->types, $options['core_types']));
108+
if ($input->getOption('show-deprecated')) {
109+
$options['core_types'] = $this->filterTypesByDeprecated($options['core_types']);
110+
$options['service_types'] = $this->filterTypesByDeprecated($options['service_types']);
111+
}
99112
$options['extensions'] = $this->extensions;
100113
$options['guessers'] = $this->guessers;
101114
foreach ($options as $k => $list) {
@@ -114,7 +127,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
114127
$message = sprintf('Option "%s" is not defined in "%s".', $option, \get_class($resolvedType->getInnerType()));
115128

116129
if ($alternatives = $this->findAlternatives($option, $object->getDefinedOptions())) {
117-
if (1 == \count($alternatives)) {
130+
if (1 === \count($alternatives)) {
118131
$message .= "\n\nDid you mean this?\n ";
119132
} else {
120133
$message .= "\n\nDid you mean one of these?\n ";
@@ -134,6 +147,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
134147

135148
$helper = new DescriptorHelper();
136149
$options['format'] = $input->getOption('format');
150+
$options['show_deprecated'] = $input->getOption('show-deprecated');
137151
$helper->describe($io, $object, $options);
138152
}
139153

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

153167
$allTypes = array_merge($this->getCoreTypes(), $this->types);
154168
if ($alternatives = $this->findAlternatives($shortClassName, $allTypes)) {
155-
if (1 == \count($alternatives)) {
169+
if (1 === \count($alternatives)) {
156170
$message .= "\n\nDid you mean this?\n ";
157171
} else {
158172
$message .= "\n\nDid you mean one of these?\n ";
@@ -184,6 +198,22 @@ private function getCoreTypes()
184198
return $coreTypes;
185199
}
186200

201+
private function filterTypesByDeprecated(array $types): array
202+
{
203+
$typesWithDeprecatedOptions = array();
204+
foreach ($types as $class) {
205+
$optionsResolver = $this->formRegistry->getType($class)->getOptionsResolver();
206+
foreach ($optionsResolver->getDefinedOptions() as $option) {
207+
if ($optionsResolver->isDeprecated($option)) {
208+
$typesWithDeprecatedOptions[] = $class;
209+
break;
210+
}
211+
}
212+
}
213+
214+
return $typesWithDeprecatedOptions;
215+
}
216+
187217
private function findAlternatives($name, array $collection)
188218
{
189219
$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