| Conditions | 3 |
| Paths | 3 |
| Total Lines | 73 |
| Code Lines | 44 |
| 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 |
||
| 88 | public function execute(InputInterface $input, OutputInterface $output) |
||
| 89 | { |
||
| 90 | $traitName = $input->getOption('classname'); |
||
| 91 | $outputFileName = sprintf( |
||
| 92 | '%s/%s.php', |
||
| 93 | $input->getOption('destination'), |
||
|
|
|||
| 94 | $traitName |
||
| 95 | ); |
||
| 96 | |||
| 97 | $metaDataEntries = $this->entityManager->getMetadataFactory()->getAllMetadata(); |
||
| 98 | $trait = $this->generateTrait( |
||
| 99 | $input, |
||
| 100 | $output |
||
| 101 | ); |
||
| 102 | |||
| 103 | foreach ($metaDataEntries as $metaData) { |
||
| 104 | $method = $this->generateMethod( |
||
| 105 | $input, |
||
| 106 | $output, |
||
| 107 | $metaData |
||
| 108 | ); |
||
| 109 | |||
| 110 | try { |
||
| 111 | $trait->addMethodFromGenerator($method); |
||
| 112 | |||
| 113 | } catch (InvalidArgumentException $e) { |
||
| 114 | $output->writeln( |
||
| 115 | sprintf( |
||
| 116 | 'Method "%s" already exists in this class', |
||
| 117 | $method->getName() |
||
| 118 | ) |
||
| 119 | ); |
||
| 120 | |||
| 121 | $reflection = new \ReflectionClass($metaData->getName()); |
||
| 122 | |||
| 123 | $method->setName( |
||
| 124 | sprintf( |
||
| 125 | 'get%sRepository%s', |
||
| 126 | $reflection->getShortName(), |
||
| 127 | str_replace('.', '', uniqid('', true)) |
||
| 128 | ) |
||
| 129 | ); |
||
| 130 | |||
| 131 | $trait->addMethodFromGenerator($method); |
||
| 132 | |||
| 133 | $output->writeln( |
||
| 134 | sprintf( |
||
| 135 | 'Refactored the method to "%s". Please refactor to a usable name you will remember', |
||
| 136 | $method->getName() |
||
| 137 | ) |
||
| 138 | ); |
||
| 139 | $output->writeln(''); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | $file = new FileGenerator(); |
||
| 144 | $file->setClass($trait); |
||
| 145 | |||
| 146 | file_put_contents( |
||
| 147 | $outputFileName, |
||
| 148 | $file->generate() |
||
| 149 | ); |
||
| 150 | |||
| 151 | $output->writeln(''); |
||
| 152 | |||
| 153 | $output->writeln( |
||
| 154 | sprintf( |
||
| 155 | 'Trait created in "%s"', |
||
| 156 | $outputFileName |
||
| 157 | ) |
||
| 158 | ); |
||
| 159 | |||
| 160 | $output->writeln(''); |
||
| 161 | } |
||
| 258 | } |