PersonName::possessive()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Druc\PersonName;
4
5
class PersonName
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
6
{
7
    /** @var string */
8
    public $fullName;
9
10
    /**
11
     * PersonName constructor.
12
     * @param string $fullName
13
     */
14 30
    private function __construct(string $fullName = '')
15
    {
16 30
        $this->fullName = $fullName;
17 30
    }
18
19
    /**
20
     * @param string $fullName
21
     * @return PersonName
22
     */
23 30
    public static function make(string $fullName)
24
    {
25 30
        return new static($fullName);
26
    }
27
28
    /**
29
     * @param $property
30
     * @return null
31
     */
32 30
    public function __get($property)
33
    {
34 30
        if (in_array($property, get_class_methods($this))) {
35 30
            return $this->{$property}();
36
        }
37
38
        return null;
39
    }
40
41
    /**
42
     * @return string
43
     */
44 3
    public function full()
45
    {
46 3
        return $this->fullName;
47
    }
48
49
    /**
50
     * @return string
51
     */
52 15
    public function first()
53
    {
54 15
        return preg_split('/\s+/', $this->fullName, 2)[0];
55
    }
56
57
    /**
58
     * @return string
59
     */
60 21
    public function last()
61
    {
62 21
        return preg_split('/\s+/', $this->fullName, 2)[1];
63
    }
64
65
    /**
66
     * @return string
67
     */
68 3
    public function initials()
69
    {
70 3
        preg_match_all('/\b(\S)\S*/', $this->fullName, $matches);
71 3
        return implode('', $matches[1]);
72
    }
73
74
    /**
75
     * First name + Last name initial
76
     * Ex: Constantin Druc Pinsmile -> Constantin D.
77
     * @return string
78
     */
79 6
    public function familiar()
80
    {
81 6
        return $this->first() . ' ' . substr($this->last(), 0, 1) . '.';
82
    }
83
84
    /**
85
     * Ex: Constantin Druc Pinsmile -> CDP
86
     * @return string
87
     */
88 3
    public function abbreviated()
89
    {
90 3
        return substr($this->first, 0, 1) . '. ' . $this->last();
0 ignored issues
show
Documentation introduced by
The property first does not exist on object<Druc\PersonName\PersonName>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
91
    }
92
93
    /**
94
     * Last name, First name
95
     * Ex: Constantin Druc Pinsmile -> Druc Pinsmile, Constantin
96
     * @return string
97
     */
98 3
    public function sorted()
99
    {
100 3
        return $this->last() . ', ' . $this->first();
101
    }
102
103
    /**
104
     * Lowercase and whitespace striped version of the familiar name
105
     * @return string
106
     */
107 3
    public function mentionable()
108
    {
109 3
        return rtrim(strtolower(preg_replace('/\s+/', '', $this->familiar())), ". ");
110
    }
111
112
    /**
113
     * @return string
114
     */
115 6
    public function possessive()
116
    {
117 6
        if (substr($this->last(), -1, 1) !== 's') {
118 3
            return $this->fullName . "'s";
119
        }
120
121 3
        return $this->fullName . "'";
122
    }
123
}
124