Completed
Pull Request — master (#15)
by Toan
05:34
created

Path   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 12.05 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 10
loc 83
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setData() 10 10 2
A getPath() 0 14 4
A initDefaultPaths() 0 8 1
B getHomePath() 0 18 5

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
 * Copyright © 2017 Toan Nguyen. All rights reserved.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Gojira\Framework\App\Configuration;
10
11
use Gojira\Framework\Data\DataObject;
12
use Gojira\Application;
13
14
/**
15
 * Base class to work with path items
16
 *
17
 * @package Gojira\Framework\App\Configuration
18
 * @author  Toan Nguyen <[email protected]>
19
 */
20
class Path extends DataObject implements PathInterface
21
{
22
    /**
23
     * Overwrite data in the object.
24
     *
25
     * The $key parameter can be string or array.
26
     * If $key is string, the attribute value will be overwritten by $value
27
     *
28
     * If $key is an array, it will overwrite all the data in the object.
29
     *
30
     * @param string|array $key
31
     * @param mixed        $value
32
     *
33
     * @return $this
34
     */
35 View Code Duplication
    public function setData($key, $value = null)
1 ignored issue
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...
36
    {
37
        if ($key === (array)$key) {
38
            $this->_data = $key;
39
        } else {
40
            $this->_data[$key] = $value;
41
        }
42
43
        return $this;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getPath($key = null)
50
    {
51
        $basePath = $this->getHomePath() . self::DS . '.' . strtolower(Application::CODENAME) . self::DS;
52
53
        switch ($key) {
54
            case self::CONFIG_PATH:
55
                return $basePath . self::CONFIG_FILE;
56
            case self::CACHE_PATH:
57
                return $basePath . self::CACHE_FILE;
58
            case self::BASE_PATH:
59
            default:
60
                return $basePath;
61
        }
62
    }
63
64
    /**
65
     * Returns default paths for config item
66
     *
67
     * @return array
68
     */
69
    public function initDefaultPaths()
70
    {
71
        return [
72
            self::BASE_PATH => $this->getPath(),
73
            self::CONFIG_PATH => $this->getPath(self::CONFIG_PATH),
74
            self::CACHE_PATH => $this->getPath(self::CACHE_PATH)
75
        ];
76
    }
77
78
    /**
79
     * Returns the user's home directory
80
     *
81
     * @return null|string
82
     * @SuppressWarnings(PHPMD.Superglobals)
83
     */
84
    private function getHomePath()
85
    {
86
        // Cannot use $_SERVER superglobal since that's empty during UnitUnishTestCase
87
        // getenv('HOME') isn't set on Windows and generates a Notice.
88
        $home = getenv('HOME');
89
        if (!empty($home)) {
90
            // home should never end with a trailing slash.
91
            $home = rtrim($home, '/');
92
        } elseif (!empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) {
93
            // home on windows
94
            $home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'];
95
            // If HOMEPATH is a root directory the path can end with a slash. Make sure
96
            // that doesn't happen.
97
            $home = rtrim($home, '\\/');
98
        }
99
100
        return empty($home) ? null : $home;
101
    }
102
}
103