Completed
Push — master ( d83f32...d4821e )
by Rémi
04:29
created

TweetParameters::__construct()   A

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 45
    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 45
        $this->text = $text;
66 45
        $this->replyToId = $replyToId;
67 45
        $this->sensitive = $sensitive;
68 45
        $this->coordinates = $coordinates;
69 45
        $this->placeId = $placeId;
70 45
        $this->displayCoordinates = $displayCoordinates;
71 45
        $this->trimUser = $trimUser;
72 45
        $this->mediaIds = $mediaIds;
0 ignored issues
show
Documentation Bug introduced by
It seems like $mediaIds of type array<integer,object<int>> is incompatible with the declared type array<integer,integer> of property $mediaIds.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
73 45
        $this->enableDirectMessageCommands = $enableDirectMessageCommands;
74 45
        $this->failDirectMessageCommands = $failDirectMessageCommands;
75 45
    }
76
77
    /**
78
     * @return array
79
     */
80 9
    public function toArray()
81
    {
82
        $parameters = [
83 9
            'status' => $this->text,
84 9
            'possibly_sensitive' => $this->sensitive ? 'true' : 'false',
85 9
            'trim_user' => $this->trimUser ? 'true' : 'false',
86 9
            'enable_dm_commands' => $this->enableDirectMessageCommands ? 'true' : 'false',
87 9
            'fail_dm_commands' => $this->failDirectMessageCommands ? 'true' : 'false'
88 6
        ];
89
90 9
        if ($this->replyToId !== null) {
91
            $parameters['in_reply_to_status_id'] = $this->replyToId;
92
        }
93
94 9
        if ($this->coordinates !== null) {
95
            $parameters = array_merge($parameters, $this->coordinates->toArray());
96
97
            $parameters['display_coordinates'] = $this->displayCoordinates ? 'true' : 'false';
98
        }
99
100 9
        if ($this->placeId !== null) {
101
            $parameters['place_id'] = $this->placeId;
102
        }
103
104 9
        if (!empty($this->mediaIds)) {
105
            $parameters['media_ids'] = implode(',', $this->mediaIds);
106
        }
107
108 9
        return $parameters;
109
    }
110
}
111