Phlack::toParameters()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.2
cc 4
eloc 8
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Crummy\Phlack;
4
5
use Crummy\Phlack\Bridge\Guzzle\PhlackClient;
6
use Crummy\Phlack\Bridge\Guzzle\Response\MessageResponse;
7
use Crummy\Phlack\Common\Exception\UnexpectedTypeException;
8
use Guzzle\Common\Collection;
9
use JsonSerializable;
10
11
class Phlack extends Collection
12
{
13
    /**
14
     * Phlack Constructor.
15
     *
16
     * @param mixed $client
17
     *
18
     * @throws UnexpectedTypeException
19
     */
20 15
    public function __construct($client)
21
    {
22 15
        if (is_string($client) || is_array($client)) {
23 2
            $client = new PhlackClient($client);
0 ignored issues
show
Bug introduced by
It seems like $client defined by new \Crummy\Phlack\Bridg...e\PhlackClient($client) on line 23 can also be of type array; however, Crummy\Phlack\Bridge\Guz...ckClient::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
24 15
        } elseif (!$client instanceof PhlackClient) {
25 1
            throw new UnexpectedTypeException($client, ['string', 'array', 'Crummy\Phlack\Bridge\Guzzle\PhlackClient']);
26
        }
27
28 14
        parent::__construct([
29 14
            'client'   => $client,
30 14
            'builders' => [],
31
            'commands' => [
32 14
                'send' => 'Send',
33 14
            ],
34 14
        ]);
35 14
    }
36
37
    /**
38
     * Phlack Factory.
39
     *
40
     * @param array|string $config
41
     *
42
     * @return Phlack
43
     */
44 2
    public static function factory($config = [])
45
    {
46 2
        return new self(new PhlackClient($config));
0 ignored issues
show
Bug introduced by
It seems like $config defined by parameter $config on line 44 can also be of type array; however, Crummy\Phlack\Bridge\Guz...ckClient::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
47
    }
48
49
    /**
50
     * @return Phlack
51
     */
52 1
    public static function fromConfig(array $config = [], array $defaults = [], array $required = [])
53
    {
54 1
        return new self(new PhlackClient($config));
0 ignored issues
show
Documentation introduced by
$config is of type array, 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...
55
    }
56
57
    /**
58
     * @param string|array|JsonSerializable $message
59
     *
60
     * @throws UnexpectedTypeException
61
     *
62
     * @return MessageResponse
63
     */
64 5
    public function send($message)
65
    {
66 5
        return $this['client']->getCommand($this['commands']['send'], $this->toParameters($message))->execute();
67
    }
68
69
    /**
70
     * @return Bridge\Guzzle\PhlackClient
71
     */
72 6
    public function getClient()
73
    {
74 6
        return $this['client'];
75
    }
76
77
    /**
78
     * @return Builder\MessageBuilder
79
     */
80 2
    public function getMessageBuilder()
81
    {
82 2
        if (!isset($this['builders']['message'])) {
83 2
            $this->setPath('builders/message', new Builder\MessageBuilder());
84 2
        }
85
86 2
        return $this['builders']['message'];
87
    }
88
89
    /**
90
     * @return Builder\AttachmentBuilder
91
     */
92 1
    public function getAttachmentBuilder()
93
    {
94 1
        if (!isset($this['builders']['attachment'])) {
95 1
            $this->setPath('builders/attachment', new Builder\AttachmentBuilder());
96 1
        }
97
98 1
        return $this['builders']['attachment'];
99
    }
100
101
    /**
102
     * @param mixed $message
103
     *
104
     * @throws UnexpectedTypeException
105
     *
106
     * @return array
107
     */
108 5
    private function toParameters($message)
109
    {
110 5
        if (is_string($message)) {
111 1
            return $this->getMessageBuilder()->setText($message)->create()->jsonSerialize();
112 4
        } elseif (is_array($message)) {
113 1
            return $message;
114 3
        } elseif ($message instanceof JsonSerializable) {
115 2
            return $message->jsonSerialize();
116
        }
117
118 1
        throw new UnexpectedTypeException($message, ['string', 'array', 'JsonSerializable']);
119
    }
120
}
121