Completed
Push — master ( da873d...74929b )
by Tobias
03:47
created

CredentialResponseItem::create()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 6
nc 16
nop 1
crap 30
1
<?php
2
3
/*
4
 * Copyright (C) 2013-2016 Mailgun
5
 *
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace Mailgun\Model\Domain;
11
12
/**
13
 * @author Sean Johnson <[email protected]>
14
 */
15
final class CredentialResponseItem
16
{
17
    /**
18
     * @var int|null
19
     */
20
    private $sizeBytes;
21
22
    /**
23
     * @var \DateTime
24
     */
25
    private $createdAt;
26
27
    /**
28
     * @var string
29
     */
30
    private $mailbox;
31
32
    /**
33
     * @var string
34
     */
35
    private $login;
36
37
    /**
38
     * @param array $data
39
     *
40
     * @return self
41
     */
42
    public static function create(array $data)
43
    {
44
        $sizeBytes = isset($data['size_bytes']) ? $data['size_bytes'] : null;
45
        $mailbox = isset($data['mailbox']) ? $data['mailbox'] : null;
46
        $login = isset($data['login']) ? $data['login'] : null;
47
        $createdAt = isset($data['created_at']) ? new \DateTime($data['created_at']) : null;
48
49
        return new self($sizeBytes, $createdAt, $mailbox, $login);
0 ignored issues
show
Bug introduced by
It seems like $createdAt defined by isset($data['created_at'...a['created_at']) : null on line 47 can be null; however, Mailgun\Model\Domain\Cre...onseItem::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
50
    }
51
52
    /**
53
     * @param int       $sizeBytes
54
     * @param \DateTime $createdAt
55
     * @param string    $mailbox
56
     * @param string    $login
57
     */
58
    private function __construct($sizeBytes, \DateTime $createdAt, $mailbox, $login)
59
    {
60
        $this->sizeBytes = $sizeBytes;
61
        $this->createdAt = $createdAt;
62
        $this->mailbox = $mailbox;
63
        $this->login = $login;
64
    }
65
66
    /**
67
     * @return int|null
68
     */
69
    public function getSizeBytes()
70
    {
71
        return $this->sizeBytes;
72
    }
73
74
    /**
75
     * @return \DateTime
76
     */
77
    public function getCreatedAt()
78
    {
79
        return $this->createdAt;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getMailbox()
86
    {
87
        return $this->mailbox;
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getLogin()
94
    {
95
        return $this->login;
96
    }
97
}
98