Completed
Pull Request — 2.2 (#5)
by Pierre
01:56
created

WebLibraryManager   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 151
Duplicated Lines 37.75 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 20
c 2
b 0
f 1
lcom 1
cbo 3
dl 57
loc 151
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B addLibrary() 0 17 5
A setWebLibraries() 0 5 2
A toHtml() 0 12 1
A toCss() 19 19 4
A toJs() 19 19 4
A toAdditional() 19 19 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Mouf\Html\Utils\WebLibraryManager;
3
4
use Mouf\MoufManager;
5
6
use Mouf\Html\HtmlElement\HtmlElementInterface;
7
8
/**
9
 * This class is in charge of including and keeping track of Javascript and CSS libraries into an HTML page.
10
 * <p>JS and CSS files are grouped into <b>WebLibraries</b>. If you want to add a new library, just create an instance
11
 * of the <b>WebLibrary</b> class, and add it to the <b>WebLibraryManager</b>.</p>
12
 * <p>You can use the <b>WebLibraryManager</b> class to add JS/CSS libraries. It will keep track of dependencies and ensure each file is included
13
 * only once.</p>
14
15
 * <p>If you have specific needs and don't want to use the <b>WebLibrary</b> class, you can either create your own class
16
 * that implements the WebLibraryInterface, or provide your own "renderer".</p>
17
 *
18
 *
19
 * @author David Négrier
20
 * @Component
21
 */
22
class WebLibraryManager implements HtmlElementInterface {
23
    const CSS = 'css';
24
    const JS = 'js';
25
    const ADDITIONAL = 'additional';
26
27
    /**
28
     * The array of all included libraries.
29
     *
30
     * @var array<WebLibraryInterface>
31
     */
32
    private $webLibraries = array();
33
34
    /**
35
     * false if the toHtml method has not yet been called, true if it has been called.
36
     * @var boolean
37
     */
38
    private $rendered = false;
39
40
    /**
41
     * Adds a library to the list of libraries that should be loaded in the web page.
42
     * <p>The function will also load the dependencies (if any) and will have no effect if the library has already been loaded.</p>
43
     *
44
     * @param WebLibraryInterface $library
45
     */
46
    public function addLibrary(WebLibraryInterface $library) {
47
        if ($this->rendered) {
48
            throw new WebLibraryException("The libraries have already been rendered. This call to addLibrary should be performed BEFORE the toHtml method of WebLibraryManager is called.");
49
        }
50
        if (array_search($library, $this->webLibraries) === false) {
51
            // Let's start by adding dependencies.
52
            $dependencies = $library->getDependencies();
53
            if ($dependencies) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $dependencies of type Mouf\Html\Utils\WebLibra...r\WebLibraryInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
54
                foreach ($dependencies as $dependency) {
55
                    /* @var $dependency WebLibraryInterface */
56
                    $this->addLibrary($dependency);
57
                }
58
            }
59
60
            $this->webLibraries[] = $library;
61
        }
62
    }
63
64
    // TODO: add: addJs and addCss file
65
66
    /**
67
     * The list of all libraries that should be loaded in the web page.
68
     * <p>If you do not pass all dependencies of a library, the dependencies will be loaded automatically.</p>
69
     *
70
     * @Property
71
     * @param array<WebLibraryInterface> $libraries
72
     */
73
    public function setWebLibraries($libraries) {
74
        foreach ($libraries as $library) {
75
            $this->addLibrary($library);
76
        }
77
    }
78
79
    /**
80
     * Renders the HTML in charge of loading CSS and JS files.
81
     * The Html is echoed directly into the output.
82
     * This function should be called within the head tag.
83
     *
84
     */
85
    public function toHtml()
86
    {
87
        /*if ($this->rendered) {
88
            throw new WebLibraryException("The library has already been rendered.");
89
        }*/
90
91
        $this->cssToHtml();
0 ignored issues
show
Bug introduced by
The method cssToHtml() does not exist on Mouf\Html\Utils\WebLibra...nager\WebLibraryManager. Did you maybe mean toHtml()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
92
        $this->jsToHtml();
0 ignored issues
show
Bug introduced by
The method jsToHtml() does not exist on Mouf\Html\Utils\WebLibra...nager\WebLibraryManager. Did you maybe mean toHtml()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
93
        $this->additionalToHtml();
0 ignored issues
show
Bug introduced by
The method additionalToHtml() does not exist on Mouf\Html\Utils\WebLibra...nager\WebLibraryManager. Did you maybe mean toHtml()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
94
95
        $this->rendered = true;
96
    }
97
98
    /**
99
     * Renders the HTML in charge of loading CSS files.
100
     * The Html is echoed directly into the output.
101
     *
102
     */
103 View Code Duplication
    public function toCss()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105
        $defaultWebLibraryRenderer = null;
106
107
        foreach ($this->webLibraries as $library) {
108
            /* @var $library WebLibraryInterface */
109
            $renderer = $library->getRenderer();
110
            if ($renderer == null) {
111
                if ($defaultWebLibraryRenderer == null) {
112
                    $defaultWebLibraryRenderer = MoufManager::getMoufManager()->getInstance(
113
                        'defaultWebLibraryRenderer'
114
                    );
115
                }
116
                $renderer = $defaultWebLibraryRenderer;
117
            }
118
            /* @var $renderer WebLibraryRendererInterface */
119
            $renderer->toCssHtml($library);
120
        }
121
    }
122
123
    /**
124
     * Renders the HTML in charge of loading JS files.
125
     * The Html is echoed directly into the output.
126
     *
127
     */
128 View Code Duplication
    public function toJs()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        $defaultWebLibraryRenderer = null;
131
132
        foreach ($this->webLibraries as $library) {
133
            /* @var $library WebLibraryInterface */
134
            $renderer = $library->getRenderer();
135
            if ($renderer == null) {
136
                if ($defaultWebLibraryRenderer == null) {
137
                    $defaultWebLibraryRenderer = MoufManager::getMoufManager()->getInstance(
138
                        'defaultWebLibraryRenderer'
139
                    );
140
                }
141
                $renderer = $defaultWebLibraryRenderer;
142
            }
143
            /* @var $renderer WebLibraryRendererInterface */
144
            $renderer->toJsHtml($library);
145
        }
146
    }
147
148
    /**
149
     * Renders the HTML in charge of loading additional files.
150
     * The Html is echoed directly into the output.
151
     *
152
     */
153 View Code Duplication
    public function toAdditional()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155
        $defaultWebLibraryRenderer = null;
156
157
        foreach ($this->webLibraries as $library) {
158
            /* @var $library WebLibraryInterface */
159
            $renderer = $library->getRenderer();
160
            if ($renderer == null) {
161
                if ($defaultWebLibraryRenderer == null) {
162
                    $defaultWebLibraryRenderer = MoufManager::getMoufManager()->getInstance(
163
                        'defaultWebLibraryRenderer'
164
                    );
165
                }
166
                $renderer = $defaultWebLibraryRenderer;
167
            }
168
            /* @var $renderer WebLibraryRendererInterface */
169
            $renderer->toAdditionalHtml($library);
170
        }
171
    }
172
}
173
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...