LeaderboardLadderEndpointController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A playerLadder() 0 14 1
B update() 0 33 4
A lastUpdate() 0 18 3
1
<?php
2
3
// @todo Go over this ENTIRE file again as there's been major refactors since and it's likely broken!
4
5
namespace Ps2alerts\Api\Controller\Endpoint\Leaderboards;
6
7
use Ps2alerts\Api\Controller\Endpoint\AbstractEndpointController;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
class LeaderboardLadderEndpointController extends AbstractEndpointController
12
{
13
    public function playerLadder(ServerRequestInterface $request, ResponseInterface $response, 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...
Unused Code introduced by
The parameter $response 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...
14
    {
15
        $redis = $this->getRedisDriver();
0 ignored issues
show
Bug introduced by
The method getRedisDriver() does not seem to exist on object<Ps2alerts\Api\Con...dderEndpointController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
16
        $metric = $args['metric'];
17
        $server = $args['server'];
18
19
        $list = "ps2alerts:api:leaderboards:players:{$metric}:listById-{$server}";
20
21
        var_dump($list);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($list); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
22
23
        $entries = $redis->get($list);
24
25
        var_dump($entries);
26
    }
27
28
    /**
29
     * Prompts the Leaderboard:Check command to resync the leaderboards
30
     *
31
     * @param  ServerRequestInterface  $request
32
     * @param  ResponseInterface $response
33
     *
34
     * @return ResponseInterface
35
     */
36
    public function update(ServerRequestInterface $request, ResponseInterface $response)
0 ignored issues
show
Coding Style introduced by
update uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
update uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
37
    {
38
        $config = $this->getConfig();
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
39
40
        // Only accept commands from internal IP
41
        $ip = $request->getClientIp();
0 ignored issues
show
Bug introduced by
The method getClientIp() does not seem to exist on object<Psr\Http\Message\ServerRequestInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
43
        if ($ip !== $_SERVER['SERVER_ADDR']) {
44
            $response->setStatusCode(404);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\ResponseInterface as the method setStatusCode() does only exist in the following implementations of said interface: Zend\Diactoros\Response, Zend\Diactoros\Response\EmptyResponse, Zend\Diactoros\Response\HtmlResponse, Zend\Diactoros\Response\JsonResponse, Zend\Diactoros\Response\RedirectResponse, Zend\Diactoros\Response\TextResponse.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
45
            return $response;
46
        }
47
48
        $server = $_GET['server'];
49
50
        $redis = $this->getRedisDriver();
0 ignored issues
show
Bug introduced by
The method getRedisDriver() does not seem to exist on object<Ps2alerts\Api\Con...dderEndpointController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
        $key = "ps2alerts:api:leaderboards:status:{$server}";
52
53
        // If we have a key, change the flag to force exists so the cronjob can run
54
        if ($redis->exists($key)) {
55
            $data = json_decode($redis->get($key));
56
57
             // Ignore if already flagged as being updated
58
            if ($data->beingUpdated == 0) {
59
                $data->forceUpdate = 1;
60
                $redis->set($key, json_encode($data));
61
            }
62
        } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
63
            // Panic.
64
        }
65
66
        $response = $response->withStatus(202);
67
        return $response;
68
    }
69
70
    /**
71
     * Returns a list of times that a server leaderboard has been updated
72
     *
73
     * @param  ServerRequestInterface  $request
74
     * @param  ResponseInterface $response
75
     *
76
     * @return ResponseInterface
77
     */
78
    public function lastUpdate(ServerRequestInterface $request, ResponseInterface $response)
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...
Unused Code introduced by
The parameter $response 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...
79
    {
80
        $config = $this->getConfig();
81
        $redis = $this->getRedisDriver();
0 ignored issues
show
Bug introduced by
The method getRedisDriver() does not seem to exist on object<Ps2alerts\Api\Con...dderEndpointController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
83
        $data = [];
84
85
        foreach($config['servers'] as $server) {
86
            $key = "ps2alerts:api:leaderboards:status:{$server}";
87
88
            if ($redis->exists($key)) {
89
                $entry = json_decode($redis->get($key));
90
                $data[$server] = $this->createItem($entry, new LeaderboardUpdatedTransformer);
0 ignored issues
show
Bug introduced by
The method createItem() does not seem to exist on object<Ps2alerts\Api\Con...dderEndpointController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
            }
92
        }
93
94
        return $this->respondWithData($data);
95
    }
96
}
97