Completed
Push — master ( 62e1cb...b85dc3 )
by Matthew
02:30
created

WeaponMetricsEndpoint::readSingleByMetric()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 15
loc 15
rs 9.4286
cc 2
eloc 8
nc 2
nop 2
1
<?php
2
3
namespace Ps2alerts\Api\Controller\Metrics;
4
5
use League\Route\Http\JsonResponse as Response;
6
use Ps2alerts\Api\Controller\EndpointBaseController;
7
use Ps2alerts\Api\Loader\Metrics\WeaponMetricsLoader;
8
use Symfony\Component\HttpFoundation\Request;
9
10 View Code Duplication
class WeaponMetricsEndpoint extends EndpointBaseController
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
11
{
12
    /**
13
     * Construct
14
     *
15
     * @param \Ps2alerts\Api\Loader\Metrics\WeaponMetricsLoader $loader
16
     */
17
    public function __construct(WeaponMetricsLoader $loader)
18
    {
19
        $this->loader = $loader;
20
    }
21
22
    /**
23
     * Returns a single entry by metric
24
     *
25
     * @param  Symfony\Component\HttpFoundation\Request $request
26
     * @param  array                                    $args
27
     *
28
     * @return \League\Route\Http\JsonResponse
29
     */
30
    public function readSingleByMetric(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...
31
    {
32
        $this->loader->setMetrics([
0 ignored issues
show
Bug introduced by
The method setMetrics does only exist in Ps2alerts\Api\Loader\Met...ics\WeaponMetricsLoader, but not in Ps2alerts\Api\Loader\Res...tfitTotalsMetricsLoader.

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...
33
            'col'   => 'weaponID',
34
            'value' => $args['weaponID']
35
        ]);
36
37
        $return = $this->loader->readSingle($args['resultID']);
0 ignored issues
show
Bug introduced by
The method readSingle does only exist in Ps2alerts\Api\Loader\Met...Api\Loader\ResultLoader, but not in Ps2alerts\Api\Loader\Sta...tfitTotalsMetricsLoader.

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...
38
39
        if (empty($return)) {
40
            return new Response\NoContent();
41
        }
42
43
        return new Response\Ok($return);
44
    }
45
}
46