Completed
Pull Request — master (#18)
by Randy
01:46
created

JSendResponseTrait::getStatusCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Demv\JSend;
4
5
use Dgame\Ensurance\Exception\EnsuranceException;
6
use GuzzleHttp\Psr7\MessageTrait;
7
use function Dgame\Ensurance\ensure;
8
9
/**
10
 * Trait JSendResponseTrait
11
 * @package Demv\JSend
12
 */
13
trait JSendResponseTrait
14
{
15
    use MessageTrait;
16
17
    /**
18
     * @var string
19
     */
20
    private $message;
21
    /**
22
     * @var int|null
23
     */
24
    private $code;
25
26
    /**
27
     * @param int|null $code
28
     */
29
    final public function respond(int $code = null): void
30
    {
31
        $code = $code ?? $this->getStatusCode();
32
        ensure($code)->isInt()->isBetween(100, 511);
33
        header('Content-Type: application/json; charset="UTF-8"', true, $code);
34
        print json_encode($this);
35
        exit;
36
    }
37
38
    /**
39
     * @return int
40
     */
41
    final public function getStatusCode()
42
    {
43
        return $this->code ?? $this->getDefaultHttpStatusCode();
0 ignored issues
show
Bug introduced by
It seems like getDefaultHttpStatusCode() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
44
    }
45
46
    /**
47
     * @param        $code
48
     * @param string $reasonPhrase
49
     *
50
     * @return JSend|JSendInterface
51
     * @throws EnsuranceException
52
     */
53
    public function withStatus($code, $reasonPhrase = '')
54
    {
55
        $decoded           = $this->asArray();
0 ignored issues
show
Bug introduced by
It seems like asArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
56
        $decoded['status'] = (string) Status::fromStatusCode($code ?? $this->getDefaultHttpStatusCode());
0 ignored issues
show
Bug introduced by
It seems like getDefaultHttpStatusCode() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
57
58
        $jsend       = JSend::from($decoded)->into();
59
        $jsend->code = $code;
0 ignored issues
show
Bug introduced by
Accessing code on the interface Demv\JSend\JSendInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
60
        if (!empty($reasonPhrase)) {
61
            $jsend->message = $reasonPhrase;
0 ignored issues
show
Bug introduced by
Accessing message on the interface Demv\JSend\JSendInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
62
        }
63
64
        return $jsend;
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function getReasonPhrase()
71
    {
72
        return $this->message;
73
    }
74
}
75