Passed
Push — master ( a5e8db...945884 )
by Danny
02:33
created

GlobalConfiguration   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 13.51 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 10
loc 74
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 18 2
A get() 10 10 2
A configSkeleton() 0 6 1
A exists() 0 4 1
A getConfigFile() 0 4 1

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
namespace PressCLI\Option;
3
4
use Symfony\Component\Console\Output\OutputInterface;
5
6
class GlobalConfiguration
7
{
8
    /**
9
     * Creates the configuration file.
10
     *
11
     * @param  OutputInterface $output
12
     */
13
    public static function create(OutputInterface $output)
14
    {
15
        $output->writeln("<info>Creating global configuration...</info>");
16
        $configFile = self::getConfigFile();
17
18
        if (self::exists()) {
19
            $output->writeln("<comment>Global configuration already exists at {$configFile}.</comment>");
20
21
            return;
22
        }
23
24
        // Add configuration from skeleton.
25
        $config = json_encode(self::configSkeleton(), JSON_PRETTY_PRINT);
26
        $config = stripslashes($config);
27
        file_put_contents($configFile, $config);
28
29
        $output->writeln("<info>Global configuration created at {$configFile}!</info>");
30
    }
31
32
    /**
33
     * Get the configuration.
34
     *
35
     * @return array
36
     */
37 View Code Duplication
    public static function get()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
38
    {
39
        if (!self::exists()) {
40
            return [];
41
        }
42
43
        $config = json_decode(file_get_contents(self::getConfigFile()), true);
44
45
        return $config;
46
    }
47
48
    /**
49
     * Configuration skeleton
50
     *
51
     * @return string
52
     */
53
    protected static function configSkeleton()
54
    {
55
        $config = presscli_config();
56
57
        return $config;
58
    }
59
60
    /**
61
     * Checks if the configuration exists.
62
     *
63
     * @return boolean
64
     */
65
    public static function exists()
66
    {
67
        return file_exists(self::getConfigFile());
68
    }
69
70
    /**
71
     * Retrieves the configuration file.
72
     *
73
     * @return string The path to the configuration file.
74
     */
75
    protected static function getConfigFile()
0 ignored issues
show
Coding Style introduced by
getConfigFile uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
76
    {
77
        return "{$_SERVER['HOME']}/.press-cli.json";
78
    }
79
}
80