ModuleTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 14
loc 70
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 19 1
A tearDown() 0 3 1
A testGetInstance() 0 6 1
A testGetCart() 0 5 1
A testCreateUrl() 0 7 1
A testOrderButton() 7 7 1
A testPaymentMethods() 7 7 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
 * Cart module for Yii2
5
 *
6
 * @link      https://github.com/hiqdev/yii2-cart
7
 * @package   yii2-cart
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\yii2\cart\tests\unit;
13
14
use hiqdev\yii2\cart\Module;
15
use hiqdev\yii2\cart\ShoppingCart;
16
use Yii;
17
use yii\helpers\Url;
18
use yii\web\Application;
19
20
/**
21
 * Module test suite.
22
 */
23
class ModuleTest extends \PHPUnit\Framework\TestCase
24
{
25
    /**
26
     * @var Module
27
     */
28
    protected $object;
29
30
    protected function setUp()
31
    {
32
        $application = new Application([
0 ignored issues
show
Unused Code introduced by
$application is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
33
            'id'       => 'mock',
34
            'basePath' => dirname(dirname(__DIR__)),
35
            'modules'  => [
36
                'cart' => [
37
                    'class' => Module::class,
38
                ],
39
            ],
40
            'components' => [
41
                'urlManager' => [
42
                    'enablePrettyUrl' => true,
43
                    'showScriptName'  => false,
44
                ],
45
            ],
46
        ]);
47
        $this->object = Yii::$app->getModule('cart');
48
    }
49
50
    protected function tearDown()
51
    {
52
    }
53
54
    public function testGetInstance()
55
    {
56
        $module = Module::getInstance();
57
        $this->assertInstanceOf(Module::class, $module);
58
        $this->assertSame($this->object, $module);
59
    }
60
61
    public function testGetCart()
62
    {
63
        $this->assertInstanceOf(ShoppingCart::class, $this->object->getCart());
64
        $this->assertSame(Yii::$app->getModule('cart')->get('cart'), $this->object->cart);
0 ignored issues
show
Documentation introduced by
The property cart does not exist on object<hiqdev\yii2\cart\Module>. 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...
65
    }
66
67
    public function testCreateUrl()
68
    {
69
        $this->assertStringEndsWith(Url::to('/cart/cart/index'),             $this->object->createUrl());
70
        $this->assertStringEndsWith(Url::to('/cart/cart/something'),         $this->object->createUrl('something'));
71
        $this->assertStringEndsWith(Url::to(['/cart/cart/order', 'a' => 2]), $this->object->createUrl(['order', 'a' => 2]));
72
        $this->assertStringEndsWith(Url::to(['/cart/test/order', 'a' => 2]), $this->object->createUrl(['test/order', 'a' => 2]));
73
    }
74
75
    protected $testString = 'a and b';
76
77 View Code Duplication
    public function testOrderButton()
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...
78
    {
79
        $this->object->setOrderButton($this->testString);
80
        $this->assertSame($this->testString, $this->object->getOrderButton());
81
        $this->object->setOrderButton(function () { return $this->testString; });
82
        $this->assertSame($this->testString, $this->object->getOrderButton());
83
    }
84
85 View Code Duplication
    public function testPaymentMethods()
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...
86
    {
87
        $this->object->setPaymentMethods($this->testString);
0 ignored issues
show
Documentation Bug introduced by
The method setPaymentMethods does not exist on object<hiqdev\yii2\cart\Module>? 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...
88
        $this->assertSame($this->testString, $this->object->getPaymentMethods());
89
        $this->object->setPaymentMethods(function () { return $this->testString; });
0 ignored issues
show
Documentation Bug introduced by
The method setPaymentMethods does not exist on object<hiqdev\yii2\cart\Module>? 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...
90
        $this->assertSame($this->testString, $this->object->getPaymentMethods());
91
    }
92
}
93