Completed
Push — master ( 54b8af...0621ed )
by Andrii
13:32
created

DumpController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 42.86 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 0
cbo 3
dl 24
loc 56
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A actionComponent() 12 12 3
A actionController() 12 12 3
A actionAlias() 0 13 3

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
 * Automation tool mixed with code generator for easier continuous development.
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\console;
12
13
use Symfony\Component\Yaml\Yaml;
14
use Yii;
15
16
/**
17
 * Dump available definitions: components, controllers, aliases.
18
 */
19
class DumpController extends \yii\console\Controller
20
{
21
    public $defaultAction = 'component';
22
23
    /**
24
     * Dump defined components.
25
     * @param string $name component name
26
     */
27 View Code Duplication
    public function actionComponent($name = null)
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...
28
    {
29
        $data = Yii::$app->getComponents();
30
        if ($name) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $name of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
31
            if (empty($data[$name])) {
32
                Yii::error("'$name' not defined");
33
                return;
34
            }
35
            $data = [$name => $data[$name]];
36
        }
37
        echo Yaml::dump($data, 4);
38
    }
39
40
    /**
41
     * Dump defined controllers.
42
     * @param string $name controller name
43
     */
44 View Code Duplication
    public function actionController($name = null)
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...
45
    {
46
        $data = Yii::$app->controllerMap;
47
        if ($name) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $name of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
48
            if (empty($data[$name])) {
49
                Yii::error("'$name' not defined");
50
                return;
51
            }
52
            $data = [$name => $data[$name]];
53
        }
54
        echo Yaml::dump($data, 4);
55
    }
56
57
    /**
58
     * Dump defined aliases.
59
     * @param string $alias alias to resolve
60
     */
61
    public function actionAlias($alias = null)
62
    {
63
        $data = Yii::$aliases;
64
        if ($alias) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $alias of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
65
            $dest = Yii::getAlias($alias, false);
66
            if (empty($dest)) {
67
                Yii::error("'$alias' not defined");
68
                return;
69
            }
70
            $data = [$alias => $dest];
71
        }
72
        echo Yaml::dump($data, 4);
73
    }
74
}
75