ConfiguredClientsStrategyTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 44.44 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetCandidates() 0 14 1
A testGetCandidatesEmpty() 0 10 1
A testGetCandidatesEmptyAsync() 12 12 1
A testGetCandidatesEmptySync() 12 12 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
declare(strict_types=1);
4
5
namespace Http\HttplugBundle\Tests\Unit\Discovery;
6
7
use Http\Client\HttpAsyncClient;
8
use Http\Client\HttpClient;
9
use Http\HttplugBundle\Discovery\ConfiguredClientsStrategy;
10
use PHPUnit\Framework\TestCase;
11
12
class ConfiguredClientsStrategyTest extends TestCase
13
{
14
    public function testGetCandidates(): void
15
    {
16
        $httpClient = $this->getMockBuilder(HttpClient::class)->getMock();
17
        $httpAsyncClient = $this->getMockBuilder(HttpAsyncClient::class)->getMock();
18
        $strategy = new ConfiguredClientsStrategy($httpClient, $httpAsyncClient);
0 ignored issues
show
Documentation introduced by
$httpClient is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a null|object<Http\Client\HttpClient>.

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...
Documentation introduced by
$httpAsyncClient is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a null|object<Http\Client\HttpAsyncClient>.

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...
19
20
        $candidates = $strategy::getCandidates(HttpClient::class);
21
        $candidate = array_shift($candidates);
22
        $this->assertEquals($httpClient, $candidate['class']());
23
24
        $candidates = $strategy::getCandidates(HttpAsyncClient::class);
25
        $candidate = array_shift($candidates);
26
        $this->assertEquals($httpAsyncClient, $candidate['class']());
27
    }
28
29
    public function testGetCandidatesEmpty(): void
30
    {
31
        $strategy = new ConfiguredClientsStrategy(null, null);
32
33
        $candidates = $strategy::getCandidates(HttpClient::class);
34
        $this->assertEquals([], $candidates);
35
36
        $candidates = $strategy::getCandidates(HttpAsyncClient::class);
37
        $this->assertEquals([], $candidates);
38
    }
39
40 View Code Duplication
    public function testGetCandidatesEmptyAsync(): void
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...
41
    {
42
        $httpClient = $this->getMockBuilder(HttpClient::class)->getMock();
43
        $strategy = new ConfiguredClientsStrategy($httpClient, null);
0 ignored issues
show
Documentation introduced by
$httpClient is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a null|object<Http\Client\HttpClient>.

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...
44
45
        $candidates = $strategy::getCandidates(HttpClient::class);
46
        $candidate = array_shift($candidates);
47
        $this->assertEquals($httpClient, $candidate['class']());
48
49
        $candidates = $strategy::getCandidates(HttpAsyncClient::class);
50
        $this->assertEquals([], $candidates);
51
    }
52
53 View Code Duplication
    public function testGetCandidatesEmptySync(): void
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...
54
    {
55
        $httpAsyncClient = $this->getMockBuilder(HttpAsyncClient::class)->getMock();
56
        $strategy = new ConfiguredClientsStrategy(null, $httpAsyncClient);
0 ignored issues
show
Documentation introduced by
$httpAsyncClient is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a null|object<Http\Client\HttpAsyncClient>.

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...
57
58
        $candidates = $strategy::getCandidates(HttpClient::class);
59
        $this->assertEquals([], $candidates);
60
61
        $candidates = $strategy::getCandidates(HttpAsyncClient::class);
62
        $candidate = array_shift($candidates);
63
        $this->assertEquals($httpAsyncClient, $candidate['class']());
64
    }
65
}
66