Skip to content

Commit af3f16c

Browse files
committed
Add PhpStan and implement suggested fixes
1 parent 4fe0e6d commit af3f16c

14 files changed

+77
-28
lines changed

src/Bridge/FormFactory/ConsoleFormWithDefaultValuesAndOptionsFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Matthias\SymfonyConsoleForm\Bridge\FormFactory;
44

5+
use Matthias\SymfonyConsoleForm\Form\FormUtil;
56
use Symfony\Component\Console\Input\InputInterface;
67
use Symfony\Component\Form\Extension\Core\Type\FormType;
78
use Symfony\Component\Form\Extension\Csrf\Type\FormTypeCsrfExtension;
@@ -37,13 +38,13 @@ public function __construct(FormFactoryInterface $formFactory, FormRegistryInter
3738
* @param InputInterface $input
3839
* @param array $options
3940
*
40-
* @return \Symfony\Component\Form\Form
41+
* @return \Symfony\Component\Form\FormInterface
4142
*/
4243
public function create($formType, InputInterface $input, array $options = [])
4344
{
4445
$options = $this->addDefaultOptions($options);
4546

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

4849
foreach ($formBuilder as $name => $childBuilder) {
4950
/* @var FormBuilderInterface $childBuilder */

src/Bridge/Interaction/CollectionInteractor.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Matthias\SymfonyConsoleForm\Bridge\Interaction\Exception\FormNotReadyForInteraction;
77
use Matthias\SymfonyConsoleForm\Console\Formatter\Format;
88
use Matthias\SymfonyConsoleForm\Form\FormUtil;
9+
use RuntimeException;
910
use Symfony\Component\Console\Helper\HelperSet;
1011
use Symfony\Component\Console\Helper\QuestionHelper;
1112
use Symfony\Component\Console\Input\InputInterface;
@@ -122,7 +123,13 @@ private function askIfContinueToAdd(
122123
*/
123124
private function questionHelper(HelperSet $helperSet)
124125
{
125-
return $helperSet->get('question');
126+
$helper = $helperSet->get('question');
127+
128+
if (!$helper instanceof QuestionHelper) {
129+
throw new RuntimeException('HelperSet does not contain valid QuestionHelper');
130+
}
131+
132+
return $helper;
126133
}
127134

128135
/**

src/Bridge/Interaction/FieldInteractor.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Matthias\SymfonyConsoleForm\Bridge\Interaction\Exception\CanNotInteractWithForm;
66
use Matthias\SymfonyConsoleForm\Bridge\Transformer\TransformerResolver;
7+
use RuntimeException;
78
use Symfony\Component\Console\Helper\HelperSet;
89
use Symfony\Component\Console\Helper\QuestionHelper;
910
use Symfony\Component\Console\Input\InputInterface;
@@ -57,6 +58,12 @@ public function interactWith(
5758
*/
5859
private function questionHelper(HelperSet $helperSet)
5960
{
60-
return $helperSet->get('question');
61+
$helper = $helperSet->get('question');
62+
63+
if (!$helper instanceof QuestionHelper) {
64+
throw new RuntimeException('HelperSet does not contain valid QuestionHelper');
65+
}
66+
67+
return $helper;
6168
}
6269
}

src/Bridge/Transformer/ChoiceTransformer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
use Matthias\SymfonyConsoleForm\Console\Helper\Question\AlwaysReturnKeyOfChoiceQuestion;
66
use Symfony\Component\Console\Question\Question;
7-
use Symfony\Component\Form\Form;
7+
use Symfony\Component\Form\FormInterface;
88

99
class ChoiceTransformer extends AbstractTransformer
1010
{
1111
/**
12-
* @param Form $form
12+
* @param FormInterface $form
1313
*
1414
* @return Question
1515
*/
16-
public function transform(Form $form)
16+
public function transform(FormInterface $form)
1717
{
1818
$formView = $form->createView();
1919

src/Bridge/Transformer/FormToQuestionTransformer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
namespace Matthias\SymfonyConsoleForm\Bridge\Transformer;
44

55
use Symfony\Component\Console\Question\Question;
6-
use Symfony\Component\Form\Form;
6+
use Symfony\Component\Form\FormInterface;
77

88
/**
99
* Transform the given form (field) to a Question used by the Console Component to interact with the user.
1010
*/
1111
interface FormToQuestionTransformer
1212
{
1313
/**
14-
* @param Form $form
14+
* @param FormInterface $form
1515
*
1616
* @return Question
1717
*/
18-
public function transform(Form $form);
18+
public function transform(FormInterface $form);
1919
}

src/Bridge/Transformer/PasswordTransformer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22

33
namespace Matthias\SymfonyConsoleForm\Bridge\Transformer;
44

5-
use Symfony\Component\Form\Form;
5+
use Symfony\Component\Console\Question\Question;
6+
use Symfony\Component\Form\FormInterface;
67

78
class PasswordTransformer extends TextTransformer
89
{
910
/**
10-
* @param Form $form
11+
* @param FormInterface $form
1112
*
1213
* @return Question
1314
*/
14-
public function transform(Form $form)
15+
public function transform(FormInterface $form)
1516
{
1617
$question = parent::transform($form);
1718
$question->setHidden(true);

src/Bridge/Transformer/TextTransformer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
namespace Matthias\SymfonyConsoleForm\Bridge\Transformer;
44

55
use Symfony\Component\Console\Question\Question;
6-
use Symfony\Component\Form\Form;
6+
use Symfony\Component\Form\FormInterface;
77

88
class TextTransformer extends AbstractTransformer
99
{
1010
/**
11-
* @param Form $form
11+
* @param FormInterface $form
1212
*
1313
* @return Question
1414
*/
15-
public function transform(Form $form)
15+
public function transform(FormInterface $form)
1616
{
1717
return new Question($this->questionFrom($form), $this->defaultValueFrom($form));
1818
}

src/Console/EventListener/RegisterHelpersEventListener.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Matthias\SymfonyConsoleForm\Console\EventListener;
44

55
use Matthias\SymfonyConsoleForm\Console\Helper\HelperCollection;
6+
use RuntimeException;
67
use Symfony\Component\Console\Event\ConsoleCommandEvent;
78

89
class RegisterHelpersEventListener
@@ -25,7 +26,13 @@ public function __construct(HelperCollection $helperCollection)
2526
*/
2627
public function onConsoleCommand(ConsoleCommandEvent $event)
2728
{
28-
$helperSet = $event->getCommand()->getHelperSet();
29+
$command = $event->getCommand();
30+
31+
if (null === $command) {
32+
throw new RuntimeException('Received ConsoleCommandEvent without Command instance!');
33+
}
34+
35+
$helperSet = $command->getHelperSet();
2936

3037
$this->helperCollection->addTo($helperSet);
3138
}

src/Console/Helper/FormHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function interactUsingForm(
7171
if (!$form->isValid()) {
7272
$output->write(sprintf('Invalid data provided: %s', $form->getErrors(true, false)));
7373
array_map(
74-
function (FormInterface $formField) use ($form, &$validFormFields) {
74+
function (FormInterface $formField) use (&$validFormFields) {
7575
if ($formField->isValid()) {
7676
$validFormFields[] = $formField->getName();
7777
}

src/Console/Helper/Question/AlwaysReturnKeyOfChoiceQuestion.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class AlwaysReturnKeyOfChoiceQuestion extends ChoiceQuestion
2323
private $_errorMessage = 'Value "%s" is invalid';
2424

2525
/**
26-
* @param string $question The question to ask to the user
27-
* @param array $choices The list of available choices
28-
* @param mixed $default The default answer to return
26+
* @param string $question The question to ask to the user
27+
* @param array $choiceViews The list of available choices
28+
* @param mixed $default The default answer to return
2929
*/
3030
public function __construct($question, array $choiceViews, $default = null)
3131
{

0 commit comments

Comments
 (0)