JS   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveSettings() 0 20 3
A __toString() 0 4 1
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