Completed
Push — master ( 1fa2c7...4160d6 )
by Mathieu
03:42
created

AppConfig   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 131
Duplicated Lines 6.87 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 3
Bugs 1 Features 2
Metric Value
wmc 13
c 3
b 1
f 2
lcom 2
cbo 2
dl 9
loc 131
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A set_routes() 0 5 1
A routes() 0 4 1
A set_routables() 0 5 1
A routables() 0 4 1
A set_modules() 0 5 1
A modules() 0 4 1
A set_languages() 0 5 1
A add_language() 0 5 1
A languages() 0 4 1
A set_translation() 9 9 3
A translation() 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
3
namespace Charcoal\App;
4
5
// Module `charcoal-config` dependencies
6
use \Charcoal\Config\AbstractConfig;
7
8
// Module `charcoal-core` dependencies
9
use \Charcoal\Translation\TranslationConfig;
10
11
// Intra-module (`charcoal-app`) dependencies
12
use \Charcoal\App\Language\Language;
13
14
/**
15
* Charcoal App configuration
16
*/
17
class AppConfig extends AbstractConfig
18
{
19
    /**
20
    * @var array $routes
21
    */
22
    private $routes = [];
23
24
    /**
25
    * @var array $routables
26
    */
27
    private $routables = [];
28
29
    /**
30
    * @var array $modules
31
    */
32
    private $modules = [];
33
34
    /**
35
    * @param array $routes
36
    * @return AppConfig Chainable
37
    */
38
    public function set_routes(array $routes)
39
    {
40
        $this->routes = $routes;
41
        return $this;
42
    }
43
44
    /**
45
    * @return array
46
    */
47
    public function routes()
48
    {
49
        return $this->routes;
50
    }
51
52
    /**
53
    * @param array $routes
0 ignored issues
show
Bug introduced by
There is no parameter named $routes. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
54
    * @return AppConfig Chainable
55
    */
56
    public function set_routables(array $routables)
57
    {
58
        $this->routables = $routables;
59
        return $this;
60
    }
61
62
    /**
63
    * @return array
64
    */
65
    public function routables()
66
    {
67
        return $this->routables;
68
    }
69
70
    /**
71
    * @param array $modules
72
    * @return AppConfig Chainable
73
    */
74
    public function set_modules(array $modules)
75
    {
76
        $this->modules = $modules;
77
        return $this;
78
    }
79
80
    /**
81
    * @return array
82
    */
83
    public function modules()
84
    {
85
        return $this->modules;
86
    }
87
88
    /**
89
    * Set the application's available languages
90
    *
91
    * @param  Language[] $lang
0 ignored issues
show
Bug introduced by
There is no parameter named $lang. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
92
    * @return self
93
    */
94
    public function set_languages(array $languages)
95
    {
96
        $this->languages = $languages;
0 ignored issues
show
Bug introduced by
The property languages does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
97
        return $this;
98
    }
99
100
    /**
101
    * Add or update an available language to the application
102
    *
103
    * @param  Language $lang
104
    * @return self
105
    */
106
    public function add_language(Language $lang)
107
    {
108
        $this->languages[$lang->ident()] = $lang;
109
        return $this;
110
    }
111
112
    /**
113
    * Get the application's list of available languages
114
    *
115
    * @return Language[]
116
    */
117
    public function languages()
118
    {
119
        return $this->languages;
120
    }
121
122
    /**
123
    * Set the application's global TranslationConfig
124
    *
125
    * @param  array|TranslationConfig $translation
126
    * @return self
127
    */
128 View Code Duplication
    public function set_translation($translation)
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...
129
    {
130
        if ($translation instanceof TranslationConfig) {
0 ignored issues
show
Bug introduced by
The class Charcoal\Translation\TranslationConfig does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
131
            $this->translation_config = $translation;
0 ignored issues
show
Bug introduced by
The property translation_config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
132
        } elseif (is_array($translation)) {
133
            $this->translation_config = new TranslationConfig($translation);
134
        }
135
        return $this;
136
    }
137
138
    /**
139
    * Get the application's global TranslationConfig
140
    *
141
    * @return TranslationConfig
142
    */
143
    public function translation()
144
    {
145
        return $this->translation_config;
146
    }
147
}
148