GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ElasticsMonitoring::getMetricName()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 8
rs 9.4285
c 1
b 0
f 1
1
<?php
2
namespace CloudWatchScript\Plugins;
3
4
use CloudWatchScript\AbstractMonitoring;
5
6
/**
7
 * Check Solr using ping URL.
8
 * Add and configure the folliwong lines to the config file
9
 * "Elastics" : {
10
 *         "name" : "Name of metric and alarm",
11
 *         "url": "http://127.0.0.1:9200/_cluster/health",
12
 *         "namespace": "Metric/Namespace",
13
 *         "description": "Description"
14
 * }
15
 */
16
class ElasticsMonitoring extends AbstractMonitoring
17
{
18
    private $elasticsUrl;
19
    /**
20
     * @param array $config
21
     * @param String $name
22
     */
23
    public function __construct($config, $name)
24
    {
25
        parent::__construct($config, $name);
26
        $this->elasticsUrl = $this->config->url;
27
    }
28
   /*
29
    * Check Elastics health check
30
    * @return array
31
    */
32
   public function getMetric()
33
   {
34
       $elasticsCheck = @file_get_contents($this->elasticsUrl);
35
       if ($elasticsCheck === false) {
36
           return 0;
37
       }
38
       $elasticsCheck = json_decode($elasticsCheck, true);
39
       $status = array("green" => 2, "yellow" => 1, "red" => 0);
40
       return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('status' =>...s_percent_as_number']); (array) is incompatible with the return type declared by the abstract method CloudWatchScript\AbstractMonitoring::getMetric of type string.

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...
41
         "status" => $status[$elasticsCheck['status']],
42
         "number_of_nodes" => $elasticsCheck['number_of_nodes'],
43
         "number_of_data_nodes" => $elasticsCheck['number_of_data_nodes'],
44
         "active_primary_shards" => $elasticsCheck['active_primary_shards'],
45
         "active_shards" => $elasticsCheck['active_shards'],
46
         "unassigned_shards" => $elasticsCheck['unassigned_shards'],
47
         "active_shards_percent_as_number" => $elasticsCheck['active_shards_percent_as_number']
48
       );
49
   }
50
51
   /**
52
    * @return array
53
    */
54
   public function getUnit($alarmName=null)
55
   {
56
       if($alarmName === null) {
57
         return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('status' =>..._number' => 'Percent'); (array<string,string>) is incompatible with the return type declared by the abstract method CloudWatchScript\AbstractMonitoring::getUnit of type integer.

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...
58
         "status" => "None",
59
         "number_of_nodes" => "Count",
60
         "number_of_data_nodes" => "Count",
61
         "active_primary_shards" => "Count",
62
         "active_shards" => "Count",
63
         "unassigned_shards" => "Count",
64
         "active_shards_percent_as_number" => "Percent"
65
       );
66
     }
67
     else {
68
       switch ($alarmName) {
69
         case $this->name . " status warning":
70
           return "None";
71
         case $this->name . " status error":
72
           return "None";
73
       }
74
     }
75
   }
76
77
    /**
78
     * @return integer 1
79
     */
80
    public function getAlarms()
81
    {
82
        return array(
83
                    array("ComparisonOperator" => "LessThanThreshold",
84
                          "Threshold" => 2,
85
                          "Name" => $this->name . " status warning"
86
                    ),
87
                    array("ComparisonOperator" => "LessThanThreshold",
88
                          "Threshold" => 1,
89
                          "Name" => $this->name . " status error"
90
                    )
91
        );
92
    }
93
94
    /**
95
     * @return The metrics name associate to an alarm name.
96
     */
97
    public function getMetricName($alarm) {
98
      switch ($alarm) {
99
        case $this->name . " status warning":
100
          return $this->name . " status";
101
        case $this->name . " status error":
102
          return $this->name . " status";
103
      }
104
    }
105
}
106