AccessLog::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 10
dl 0
loc 23
ccs 0
cts 22
cp 0
crap 2
rs 9.9332

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Entity;
6
7
use DateTime;
8
use Doctrine\ORM\Mapping as ORM;
9
10
/**
11
 * @ORM\Entity
12
 * @ORM\Table(
13
 *   name="access_log",
14
 *   indexes={
15
 *     @ORM\Index(name="created_at_idx", columns={"created_at"}),
16
 *     @ORM\Index(name="email_idx", columns={"email"}),
17
 *   },
18
 *   options={
19
 *     "comment" = "Access/auth log",
20
 *      "charset"="utf8mb4",
21
 *      "collate"="utf8mb4_unicode_ci"
22
 *   }
23
 * )
24
 */
25
class AccessLog implements \JsonSerializable
26
{
27
    /**
28
     * @ORM\Id
29
     * @ORM\Column(name="id", type="integer")
30
     * @ORM\GeneratedValue(strategy="IDENTITY")
31
     *
32
     * @var int
33
     */
34
    private $id;
35
36
    /**
37
     * @ORM\Column(name="log_type", type="string", length=16)
38
     *
39
     * @var string
40
     */
41
    private $log_type;
42
43
    /**
44
     * @ORM\Column(name="email", type="string", length=32, nullable=true)
45
     *
46
     * @var string
47
     */
48
    private $email;
49
50
    /**
51
     * @ORM\Column(name="language", type="string", length=5, nullable=true)
52
     *
53
     * @var string
54
     */
55
    private $language;
56
57
    /**
58
     * @ORM\Column(name="ip_address", type="string", length=32, nullable=true)
59
     *
60
     * @var string
61
     */
62
    private $ip_address;
63
64
    /**
65
     * @ORM\Column(name="browser", type="string", length=32, nullable=true)
66
     *
67
     * @var string
68
     */
69
    private $browser;
70
71
    /**
72
     * @ORM\Column(name="browser_version", type="string", length=10, nullable=true)
73
     *
74
     * @var string
75
     */
76
    private $browser_version;
77
78
    /**
79
     * @ORM\Column(name="os", type="string", length=10, nullable=true)
80
     *
81
     * @var string
82
     */
83
    private $os;
84
85
    /**
86
     * @ORM\Column(name="device_type", type="string", length=20, nullable=true)
87
     *
88
     * @var string
89
     */
90
    private $device_type;
91
92
    /**
93
     * @ORM\Column(name="user_agent", type="string", length=50, nullable=true)
94
     *
95
     * @var string
96
     */
97
    private $user_agent;
98
99
    /**
100
     * @ORM\Column(type="datetime", nullable=true, options={"comment" = "Record creation timestamp"})
101
     *
102
     * @var \DateTime
103
     */
104
    private $created_at;
105
106
    public function __construct(
107
        string $logType,
108
        ?string $email = null,
109
        ?string $language = null,
110
        ?string $ipAddress = null,
111
        ?string $userAgent = null,
112
        ?string $browser = null,
113
        ?string $browserVersion = null,
114
        ?string $os = null,
115
        ?string $deviceType = null,
116
        ?DateTime $createdAt = null
117
    ) {
118
        $this->log_type        = mb_substr($logType, 0, 16);
119
        $this->email           = mb_substr($email ?? '', 0, 32);
120
        $this->language        = mb_substr($language ?? '', 0, 5);
121
        $this->ip_address      = mb_substr($ipAddress ?? '', 0, 32);
122
        $this->user_agent      = mb_substr($userAgent ?? '', 0, 50);
123
        $this->browser         = mb_substr($browser ?? '', 0, 32);
124
        $this->browser_version = mb_substr($browserVersion ?? '', 0, 10);
125
        $this->device_type     = mb_substr($deviceType ?? '', 0, 10);
126
        $this->os              = mb_substr($os ?? '', 0, 32);
127
128
        $this->created_at = $createdAt ?? new DateTime();
129
    }
130
131
    /**
132
     * @return array<string, mixed>
133
     */
134
    public function jsonSerialize(): array
135
    {
136
        return [
137
            'id'              => $this->id,
138
            'log_type'        => $this->log_type,
139
            'email'           => $this->email,
140
            'language'	 	     => $this->language,
141
            'ip_address'      => $this->ip_address,
142
            'user_agent'	     => $this->user_agent,
143
            'browser'	        => $this->browser,
144
            'browser_version' => $this->browser_version,
145
            'os'	 	           => $this->os,
146
            'device_type'     => $this->device_type,
147
            'created_at'      => $this->created_at,
148
        ];
149
    }
150
}
151