TweetParameters::toArray()   F
last analyzed

Complexity

Conditions 10
Paths 384

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 21
cts 21
cp 1
rs 3.1304
c 0
b 0
f 0
cc 10
eloc 17
nc 384
nop 0
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Twitter\API\REST\DTO;
4
5
use Twitter\API\REST\ApiParameters;
6
7
class TweetParameters implements ApiParameters
8
{
9
    /** @var string */
10
    private $text;
11
12
    /** @var int */
13
    private $replyToId;
14
15
    /** @var bool */
16
    private $sensitive;
17
18
    /** @var Coordinates */
19
    private $coordinates;
20
21
    /** @var string */
22
    private $placeId;
23
24
    /** @var bool */
25
    private $displayCoordinates;
26
27
    /** @var bool */
28
    private $trimUser;
29
30
    /** @var int[] */
31
    private $mediaIds;
32
33
    /** @var bool */
34
    private $enableDirectMessageCommands;
35
36
    /** @var bool */
37
    private $failDirectMessageCommands;
38
39
    /**
40
     * TweetParameters constructor.
41
     *
42
     * @param string      $text
43
     * @param int         $replyToId
44
     * @param bool        $sensitive
45
     * @param Coordinates $coordinates
46
     * @param string      $placeId
47
     * @param bool        $displayCoordinates
48
     * @param bool        $trimUser
49
     * @param int[]       $mediaIds
50
     * @param bool        $enableDirectMessageCommands
51
     * @param bool        $failDirectMessageCommands
52
     */
53 66
    public function __construct(
54
        $text,
55
        $replyToId = null,
56
        $sensitive = false,
57
        Coordinates $coordinates = null,
58
        $placeId = null,
59
        $displayCoordinates = false,
60
        $trimUser = false,
61
        array $mediaIds = [],
62
        $enableDirectMessageCommands = true,
63
        $failDirectMessageCommands = false
64
    ) {
65 66
        $this->text = $text;
66 66
        $this->replyToId = $replyToId;
67 66
        $this->sensitive = $sensitive;
68 66
        $this->coordinates = $coordinates;
69 66
        $this->placeId = $placeId;
70 66
        $this->displayCoordinates = $displayCoordinates;
71 66
        $this->trimUser = $trimUser;
72 66
        $this->mediaIds = $mediaIds;
73 66
        $this->enableDirectMessageCommands = $enableDirectMessageCommands;
74 66
        $this->failDirectMessageCommands = $failDirectMessageCommands;
75 66
    }
76
77
    /**
78
     * @return array
79
     */
80 15
    public function toArray()
81
    {
82
        $parameters = [
83 15
            'status' => $this->text,
84 15
            'possibly_sensitive' => $this->sensitive ? 'true' : 'false',
85 15
            'trim_user' => $this->trimUser ? 'true' : 'false',
86 15
            'enable_dm_commands' => $this->enableDirectMessageCommands ? 'true' : 'false',
87 15
            'fail_dm_commands' => $this->failDirectMessageCommands ? 'true' : 'false'
88 10
        ];
89
90 15
        if ($this->replyToId !== null) {
91 6
            $parameters['in_reply_to_status_id'] = $this->replyToId;
92 4
        }
93
94 15
        if ($this->coordinates !== null) {
95 6
            $parameters = array_merge($parameters, $this->coordinates->toArray());
96
97 6
            $parameters['display_coordinates'] = $this->displayCoordinates ? 'true' : 'false';
98 4
        }
99
100 15
        if ($this->placeId !== null) {
101 6
            $parameters['place_id'] = $this->placeId;
102 4
        }
103
104 15
        if (!empty($this->mediaIds)) {
105 6
            $parameters['media_ids'] = implode(',', $this->mediaIds);
106 4
        }
107
108 15
        return $parameters;
109
    }
110
}
111