| Total Complexity | 40 |
| Total Lines | 179 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like GenerateTypeScriptInterfaceCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GenerateTypeScriptInterfaceCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class GenerateTypeScriptInterfaceCommand extends Command |
||
| 14 | { |
||
| 15 | /** @var EntityManagerInterface */ |
||
| 16 | protected $entityManager; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * GenerateTypeScriptInterfaceCommand constructor. |
||
| 20 | * @param EntityManagerInterface $entityManager |
||
| 21 | */ |
||
| 22 | public function __construct(EntityManagerInterface $entityManager) |
||
| 23 | { |
||
| 24 | $this->entityManager = $entityManager; |
||
| 25 | parent::__construct(); |
||
| 26 | } |
||
| 27 | |||
| 28 | protected function configure() |
||
| 29 | { |
||
| 30 | parent::configure(); |
||
| 31 | |||
| 32 | $this |
||
| 33 | ->setName('orm:generate-ts-interfaces') |
||
| 34 | ->setDescription('Generate typescript interfaces') |
||
| 35 | ->setHelp('Generates typescript interfaces for use in frontend development') |
||
| 36 | ->addOption( |
||
| 37 | 'output', |
||
| 38 | 'o', |
||
| 39 | InputOption::VALUE_OPTIONAL, |
||
| 40 | 'Output path', |
||
| 41 | getcwd() . '/data/interfaces' |
||
| 42 | ) |
||
| 43 | ->addOption( |
||
| 44 | 'force', |
||
| 45 | 'f', |
||
| 46 | InputOption::VALUE_OPTIONAL, |
||
| 47 | 'Overwrite existing interfaces', |
||
| 48 | true |
||
| 49 | ) |
||
| 50 | ->addOption( |
||
| 51 | 'filter', |
||
| 52 | null, |
||
| 53 | InputOption::VALUE_OPTIONAL, |
||
| 54 | 'filter the list of entities interfaces are created for' |
||
| 55 | ) |
||
| 56 | ->addOption( |
||
| 57 | 'namespace', |
||
| 58 | null, |
||
| 59 | InputOption::VALUE_OPTIONAL, |
||
| 60 | 'Declares the namespace' |
||
| 61 | ); |
||
| 62 | } |
||
| 63 | |||
| 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 | ) |
||
| 146 | ); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $type |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | private function toTypeScript(string $type): string |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | } |