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 |             ->setName('init') | 
            ||
| 38 |             ->setDescription('Initialize deployer in your project') | 
            ||
| 39 |             ->addOption('template', 't', InputOption::VALUE_OPTIONAL, 'The template of you project') | 
            ||
| 40 |             ->addOption('filepath', null, InputOption::VALUE_OPTIONAL, 'The file path (default "deploy.php")', 'deploy.php'); | 
            ||
| 41 | }  | 
            ||
| 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 | 12 | $output->writeln([  | 
            |
| 53 | $formatter->formatBlock(sprintf(  | 
            ||
| 54 | 12 | 'The file "%s" already exist.',  | 
            |
| 55 | 12 | $filepath,  | 
            |
| 56 | ), 'bg=red;fg=white', true),  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 57 | 12 | ]);  | 
            |
| 58 | 12 | return 2;  | 
            |
| 59 | }  | 
            ||
| 60 | |||
| 61 | $project = 'my_project';  | 
            ||
| 62 | $repository = '';  | 
            ||
| 63 | 12 | $hosts = [];  | 
            |
| 64 | $allow = true;  | 
            ||
| 65 | |||
| 66 | 12 |         if ($template === null) { | 
            |
| 67 | 12 | $io = new SymfonyStyle($input, $output);  | 
            |
| 68 | 12 | ||
| 69 | 12 | // Welcome message  | 
            |
| 70 | 12 |             $output->writeln(" | 
            |
| 71 | 12 | _____ _  | 
            |
| 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([  | 
            ||
| 120 | 'Contribute to the Deployer Development',  | 
            ||
| 121 | '',  | 
            ||
| 122 | 'In order to help development and improve the features in Deployer,',  | 
            ||
| 123 | 'Deployer has a setting for usage data collection. This function',  | 
            ||
| 124 | 'collects anonymous usage data and sends it to Deployer. The data is',  | 
            ||
| 125 | 'used in Deployer development to get reliable statistics on which',  | 
            ||
| 126 | 'features are used (or not used). The information is not traceable',  | 
            ||
| 127 | 'to any individual or organization. Participation is voluntary,',  | 
            ||
| 128 | 'and you can change your mind at any time.',  | 
            ||
| 129 | '',  | 
            ||
| 130 | 'Anonymous usage data contains Deployer version, php version, os type,',  | 
            ||
| 131 | 'name of the command being executed and whether it was successful or not,',  | 
            ||
| 132 | 'exception class name, count of hosts and anonymized project hash.',  | 
            ||
| 133 | '',  | 
            ||
| 134 | 'This function will not affect the performance of Deployer as',  | 
            ||
| 135 | 'the data is insignificant and transmitted in a separate process.',  | 
            ||
| 136 | '',  | 
            ||
| 137 | 'We appreciate your involvement!',  | 
            ||
| 138 | ]);  | 
            ||
| 139 | |||
| 140 |             $allow = $io->confirm('Do you confirm?'); | 
            ||
| 141 | }  | 
            ||
| 142 | |||
| 143 | |||
| 144 | file_put_contents($filepath, $initializer->getTemplate($template, $project, $repository, $hosts, $allow));  | 
            ||
| 145 | |||
| 146 | $output->writeln(sprintf(  | 
            ||
| 147 | '<info>Successfully created</info> <comment>%s</comment>',  | 
            ||
| 148 | $filepath  | 
            ||
| 149 | ));  | 
            ||
| 150 | |||
| 151 | return 0;  | 
            ||
| 152 | }  | 
            ||
| 153 | |||
| 154 | /**  | 
            ||
| 155 | * Create a initializer system  | 
            ||
| 156 | *  | 
            ||
| 157 | * @return Initializer  | 
            ||
| 158 | */  | 
            ||
| 159 | private function createInitializer()  | 
            ||
| 160 |     { | 
            ||
| 161 | $initializer = new Initializer();  | 
            ||
| 162 | |||
| 163 |         $initializer->addTemplate('Common', new CommonTemplate()); | 
            ||
| 164 |         $initializer->addTemplate('Laravel', new LaravelTemplate()); | 
            ||
| 165 |         $initializer->addTemplate('Symfony', new SymfonyTemplate()); | 
            ||
| 166 | 12 |         $initializer->addTemplate('Yii', new YiiTemplate()); | 
            |
| 167 |         $initializer->addTemplate('Yii2 Basic App', new Yii2BasicAppTemplate()); | 
            ||
| 168 | 12 |         $initializer->addTemplate('Yii2 Advanced App', new Yii2AdvancedAppTemplate()); | 
            |
| 169 |         $initializer->addTemplate('Zend Framework', new ZendTemplate()); | 
            ||
| 170 | 12 |         $initializer->addTemplate('CakePHP', new CakeTemplate()); | 
            |
| 171 | 12 |         $initializer->addTemplate('CodeIgniter', new CodeIgniterTemplate()); | 
            |
| 172 | 12 |         $initializer->addTemplate('Drupal', new DrupalTemplate()); | 
            |
| 173 | 12 |         $initializer->addTemplate('TYPO3', new Typo3Template()); | 
            |
| 174 | 12 | ||
| 175 | 12 | return $initializer;  | 
            |
| 176 | 12 | }  | 
            |
| 177 | }  | 
            ||
| 178 |