Skip to content

Commit 86e8d5e

Browse files
Modernize the code a bit
Add PHP 7 parameter and return type hints. Don't allow passing instantiated FormType objects.
1 parent 81ffea5 commit 86e8d5e

File tree

45 files changed

+142
-500
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+142
-500
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class TestCommand extends Command
106106
$formHelper = $this->getHelper('form');
107107
/** @var FormHelper $formHelper */
108108

109-
$formData = $formHelper->interactUsingForm(new DemoType(), $input, $output);
109+
$formData = $formHelper->interactUsingForm(DemoType::class, $input, $output);
110110

111111
// $formData is the valid and populated form data object/array
112112
...

src/Bridge/FormFactory/ConsoleFormFactory.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,8 @@
44

55
use Symfony\Component\Console\Input\InputInterface;
66
use Symfony\Component\Form\FormInterface;
7-
use Symfony\Component\Form\FormTypeInterface;
87

98
interface ConsoleFormFactory
109
{
11-
/**
12-
* @param string|FormTypeInterface $formType
13-
* @param InputInterface $input
14-
* @param array $options
15-
*
16-
* @return FormInterface
17-
*/
18-
public function create($formType, InputInterface $input, array $options = []);
10+
public function create(string $formType, InputInterface $input, array $options = []): FormInterface;
1911
}

src/Bridge/FormFactory/ConsoleFormWithDefaultValuesAndOptionsFactory.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace Matthias\SymfonyConsoleForm\Bridge\FormFactory;
44

5-
use Matthias\SymfonyConsoleForm\Form\FormUtil;
65
use Symfony\Component\Console\Input\InputInterface;
76
use Symfony\Component\Form\Extension\Core\Type\FormType;
87
use Symfony\Component\Form\Extension\Csrf\Type\FormTypeCsrfExtension;
98
use Symfony\Component\Form\FormFactoryInterface;
9+
use Symfony\Component\Form\FormInterface;
1010
use Symfony\Component\Form\FormRegistryInterface;
11-
use Symfony\Component\Form\FormTypeInterface;
1211
use Symfony\Component\Form\Test\FormBuilderInterface;
1312

1413
class ConsoleFormWithDefaultValuesAndOptionsFactory implements ConsoleFormFactory
@@ -23,31 +22,20 @@ class ConsoleFormWithDefaultValuesAndOptionsFactory implements ConsoleFormFactor
2322
*/
2423
private $formRegistry;
2524

26-
/**
27-
* @param FormFactoryInterface $formFactory
28-
* @param FormRegistryInterface $formRegistry
29-
*/
3025
public function __construct(FormFactoryInterface $formFactory, FormRegistryInterface $formRegistry)
3126
{
3227
$this->formFactory = $formFactory;
3328
$this->formRegistry = $formRegistry;
3429
}
3530

36-
/**
37-
* @param string|FormTypeInterface $formType
38-
* @param InputInterface $input
39-
* @param array $options
40-
*
41-
* @return \Symfony\Component\Form\FormInterface
42-
*/
43-
public function create($formType, InputInterface $input, array $options = [])
31+
public function create(string $formType, InputInterface $input, array $options = []): FormInterface
4432
{
4533
$options = $this->addDefaultOptions($options);
4634

47-
$formBuilder = $this->formFactory->createBuilder(FormUtil::formTypeToString($formType), null, $options);
35+
$formBuilder = $this->formFactory->createBuilder($formType, null, $options);
4836

4937
foreach ($formBuilder as $name => $childBuilder) {
50-
/* @var FormBuilderInterface $childBuilder */
38+
/** @var FormBuilderInterface $childBuilder */
5139
if (!$input->hasOption($name)) {
5240
continue;
5341
}
@@ -63,12 +51,7 @@ public function create($formType, InputInterface $input, array $options = [])
6351
return $formBuilder->getForm();
6452
}
6553

66-
/**
67-
* @param array $options
68-
*
69-
* @return array
70-
*/
71-
private function addDefaultOptions(array $options)
54+
private function addDefaultOptions(array $options): array
7255
{
7356
$defaultOptions = [];
7457
// hack to prevent validation error "The CSRF token is invalid."

src/Bridge/Interaction/CollectionInteractor.php

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ public function __construct(FormInteractor $formInteractor)
3131
}
3232

3333
/**
34-
* @param FormInterface $form
35-
* @param HelperSet $helperSet
36-
* @param InputInterface $input
37-
* @param OutputInterface $output
38-
*
3934
* @throws CanNotInteractWithForm If the input isn't interactive
4035
* @throws FormNotReadyForInteraction If the "collection" form hasn't the option "allow_add"
4136
*
@@ -94,18 +89,11 @@ public function interactWith(
9489
return $submittedData;
9590
}
9691

97-
/**
98-
* @param HelperSet $helperSet
99-
* @param InputInterface $input
100-
* @param OutputInterface $output
101-
*
102-
* @return string
103-
*/
10492
private function askIfContinueToAdd(
10593
HelperSet $helperSet,
10694
InputInterface $input,
10795
OutputInterface $output
108-
) {
96+
): string {
10997
return $this->questionHelper($helperSet)->ask(
11098
$input,
11199
$output,
@@ -116,12 +104,7 @@ private function askIfContinueToAdd(
116104
);
117105
}
118106

119-
/**
120-
* @param HelperSet $helperSet
121-
*
122-
* @return QuestionHelper
123-
*/
124-
private function questionHelper(HelperSet $helperSet)
107+
private function questionHelper(HelperSet $helperSet): QuestionHelper
125108
{
126109
$helper = $helperSet->get('question');
127110

@@ -132,11 +115,7 @@ private function questionHelper(HelperSet $helperSet)
132115
return $helper;
133116
}
134117

135-
/**
136-
* @param FormInterface $form
137-
* @param OutputInterface $output
138-
*/
139-
private function printHeader(FormInterface $form, OutputInterface $output)
118+
private function printHeader(FormInterface $form, OutputInterface $output): void
140119
{
141120
$output->writeln(
142121
strtr(
@@ -148,11 +127,7 @@ private function printHeader(FormInterface $form, OutputInterface $output)
148127
);
149128
}
150129

151-
/**
152-
* @param int $entryNumber
153-
* @param OutputInterface $output
154-
*/
155-
private function printEditEntryHeader($entryNumber, OutputInterface $output)
130+
private function printEditEntryHeader(int $entryNumber, OutputInterface $output): void
156131
{
157132
$output->writeln(
158133
strtr(
@@ -164,11 +139,7 @@ private function printEditEntryHeader($entryNumber, OutputInterface $output)
164139
);
165140
}
166141

167-
/**
168-
* @param int $entryNumber
169-
* @param OutputInterface $output
170-
*/
171-
private function printAddEntryHeader($entryNumber, OutputInterface $output)
142+
private function printAddEntryHeader(int $entryNumber, OutputInterface $output): void
172143
{
173144
$output->writeln(
174145
strtr(
@@ -180,20 +151,12 @@ private function printAddEntryHeader($entryNumber, OutputInterface $output)
180151
);
181152
}
182153

183-
/**
184-
* @param HelperSet $helperSet
185-
* @param InputInterface $input
186-
* @param OutputInterface $output
187-
* @param int $entryNumber
188-
*
189-
* @return string
190-
*/
191154
private function askIfExistingEntryShouldBeAdded(
192155
HelperSet $helperSet,
193156
InputInterface $input,
194157
OutputInterface $output,
195-
$entryNumber
196-
) {
158+
int $entryNumber
159+
): string {
197160
return $this->questionHelper($helperSet)->ask(
198161
$input,
199162
$output,

src/Bridge/Interaction/CompoundInteractor.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ public function __construct(FormInteractor $formInteractor)
2626
}
2727

2828
/**
29-
* @param FormInterface $form
30-
* @param HelperSet $helperSet
31-
* @param InputInterface $input
32-
* @param OutputInterface $output
33-
*
3429
* @throws CanNotInteractWithForm If the input isn't interactive or a compound form
3530
*
3631
* @return array

src/Bridge/Interaction/DelegatingInteractor.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,15 @@ class DelegatingInteractor implements FormInteractor
1515
*/
1616
private $delegates = [];
1717

18-
/**
19-
* @param FormInteractor $interactor
20-
*/
21-
public function addInteractor(FormInteractor $interactor)
18+
public function addInteractor(FormInteractor $interactor): void
2219
{
2320
$this->delegates[] = $interactor;
2421
}
2522

2623
/**
27-
* @param FormInterface $form
28-
* @param HelperSet $helperSet
29-
* @param InputInterface $input
30-
* @param OutputInterface $output
31-
*
3224
* @throws CanNotInteractWithForm If no delegate was able to interact with this form
25+
*
26+
* @return mixed
3327
*/
3428
public function interactWith(
3529
FormInterface $form,

src/Bridge/Interaction/Exception/CanNotInteractWithForm.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Matthias\SymfonyConsoleForm\Bridge\Interaction\Exception;
44

5-
class CanNotInteractWithForm extends \LogicException
5+
use LogicException;
6+
7+
class CanNotInteractWithForm extends LogicException
68
{
79
}

src/Bridge/Interaction/Exception/FormNotReadyForInteraction.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Matthias\SymfonyConsoleForm\Bridge\Interaction\Exception;
44

5-
class FormNotReadyForInteraction extends \LogicException
5+
use LogicException;
6+
7+
class FormNotReadyForInteraction extends LogicException
68
{
79
}

src/Bridge/Interaction/Exception/NoNeedToInteractWithForm.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Matthias\SymfonyConsoleForm\Bridge\Interaction\Exception;
44

5-
class NoNeedToInteractWithForm extends \LogicException
5+
use LogicException;
6+
7+
class NoNeedToInteractWithForm extends LogicException
68
{
79
}

src/Bridge/Interaction/FieldInteractor.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,9 @@ public function __construct(TransformerResolver $transformerResolver)
2727
}
2828

2929
/**
30-
* @param FormInterface $form
31-
* @param HelperSet $helperSet
32-
* @param InputInterface $input
33-
* @param OutputInterface $output
34-
*
3530
* @throws CanNotInteractWithForm The input isn't interactive
3631
*
37-
* @return string
32+
* @return mixed
3833
*/
3934
public function interactWith(
4035
FormInterface $form,
@@ -51,12 +46,7 @@ public function interactWith(
5146
return $this->questionHelper($helperSet)->ask($input, $output, $question);
5247
}
5348

54-
/**
55-
* @param HelperSet $helperSet
56-
*
57-
* @return QuestionHelper
58-
*/
59-
private function questionHelper(HelperSet $helperSet)
49+
private function questionHelper(HelperSet $helperSet): QuestionHelper
6050
{
6151
$helper = $helperSet->get('question');
6252

0 commit comments

Comments
 (0)