Completed
Push — master ( 4cc961...289825 )
by Andrii
03:30
created

PhpunitController   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 145
Duplicated Lines 13.79 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 30
c 3
b 0
f 0
lcom 2
cbo 3
dl 20
loc 145
ccs 0
cts 76
cp 0
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfiguration() 0 4 1
A getBootstrapFile() 0 12 2
A actionBootstrap() 0 6 2
A actionMake() 0 4 1
B actionRun() 0 15 6
A getVersion() 0 10 2
A actionGenfake() 10 10 3
A genFake() 0 9 1
A actionGentest() 10 10 3
A genSkel() 0 7 1
A buildNamespace() 0 4 2
A buildTestNamespace() 0 4 1
A buildClass() 0 4 1
A buildTestClass() 0 4 1
A buildPath() 0 4 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
/*
4
 * PHPUnit plugin for HiDev
5
 *
6
 * @link      https://github.com/hiqdev/hidev-phpunit
7
 * @package   hidev-phpunit
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\phpunit\controllers;
13
14
use hidev\base\File;
15
use hidev\handlers\BaseHandler;
16
use Yii;
17
18
/**
19
 * Goal for Phpunit.
20
 */
21
class PhpunitController extends \hidev\controllers\CommonController
22
{
23
    protected $_before = ['phpunit/bootstrap', 'phpunit.xml.dist'];
24
25
    protected $_version;
26
27
    protected $_bootstrapFile;
28
29
    public $force;
30
    public $colors;
31
    public $coverageText;
32
    public $coverageClover;
33
34
    public function getConfiguration()
35
    {
36
        return $this->getGoal('phpunit.xml.dist');
0 ignored issues
show
Documentation Bug introduced by
The method getGoal does not exist on object<hidev\phpunit\con...lers\PhpunitController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
37
    }
38
39
    public function getBootstrapFile()
40
    {
41
        if ($this->_bootstrapFile === null) {
42
            $this->_bootstrapFile = Yii::createObject([
43
                'class'     => File::class,
44
                'template'  => 'phpunit/bootstrap.twig',
45
                'path'      => 'tests/_bootstrap.php',
46
            ]);
47
        }
48
49
        return $this->_bootstrapFile;
50
    }
51
52
    public function actionBootstrap()
53
    {
54
        if (!$this->getBootstrapFile()->exists()) {
55
            $this->getBootstrapFile()->save();
56
        }
57
    }
58
59
    public function actionMake()
60
    {
61
        return $this->actionRun();
62
    }
63
64
    public function actionRun()
65
    {
66
        $args = [];
67
        if ($this->coverageText) {
68
            $args[] = '--coverage-text';
69
        }
70
        if ($this->coverageClover) {
71
            $args[] = '--coverage-clover=' . (is_string($this->coverageClover) ? $this->coverageClover : 'coverage.clover');
72
        }
73
        if ($this->colors) {
74
            $args[] = '--colors' . (version_compare($this->getVersion(), '4.7.0', '>=') ? '=' . $this->colors : '');
75
        }
76
77
        return $this->passthru('phpunit', $args);
0 ignored issues
show
Documentation introduced by
$args is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
    }
79
80
    public function getVersion()
81
    {
82
        if ($this->_version === null) {
83
            $lines = $this->exec('phpunit', ['--version']);
0 ignored issues
show
Documentation introduced by
array('--version') is of type array<integer,string,{"0":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
            $a = explode(' ', $lines[0], 3)[1];
85
            $this->_version = $a;
86
        }
87
88
        return $this->_version;
89
    }
90
91 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...
92
    {
93
        $path = $this->buildFakePath($file);
94
        if (!$this->force && file_exists($path)) {
95
            Yii::warning("already exists: $path");
96
            return 1;
97
        }
98
99
        return $this->genFake($file, $path);
100
    }
101
102
    public function genFake($file, $path)
103
    {
104
        $text = $this->getView()->render('phpunit/fake.twig', [
105
            'class'         => $this->buildClass($file),
106
            'namespace'     => $this->buildTestNamespace(),
107
            'name'          => $file,
108
        ]);
109
        BaseHandler::write($path, $text);
110
    }
111
112 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...
113
    {
114
        $path = $this->buildTestPath($file);
115
        if (!$this->force && file_exists($path)) {
116
            Yii::warning("already exists: $path");
117
            return 1;
118
        }
119
120
        return $this->genSkel($file);
121
    }
122
123
    public function genSkel($file)
124
    {
125
        return $this->passthru('phpunit-skelgen', [
0 ignored issues
show
Documentation introduced by
array('generate-test', '...->buildTestPath($file)) is of type array<integer,string,{"0..."string","7":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
126
            'generate-test', '--bootstrap', 'tests/_bootstrap.php', '--',
127
            $this->buildClass($file), $this->buildPath($file), $this->buildTestClass($file), $this->buildTestPath($file),
128
        ]);
129
    }
130
131
    public function buildNamespace($dir = '')
132
    {
133
        return $this->package->namespace . ($dir ? '\\' . strtr($dir, '/', '\\') : '');
0 ignored issues
show
Documentation introduced by
The property package does not exist on object<hidev\phpunit\con...lers\PhpunitController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
134
    }
135
136
    public function buildTestNamespace($dir = 'tests\\unit')
137
    {
138
        return $this->buildNamespace($dir);
139
    }
140
141
    public function buildClass($file, $dir = '')
142
    {
143
        return $this->buildNamespace($dir) . '\\' . strtr($file, '/', '\\');
144
    }
145
146
    public function buildTestClass($file, $dir = 'tests\\unit')
147
    {
148
        return $this->buildClass($file, $dir);
149
    }
150
151
    public function buildPath($file, $dir = 'src', $prefix = '', $postfix = '')
152
    {
153
        return $dir . DIRECTORY_SEPARATOR . $prefix . $file . $postfix . '.php';
154
    }
155
156
    public function buildTestPath($file, $dir = 'tests/unit', $prefix = '', $postfix = 'Test')
157
    {
158
        return $this->buildPath($file, $dir, $prefix, $postfix);
159
    }
160
161
    public function buildFakePath($file, $dir = 'tests/unit', $prefix = 'Fake', $postfix = '')
162
    {
163
        return $this->buildPath($file, $dir, $prefix, $postfix);
164
    }
165
}
166