Completed
Push — master ( 8ed474...52c8eb )
by Tristan
03:25
created

Format::format()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
rs 4.9091
cc 9
eloc 21
nc 9
nop 1
1
<?php
2
3
namespace Enzyme\Name;
4
5
use Stringy\Stringy as S;
6
7
class Format
8
{
9
    /**
10
     * The simple name instance to format
11
     *
12
     * @var Simple
13
     */
14
    protected $name;
15
16
    /**
17
     * Create a new formatter.
18
     *
19
     * @param Simple $name The name to format.
20
     */
21
    public function __construct(Simple $name)
22
    {
23
        $this->name = $name;
24
    }
25
26
    /**
27
     * A quick way to format a name.
28
     *
29
     * @param Simple $name          The name to format.
30
     * @param string $format_string The format string.
31
     *
32
     * @return string
33
     */
34
    public static function nameLike(Simple $name, $format_string)
35
    {
36
        $fmt = new static($name);
37
        return $fmt->like($format_string);
38
    }
39
40
    /**
41
     * Formats the name like the given format string describes.
42
     *
43
     * @param string $format_string The format string.
44
     *
45
     * @return string
46
     */
47
    public function like($format_string)
48
    {
49
        $string = S::create($format_string)->collapseWhitespace();
50
        $parts = $string->split(' ');
51
52
        $final_parts = [];
53
        foreach ($parts as $part) {
54
            $final_parts[] = $this->format($part);
55
        }
56
57
        return S::create(implode(' ', $final_parts))->collapseWhitespace();
58
    }
59
60
    /**
61
     * Format the given name segment/part.
62
     *
63
     * @param string $part The part.
64
     *
65
     * @return string
66
     */
67
    protected function format($part)
68
    {
69
        $part = S::create($part);
70
71
        switch ($part->substr(0, 2)) {
72
            case 'Pr':
73
                return $part->replace('Prefix', $this->tryGetLongVersion($this->name->getPrefix()));
0 ignored issues
show
Documentation introduced by
$this->name->getPrefix() is of type object<Enzyme\Name\Part>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
75
            case 'Fi':
76
                return $part->replace('First', $this->tryGetLongVersion($this->name->getFirst()));
0 ignored issues
show
Documentation introduced by
$this->name->getFirst() is of type object<Enzyme\Name\Part>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
78
            case 'Mi':
79
                return $part->replace('Middle', $this->tryGetLongVersion($this->name->getMiddle()));
0 ignored issues
show
Documentation introduced by
$this->name->getMiddle() is of type object<Enzyme\Name\Part>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
80
81
            case 'La':
82
                return $part->replace('Last', $this->tryGetLongVersion($this->name->getLast()));
0 ignored issues
show
Documentation introduced by
$this->name->getLast() is of type object<Enzyme\Name\Part>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
84
            case 'P.':
85
                return $part->replace('P.', $this->tryGetShortVersion($this->name->getPrefix()));
0 ignored issues
show
Documentation introduced by
$this->name->getPrefix() is of type object<Enzyme\Name\Part>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
86
87
            case 'F.':
88
                return $part->replace('F.', $this->tryGetShortVersion($this->name->getFirst()));
0 ignored issues
show
Documentation introduced by
$this->name->getFirst() is of type object<Enzyme\Name\Part>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
90
            case 'M.':
91
                return $part->replace('M.', $this->tryGetShortVersion($this->name->getMiddle()));
0 ignored issues
show
Documentation introduced by
$this->name->getMiddle() is of type object<Enzyme\Name\Part>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
92
93
            case 'L.':
94
                return $part->replace('L.', $this->tryGetShortVersion($this->name->getLast()));
0 ignored issues
show
Documentation introduced by
$this->name->getLast() is of type object<Enzyme\Name\Part>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
96
            default:
97
                return $part;
98
        }
99
    }
100
101
    /**
102
     * Tries to extract the long version of the given name from this part.
103
     *
104
     * @param string $part The part to process.
105
     *
106
     * @return string
107
     */
108
    protected function tryGetLongVersion($part)
109
    {
110
        return $part !== null
111
            ? $part->long()
0 ignored issues
show
Bug introduced by
The method long cannot be called on $part (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
112
            : '';
113
    }
114
115
    /**
116
     * Tries to extract the short version of the given name from this part.
117
     *
118
     * @param string $part The part to process.
119
     *
120
     * @return string
121
     */
122
    protected function tryGetShortVersion($part)
123
    {
124
        return $part !== null
125
            ? $part->short()
0 ignored issues
show
Bug introduced by
The method short cannot be called on $part (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
126
            : '';
127
    }
128
}