Completed
Push — master ( 38e886...df2920 )
by Oleg
04:55
created

RenderTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 13.79 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 58
wmc 5
lcom 1
cbo 7
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B getBuilder() 8 28 1
A getStub() 0 4 1
A makeTest() 0 10 1
A testBladeRender() 0 5 1
A testBasicRender() 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
namespace Malezha\Menu\Tests;
3
4
use Illuminate\Contracts\Container\Container;
5
use Malezha\Menu\Contracts\Attributes;
6
use Malezha\Menu\Contracts\Builder;
7
use Malezha\Menu\Contracts\Item;
8
use Malezha\Menu\Contracts\MenuRender;
9
use Malezha\Menu\Render\Basic;
10
use Malezha\Menu\Render\Blade;
11
12
class RenderTest extends TestCase
13
{
14
    protected function getBuilder()
15
    {
16
        /** @var Builder $builder */
17
        $builder = $this->app->make(Builder::class, [
18
            'name' => 'test',
19
            'activeAttributes' => $this->app->make(Attributes::class, ['attributes' => ['class' => 'active']]),
20
            'attributes' => $this->app->make(Attributes::class, ['attributes' => ['class' => 'menu']]),
21
        ]);
22
        $index = $builder->add('index', 'Index Page', url('/'));
23
        $index->getLink()->getAttributes()->push(['class' => 'menu-link']);
24
25
        $builder->group('orders', function(Item $item) {
26
            $item->getAttributes()->push(['class' => 'child-menu']);
27
28
            $link = $item->getLink();
29
            $link->setTitle('Orders');
30
            $link->setUrl('javascript:;');
31 View Code Duplication
        }, function(Builder $menu) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
32
            $menu->add('all', 'All', url('/orders/all'));
0 ignored issues
show
Bug introduced by
It seems like url('/orders/all') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Malezha\Menu\Contracts\Builder::add() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
33
            $menu->add('type_1', 'Type 1', url('/orders/1'), [], ['class' => 'text-color-red']);
0 ignored issues
show
Bug introduced by
It seems like url('/orders/1') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Malezha\Menu\Contracts\Builder::add() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
34
35
            $menu->add('type_2', 'Type 2', url('/orders/2'), [], [], function(Item $item) {
0 ignored issues
show
Bug introduced by
It seems like url('/orders/2') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Malezha\Menu\Contracts\Builder::add() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
36
                $item->getLink()->getAttributes()->push(['data-attribute' => 'value']);
37
            });
38
        });
39
40
        return $builder;
41
    }
42
43
    protected function getStub()
44
    {
45
        return $file = file_get_contents(__DIR__ . '/stub/menu.html');
0 ignored issues
show
Unused Code introduced by
$file 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...
46
    }
47
48
    protected function makeTest($render)
49
    {
50
        $this->app->instance('menu.render', $render);
51
        $this->app->alias('menu.render', MenuRender::class);
52
53
        $builder = $this->getBuilder();
54
        $stub = $this->getStub();
55
        $this->assertAttributeInstanceOf(get_class($render), 'viewFactory', $builder);
56
        $this->assertEquals($stub, $builder->render());
57
    }
58
59
    public function testBladeRender()
60
    {
61
        $this->makeTest(new Blade($this->app));
62
63
    }
64
65
    public function testBasicRender()
66
    {
67
        $this->makeTest(new Basic($this->app));
68
    }
69
}