ErrorLogHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 45
ccs 0
cts 11
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A assertValidMessageType() 0 6 2
A write() 0 4 1
1
<?php
2
3
namespace Oqq\Minc\Log\Handler;
4
5
use Oqq\Minc\Log\Record;
6
7
/**
8
 * @author Eric Braun <[email protected]>
9
 */
10
class ErrorLogHandler extends AbstractHandler
11
{
12
    const TYPE_PHP = 0;
13
    const TYPE_SAPI = 4;
14
15
    /** @var int */
16
    protected $messageType;
17
18
    /**
19
     * @param int $messageType
20
     * @param int $level
21
     * @param bool $pass
22
     */
23
    public function __construct(
24
        $messageType = self::TYPE_PHP,
25
        $level = HandlerInterface::DEFAULT_LEVEL,
26
        $pass = HandlerInterface::DEFAULT_PASS
27
    ) {
28
        $this->assertValidMessageType($messageType);
29
30
        parent::__construct($level, $pass);
31
32
        $this->messageType = $messageType;
33
    }
34
35
    /**
36
     * @param mixed $messageType
37
     *
38
     * @throws \InvalidArgumentException
39
     */
40
    protected function assertValidMessageType($messageType)
41
    {
42
        if (!in_array($messageType, [self::TYPE_PHP, self::TYPE_SAPI], true)) {
43
            throw new \InvalidArgumentException('MessageType "%s" is not valid', $messageType);
44
        }
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    protected function write(Record $record)
51
    {
52
        return error_log($this->getFormattedMessage($record), $this->messageType);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return error_log($this->...), $this->messageType); (boolean) is incompatible with the return type declared by the abstract method Oqq\Minc\Log\Handler\AbstractHandler::write of type null|false.

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...
53
    }
54
}
55