Conditions | 10 |
Paths | 84 |
Total Lines | 47 |
Code Lines | 32 |
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 |
||
62 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
63 | { |
||
64 | $io = new SymfonyStyle($input, $output); |
||
65 | |||
66 | /** @var KernelInterface $kernel */ |
||
67 | $kernel = $this->getApplication()->getKernel(); |
||
|
|||
68 | $transPaths = []; |
||
69 | if ($this->defaultTransPath) { |
||
70 | $transPaths[] = $this->defaultTransPath; |
||
71 | } |
||
72 | $currentName = 'default directory'; |
||
73 | // Override with provided Bundle info (this section copied from symfony's translation:update command) |
||
74 | if (null !== ($bundle = $input->getArgument('bundle'))) { |
||
75 | try { |
||
76 | $foundBundle = $kernel->getBundle($bundle); |
||
77 | $bundleDir = $foundBundle->getPath(); |
||
78 | $transPaths = [is_dir($bundleDir . '/Resources/translations') ? $bundleDir . '/Resources/translations' : $bundleDir . '/translations']; |
||
79 | $currentName = $foundBundle->getName(); |
||
80 | } catch (\InvalidArgumentException) { |
||
81 | // such a bundle does not exist, so treat the argument as path |
||
82 | $path = $bundle; |
||
83 | $transPaths = [$path . '/translations']; |
||
84 | if (!is_dir($transPaths[0]) && !isset($transPaths[1])) { |
||
85 | throw new InvalidArgumentException(sprintf('"%s" is neither an enabled bundle nor a directory.', $transPaths[0])); |
||
86 | } |
||
87 | } |
||
88 | } |
||
89 | $io->title('Translation Messages Key to Value Transformer'); |
||
90 | $io->comment(sprintf('Transforming translation files for "<info>%s</info>"', $currentName)); |
||
91 | $finder = new Finder(); |
||
92 | $fs = new Filesystem(); |
||
93 | foreach ($finder->files()->in($transPaths)->name(['*.yaml', '*.yml']) as $file) { |
||
94 | $io->text(sprintf('<comment>Parsing %s</comment>', $file->getBasename())); |
||
95 | $messages = Yaml::parseFile($file->getRealPath()); |
||
96 | foreach ($messages as $key => $message) { |
||
97 | if (null === $message) { |
||
98 | $messages[$key] = $key; |
||
99 | } |
||
100 | } |
||
101 | $io->text(sprintf('<info>Dumping %s</info>', $file->getBasename())); |
||
102 | ksort($messages); // sort the messages by key |
||
103 | $fs->dumpFile($file->getRealPath(), Yaml::dump($messages)); |
||
104 | } |
||
105 | |||
106 | $io->success('Success!'); |
||
107 | |||
108 | return Command::SUCCESS; |
||
109 | } |
||
111 |