Completed
Push — master ( 16b7b3...90dfdb )
by Ryan
13:15
created

Person   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
lcom 4
cbo 3
dl 0
loc 138
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
C __toString() 0 24 8
A getNode() 0 4 1
A getName() 0 4 1
A getUrl() 0 4 1
A getEmail() 0 4 1
A getAvatar() 0 4 1
1
<?php
2
/**
3
 * Copyright (c) 2017 Ryan Parman <http://ryanparman.com>.
4
 * Copyright (c) 2017 Contributors.
5
 *
6
 * http://opensource.org/licenses/Apache2.0
7
 */
8
9
declare(strict_types=1);
10
11
namespace SimplePie\Type;
12
13
use DOMNode;
14
use Psr\Log\LoggerInterface;
15
use SimplePie\Configuration as C;
16
use SimplePie\Mixin as T;
17
18
class Person extends AbstractType implements TypeInterface, C\SetLoggerInterface
19
{
20
    use T\LoggerTrait;
21
22
    /**
23
     * The DOMNode element to parse.
24
     *
25
     * @var DOMNode
26
     */
27
    protected $node;
28
29
    /**
30
     * The person's name.
31
     *
32
     * @var string
33
     */
34
    protected $name;
35
36
    /**
37
     * The person's URL.
38
     *
39
     * @var string
40
     */
41
    protected $uri;
42
43
    /**
44
     * The person's email address.
45
     *
46
     * @var string
47
     */
48
    protected $email;
49
50
    /**
51
     * The person's avatar.
52
     *
53
     * @var string
54
     */
55
    protected $avatar;
56
57
    /**
58
     * Constructs a new instance of this class.
59
     *
60
     * @param DOMNode|null    $node   The `DOMNode` element to parse.
61
     * @param LoggerInterface $logger The PSR-3 logger.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $logger not be null|LoggerInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
62
     */
63
    public function __construct(?DOMNode $node = null, LoggerInterface $logger = null)
64
    {
65
        if ($node) {
66
            $this->logger = $logger ?? new NullLogger();
0 ignored issues
show
Documentation Bug introduced by
It seems like $logger ?? new \SimplePie\Type\NullLogger() can also be of type object<SimplePie\Type\NullLogger>. However, the property $logger is declared as type object<Psr\Log\LoggerInterface>. 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...
67
            $this->node   = $node;
68
            $this->name   = new Node($this->node);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \SimplePie\Type\Node($this->node) of type object<SimplePie\Type\Node> is incompatible with the declared type string of property $name.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
69
70
            foreach ($this->node->childNodes as $child) {
71
                $this->{$child->tagName} = new Node($child);
72
            }
73
        }
74
    }
75
76
    /**
77
     * Converts this object into a string representation.
78
     *
79
     * @return string
80
     */
81
    public function __toString(): string
82
    {
83
        if (null !== $this->name && null !== $this->uri) {
84
            return \trim(\sprintf('%s <%s>', (string) $this->name, (string) $this->uri));
85
        }
86
87
        if (null !== $this->name && null !== $this->email) {
88
            return \trim(\sprintf('%s <%s>', (string) $this->name, (string) $this->email));
89
        }
90
91
        if (null !== $this->name) {
92
            return \trim((string) $this->name);
93
        }
94
95
        if (null !== $this->uri) {
96
            return \trim((string) $this->uri);
97
        }
98
99
        if (null !== $this->email) {
100
            return \trim((string) $this->email);
101
        }
102
103
        return 'Unknown';
104
    }
105
106
    /**
107
     * Gets the DOMNode element.
108
     *
109
     * @return DOMNode|null
110
     */
111
    public function getNode(): ?DOMNode
112
    {
113
        return $this->node;
114
    }
115
116
    /**
117
     * Gets the person's name.
118
     *
119
     * @return Node
120
     */
121
    public function getName(): Node
122
    {
123
        return $this->name ?? new Node();
124
    }
125
126
    /**
127
     * Gets the person's URL.
128
     *
129
     * @return Node
130
     */
131
    public function getUrl(): Node
132
    {
133
        return $this->uri ?? new Node();
134
    }
135
136
    /**
137
     * Gets the person's email address.
138
     *
139
     * @return Node
140
     */
141
    public function getEmail(): Node
142
    {
143
        return $this->email ?? new Node();
144
    }
145
146
    /**
147
     * Gets the person's avatar.
148
     *
149
     * @return Node
150
     */
151
    public function getAvatar(): Node
152
    {
153
        return $this->avatar ?? new Node();
154
    }
155
}
156