Completed
Push — master ( 0cb788...5f9089 )
by Matthew
03:43
created

AlertEndpointController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 50
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A listLatest() 0 10 2
A listActive() 0 10 2
1
<?php
2
3
namespace Ps2alerts\Api\Controller\Alerts;
4
5
use League\Route\Http\JsonResponse as Response;
6
use Ps2alerts\Api\Controller\EndpointBaseController;
7
use Ps2alerts\Api\QueryObjects\QueryObject;
8
use Ps2alerts\Api\Loader\AlertLoader;
9
use Symfony\Component\HttpFoundation\Request;
10
11
class AlertEndpointController extends EndpointBaseController
12
{
13
    /**
14
     * Construct
15
     *
16
     * @param \Ps2alerts\Api\Loader\AlertLoader $loader
17
     */
18
    public function __construct(AlertLoader $loader)
19
    {
20
        $this->loader = $loader;
21
    }
22
23
    /**
24
     * List recent alerts in the last 48 hours
25
     *
26
     * @param \Symfony\Component\HttpFoundation\Request $request
27
     * @param array                                     $args
28
     *
29
     * @return \League\Route\Http\JsonResponse
30
     */
31
    public function listLatest(Request $request, array $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
    {
33
        $return = $this->loader->readRecent($args);
0 ignored issues
show
Bug introduced by
The method readRecent does only exist in Ps2alerts\Api\Loader\AlertLoader, but not in Ps2alerts\Api\Loader\Met...\PlayerStatisticsLoader.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
34
35
        if (empty($return)) {
36
            return new Response\NoContent();
37
        }
38
39
        return new Response\Ok($return);
40
    }
41
42
    /**
43
     * List alerts that are currently active
44
     *
45
     * @param \Symfony\Component\HttpFoundation\Request $request
46
     * @param array                                     $args
47
     *
48
     * @return \League\Route\Http\JsonResponse
49
     */
50
    public function listActive(Request $request, array $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
    {
52
        $return = $this->loader->readActive($args);
0 ignored issues
show
Bug introduced by
The method readActive does only exist in Ps2alerts\Api\Loader\AlertLoader, but not in Ps2alerts\Api\Loader\Met...\PlayerStatisticsLoader.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
53
54
        if (empty($return)) {
55
            return new Response\NoContent();
56
        }
57
58
        return new Response\Ok($return);
59
    }
60
}
61