Writer::__construct()   A
last analyzed

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
/**
4
 * This file is part of Laravel Meetups.
5
 *
6
 * (c) Nuno Maduro <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 */
11
12
namespace LaravelMeetups\Jobs\Catalog;
13
14
use LaravelMeetups\Contracts\Config;
15
use LaravelMeetups\Contracts\Jobs\Writer as Contract;
16
use Symfony\Component\Console\Style\StyleInterface;
17
18
/**
19
 * Class Writer.
20
 */
21
class Writer implements Contract
22
{
23
    /**
24
     * Holds a instance of config.
25
     *
26
     * @var Config
27
     */
28
    private $config;
29
30
    /**
31
     * Holds a instance of SymfonyStyle.
32
     *
33
     * @var Config
34
     */
35
    private $io;
36
37
    /**
38
     * Holds the headers.
39
     *
40
     * @var Config
41
     */
42
    private $headers;
43
44
    /**
45
     * Holds the rows.
46
     *
47
     * @var Config
48
     */
49
    private $rows;
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function __construct(Config $config, StyleInterface $io)
55
    {
56
        $this->config = $config;
57
        $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...
58
        $this->setHeaders();
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function write()
65
    {
66
        $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...
67
68
        return $this;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function setRows(array $rows)
75
    {
76
        $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...
77
78
        return $this;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function getRows()
85
    {
86
        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...
87
    }
88
89
    /**
90
     * Configures the table instance with
91
     * the headers provided by the config list elements.
92
     *
93
     * @return $this
94
     */
95
    private function setHeaders()
96
    {
97
        $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...
98
            return (new $elem())->getName();
99
        }, $this->config->getCatalogProviders());
100
101
        return $this;
102
    }
103
}
104