Completed
Pull Request — development (#2329)
by Joshua
09:15
created

Theme::addJavascriptVar()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 8.8571
cc 5
eloc 5
nc 4
nop 2
crap 5
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 23 and the first side effect is on line 20.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * The main theme class
5
 *
6
 * @name      ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
9
 *
10
 * This software is a derived product, based on:
11
 *
12
 * Simple Machines Forum (SMF)
13
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
14
 * license:		BSD, See included LICENSE.TXT for terms and conditions.
15
 *
16
 * @version 1.1 dev
17
 *
18
 */
19
20 1
if (!defined('ELK'))
21 1
    die('No access...');
22
23
abstract class Theme
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
    const STANDARD = 'standard';
26
    const DEFERRED = 'defer';
27
    const ALL = -1;
28
29
    protected $id;
30
31
    protected $templates;
32
    protected $layers;
33
34
    protected $html_headers = array();
35
    protected $links = array();
36
    protected $js_files = array();
37
    protected $js_inline = array(
38
        'standard'  => array(),
39
        'defer'     => array()
40
    );
41
    protected $js_vars = array();
42
    protected $css_files = array();
43
44
    protected $rtl;
45
46
    /**
47
     * @param int $id
48
     */
49 1
    public function __construct($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
__construct uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
50
    {
51 1
        $this->layers = Template_Layers::getInstance();
52 1
        $this->templates = Templates::getInstance();
53
54 1
        $this->css_files = &$GLOBALS['context']['css_files'];
55 1
        $this->js_files = &$GLOBALS['context']['js_files'];
56 1
    }
57
58
    /**
59
     * Add a Javascript variable for output later (for feeding text strings and similar to JS)
60
     *
61
     * @param mixed[] $vars array of vars to include in the output done as 'varname' => 'var value'
62
     * @param bool $escape = false, whether or not to escape the value
63
     */
64 1
    public function addJavascriptVar($vars, $escape = false)
65
    {
66 1
        if (empty($vars) || !is_array($vars))
67 1
            return;
68
69 1
        foreach ($vars as $key => $value)
70 1
            $this->js_vars[$key] = !empty($escape) ? JavaScriptEscape($value) : $value;
71 1
    }
72
73
    public function getJavascriptVars()
74
    {
75
        return $this->js_vars;
76
    }
77
78
    /**
79
     * @param int|self::ALL $type One of ALL, SELF, DEFERRED class constants
0 ignored issues
show
Documentation introduced by
The doc-type int|self::ALL could not be parsed: Unknown type name "self::ALL" at position 4. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
80
     * @return array
81
     * @throws Exception if the type is not known
82
     */
83
    public function getInlineJavascript($type = self::ALL)
84
    {
85
        switch ($type)
86
        {
87
            case self::ALL:
88
                return $this->js_inline;
89
            case self::DEFERRED:
90
                return $this->js_inline[self::DEFERRED];
91
            case self::STANDARD:
92
                return $this->js_inline[self::STANDARD];
93
        }
94
95
        throw new \Exception('Unknown inline Javascript type');
96
    }
97
98
    /**
99
     * Add a block of inline Javascript code to be executed later
100
     *
101
     * What it does:
102
     * - only use this if you have to, generally external JS files are better, but for very small scripts
103
     *   or for scripts that require help from PHP/whatever, this can be useful.
104
     * - all code added with this function is added to the same <script> tag so do make sure your JS is clean!
105
     *
106
     * @param string $javascript
107
     * @param bool $defer = false, define if the script should load in <head> or before the closing <html> tag
108
     */
109
    function addInlineJavascript($javascript, $defer = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
110
    {
111
        if (!empty($javascript))
112
            $this->js_inline[(!empty($defer) ? self::DEFERRED : self::STANDARD)][] = $javascript;
113
    }
114
115
    public function setRTL($toggle)
116
    {
117
        $this->rtl = (bool) $toggle;
118
        return $this;
119
    }
120
}