| Conditions | 8 | 
| Paths | 36 | 
| Total Lines | 90 | 
| Code Lines | 51 | 
| 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 | ||
| 99 | public function execute(InputInterface $input, OutputInterface $output) | ||
| 100 |     { | ||
| 101 | /** @var string $traitName */ | ||
| 102 |         $traitName = (string)$input->getOption('className'); | ||
| 103 | |||
| 104 | /** @var string $destination */ | ||
| 105 |         $destination = (string)$input->getOption('output'); | ||
| 106 | |||
| 107 | $outputFileName = sprintf( | ||
| 108 | '%s/%s.php', | ||
| 109 | $destination, | ||
| 110 | $traitName | ||
| 111 | ); | ||
| 112 | |||
| 113 | $metaDataEntries = $this->entityManager->getMetadataFactory()->getAllMetadata(); | ||
| 114 | $trait = $this->generateTrait($input); | ||
| 115 | |||
| 116 |         foreach ($metaDataEntries as $metaData) { | ||
| 117 |             if ($filter = $input->getOption('filter')) { | ||
| 118 |                 if (strpos($metaData->getName(), $filter) === false) { | ||
| 119 | continue; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | $method = $this->generateMethod( | ||
| 124 | $input, | ||
| 125 | $metaData | ||
| 126 | ); | ||
| 127 | |||
| 128 |             try { | ||
| 129 | $trait->addMethodFromGenerator($method); | ||
| 130 |             } catch (InvalidArgumentException $e) { | ||
| 131 | $output->writeln( | ||
| 132 | sprintf( | ||
| 133 | 'Method "%s" already exists in this class', | ||
| 134 | $method->getName() | ||
| 135 | ) | ||
| 136 | ); | ||
| 137 | |||
| 138 | $reflection = new \ReflectionClass($metaData->getName()); | ||
| 139 | |||
| 140 | $method->setName( | ||
| 141 | sprintf( | ||
| 142 | 'get%sRepository%s', | ||
| 143 | $reflection->getShortName(), | ||
| 144 |                         str_replace('.', '', uniqid('', true)) | ||
| 145 | ) | ||
| 146 | ); | ||
| 147 | |||
| 148 | $trait->addMethodFromGenerator($method); | ||
| 149 | |||
| 150 | $output->writeln( | ||
| 151 | sprintf( | ||
| 152 | 'Refactored the method to "%s". Please refactor to a usable name you will remember', | ||
| 153 | $method->getName() | ||
| 154 | ) | ||
| 155 | ); | ||
| 156 |                 $output->writeln(''); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | $file = new FileGenerator(); | ||
| 161 | $file->setClass($trait); | ||
| 162 | |||
| 163 |         if (!is_dir($destination)) { | ||
| 164 | mkdir($destination, 0777, true); | ||
| 165 | } | ||
| 166 | |||
| 167 |         if (!$input->getOption('force')) { | ||
| 168 |             if (file_exists($outputFileName)) { | ||
| 169 |                 $output->writeln('File already exists. Use "-f" to force overwriting the existing file'); | ||
| 170 | return; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | file_put_contents( | ||
| 175 | $outputFileName, | ||
| 176 | $file->generate() | ||
| 177 | ); | ||
| 178 | |||
| 179 |         $output->writeln(''); | ||
| 180 | |||
| 181 | $output->writeln( | ||
| 182 | sprintf( | ||
| 183 | 'Trait created in "%s"', | ||
| 184 | $outputFileName | ||
| 185 | ) | ||
| 186 | ); | ||
| 187 | |||
| 188 |         $output->writeln(''); | ||
| 189 | } | ||
| 289 |