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

Writer::setValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

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 3
nc 1
nop 1
1
<?php
2
3
namespace LaravelMeetups\Jobs\Detail;
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 StyleInterface.
20
     *
21
     * @var StyleInterface
22
     */
23
    private $io;
24
25
    /**
26
     * Holds the rows.
27
     *
28
     * @var Config
29
     */
30
    private $rows;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function __construct(Config $config, StyleInterface $io)
36
    {
37
        $this->config = $config;
38
        $this->io = $io;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function write()
45
    {
46
        $this->io->title(current($this->rows));
47
        $this->io->listing($this->rows);
0 ignored issues
show
Documentation introduced by
$this->rows is of type object<LaravelMeetups\Contracts\Config>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
49
        return $this;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function setRows(array $rows)
56
    {
57
        $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...
58
59
        return $this;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getRows()
66
    {
67
        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...
68
    }
69
}