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

AbstractMetricsLoader::getMetrics()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Ps2alerts\Api\Loader\Metrics;
4
5
use Ps2alerts\Api\Loader\AbstractLoader;
6
use Ps2alerts\Api\QueryObjects\QueryObject;
7
8
abstract class AbstractMetricsLoader extends AbstractLoader
9
{
10
    /**
11
     * Allows injection of where statements
12
     *
13
     * @var array
14
     */
15
    protected $metrics = [];
16
17
    /**
18
     * Returns metrics for a particular result
19
     *
20
     * @param string $id
21
     *
22
     * @return array
23
     */
24
    public function readSingle($id)
25
    {
26
        $redisKey = "{$this->getCacheNamespace()}{$id}:{$this->getType()}";
27
28
        if (! empty($this->getMetrics())) {
29
            foreach ($this->getMetrics() as $metric) {
30
                $redisKey .= ":{$metric['col']}{$op}{$metric['value']}";
0 ignored issues
show
Bug introduced by
The variable $op seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
31
            }
32
        }
33
34
        if ($this->checkRedis($redisKey)) {
35
            return $this->getFromRedis($redisKey);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getFromRedis($redisKey); (string) is incompatible with the return type documented by Ps2alerts\Api\Loader\Met...tricsLoader::readSingle of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
36
        }
37
38
        $queryObject = new QueryObject;
39
        $queryObject->addWhere([
40
            'col'   => 'result',
41
            'value' => $id
42
        ]);
43
44
        if (! empty($this->getMetrics())) {
45
            foreach ($this->getMetrics() as $metric) {
46
                $op = (isset($metric['op']) ? $metric['op'] : '=');
47
                $queryObject->addWhere([
48
                    'col'   => $metric['col'],
49
                    'op'    => $op,
50
                    'value' => $metric['value']
51
                ]);
52
            }
53
        }
54
55
        return $this->cacheAndReturn(
56
            $this->repository->read($queryObject),
0 ignored issues
show
Bug introduced by
The property repository does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
57
            $redisKey
58
        );
59
    }
60
61
    /**
62
     * Allows setting of metrics to filter
63
     *
64
     * @param array
65
     */
66
    public function setMetrics($metric)
67
    {
68
        if (! empty($metric)) {
69
            // Don't allow setting if the proper data isn't there.
70
            // Prevents and kind of errors later on.
71
            if (! empty($metric['col']) && ! empty($metric['value'])) {
72
                $this->metrics[] = $metric;
73
            }
74
        }
75
    }
76
77
    /**
78
     * Pulls in metrics
79
     *
80
     * @return array
81
     */
82
    public function getMetrics()
83
    {
84
        return $this->metrics;
85
    }
86
}
87