| Conditions | 12 |
| Paths | 68 |
| Total Lines | 81 |
| Code Lines | 50 |
| 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 |
||
| 64 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 65 | { |
||
| 66 | $destination = $input->getOption('output'); |
||
| 67 | $namespace = $input->getOption('namespace'); |
||
| 68 | $overwrite = $input->hasOption('force'); |
||
| 69 | $metaDataEntries = $this->entityManager->getMetadataFactory()->getAllMetadata(); |
||
| 70 | |||
| 71 | if (!is_dir($destination)) { |
||
|
|
|||
| 72 | mkdir($destination, 0777, true); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** @var ClassMetadata $metaData */ |
||
| 76 | foreach ($metaDataEntries as $metaData) { |
||
| 77 | if ($filter = $input->getOption('filter')) { |
||
| 78 | if (strpos($metaData->getName(), $filter) === false) { |
||
| 79 | $output->writeln( |
||
| 80 | sprintf( |
||
| 81 | 'Filtering out %s...', |
||
| 82 | $metaData->getName() |
||
| 83 | ), |
||
| 84 | OutputInterface::VERBOSITY_VERY_VERBOSE |
||
| 85 | ); |
||
| 86 | continue; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | $fileName = $destination . '/' . $metaData->reflClass->getShortName() . 'Interface.ts'; |
||
| 91 | // TODO handle duplicate names |
||
| 92 | |||
| 93 | $indent = ''; |
||
| 94 | $fileContent = ''; |
||
| 95 | |||
| 96 | if ($namespace) { |
||
| 97 | $fileContent = sprintf("%snamespace %s {", $indent, $namespace) . PHP_EOL; |
||
| 98 | $indent = ' '; |
||
| 99 | } |
||
| 100 | |||
| 101 | $fileContent .= $indent . sprintf( |
||
| 102 | "%sinterface %s {" . PHP_EOL, |
||
| 103 | $namespace ? 'export ': '', |
||
| 104 | $metaData->reflClass->getShortName() |
||
| 105 | ); |
||
| 106 | $indent .= ' '; |
||
| 107 | |||
| 108 | foreach ($metaData->fieldMappings as $fieldMapping) { |
||
| 109 | $fileContent .= sprintf( |
||
| 110 | '%s%s%s: %s;' . PHP_EOL, |
||
| 111 | $indent, |
||
| 112 | $fieldMapping['fieldName'], |
||
| 113 | $fieldMapping['nullable'] ? '?' : '', |
||
| 114 | $this->toTypeScript($fieldMapping['type']) |
||
| 115 | ); |
||
| 116 | } |
||
| 117 | |||
| 118 | $indent = substr($indent, 0, -4); |
||
| 119 | $fileContent .= $indent . '}' . PHP_EOL; |
||
| 120 | |||
| 121 | if ($namespace) { |
||
| 122 | $indent = ''; |
||
| 123 | $fileContent .= sprintf("%s}", $indent) . PHP_EOL; |
||
| 124 | } |
||
| 125 | |||
| 126 | if (file_exists($fileName) && !$overwrite) { |
||
| 127 | $output->writeln( |
||
| 128 | sprintf( |
||
| 129 | '%s already exists. Skipping...', |
||
| 130 | $fileName |
||
| 131 | ), |
||
| 132 | OutputInterface::VERBOSITY_VERBOSE |
||
| 133 | ); |
||
| 134 | |||
| 135 | continue; |
||
| 136 | } |
||
| 137 | |||
| 138 | file_put_contents($fileName, $fileContent); |
||
| 139 | } |
||
| 140 | |||
| 141 | $output->writeln( |
||
| 142 | sprintf( |
||
| 143 | 'TypeScript interfaces written to "%s"', |
||
| 144 | $destination |
||
| 145 | ) |
||
| 195 | } |