Completed
Push — master ( d391fa...20fef6 )
by Daniel
02:31
created

knownCloudFlareJavascript()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4286
cc 3
eloc 13
nc 3
nop 1
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2015 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\common_lib;
30
31
/**
32
 * DOM component functions
33
 *
34
 * @author Daniel Popiniuc
35
 */
36
trait DomComponentsByDanielGPwithCDN
37
{
38
39
    private function knownCloudFlareJavascript($jsFileName)
40
    {
41
        $justFile = pathinfo($jsFileName)['basename'];
42
        switch ($justFile) {
43
            case 'jquery.placeholder.min.js':
44
                $version             = 'jquery-placeholder/2.0.8/';
45
                $fnExistanceToVerify = 'jQuery.placeholder';
46
                break;
47
            case 'jquery.easing.1.3.min.js':
48
                $version             = 'jquery-easing/1.3/';
49
                $fnExistanceToVerify = 'jQuery.easing["jswing"]';
50
                $justFile            = str_replace('.1.3', '', $justFile);
51
                break;
52
        }
53
        return ['justFile' => $justFile, 'version' => $version, 'ExistanceToVerify' => $fnExistanceToVerify];
0 ignored issues
show
Bug introduced by
The variable $version does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $fnExistanceToVerify does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
54
    }
55
56
    /**
57
     * Manages all known CSS that can be handled through CDNs
58
     *
59
     * @param string $cssFileName
60
     * @return array
61
     */
62
    protected function setCssFileCDN($cssFileName)
63
    {
64
        $onlyFileName = pathinfo($cssFileName)['basename'];
65
        if (strpos($onlyFileName, 'font-awesome-') !== false) {
66
            $patternFound = $this->setCssFileCDNforFontAwesome($cssFileName);
67
        } else {
68
            $patternFound = null;
69
        }
70 View Code Duplication
        if (is_null($patternFound)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
71
            $patternFound = [
72
                false,
73
                filter_var($cssFileName, FILTER_SANITIZE_STRING),
74
            ];
75
        }
76
        return $patternFound;
77
    }
78
79
    /**
80
     * Returns css link to a given file
81
     * Returns an array with CDN call of a known Font-websome css
82
     *
83
     * @param string $cssFile
0 ignored issues
show
Documentation introduced by
There is no parameter named $cssFile. Did you maybe mean $cssFileName?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
84
     * @return string
85
     */
86
    private function setCssFileCDNforFontAwesome($cssFileName)
87
    {
88
        $patternFound = [
89
            true,
90
            implode('', [
91
                '//cdnjs.cloudflare.com/ajax/libs/font-awesome/',
92
                str_replace(['font-awesome-', '.min.css'], '', pathinfo($cssFileName)['basename']),
93
                '/css/font-awesome.min.css',
94
            ])
95
        ];
96
        return $patternFound;
97
    }
98
99
    /**
100
     * Manages all known Javascript that can be handled through CDNs
101
     *
102
     * @param string $jsFileName
103
     * @return array
104
     */
105
    protected function setJavascriptFileCDN($jsFileName)
106
    {
107
        $onlyFileName = pathinfo($jsFileName)['basename'];
108
        /**
109
         * if within local network makes no sense to use CDNs
110
         */
111
        if (strpos($onlyFileName, 'jquery-') !== false) {
112
            $patternFound = $this->setJavascriptFileCDNjQuery($jsFileName);
113
        } elseif (strpos($onlyFileName, 'jquery.placeholder.min.js') !== false) {
114
            $patternFound = $this->setJavascriptFileCDNjQueryLibs($jsFileName);
115
        } elseif (strpos($onlyFileName, 'jquery.easing.1.3.min.js') !== false) {
116
            $patternFound = $this->setJavascriptFileCDNjQueryLibs($jsFileName);
117
        } elseif (strpos($onlyFileName, 'highcharts-') !== false) {
118
            $patternFound = $this->setJavascriptFileCDNforHighCharts($jsFileName);
119
        } elseif (strpos($onlyFileName, 'exporting-') !== false) {
120
            $patternFound = $this->setJavascriptFileCDNforHighChartsExporting($jsFileName);
121
        } else {
122
            $patternFound = null;
123
        }
124 View Code Duplication
        if (is_null($patternFound)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
125
            $patternFound = [
126
                false,
127
                filter_var($jsFileName, FILTER_SANITIZE_STRING),
128
                '',
129
            ];
130
        }
131
        return $patternFound;
132
    }
133
134
    /**
135
     * Returns an array with CDN call of a known Javascript library
136
     * and fall-back line that points to local cache of it
137
     * specific for HighCharts
138
     *
139
     * @param string $jsFileName
140
     * @return array
141
     */
142 View Code Duplication
    private function setJavascriptFileCDNforHighCharts($jsFileName)
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...
143
    {
144
        $patternFound   = null;
145
        $jQueryPosition = strpos($jsFileName, 'highcharts');
146
        if ($jQueryPosition !== false) {
147
            $patternFound = [
148
                true,
149
                implode('', [
150
                    '//cdnjs.cloudflare.com/ajax/libs/highcharts/',
151
                    str_replace(['highcharts-', '.js'], '', pathinfo($jsFileName)['basename']),
152
                    '/highcharts.js',
153
                ]),
154
                implode('', [
155
                    '<script>!window.Highcharts && document.write(\'<script src="',
156
                    filter_var($jsFileName, FILTER_SANITIZE_STRING),
157
                    '">\x3C/script>\')</script>'
158
                ])
159
            ];
160
        }
161
        return $patternFound;
162
    }
163
164
    /**
165
     * Returns an array with CDN call of a known Javascript library
166
     * and fall-back line that points to local cache of it
167
     * specific for HighCharts Exporting feature
168
     *
169
     * @param string $jsFileName
170
     * @return array
171
     */
172 View Code Duplication
    private function setJavascriptFileCDNforHighChartsExporting($jsFileName)
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...
173
    {
174
        $patternFound   = null;
175
        $jQueryPosition = strpos($jsFileName, 'exporting');
176
        if ($jQueryPosition !== false) {
177
            $patternFound = [
178
                true,
179
                implode('', [
180
                    '//cdnjs.cloudflare.com/ajax/libs/highcharts/',
181
                    str_replace(['exporting-', '.js'], '', pathinfo($jsFileName)['basename']),
182
                    '/modules/exporting.js',
183
                ]),
184
                implode('', [
185
                    '<script>!window.Highcharts.post && document.write(\'<script src="',
186
                    filter_var($jsFileName, FILTER_SANITIZE_STRING),
187
                    '">\x3C/script>\')</script>'
188
                ])
189
            ];
190
        }
191
        return $patternFound;
192
    }
193
194
    /**
195
     * Returns an array with CDN call of a known Javascript library
196
     * and fall-back line that points to local cache of it
197
     * specific for jQuery
198
     *
199
     * @param string $jsFileName
200
     * @return array
201
     */
202
    private function setJavascriptFileCDNjQuery($jsFileName)
203
    {
204
        $patternFound       = null;
205
        $jQueryPosition     = strpos($jsFileName, 'jquery-');
206
        $jQueryMajorVersion = substr($jsFileName, 7, 1);
207
        if (($jQueryPosition !== false) && is_numeric($jQueryMajorVersion) && (substr($jsFileName, -7) == '.min.js')) {
208
            $patternFound = [
209
                true,
210
                implode('', [
211
                    '//cdnjs.cloudflare.com/ajax/libs/jquery/',
212
                    str_replace(['jquery-', '.min.js'], '', pathinfo($jsFileName)['basename']),
213
                    '/jquery.min.js',
214
                ]),
215
                implode('', [
216
                    '<script>window.jQuery || document.write(\'<script src="',
217
                    filter_var($jsFileName, FILTER_SANITIZE_STRING),
218
                    '">\x3C/script>\')</script>'
219
                ])
220
            ];
221
        }
222
        return $patternFound;
223
    }
224
225
    /**
226
     * Returns an array with CDN call of a known Javascript library
227
     * and fall-back line that points to local cache of it
228
     * specific for jQuery Libraries
229
     *
230
     * @param string $jsFileName
231
     * @return array
232
     */
233
    private function setJavascriptFileCDNjQueryLibs($jsFileName)
234
    {
235
        $patternFound = null;
236
        $eArray       = $this->knownCloudFlareJavascript(filter_var($jsFileName, FILTER_SANITIZE_STRING));
237
        if (!is_null($eArray['version'])) {
238
            $patternFound = [
239
                true,
240
                implode('', [
241
                    '//cdnjs.cloudflare.com/ajax/libs/',
242
                    $eArray['version'],
243
                    $eArray['justFile'],
244
                ]),
245
                implode('', [
246
                    '<script>' . $eArray['ExistanceToVerify'] . ' || document.write(\'<script src="',
247
                    filter_var($jsFileName, FILTER_SANITIZE_STRING),
248
                    '">\x3C/script>\')</script>'
249
                ])
250
            ];
251
        }
252
        return $patternFound;
253
    }
254
}
255