RequestContext::mapFiles()   A
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 1
nop 1
dl 0
loc 30
rs 9.1288
c 0
b 0
f 0
1
<?php
2
3
namespace Facade\FlareClient\Context;
4
5
use BadMethodCallException;
6
use Symfony\Component\HttpFoundation\File\UploadedFile;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Session\SessionInterface;
9
use Symfony\Component\Mime\Exception\InvalidArgumentException;
10
use Throwable;
11
12
class RequestContext implements ContextInterface
13
{
14
    /** @var \Symfony\Component\HttpFoundation\Request|null */
15
    protected $request;
16
17
    public function __construct(Request $request = null)
18
    {
19
        $this->request = $request ?? Request::createFromGlobals();
0 ignored issues
show
Documentation Bug introduced by
It seems like $request ?? \Symfony\Com...st::createFromGlobals() can also be of type object<self>. However, the property $request is declared as type object<Symfony\Component...oundation\Request>|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
20
    }
21
22
    public function getRequest(): array
23
    {
24
        return [
25
            'url' => $this->request->getUri(),
26
            'ip' => $this->request->getClientIp(),
27
            'method' => $this->request->getMethod(),
28
            'useragent' => $this->request->headers->get('User-Agent'),
29
        ];
30
    }
31
32
    private function getFiles(): array
33
    {
34
        if (is_null($this->request->files)) {
35
            return [];
36
        }
37
38
        return $this->mapFiles($this->request->files->all());
39
    }
40
41
    protected function mapFiles(array $files)
42
    {
43
        return array_map(function ($file) {
44
            if (is_array($file)) {
45
                return $this->mapFiles($file);
46
            }
47
48
            if (! $file instanceof UploadedFile) {
49
                return;
50
            }
51
52
            try {
53
                $fileSize = $file->getSize();
54
            } catch (\RuntimeException $e) {
55
                $fileSize = 0;
56
            }
57
58
            try {
59
                $mimeType = $file->getMimeType();
60
            } catch (InvalidArgumentException $e) {
61
                $mimeType = 'undefined';
62
            }
63
64
            return [
65
                'pathname' => $file->getPathname(),
66
                'size' => $fileSize,
67
                'mimeType' => $mimeType,
68
            ];
69
        }, $files);
70
    }
71
72
    public function getSession(): array
73
    {
74
        try {
75
            $session = $this->request->getSession();
76
        } catch (BadMethodCallException $exception) {
77
            $session = [];
78
        }
79
80
        return $session ? $this->getValidSessionData($session) : [];
81
    }
82
83
    /**
84
     * @param SessionInterface $session
85
     * @return array
86
     */
87
    protected function getValidSessionData($session): array
88
    {
89
        try {
90
            json_encode($session->all());
91
        } catch (Throwable $e) {
92
            return [];
93
        }
94
95
        return $session->all();
96
    }
97
98
    public function getCookies(): array
99
    {
100
        return $this->request->cookies->all();
101
    }
102
103
    public function getHeaders(): array
104
    {
105
        return $this->request->headers->all();
106
    }
107
108
    public function getRequestData(): array
109
    {
110
        return [
111
            'queryString' => $this->request->query->all(),
112
            'body' => $this->request->request->all(),
113
            'files' => $this->getFiles(),
114
        ];
115
    }
116
117
    public function toArray(): array
118
    {
119
        return [
120
            'request' => $this->getRequest(),
121
            'request_data' => $this->getRequestData(),
122
            'headers' => $this->getHeaders(),
123
            'cookies' => $this->getCookies(),
124
            'session' => $this->getSession(),
125
        ];
126
    }
127
}
128