Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 35 | { |
||
| 36 | $this |
||
| 37 | 9 | ->setName('init') |
|
| 38 | 9 | ->setDescription('Initialize deployer in your project') |
|
| 39 | 9 | ->addOption('template', 't', InputOption::VALUE_OPTIONAL, 'The template of you project') |
|
| 40 | 9 | ->addOption('filepath', null, InputOption::VALUE_OPTIONAL, 'The file path (default "deploy.php")', 'deploy.php'); |
|
| 41 | 9 | } |
|
| 42 | |||
| 43 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 44 | { |
||
| 45 | /** @var FormatterHelper $formatter */ |
||
| 46 | $formatter = $this->getHelper('formatter'); |
||
| 47 | $initializer = new Initializer(); |
||
| 48 | $template = $input->getOption('template'); |
||
| 49 | $filepath = $input->getOption('filepath'); |
||
| 50 | |||
| 51 | if (file_exists($filepath)) { |
||
| 52 | $output->writeln([ |
||
| 53 | $formatter->formatBlock( |
||
| 54 | sprintf('The file "%s" already exist.', $filepath), |
||
| 55 | 'bg=red;fg=white', true |
||
| 56 | ), |
||
| 57 | ]); |
||
| 58 | return 2; |
||
| 59 | } |
||
| 60 | |||
| 61 | $project = 'my_project'; |
||
| 62 | $repository = ''; |
||
| 63 | $hosts = []; |
||
| 64 | $allow = true; |
||
| 65 | |||
| 66 | if ($template === null) { |
||
| 67 | $io = new SymfonyStyle($input, $output); |
||
| 68 | |||
| 69 | // Welcome message |
||
| 70 | $output->writeln(" |
||
| 71 | _____ _ |
||
| 72 | | __ \ | | |
||
| 73 | | | | | ___ _ __ | | ___ _ _ ___ _ __ |
||
| 74 | | | | |/ _ \ '_ \| |/ _ \| | | |/ _ \ '__| |
||
| 75 | | |__| | __/ |_) | | (_) | |_| | __/ | |
||
| 76 | |_____/ \___| .__/|_|\___/ \__, |\___|_| |
||
| 77 | | | __/ | |
||
| 78 | |_| |___/ |
||
| 79 | "); |
||
| 80 | |||
| 81 | $io->text([ |
||
| 82 | 'Welcome to the Deployer config generator.', |
||
| 83 | 'This utility will walk you through creating a deploy.php file.', |
||
| 84 | '', |
||
| 85 | 'Press ^C at any time to quit.', |
||
| 86 | ]); |
||
| 87 | |||
| 88 | // Yes? |
||
| 89 | $io->confirm('Continue?'); |
||
| 90 | |||
| 91 | // Template |
||
| 92 | $recipes = $initializer->getRecipes(); |
||
| 93 | $template = $io->choice('Select project template', $recipes, 'common'); |
||
| 94 | |||
| 95 | // Repo |
||
| 96 | $default = false; |
||
| 97 | try { |
||
| 98 | $process = Process::fromShellCommandline('git remote get-url origin'); |
||
| 99 | $default = $process->mustRun()->getOutput(); |
||
| 100 | $default = trim($default); |
||
| 101 | } catch (RuntimeException $e) { |
||
| 102 | } |
||
| 103 | $repository = $io->ask('Repository', $default); |
||
| 104 | |||
| 105 | // Repo |
||
| 106 | $default = false; |
||
| 107 | try { |
||
| 108 | $process = Process::fromShellCommandline('basename "$PWD"'); |
||
| 109 | $default = $process->mustRun()->getOutput(); |
||
| 110 | $default = trim($default); |
||
| 111 | } catch (RuntimeException $e) { |
||
| 112 | } |
||
| 113 | $project = $io->ask('Project name', $default); |
||
| 114 | |||
| 115 | // Hosts |
||
| 116 | $hosts = explode(',', $io->ask('Hosts (comma separated)', 'deployer.org')); |
||
| 117 | |||
| 118 | // Privacy |
||
| 119 | $io->text(<<<TEXT |
||
| 120 | <info>Contribute to the Deployer Development</info> |
||
| 121 | |||
| 122 | Help development and improve features of Deployer by |
||
| 123 | an optional usage data collection program. This program |
||
| 124 | collects anonymous usage data and sends it to Deployer. |
||
| 125 | The data is used in Deployer development to get reliable |
||
| 126 | statistics on which features are used (or not used). The |
||
| 127 | information is not traceable to any individual or |
||
| 128 | organization. Participation is voluntary, and you can |
||
| 129 | change your mind at any time. |
||
| 130 | |||
| 131 | Anonymous usage data contains Deployer version, PHP version, |
||
| 132 | OS type, name of the command being executed and whether |
||
| 133 | it was successful or not, count of hosts and anonymized |
||
| 134 | project hash. This program will not affect the performance |
||
| 135 | of Deployer as the data is insignificant and transmitted |
||
| 136 | in a separate process. |
||
| 137 | |||
| 138 | We appreciate your involvement! |
||
| 139 | TEXT); |
||
| 140 | |||
| 141 | $allow = $io->confirm('Do you confirm?'); |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | file_put_contents($filepath, $initializer->getTemplate($template, $project, $repository, $hosts, $allow)); |
||
| 146 | |||
| 147 | $output->writeln(sprintf( |
||
| 148 | '<info>Successfully created</info> <comment>%s</comment>', |
||
| 149 | $filepath |
||
| 150 | )); |
||
| 151 | |||
| 152 | return 0; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Create a initializer system |
||
| 157 | * |
||
| 158 | * @return Initializer |
||
| 159 | */ |
||
| 160 | private function createInitializer() |
||
| 161 | { |
||
| 162 | $initializer = new Initializer(); |
||
| 163 | |||
| 164 | $initializer->addTemplate('Common', new CommonTemplate()); |
||
| 165 | $initializer->addTemplate('Laravel', new LaravelTemplate()); |
||
| 166 | $initializer->addTemplate('Symfony', new SymfonyTemplate()); |
||
| 167 | $initializer->addTemplate('Yii', new YiiTemplate()); |
||
| 168 | $initializer->addTemplate('Yii2 Basic App', new Yii2BasicAppTemplate()); |
||
| 169 | $initializer->addTemplate('Yii2 Advanced App', new Yii2AdvancedAppTemplate()); |
||
| 170 | $initializer->addTemplate('Zend Framework', new ZendTemplate()); |
||
| 171 | $initializer->addTemplate('CakePHP', new CakeTemplate()); |
||
| 172 | $initializer->addTemplate('CodeIgniter', new CodeIgniterTemplate()); |
||
| 173 | $initializer->addTemplate('Drupal', new DrupalTemplate()); |
||
| 174 | $initializer->addTemplate('TYPO3', new Typo3Template()); |
||
| 175 | |||
| 176 | return $initializer; |
||
|
|
|||
| 177 | } |
||
| 178 | } |
||
| 179 |