Completed
Push — master ( da9ea2...cfe8fe )
by Rougin
08:08
created

InstallWildfireCommand   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 52
Duplicated Lines 23.08 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 2
lcom 1
cbo 4
dl 12
loc 52
ccs 21
cts 21
cp 1
rs 10
c 5
b 2
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B execute() 12 37 2

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Rougin\Combustor\Commands;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
use Rougin\Combustor\Common\Tools;
9
use Rougin\Combustor\Common\Commands\InstallCommand;
10
11
/**
12
 * Install Wildfire Command
13
 *
14
 * Installs Wildfire for CodeIgniter
15
 * 
16
 * @package Combustor
17
 * @author  Rougin Royce Gutib <[email protected]>
18
 */
19
class InstallWildfireCommand extends InstallCommand
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $library = 'wildfire';
25
26
    /**
27
     * Executes the command.
28
     * 
29
     * @param \Symfony\Component\Console\Input\InputInterface   $input
30
     * @param \Symfony\Component\Console\Output\OutputInterface $output
31
     * @return object|\Symfony\Component\Console\Output\OutputInterface
32
     */
33 12
    protected function execute(InputInterface $input, OutputInterface $output)
34
    {
35
        // Adds Wildfire.php to the "libraries" directory
36 12
        $autoload = file_get_contents(APPPATH . 'config/autoload.php');
37 12
        $lines = explode(PHP_EOL, $autoload);
38
39 12
        $pattern = '/\$autoload\[\'libraries\'\] = array\((.*?)\)/';
40
41 12
        preg_match_all($pattern, $lines[60], $match);
42
43 12
        $libraries = explode(', ', end($match[1]));
44
45 12 View Code Duplication
        if ( ! in_array('\'wildfire\'', $libraries)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46 12
            array_push($libraries, '\'wildfire\'');
47
48 12
            $libraries = array_filter($libraries);
49
50 12
            $pattern = '/\$autoload\[\'libraries\'\] = array\([^)]*\);/';
51 12
            $replacement = '$autoload[\'libraries\'] = array(' . implode(', ', $libraries) . ');';
52
53 12
            $lines[60] = preg_replace($pattern, $replacement, $lines[60]);
54
55 12
            file_put_contents(APPPATH . 'config/autoload.php', implode(PHP_EOL, $lines));
56 12
        }
57
58 12
        $file = fopen(APPPATH . 'libraries/Wildfire.php', 'wb');
59 12
        $wildfire = $this->renderer->render('Libraries/Wildfire.template');
60
61 12
        file_put_contents(APPPATH . 'libraries/Wildfire.php', $wildfire);
62 12
        fclose($file);
63
64 12
        Tools::ignite();
65
66 12
        $message = 'Wildfire is now installed successfully!';
67
68 12
        return $output->writeln('<info>'.$message.'</info>');
69
    }
70
}
71