MakiMarker   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 71
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 6 1
A getPluginName() 0 4 1
A registerAssetBundle() 0 5 1
A encode() 0 19 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
8
namespace dosamigos\leaflet\plugins\makimarker;
9
10
11
use dosamigos\leaflet\Plugin;
12
use yii\web\JsExpression;
13
use yii\helpers\Json;
14
15
/**
16
 * MakiMarker allows to create map icons using Maki Icons from MapBox
17
 *
18
 * @see https://github.com/jseppi/Leaflet.MakiMarkers
19
 * @author Antonio Ramirez <[email protected]>
20
 * @link http://www.ramirezcobos.com/
21
 * @link http://www.2amigos.us/
22
 * @package dosamigos\leaflet\plugins\makimarker
23
 */
24
class MakiMarker extends Plugin
25
{
26
    /**
27
     * @var string the maki icon name
28
     * @see https://www.mapbox.com/maki/
29
     */
30
    public $icon;
31
32
    /**
33
     * Generates the code to generate a maki marker. Helper method made for speed purposes.
34
     *
35
     * @param string $icon the icon name
36
     * @param array $options the maki marker icon
37
     *
38
     * @return string the resulting js code
39
     */
40 3
    public function make($icon, $options = [])
41
    {
42 3
        $options['icon'] = $icon;
43 3
        $options = Json::encode($options);
44 3
        return new JsExpression("L.MakiMarkers.icon($options)");
45
    }
46
47
    /**
48
     * Returns the plugin name
49
     * @return string
50
     */
51 3
    public function getPluginName()
52
    {
53 3
        return 'plugin:makimarker';
54
    }
55
56
    /**
57
     * Registers plugin asset bundle
58
     *
59
     * @param \yii\web\View $view
60
     *
61
     * @return mixed
62
     * @codeCoverageIgnore
63
     */
64
    public function registerAssetBundle($view)
65
    {
66
        MakiMarkerAsset::register($view);
67
        return $this;
68
    }
69
70
    /**
71
     * Returns the javascript ready code for the object to render
72
     * @return \yii\web\JsExpression
73
     */
74 3
    public function encode()
75
    {
76 3
        $icon = $this->icon;
77
78 3
        if (empty($icon)) {
79 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...
80 3
        }
81 3
        $this->clientOptions['icon'] = $icon;
82 3
        $options = $this->getOptions();
83 3
        $name = $this->getName();
84
85 3
        $js = "L.MakiMarkers.icon($options)";
86
87 3
        if (!empty($name)) {
88 3
            $js = "var $name = $js;";
89 3
        }
90
91 3
        return new JsExpression($js);
92
    }
93
94
}
95