Completed
Push — master ( f80030...418223 )
by Alexandru
06:28
created

ExtractsRequestIdFromHeader   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
A getRequestId() 8 8 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
/*
4
 * This file is part of the Arki\RequestId library.
5
 *
6
 * (c) Alexandru Furculita <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.md.
10
 */
11
12
namespace Arki\RequestId\Integrations\Symfony\Providers;
13
14
use Arki\RequestId\Providers\RequestIdProvider;
15
use Arki\RequestId\RequestId;
16
use Symfony\Component\HttpFoundation\Request;
17
18 View Code Duplication
final class ExtractsRequestIdFromHeader implements RequestIdProvider
0 ignored issues
show
Duplication introduced by
This class 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...
19
{
20
    /**
21
     * @var string
22
     */
23
    private $headerName;
24
    /**
25
     * @var RequestIdProvider
26
     */
27
    private $fallback;
28
    /**
29
     * @var Request
30
     */
31
    private $request;
32
33
    /**
34
     * @param Request           $request
35
     * @param string            $headerName
36
     * @param RequestIdProvider $fallback
37
     */
38
    public function __construct(
39
        Request $request,
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
40
        RequestIdProvider $fallback,
41
        $headerName = RequestId::HEADER_NAME
42
    ) {
43
        $this->headerName = $headerName;
44
        $this->fallback = $fallback;
45
        $this->request = $request;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getRequestId()
52
    {
53
        if ($this->request->headers->has($this->headerName)) {
54
            return $this->request->headers->get($this->headerName);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->request->headers->get($this->headerName); of type string|array adds the type array to the return on line 54 which is incompatible with the return type declared by the interface Arki\RequestId\Providers...dProvider::getRequestId of type string|null.
Loading history...
55
        }
56
57
        return $this->fallback->getRequestId();
58
    }
59
}
60