PhpunitController   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 125
Duplicated Lines 16 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 3
dl 20
loc 125
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getComponent() 0 4 1
A actionIndex() 0 6 1
A doRun() 0 6 1
A actionGenfake() 10 11 3
A genFake() 0 9 1
A actionGentest() 10 11 3
A prepareFile() 0 4 2
A genSkel() 0 13 2
A buildNamespace() 0 4 2
A buildTestNamespace() 0 4 1
A buildClass() 0 4 1
A buildTestClass() 0 4 1
A buildPath() 0 7 1
A buildTestPath() 0 4 1
A buildFakePath() 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
 * HiDev plugin for PHPUnit
4
 *
5
 * @link      https://github.com/hiqdev/hidev-phpunit
6
 * @package   hidev-phpunit
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\phpunit\console;
12
13
use hidev\handlers\BaseHandler;
14
use Yii;
15
16
/**
17
 * PHPunit.
18
 */
19
class PhpunitController extends \hidev\base\Controller
20
{
21
    protected $_before = ['phpunit.xml.dist'];
22
23
    public $force;
24
25
    public function getComponent()
26
    {
27
        return $this->take('phpunit');
28
    }
29
30
    /**
31
     * Generates `tests/_bootstrap.php` and runs tests.
32
     */
33
    public function actionIndex()
34
    {
35
        $this->getComponent()->touchBootstrap();
36
37
        return $this->doRun();
38
    }
39
40
    protected function doRun()
41
    {
42
        $args = $this->getComponent()->prepareArgs();
43
44
        return $this->passthru('phpunit', $args);
45
    }
46
47
    /**
48
     * Generates skeleton class for fake.
49
     */
50 View Code Duplication
    public function actionGenfake($file)
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...
51
    {
52
        $path = $this->buildFakePath($file);
53
        if (!$this->force && file_exists($path)) {
54
            Yii::warning("already exists: $path");
55
56
            return 1;
57
        }
58
59
        return $this->genFake($file, $path);
60
    }
61
62
    protected function genFake($file, $path)
63
    {
64
        $text = $this->getView()->render('phpunit/fake.twig', [
65
            'class'         => $this->buildClass($file),
66
            'namespace'     => $this->buildTestNamespace(),
67
            'name'          => $file,
68
        ]);
69
        BaseHandler::write($path, $text);
70
    }
71
72
    /**
73
     * Generates skeleton class for test.
74
     */
75 View Code Duplication
    public function actionGentest($file)
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...
76
    {
77
        $path = $this->buildTestPath($file);
78
        if (!$this->force && file_exists($path)) {
79
            Yii::warning("already exists: $path");
80
81
            return 1;
82
        }
83
84
        return $this->genSkel($file);
85
    }
86
87
    protected static function prepareFile($file)
88
    {
89
        return substr($file, -4) === '.php' ? substr($file, 0, -4) : $file;
90
    }
91
92
    protected function genSkel($file)
93
    {
94
        $destPath = $this->buildTestPath($file);
95
        $dir = dirname($destPath);
96
        if (!file_exists($dir)) {
97
            mkdir($dir, 0777, true);
98
        }
99
100
        return $this->passthru('phpunit-skelgen', [
101
            'generate-test', '--bootstrap', 'tests/_bootstrap.php', '--',
102
            $this->buildClass($file), $this->buildPath($file), $this->buildTestClass($file), $destPath,
103
        ]);
104
    }
105
106
    protected function buildNamespace($dir = '')
107
    {
108
        return $this->take('package')->namespace . ($dir ? '\\' . strtr($dir, '/', '\\') : '');
109
    }
110
111
    protected function buildTestNamespace($dir = 'tests\\unit')
112
    {
113
        return $this->buildNamespace($dir);
114
    }
115
116
    protected function buildClass($file, $dir = '', $postfix = '')
117
    {
118
        return $this->buildNamespace($dir) . '\\' . strtr(static::prepareFile($file), '/', '\\') . $postfix;
119
    }
120
121
    protected function buildTestClass($file, $dir = 'tests\\unit', $postfix = 'Test')
122
    {
123
        return $this->buildClass($file, $dir, $postfix);
124
    }
125
126
    protected function buildPath($file, $dir = 'src', $prefix = '', $postfix = '')
127
    {
128
        return $dir . DIRECTORY_SEPARATOR . $prefix . static::prepareFile($file) . $postfix . '.php';
129
130
        //## XXX getting absolute path, think if needed
131
        //return strncmp($path, '/', 1) === 0 ? $path : Yii::getAlias("@root/$path");
132
    }
133
134
    protected function buildTestPath($file, $dir = 'tests/unit', $prefix = '', $postfix = 'Test')
135
    {
136
        return $this->buildPath($file, $dir, $prefix, $postfix);
137
    }
138
139
    protected function buildFakePath($file, $dir = 'tests/unit', $prefix = 'Fake', $postfix = '')
140
    {
141
        return $this->buildPath($file, $dir, $prefix, $postfix);
142
    }
143
}
144