AbstractPersistentDiscoveryTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 163
Duplicated Lines 58.9 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 6
dl 96
loc 163
ccs 113
cts 113
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testAddBindingKeepsStoredBindings() 0 11 1
A testAddBindingInitializesLoadedBindings() 0 23 1
A testRemoveBindingsDoesNotInitializeLoadedBindings() 16 16 1
A testRemoveBindingsWithTypeDoesNotInitializeLoadedBindings() 16 16 1
A testRemoveBindingsWithTypeAndParameterWorksOnLoadedDiscovery() 0 23 1
A testRemoveBindingTypeDoesNotInitializeLoadedBindings() 16 16 1
B testFindBindingsInitializesLoadedBindings() 24 24 1
B testGetBindingsInitializesLoadedBindings() 24 24 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 puli/discovery package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Discovery\Test;
13
14
use Puli\Discovery\Api\Type\BindingParameter;
15
use Puli\Discovery\Api\Type\BindingType;
16
use Puli\Discovery\Test\Fixtures\Foo;
17
use Puli\Discovery\Test\Fixtures\StringBinding;
18
use Webmozart\Expression\Expr;
19
20
/**
21
 * @since  1.0
22
 *
23
 * @author Bernhard Schussek <[email protected]>
24
 */
25
abstract class AbstractPersistentDiscoveryTest extends AbstractEditableDiscoveryTest
26
{
27 2
    public function testAddBindingKeepsStoredBindings()
28
    {
29 2
        $discovery = $this->createDiscovery();
30 2
        $discovery->addBindingType(new BindingType(Foo::clazz, self::STRING_BINDING));
31 2
        $discovery->addBinding($binding1 = new StringBinding('string1', Foo::clazz));
32
33 2
        $discovery = $this->loadDiscoveryFromStorage($discovery);
34 2
        $discovery->addBinding($binding2 = new StringBinding('string2', Foo::clazz));
35
36 2
        $this->assertEquals(array($binding1, $binding2), $discovery->getBindings());
37 2
    }
38
39 1
    public function testAddBindingInitializesLoadedBindings()
40
    {
41 1
        $binding1 = new StringBinding('string1', Foo::clazz);
42 1
        $binding2 = new StringBinding('string2', Foo::clazz);
43
44 1
        $this->initializer->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Puli\Discovery\Api\Bindi...izer\BindingInitializer.

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...
45 1
            ->method('acceptsBinding')
46 1
            ->willReturn(true);
47
48 1
        $this->initializer->expects($this->exactly(2))
49 1
            ->method('initializeBinding')
50 1
            ->withConsecutive(
51 1
                array($binding1),
52 1
                array($binding2)
53
            );
54
55 1
        $discovery = $this->createDiscovery();
56 1
        $discovery->addBindingType(new BindingType(Foo::clazz, self::STRING_BINDING));
57 1
        $discovery->addBinding($binding1);
58
59 1
        $discovery = $this->loadDiscoveryFromStorage($discovery, array($this->initializer));
0 ignored issues
show
Documentation introduced by
array($this->initializer) is of type array<integer,object<PHP...\BindingInitializer>"}>, but the function expects a array<integer,object<Pul...er\BindingInitializer>>.

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...
60 1
        $discovery->addBinding($binding2);
61 1
    }
62
63 2 View Code Duplication
    public function testRemoveBindingsDoesNotInitializeLoadedBindings()
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 2
        $this->initializer->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Puli\Discovery\Api\Bindi...izer\BindingInitializer.

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...
66 2
            ->method('acceptsBinding');
67
68 2
        $this->initializer->expects($this->never())
69 2
            ->method('initializeBinding');
70
71 2
        $discovery = $this->createDiscovery();
72 2
        $discovery->addBindingType(new BindingType(Foo::clazz, self::STRING_BINDING));
73 2
        $discovery->addBinding(new StringBinding('string1', Foo::clazz));
74 2
        $discovery->addBinding(new StringBinding('string2', Foo::clazz));
75
76 2
        $discovery = $this->loadDiscoveryFromStorage($discovery, array($this->initializer));
0 ignored issues
show
Documentation introduced by
array($this->initializer) is of type array<integer,object<PHP...\BindingInitializer>"}>, but the function expects a array<integer,object<Pul...er\BindingInitializer>>.

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...
77 2
        $discovery->removeBindings();
78 2
    }
79
80 2 View Code Duplication
    public function testRemoveBindingsWithTypeDoesNotInitializeLoadedBindings()
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...
81
    {
82 2
        $this->initializer->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Puli\Discovery\Api\Bindi...izer\BindingInitializer.

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...
83 2
            ->method('acceptsBinding');
84
85 2
        $this->initializer->expects($this->never())
86 2
            ->method('initializeBinding');
87
88 2
        $discovery = $this->createDiscovery();
89 2
        $discovery->addBindingType(new BindingType(Foo::clazz, self::STRING_BINDING));
90 2
        $discovery->addBinding(new StringBinding('string1', Foo::clazz));
91 2
        $discovery->addBinding(new StringBinding('string2', Foo::clazz));
92
93 2
        $discovery = $this->loadDiscoveryFromStorage($discovery, array($this->initializer));
0 ignored issues
show
Documentation introduced by
array($this->initializer) is of type array<integer,object<PHP...\BindingInitializer>"}>, but the function expects a array<integer,object<Pul...er\BindingInitializer>>.

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...
94 2
        $discovery->removeBindings(Foo::clazz);
95 2
    }
96
97 2
    public function testRemoveBindingsWithTypeAndParameterWorksOnLoadedDiscovery()
98
    {
99 2
        $binding1 = new StringBinding('string1', Foo::clazz, array('param2' => 'bar'));
100 2
        $binding2 = new StringBinding('string2', Foo::clazz);
101 2
        $binding3 = new StringBinding('string3', Foo::clazz, array('param1' => 'bar'));
102
103 2
        $discovery = $this->createDiscovery();
104 2
        $discovery->addBindingType(new BindingType(Foo::clazz, self::STRING_BINDING, array(
105 2
            new BindingParameter('param1', BindingParameter::OPTIONAL, 'foo'),
106 2
            new BindingParameter('param2'),
107
        )));
108 2
        $discovery->addBinding($binding1);
109 2
        $discovery->addBinding($binding2);
110 2
        $discovery->addBinding($binding3);
111
112 2
        $discovery = $this->loadDiscoveryFromStorage($discovery);
113
114
        // Bindings need to be initialized for this to work
115 2
        $discovery->removeBindings(Foo::clazz, Expr::method('getParameterValue', 'param1', Expr::same('foo')));
116
117 2
        $this->assertEquals(array($binding3), $discovery->findBindings(Foo::clazz));
118 2
        $this->assertEquals(array($binding3), $discovery->getBindings());
119 2
    }
120
121 2 View Code Duplication
    public function testRemoveBindingTypeDoesNotInitializeLoadedBindings()
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...
122
    {
123 2
        $this->initializer->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Puli\Discovery\Api\Bindi...izer\BindingInitializer.

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...
124 2
            ->method('acceptsBinding');
125
126 2
        $this->initializer->expects($this->never())
127 2
            ->method('initializeBinding');
128
129 2
        $discovery = $this->createDiscovery();
130 2
        $discovery->addBindingType(new BindingType(Foo::clazz, self::STRING_BINDING));
131 2
        $discovery->addBinding(new StringBinding('string1', Foo::clazz));
132 2
        $discovery->addBinding(new StringBinding('string2', Foo::clazz));
133
134 2
        $discovery = $this->loadDiscoveryFromStorage($discovery, array($this->initializer));
0 ignored issues
show
Documentation introduced by
array($this->initializer) is of type array<integer,object<PHP...\BindingInitializer>"}>, but the function expects a array<integer,object<Pul...er\BindingInitializer>>.

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...
135 2
        $discovery->removeBindingType(Foo::clazz);
136 2
    }
137
138 1 View Code Duplication
    public function testFindBindingsInitializesLoadedBindings()
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...
139
    {
140 1
        $binding1 = new StringBinding('string1', Foo::clazz);
141 1
        $binding2 = new StringBinding('string2', Foo::clazz);
142
143 1
        $this->initializer->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Puli\Discovery\Api\Bindi...izer\BindingInitializer.

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...
144 1
            ->method('acceptsBinding')
145 1
            ->willReturn(true);
146
147 1
        $this->initializer->expects($this->exactly(2))
148 1
            ->method('initializeBinding')
149 1
            ->withConsecutive(
150 1
                array($binding1),
151 1
                array($binding2)
152
            );
153
154 1
        $discovery = $this->createDiscovery();
155 1
        $discovery->addBindingType(new BindingType(Foo::clazz, self::STRING_BINDING));
156 1
        $discovery->addBinding($binding1);
157 1
        $discovery->addBinding($binding2);
158
159 1
        $discovery = $this->loadDiscoveryFromStorage($discovery, array($this->initializer));
0 ignored issues
show
Documentation introduced by
array($this->initializer) is of type array<integer,object<PHP...\BindingInitializer>"}>, but the function expects a array<integer,object<Pul...er\BindingInitializer>>.

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...
160 1
        $discovery->findBindings(Foo::clazz);
161 1
    }
162
163 1 View Code Duplication
    public function testGetBindingsInitializesLoadedBindings()
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...
164
    {
165 1
        $binding1 = new StringBinding('string1', Foo::clazz);
166 1
        $binding2 = new StringBinding('string2', Foo::clazz);
167
168 1
        $this->initializer->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Puli\Discovery\Api\Bindi...izer\BindingInitializer.

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...
169 1
            ->method('acceptsBinding')
170 1
            ->willReturn(true);
171
172 1
        $this->initializer->expects($this->exactly(2))
173 1
            ->method('initializeBinding')
174 1
            ->withConsecutive(
175 1
                array($binding1),
176 1
                array($binding2)
177
            );
178
179 1
        $discovery = $this->createDiscovery();
180 1
        $discovery->addBindingType(new BindingType(Foo::clazz, self::STRING_BINDING));
181 1
        $discovery->addBinding($binding1);
182 1
        $discovery->addBinding($binding2);
183
184 1
        $discovery = $this->loadDiscoveryFromStorage($discovery, array($this->initializer));
0 ignored issues
show
Documentation introduced by
array($this->initializer) is of type array<integer,object<PHP...\BindingInitializer>"}>, but the function expects a array<integer,object<Pul...er\BindingInitializer>>.

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...
185 1
        $discovery->getBindings();
186 1
    }
187
}
188