JS::resolveSettings()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 0
dl 0
loc 20
ccs 8
cts 8
cp 1
crap 3
rs 9.6
c 0
b 0
f 0
1
<?php namespace Rocket\UI\Script\Support\Laravel5;
2
3
use Illuminate\Contracts\Support\Arrayable;
4
use Illuminate\Contracts\Support\Jsonable;
5
6
/**
7
 * Javascript class
8
 *
9
 * Store all javascript needed for the page to output it in a single block at the end of the page
10
 */
11
class JS extends \Rocket\UI\Script\JS
12
{
13
    /**
14
     * Walk the settings array to output them as a pure array
15
     *
16 12
     * @return string
17
     */
18 12
    protected function resolveSettings()
19 12
    {
20
        $queue_copy = $this->queue['setting'];
21
22
        array_walk_recursive(
23
            $queue_copy,
24
            function (&$content) {
25
26 12
                if ($content instanceof Arrayable) {
27
                    $content = $content->toArray();
28 12
                }
29
30 12
                if ($content instanceof Jsonable) {
31 12
                    $content = $content->toJson();
32
                }
33
            }
34 12
        );
35 3
36
        return $queue_copy;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $queue_copy; (array) is incompatible with the return type of the parent method Rocket\UI\Script\JS::resolveSettings 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...
37
    }
38 12
39 3
    /**
40
     * This method is used by Twig/TwigBridge when adding scripts in templates
41 12
     * @return string
42
     */
43
    public function __toString()
44 12
    {
45
        return '';
46
    }
47
}
48