Passed
Push — master ( 5b5e9c...5b3e8e )
by Jay
04:01
created

Syslog   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getIdentifier() 0 4 1
A setIdentifier() 0 5 1
A configure() 0 5 1
1
<?php
2
namespace FMUP\Logger\Channel;
3
4
use FMUP\Logger\Channel;
5
use Monolog\Handler\SyslogHandler;
6
7
class Syslog extends Channel
8
{
9
    const NAME = 'Syslog';
10
11
    private $identifier = 'webapp';
12
13
    /**
14
     * Name of syslog application (identifier)
15
     * @return string
16
     */
17 2
    public function getIdentifier()
18
    {
19 2
        return $this->identifier;
20
    }
21
22
    /**
23
     * Define identifier for syslog
24
     * @param string $identifier
25
     * @return $this
26
     */
27 1
    public function setIdentifier($identifier)
28
    {
29 1
        $this->identifier = (string)$identifier;
30 1
        return $this;
31
    }
32
33
    /**
34
     * @return $this
35
     */
36 1
    public function configure()
37
    {
38 1
        $this->getLogger()->pushHandler(new SyslogHandler($this->getIdentifier()));
39 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (FMUP\Logger\Channel\Syslog) is incompatible with the return type declared by the abstract method FMUP\Logger\Channel::configure of type Monolog\Logger.

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...
40
    }
41
}
42