Completed
Pull Request — master (#40)
by Marko
445:50 queued 380:43
created

ResourceCompilerPassTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 85
Duplicated Lines 67.06 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 57
loc 85
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
B testTwigResource() 30 30 1
B testPhpResource() 27 27 1
A createContainerBuilderMock() 0 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
 * This file is part of the Ivory CKEditor package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\CKEditorBundle\Tests\DependencyInjection\Compiler;
13
14
use FOS\CKEditorBundle\DependencyInjection\Compiler\ResourceCompilerPass;
15
use FOS\CKEditorBundle\Tests\AbstractTestCase;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
18
/**
19
 * @author GeLo <[email protected]>
20
 */
21
class ResourceCompilerPassTest extends AbstractTestCase
22
{
23
    /**
24
     * @var ResourceCompilerPass
25
     */
26
    private $compilerPass;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function setUp()
32
    {
33
        $this->compilerPass = new ResourceCompilerPass();
34
    }
35
36 View Code Duplication
    public function testTwigResource()
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...
37
    {
38
        $containerBuilder = $this->createContainerBuilderMock();
39
        $containerBuilder
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Depend...ection\ContainerBuilder.

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...
40
            ->expects($this->exactly(2))
41
            ->method('hasParameter')
42
            ->will($this->returnValueMap([
43
                ['templating.helper.form.resources', false],
44
                [$parameter = 'twig.form.resources', true],
45
            ]));
46
47
        $containerBuilder
48
            ->expects($this->once())
49
            ->method('getParameter')
50
            ->with($this->identicalTo($parameter))
51
            ->will($this->returnValue([$template = 'layout.html.twig']));
52
53
        $containerBuilder
54
            ->expects($this->once())
55
            ->method('setParameter')
56
            ->with(
57
                $this->identicalTo($parameter),
58
                $this->identicalTo([
59
                    '@FOSCKEditor/Form/ckeditor_widget.html.twig',
60
                    $template,
61
                ])
62
            );
63
64
        $this->compilerPass->process($containerBuilder);
0 ignored issues
show
Bug introduced by
It seems like $containerBuilder defined by $this->createContainerBuilderMock() on line 38 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\CKEditorBundle\Depen...CompilerPass::process() does only seem to accept object<Symfony\Component...ction\ContainerBuilder>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
65
    }
66
67 View Code Duplication
    public function testPhpResource()
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...
68
    {
69
        $containerBuilder = $this->createContainerBuilderMock();
70
        $containerBuilder
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Depend...ection\ContainerBuilder.

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...
71
            ->expects($this->exactly(2))
72
            ->method('hasParameter')
73
            ->will($this->returnValueMap([
74
                [$parameter = 'templating.helper.form.resources', true],
75
                ['twig.form.resources', false],
76
            ]));
77
78
        $containerBuilder
79
            ->expects($this->once())
80
            ->method('getParameter')
81
            ->with($this->identicalTo($parameter))
82
            ->will($this->returnValue([$template = 'layout.html.php']));
83
84
        $containerBuilder
85
            ->expects($this->once())
86
            ->method('setParameter')
87
            ->with(
88
                $this->identicalTo($parameter),
89
                $this->identicalTo(['FOSCKEditorBundle:Form', $template])
90
            );
91
92
        $this->compilerPass->process($containerBuilder);
0 ignored issues
show
Bug introduced by
It seems like $containerBuilder defined by $this->createContainerBuilderMock() on line 69 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\CKEditorBundle\Depen...CompilerPass::process() does only seem to accept object<Symfony\Component...ction\ContainerBuilder>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
93
    }
94
95
    /**
96
     * @return ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject
97
     */
98
    private function createContainerBuilderMock()
99
    {
100
        return $this->getMockBuilder(ContainerBuilder::class)
101
            ->disableOriginalConstructor()
102
            ->setMethods(['hasParameter', 'getParameter', 'setParameter'])
103
            ->getMock();
104
    }
105
}
106