ExceptionThrowerPlugin   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 9
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 1
dl 0
loc 9
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A doHandleRequest() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Http\HttplugBundle\Tests\Functional;
6
7
use GuzzleHttp\Psr7\Request;
8
use Http\Client\Common\Plugin;
9
use Http\Client\Common\PluginClient;
10
use Http\Discovery\StreamFactoryDiscovery;
11
use Http\Discovery\UriFactoryDiscovery;
12
use Http\HttplugBundle\Collector\Collector;
13
use Http\HttplugBundle\Collector\Formatter;
14
use Http\HttplugBundle\Collector\ProfileClient;
15
use Http\HttplugBundle\Collector\ProfilePlugin;
16
use Http\HttplugBundle\Collector\StackPlugin;
17
use Http\Message\Formatter\CurlCommandFormatter;
18
use Http\Message\Formatter\FullHttpMessageFormatter;
19
use Http\Mock\Client;
20
use PHPUnit\Framework\TestCase;
21
use Psr\Http\Message\RequestInterface;
22
use Symfony\Component\Cache\Adapter\ArrayAdapter;
23
use Symfony\Component\Stopwatch\Stopwatch;
24
25
class ProfilingTest extends TestCase
26
{
27
    /**
28
     * @var Collector
29
     */
30
    private $collector;
31
32
    /**
33
     * @var Formatter
34
     */
35
    private $formatter;
36
37
    /**
38
     * @var Stopwatch
39
     */
40
    private $stopwatch;
41
42
    public function setUp(): void
43
    {
44
        $this->collector = new Collector([]);
0 ignored issues
show
Unused Code introduced by
The call to Collector::__construct() has too many arguments starting with array().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
45
        $this->formatter = new Formatter(new FullHttpMessageFormatter(), new CurlCommandFormatter());
46
        $this->stopwatch = new Stopwatch();
47
    }
48
49
    public function testProfilingWithCachePlugin(): void
50
    {
51
        $client = $this->createClient([
52
            new Plugin\CachePlugin(new ArrayAdapter(), StreamFactoryDiscovery::find(), [
53
                'respect_response_cache_directives' => [],
54
                'default_ttl' => 86400,
55
            ]),
56
        ]);
57
58
        $client->sendRequest(new Request('GET', 'https://example.com'));
59
        $client->sendRequest(new Request('GET', 'https://example.com'));
60
61
        $this->assertCount(2, $this->collector->getStacks());
0 ignored issues
show
Documentation introduced by
$this->collector->getStacks() is of type array<integer,object<Htt...undle\Collector\Stack>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

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...
62
        $stack = $this->collector->getStacks()[1];
63
        $this->assertEquals('GET', $stack->getRequestMethod());
64
        $this->assertEquals('https', $stack->getRequestScheme());
65
        $this->assertEquals('/', $stack->getRequestTarget());
66
        $this->assertEquals('example.com', $stack->getRequestHost());
67
    }
68
69
    public function testProfilingWhenPluginThrowException(): void
70
    {
71
        $client = $this->createClient([
72
            new ExceptionThrowerPlugin(),
73
        ]);
74
75
        try {
76
            $this->expectException(\Exception::class);
77
            $client->sendRequest(new Request('GET', 'https://example.com'));
78
        } finally {
79
            $this->assertCount(1, $this->collector->getStacks());
0 ignored issues
show
Documentation introduced by
$this->collector->getStacks() is of type array<integer,object<Htt...undle\Collector\Stack>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

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...
80
            $stack = $this->collector->getStacks()[0];
81
            $this->assertEquals('GET', $stack->getRequestMethod());
82
            $this->assertEquals('https', $stack->getRequestScheme());
83
            $this->assertEquals('/', $stack->getRequestTarget());
84
            $this->assertEquals('example.com', $stack->getRequestHost());
85
        }
86
    }
87
88
    public function testProfiling(): void
89
    {
90
        $client = $this->createClient([
91
            new Plugin\AddHostPlugin(UriFactoryDiscovery::find()->createUri('https://example.com')),
92
            new Plugin\RedirectPlugin(),
93
            new Plugin\RetryPlugin(),
94
        ]);
95
96
        $client->sendRequest(new Request('GET', '/'));
97
98
        $this->assertCount(1, $this->collector->getStacks());
0 ignored issues
show
Documentation introduced by
$this->collector->getStacks() is of type array<integer,object<Htt...undle\Collector\Stack>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

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...
99
        $stack = $this->collector->getStacks()[0];
100
        $this->assertCount(3, $stack->getProfiles());
0 ignored issues
show
Documentation introduced by
$stack->getProfiles() is of type array<integer,object<Htt...dle\Collector\Profile>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

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...
101
        $this->assertEquals('GET', $stack->getRequestMethod());
102
        $this->assertEquals('https', $stack->getRequestScheme());
103
        $this->assertEquals('/', $stack->getRequestTarget());
104
        $this->assertEquals('example.com', $stack->getRequestHost());
105
    }
106
107
    private function createClient(array $plugins, $clientName = 'Acme', array $clientOptions = [])
108
    {
109
        $plugins = array_map(function (Plugin $plugin) {
110
            return new ProfilePlugin($plugin, $this->collector, $this->formatter, get_class($plugin));
0 ignored issues
show
Unused Code introduced by
The call to ProfilePlugin::__construct() has too many arguments starting with get_class($plugin).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
111
        }, $plugins);
112
113
        array_unshift($plugins, new StackPlugin($this->collector, $this->formatter, $clientName));
114
115
        $client = new Client();
116
        $client = new ProfileClient($client, $this->collector, $this->formatter, $this->stopwatch);
117
        $client = new PluginClient($client, $plugins, $clientOptions);
118
119
        return $client;
120
    }
121
}
122
123
class ExceptionThrowerPlugin implements Plugin
124
{
125
    use Plugin\VersionBridgePlugin;
126
127
    protected function doHandleRequest(RequestInterface $request, callable $next, callable $first): void
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $next is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $first is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
128
    {
129
        throw new \Exception();
130
    }
131
}
132