Completed
Pull Request — develop (#216)
by Tom
04:15
created

ScriptCommandTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 13.85 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 5
c 3
b 1
f 0
lcom 1
cbo 3
dl 9
loc 65
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B testExecute() 0 29 1
A getMagentoVersion() 0 4 1
A getMagentoEdition() 0 4 1
A getProductMetadata() 9 9 2

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
namespace N98\Magento\Command;
4
5
use Symfony\Component\Console\Tester\CommandTester;
6
use N98\Magento\Command\PHPUnit\TestCase;
7
8
class ScriptCommandTest extends TestCase
9
{    
0 ignored issues
show
Coding Style introduced by
The opening class brace should be on a newline by itself.
Loading history...
10
    /** @var null|\Magento\Framework\App\ProductMetadata  */
11
    protected $productMetadata = null;
12
    
13
    public function testExecute()
14
    {
15
        $application = $this->getApplication();
16
        $application->add(new ScriptCommand());
0 ignored issues
show
Bug introduced by
The method add does only exist in N98\Magento\Application, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
17
        $application->setAutoExit(false);
0 ignored issues
show
Bug introduced by
The method setAutoExit does only exist in N98\Magento\Application, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
18
        $command = $this->getApplication()->find('script');
0 ignored issues
show
Bug introduced by
The method find does only exist in N98\Magento\Application, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
19
20
        $commandTester = new CommandTester($command);
21
        $commandTester->execute(
22
            array(
23
                'command'   => $command->getName(),
24
                'filename'  => __DIR__ . '/_files/test.mr',
25
            )
26
        );
27
        
28
        // Check pre defined vars
29
        $this->assertContains('magento.root: ' . $this->getApplication()->getMagentoRootFolder(), $commandTester->getDisplay());
0 ignored issues
show
Bug introduced by
The method getMagentoRootFolder does only exist in N98\Magento\Application, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
30
        $this->assertContains('magento.version: ' . $this->getMagentoVersion(), $commandTester->getDisplay());
31
        $this->assertContains('magento.edition: ' . $this->getMagentoEdition(), $commandTester->getDisplay());
32
        
33
        $this->assertContains('magerun.version: ' . $this->getApplication()->getVersion(), $commandTester->getDisplay());
0 ignored issues
show
Bug introduced by
The method getVersion does only exist in N98\Magento\Application, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
34
35
        $this->assertContains('code', $commandTester->getDisplay());
36
        $this->assertContains('foo.sql', $commandTester->getDisplay());
37
        $this->assertContains('BAR: foo.sql.gz', $commandTester->getDisplay());
38
        $this->assertContains('Magento Websites', $commandTester->getDisplay());
39
        $this->assertContains('web/secure/base_url', $commandTester->getDisplay());
40
        $this->assertContains('web/seo/use_rewrites => 1', $commandTester->getDisplay());
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    protected function getMagentoVersion()
47
    {
48
        return $this->getProductMetadata()->getVersion();
49
    }
50
51
    /**
52
     *
53
     * @return mixed
54
     */
55
    protected function getMagentoEdition()
56
    {
57
        return $this->getProductMetadata()->getEdition();
58
    }
59
60
    /**
61
     * @return \Magento\Framework\App\ProductMetadata
62
     */
63 View Code Duplication
    protected function getProductMetadata()
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...
64
    {
65
        if(is_null($this->productMetadata)) {
66
            $objectManager         = $this->getApplication()->getObjectManager();
0 ignored issues
show
Bug introduced by
The method getObjectManager does only exist in N98\Magento\Application, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
67
            $this->productMetadata = $objectManager->get('\Magento\Framework\App\ProductMetadata');
68
        }
69
        
70
        return $this->productMetadata;
71
    }
72
}
73