Toot::__toString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Baguette\Mastodon\Service;
4
5
use Respect\Validation\Validator as v;
6
7
/**
8
 * Toot
9
 *
10
 * @author    USAMI Kenta <[email protected]>
11
 * @copyright 2017 Baguette HQ
12
 * @license   https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0
13
 * @property-read string     $toot_string
14
 * @property-read int[]      $media_ids
15
 * @property-read int|null   $in_reply_to_id
16
 * @property-read string     $visibility
17
 * @property-read bool|null  $sensitive
18
 * @property-read string|null $spoiler_text
19
 */
20
class Toot
21
{
22
    use \Teto\Object\PrivateGetter;
23
    use \Teto\Object\ReadOnly;
24
25
    const VISIBILITY_DIRECT   = 'direct';
26
    const VISIBILITY_PRIVATE  = 'private';
27
    const VISIBILITY_UNLISTED = 'unlisted';
28
    const VISIBILITY_PUBLIC   = 'public';
29
30
    private static $VISIBILITIES = [
0 ignored issues
show
Unused Code introduced by
The property $VISIBILITIES is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
31
        Toot::VISIBILITY_PUBLIC,
32
        Toot::VISIBILITY_UNLISTED,
33
        Toot::VISIBILITY_PRIVATE,
34
        Toot::VISIBILITY_DIRECT,
35
    ];
36
37
    /** @var string */
38
    private $toot_string;
39
    /** @var int[] */
40
    private $media_ids = [];
41
    /** @var int|null */
42
    private $in_reply_to_id;
43
    /** @var string|null */
44
    private $visibility;
45
    /** @var bool|null */
46
    private $sensitive;
0 ignored issues
show
Unused Code introduced by
The property $sensitive is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
47
    /** @var string|null */
48
    private $spoiler_text;
0 ignored issues
show
Unused Code introduced by
The property $spoiler_text is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
49
50
    /**
51
     * @param string[] $scopes
0 ignored issues
show
Bug introduced by
There is no parameter named $scopes. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
52
     */
53 2
    public function __construct($toot_string, array $options)
54
    {
55 2
        v::stringType()->assert($toot_string);
56 2
        $this->toot_string = $toot_string;
57
58 2
        if (isset($options['media_ids'])) {
59
            $ids = [];
60
            foreach ($options['media_ids'] as $id) {
61
                v::intType()->assert($id);
62
                $ids[] = $id;
63
            }
64
            $this->media_ids = $ids;
65
        }
66
67 2
        if (isset($options['in_reply_to_id'])) {
68
            v::intType()->assert($options['in_reply_to_id']);
69
            $this->in_reply_to_id = $options['in_reply_to_id'];
70
        }
71
72 2
        if (isset($options['visibility'])) {
73
            v::in(Toot::$VISIBILITIES)->assert($options['visibility']);
74
            $this->visibility = $options['visibility'];
75
        }
76
77 2
        if (isset($options['senstitive'])) {
78
            v::bool()->assert($options['senstitive']);
79
            $this->senstitive = $options['senstitive'];
0 ignored issues
show
Bug introduced by
The property senstitive does not seem to exist. Did you mean sensitive?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
80
        }
81 2
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function __toString()
87
    {
88
        if ($this->visibility === Toot::VISIBILITY_PUBLIC) {
89
            return $this->toot_string;
90
        }
91
92
        return "[{$this->visibility}]";
93
    }
94
}
95