Writer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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\Detail;
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 StyleInterface.
32
     *
33
     * @var StyleInterface
34
     */
35
    private $io;
36
37
    /**
38
     * Holds the rows.
39
     *
40
     * @var Config
41
     */
42
    private $rows;
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function __construct(Config $config, StyleInterface $io)
48
    {
49
        $this->config = $config;
50
        $this->io = $io;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function write()
57
    {
58
        $this->io->title(current($this->rows));
59
        $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...
60
61
        return $this;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function setRows(array $rows)
68
    {
69
        $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...
70
71
        return $this;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getRows()
78
    {
79
        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...
80
    }
81
}
82