Conditions | 6 |
Paths | 3 |
Total Lines | 76 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
43 | protected function interact(InputInterface $input, OutputInterface $output) |
||
44 | { |
||
45 | $questionHelper = $this->getQuestionHelper(); |
||
46 | $questionHelper->writeSection($output, 'Welcome to the Notification generator'); |
||
47 | |||
48 | // Get the Bundle to generate it in |
||
49 | $output->writeln( |
||
50 | [ |
||
51 | 'This command helps you generate a Notification class', |
||
52 | '', |
||
53 | 'First, give the name of the bundle to generate the notification in (eg <comment>AppBundle</comment>)', |
||
54 | ] |
||
55 | ); |
||
56 | |||
57 | $question = new Question( |
||
58 | $questionHelper->getQuestion('The bundle name', $input->getOption('bundle')), |
||
59 | $input->getOption('bundle') |
||
60 | ); |
||
61 | |||
62 | // @TODO: Add existing bundle validation |
||
63 | $question->setValidator(['Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateBundleName']); |
||
64 | $question->setNormalizer( |
||
65 | function ($value) { |
||
66 | return $value ? trim($value) : ''; |
||
67 | } |
||
68 | ); |
||
69 | $question->setMaxAttempts(2); |
||
70 | |||
71 | $bundle = $questionHelper->ask($input, $output, $question); |
||
72 | $input->setOption('bundle', $bundle); |
||
73 | |||
74 | // Get the Bundle to generate it in |
||
75 | $output->writeln( |
||
76 | [ |
||
77 | '', |
||
78 | 'Now, give the name of the new notification class (eg <comment>NewMember</comment>)', |
||
79 | ] |
||
80 | ); |
||
81 | |||
82 | // Get the new class name and validate it. |
||
83 | $question = new Question( |
||
84 | $questionHelper->getQuestion('The notification name', $input->getOption('notification_name')), |
||
85 | $input->getOption('notification_name') |
||
86 | ); |
||
87 | $question->setValidator( |
||
88 | function ($answer) { |
||
89 | // Should only contain letters. |
||
90 | $valid = preg_match('/^[a-zA-Z]+$/', $answer); |
||
91 | if (!$valid) { |
||
92 | throw new \RuntimeException( |
||
93 | 'The class name should only contain letters' |
||
94 | ); |
||
95 | } |
||
96 | |||
97 | return $answer; |
||
98 | } |
||
99 | ); |
||
100 | $question->setNormalizer( |
||
101 | function ($value) { |
||
102 | return $value ? trim($value) : ''; |
||
103 | } |
||
104 | ); |
||
105 | |||
106 | $notificationName = $questionHelper->ask($input, $output, $question); |
||
107 | $input->setOption('notification_name', $notificationName); |
||
108 | |||
109 | // ask whether to generate templates for enabled channels. |
||
110 | foreach ($this->channels as $channel) { |
||
111 | $question = $this->createYesNoQuestion($questionHelper, $input, $channel); |
||
112 | |||
113 | $generateTemplate = $questionHelper->ask($input, $output, $question); |
||
114 | if ($generateTemplate == 'y') { |
||
115 | $this->channelTemplates[] = $channel; |
||
116 | } |
||
117 | } |
||
118 | } |
||
119 | |||
201 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.