AwesomeMarker::encode()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 0
crap 3
1
<?php
2
/**
3
 * @copyright Copyright (c) 2013-2015 2amigOS! Consulting Group LLC
4
 * @link http://2amigos.us
5
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
6
 */
7
namespace dosamigos\leaflet\plugins\awesome;
8
9
10
use dosamigos\leaflet\Plugin;
11
use yii\web\JsExpression;
12
use yii\helpers\Json;
13
14
/**
15
 * AwesomeMarker allows to create map icons using FontAwesome Icons.
16
 *
17
 * Font awesome files are required to be installed
18
 *
19
 * @see https://github.com/lvoogdt/Leaflet.awesome-markers
20
 * @author Antonio Ramirez <[email protected]>
21
 * @link http://www.ramirezcobos.com/
22
 * @link http://www.2amigos.us/
23
 * @package dosamigos\leaflet\plugins\awesome
24
 */
25
class AwesomeMarker extends Plugin
26
{
27
    /**
28
     * @var string the icon name
29
     * @see https://github.com/lvoogdt/Leaflet.awesome-markers#properties
30
     */
31
    public $icon;
32
33
    /**
34
     * Generates the code to generate a maki marker. Helper method made for speed purposes.
35
     *
36
     * @param string $icon the icon name
37
     * @param array $options the maki marker icon
38
     *
39
     * @return string the resulting js code
40
     */
41 3
    public function make($icon, $options = [])
42
    {
43 3
        $options['icon'] = $icon;
44 3
        $options = Json::encode($options);
45 3
        return new JsExpression("L.AwesomeMarkers.icon($options)");
46
    }
47
48
    /**
49
     * Returns the plugin name
50
     * @return string
51
     */
52 3
    public function getPluginName()
53
    {
54 3
        return 'plugin:awesomemarker';
55
    }
56
57
    /**
58
     * Registers plugin asset bundle
59
     *
60
     * @param \yii\web\View $view
61
     *
62
     * @return mixed
63
     * @codeCoverageIgnore
64
     */
65
    public function registerAssetBundle($view)
66
    {
67
        AwesomeMarkerAsset::register($view);
68
        return $this;
69
    }
70
71
    /**
72
     * Returns the javascript ready code for the object to render
73
     * @return \yii\web\JsExpression
74
     */
75 3
    public function encode()
76
    {
77 3
        $icon = $this->icon;
78
79 3
        if (empty($icon)) {
80 3
            return "";
0 ignored issues
show
Bug Best Practice introduced by
The return type of return ''; (string) is incompatible with the return type declared by the abstract method dosamigos\leaflet\Plugin::encode of type yii\web\JsExpression.

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...
81
        }
82 3
        $this->clientOptions['icon'] = $icon;
83 3
        $options = $this->getOptions();
84 3
        $name = $this->getName();
85
86 3
        $js = "L.AwesomeMarkers.icon($options)";
87
88 3
        if (!empty($name)) {
89 3
            $js = "var $name = $js;";
90 3
        }
91
92 3
        return new JsExpression($js);
93
    }
94
95
}
96