TweetParameters::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
ccs 12
cts 12
cp 1
rs 9.0856
cc 1
eloc 21
nc 1
nop 10
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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