Passed
Push — master ( a4d511...6fbf8f )
by Morris
20:15 queued 11s
created

UserStatus   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 69
rs 10
c 1
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getClearAt() 0 2 1
A getStatus() 0 2 1
A getIcon() 0 2 1
A getMessage() 0 2 1
A getUserId() 0 2 1
A __construct() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2020, Georg Ehrke
7
 *
8
 * @author Georg Ehrke <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
namespace OCA\UserStatus\Connector;
26
27
use DateTimeImmutable;
28
use OCP\UserStatus\IUserStatus;
29
use OCA\UserStatus\Db;
30
31
class UserStatus implements IUserStatus {
32
33
	/** @var string */
34
	private $userId;
35
36
	/** @var string */
37
	private $status;
38
39
	/** @var string|null */
40
	private $message;
41
42
	/** @var string|null */
43
	private $icon;
44
45
	/** @var DateTimeImmutable|null */
46
	private $clearAt;
47
48
	/**
49
	 * UserStatus constructor.
50
	 *
51
	 * @param Db\UserStatus $status
52
	 */
53
	public function __construct(Db\UserStatus $status) {
54
		$this->userId = $status->getUserId();
55
		$this->status = $status->getStatus();
56
		$this->message = $status->getCustomMessage();
57
		$this->icon = $status->getCustomIcon();
58
59
		if ($status->getStatus() === 'invisible') {
60
			$this->status = 'offline';
61
		}
62
		if ($status->getClearAt() !== null) {
0 ignored issues
show
introduced by
The condition $status->getClearAt() !== null is always true.
Loading history...
63
			$this->clearAt = DateTimeImmutable::createFromFormat('U', (string)$status->getClearAt());
0 ignored issues
show
Documentation Bug introduced by
It seems like DateTimeImmutable::creat...)$status->getClearAt()) can also be of type false. However, the property $clearAt is declared as type DateTimeImmutable|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...
64
		}
65
	}
66
67
	/**
68
	 * @inheritDoc
69
	 */
70
	public function getUserId(): string {
71
		return $this->userId;
72
	}
73
74
	/**
75
	 * @inheritDoc
76
	 */
77
	public function getStatus(): string {
78
		return $this->status;
79
	}
80
81
	/**
82
	 * @inheritDoc
83
	 */
84
	public function getMessage(): ?string {
85
		return $this->message;
86
	}
87
88
	/**
89
	 * @inheritDoc
90
	 */
91
	public function getIcon(): ?string {
92
		return $this->icon;
93
	}
94
95
	/**
96
	 * @inheritDoc
97
	 */
98
	public function getClearAt(): ?DateTimeImmutable {
99
		return $this->clearAt;
100
	}
101
}
102