Passed
Push — master ( f10fb0...03a61c )
by Francis
01:20
created

Message::body()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 0
1
<?php
2
defined('BASEPATH') OR exit('No direct script access allowed');
3
4
class Message
5
{
6
  /**
7
   * [protected description]
8
   * @var [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
9
   */
10
  protected $message;
11
12
  public function __construct($jsonString)
13
  {
14
    $this->message = json_decode($jsonString);
15
  }
16
  /**
17
   * [header description]
18
   * @date   2019-11-22
19
   * @param  string     $key [description]
20
   * @return string          [description]
21
   */
22
  public function header(string $key):string
23
  {
24
    foreach ($this->message->payload->headers as $header) {
25
      if ($header->name == $key) {
26
        return $header->value;
27
      }
28
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
29
  }
30
  /**
31
   * [headers description]
32
   * @date   2019-11-22
33
   * @return array      [description]
34
   */
35
  public function headers():array
36
  {
37
    return $this->message->payload->headers;
38
  }
39
  /**
40
   * [body description]
41
   * @date   2019-11-22
42
   * @return string     [description]
43
   */
44
  public function body():?string
45
  {
46
    if (isset($this->message->payload->body->data)) {
47
      return base64url_decode($this->message->payload->body->data);
48
    }
49
    return null;
50
  }
51
}
52