Completed
Push — master ( d1f4bd...fa732c )
by Ross
8s
created

SimplePropertyNormalizer::formatValue()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 8.8571
cc 5
eloc 12
nc 5
nop 1
1
<?php
2
namespace League\Tactician\Logger\PropertyNormalizer;
3
4
use ReflectionClass;
5
6
/**
7
 * Quick'n'dirty property normalizer that logs the first level properties
8
 *
9
 * This is done in an extremely inefficient manner, so please never use this in
10
 * a production context, only for local debugging.
11
 */
12
class SimplePropertyNormalizer implements PropertyNormalizer
13
{
14
    /**
15
     * @param object $command
16
     * @return array
17
     */
18
    public function normalize($command)
19
    {
20
        $reflectionClass = new ReflectionClass(get_class($command));
21
22
        $properties = [];
23
        foreach ($reflectionClass->getProperties() as $property) {
24
            $property->setAccessible(true);
25
            $properties[$property->getName()] = $this->formatValue($property->getValue($command));
26
        }
27
28
        return $properties;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $properties; (array) is incompatible with the return type declared by the interface League\Tactician\Logger\...tyNormalizer::normalize 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...
29
    }
30
31
    /**
32
     * Return the given (property) value as a descriptive string
33
     *
34
     * @param mixed $value Can be literally anything
35
     * @return string
36
     */
37
    protected function formatValue($value)
38
    {
39
        switch (gettype($value)) {
40
            case 'object':
41
                return 'object(' . get_class($value) . ')';
42
            case 'array':
43
                return '*array*';
44
            case 'resource':
45
                return 'resource(' . get_resource_type($value) . ')';
46
            case 'NULL':
47
                return '*null*';
48
            default:
49
                return $value;
50
        }
51
    }
52
}
53