Completed
Branch master (f97dd7)
by Nuno
04:04 queued 01:50
created

Writer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace LaravelMeetups\Jobs\Catalog;
4
5
use LaravelMeetups\Contracts\Jobs\Writer as Contract;
6
use Symfony\Component\Console\Style\StyleInterface;
7
use LaravelMeetups\Contracts\Config;
8
9
class Writer implements Contract
10
{
11
    /**
12
     * Holds a instance of config.
13
     *
14
     * @var Config
15
     */
16
    private $config;
17
18
    /**
19
     * Holds a instance of SymfonyStyle.
20
     *
21
     * @var Config
22
     */
23
    private $io;
24
25
    /**
26
     * Holds the headers.
27
     *
28
     * @var Config
29
     */
30
    private $headers;
31
32
    /**
33
     * Holds the rows.
34
     *
35
     * @var Config
36
     */
37
    private $rows;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function __construct(Config $config, StyleInterface $io)
43
    {
44
        $this->config = $config;
45
        $this->io = $io;
0 ignored issues
show
Documentation Bug introduced by
It seems like $io of type object<Symfony\Component...e\Style\StyleInterface> is incompatible with the declared type object<LaravelMeetups\Contracts\Config> of property $io.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
        $this->setHeaders();
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function write()
53
    {
54
        $this->io->table($this->headers, $this->rows);
0 ignored issues
show
Bug introduced by
The method table() does not seem to exist on object<LaravelMeetups\Contracts\Config>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
56
        return $this;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function setRows(array $rows)
63
    {
64
        $this->rows = $rows;
0 ignored issues
show
Documentation Bug introduced by
It seems like $rows of type array is incompatible with the declared type object<LaravelMeetups\Contracts\Config> of property $rows.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
65
66
        return $this;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getRows()
73
    {
74
        return $this->rows;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->rows; (LaravelMeetups\Contracts\Config) is incompatible with the return type declared by the interface LaravelMeetups\Contracts\Jobs\Writer::getRows 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...
75
    }
76
77
    /**
78
     * Configures the table instance with
79
     * the headers provided by the config list elements.
80
     *
81
     * @return $this
82
     */
83
    private function setHeaders()
84
    {
85
        $this->headers = array_map(function($elem) {
0 ignored issues
show
Documentation Bug introduced by
It seems like array_map(function ($ele...>getCatalogProviders()) of type array is incompatible with the declared type object<LaravelMeetups\Contracts\Config> of property $headers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
86
            return (new $elem)->getName();
87
        }, $this->config->getCatalogProviders());
88
89
        return $this;
90
    }
91
}