| Conditions | 9 |
| Paths | 12 |
| Total Lines | 154 |
| 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 |
||
| 49 | protected function interact(InputInterface $input, OutputInterface $output) |
||
| 50 | { |
||
| 51 | $questionHelper = $this->getQuestionHelper(); |
||
| 52 | $questionHelper->writeSection($output, 'Welcome to the Symfony2Admingenerator'); |
||
| 53 | |||
| 54 | /* |
||
| 55 | * Namespace option |
||
| 56 | */ |
||
| 57 | $askForBundleName = true; |
||
| 58 | $namespace = $input->getOption('namespace'); |
||
| 59 | $output->writeln(array( |
||
| 60 | '', |
||
| 61 | 'Precise the full bundle namespace where you want to generate files (including vendor name if any)', |
||
| 62 | '' |
||
| 63 | )); |
||
| 64 | |||
| 65 | $question = new Question($questionHelper->getQuestion( |
||
| 66 | 'Fully qualified bundle name', |
||
| 67 | $namespace |
||
| 68 | ), $namespace); |
||
| 69 | $question->setValidator(function ($inputNamespace) { |
||
| 70 | return Validators::validateBundleNamespace($inputNamespace, false); |
||
| 71 | }); |
||
| 72 | $namespace = $questionHelper->ask($input, $output, $question); |
||
| 73 | |||
| 74 | if (strpos($namespace, '\\') === false) { |
||
| 75 | // this is a bundle name (FooBundle) not a namespace (Acme\FooBundle) |
||
| 76 | // so this is the bundle name (and it is also the namespace) |
||
| 77 | $input->setOption('bundle-name', $namespace); |
||
| 78 | $askForBundleName = false; |
||
| 79 | } |
||
| 80 | $input->setOption('namespace', $namespace); |
||
| 81 | |||
| 82 | /* |
||
| 83 | * bundle-name option |
||
| 84 | */ |
||
| 85 | if ($askForBundleName) { |
||
| 86 | $bundle = $input->getOption('bundle-name'); |
||
| 87 | // no bundle yet? Get a default from the namespace |
||
| 88 | if (!$bundle) { |
||
| 89 | $bundle = strtr($namespace, array('\\Bundle\\' => '', '\\' => '')); |
||
| 90 | } |
||
| 91 | |||
| 92 | $output->writeln(array( |
||
| 93 | '', |
||
| 94 | 'Please specify the Bundle name.', |
||
| 95 | 'Based on the namespace, we suggest <comment>'.$bundle.'</comment>.', |
||
| 96 | '', |
||
| 97 | )); |
||
| 98 | $question = new Question($questionHelper->getQuestion( |
||
| 99 | 'Bundle name', |
||
| 100 | $bundle |
||
| 101 | ), $bundle); |
||
| 102 | $question->setValidator(function($bundleName){ |
||
| 103 | return Validators::validateBundleName($bundleName); |
||
| 104 | }); |
||
| 105 | $bundle = $questionHelper->ask($input, $output, $question); |
||
| 106 | $input->setOption('bundle-name', $bundle); |
||
| 107 | } |
||
| 108 | |||
| 109 | /* |
||
| 110 | * dir option |
||
| 111 | */ |
||
| 112 | // defaults to src/ in the option |
||
| 113 | $dir = $input->getOption('dir'); |
||
| 114 | $output->writeln(array( |
||
| 115 | '', |
||
| 116 | 'Bundles are usually generated into the <info>src/</info> directory. Unless you\'re', |
||
| 117 | 'doing something custom, hit enter to keep this default!', |
||
| 118 | '', |
||
| 119 | )); |
||
| 120 | |||
| 121 | $question = new Question($questionHelper->getQuestion( |
||
| 122 | 'Target Directory', |
||
| 123 | $dir |
||
| 124 | ), $dir); |
||
| 125 | $dir = $questionHelper->ask($input, $output, $question); |
||
| 126 | $input->setOption('dir', $dir); |
||
| 127 | |||
| 128 | |||
| 129 | /* |
||
| 130 | * Generator option |
||
| 131 | */ |
||
| 132 | $generator = $input->getOption('generator'); |
||
| 133 | $output->writeln(array( |
||
| 134 | '', |
||
| 135 | 'What database manager are you using?', |
||
| 136 | '' |
||
| 137 | )); |
||
| 138 | |||
| 139 | $question = new Question($questionHelper->getQuestion( |
||
| 140 | 'Generator (doctrine, doctrine_odm, propel)', |
||
| 141 | $generator |
||
| 142 | ), $generator); |
||
| 143 | $question->setValidator(function($generator){ |
||
| 144 | if (!in_array($generator, array('doctrine', 'doctrine_odm', 'propel'))) { |
||
| 145 | throw new \InvalidArgumentException('Use a valid generator.'); |
||
| 146 | } |
||
| 147 | |||
| 148 | return $generator; |
||
| 149 | }); |
||
| 150 | $question->setAutocompleterValues(array('doctrine', 'doctrine_odm', 'propel')); |
||
|
|
|||
| 151 | $generator = $questionHelper->ask($input, $output, $question); |
||
| 152 | $input->setOption('generator', $generator); |
||
| 153 | |||
| 154 | |||
| 155 | /* |
||
| 156 | * Model name option |
||
| 157 | */ |
||
| 158 | $modelName = $input->getOption('model-name'); |
||
| 159 | $output->writeln(array( |
||
| 160 | '', |
||
| 161 | 'What is the model name you want to generate files for?', |
||
| 162 | '' |
||
| 163 | )); |
||
| 164 | $question = new Question($questionHelper->getQuestion( |
||
| 165 | 'Model name', |
||
| 166 | $modelName |
||
| 167 | ), $modelName); |
||
| 168 | $question->setValidator(function ($modelName) { |
||
| 169 | if (empty($modelName) || preg_match('#[^a-zA-Z0-9]#', $modelName)) { |
||
| 170 | throw new \InvalidArgumentException('Model name should not contain any special characters nor spaces.'); |
||
| 171 | } |
||
| 172 | |||
| 173 | return $modelName; |
||
| 174 | }); |
||
| 175 | $modelName = $questionHelper->ask($input, $output, $question); |
||
| 176 | $input->setOption('model-name', $modelName); |
||
| 177 | |||
| 178 | /* |
||
| 179 | * Prefix option |
||
| 180 | */ |
||
| 181 | $prefix = $input->getOption('prefix'); |
||
| 182 | $output->writeln(array( |
||
| 183 | '', |
||
| 184 | 'Please precise a prefix to use for YAML generator file', |
||
| 185 | '' |
||
| 186 | )); |
||
| 187 | if (!$prefix) { |
||
| 188 | $prefix = preg_replace('/[0-9]/', '', $modelName); |
||
| 189 | } |
||
| 190 | $question = new Question($questionHelper->getQuestion( |
||
| 191 | 'Prefix of yaml', |
||
| 192 | $prefix |
||
| 193 | ), $prefix); |
||
| 194 | $question->setValidator(function ($prefix) { |
||
| 195 | if (!preg_match('/([a-z]+)/i', $prefix)) { |
||
| 196 | throw new \RuntimeException('Prefix have to be a simple word'); |
||
| 197 | } |
||
| 198 | return $prefix; |
||
| 199 | }); |
||
| 200 | $prefix = $questionHelper->ask($input, $output, $question); |
||
| 201 | $input->setOption('prefix', $prefix); |
||
| 202 | } |
||
| 203 | |||
| 395 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: