Html::setParameters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 5 and the first side effect is on line 2.

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
  requireComponent("../ServerVars/ServerVars.php");
3
  requireComponent("../Module/Module.php");
4
  requireComponent("../JException/JException.php");
5
  abstract class Html extends Module {
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...
6
    public $template;
7
    public $app;
8
    public $page;
9
    public $tags;
10
    public $jsVars;
11
    public function __construct() {
12
      parent::__construct();
13
      $this->template = "";
14
      $this->app      = [];
15
      $this->page     = [];
16
      $this->jsVars   = [];
17
      $this->tags     = [
18
        "css"   => [],
19
        "js"    => [],
20
        "jsVar" => [],
21
        "base"  => ""
22
      ];
23
    }
24
    public function setParameters( $_parameters = [ "app" => null, "page" => null] ) {
25
    $this->app  = $_parameters["app"];
26
    $this->page = $_parameters["page"];
27
    }
28
    abstract public function init();
29
    public function draw() {
30
      if($this->template == "")
31
        throw new JException("The variable \$this->template must be set in class $this->name function init().");
32
      $server = new ServerVars();
33
      $this->addDipendences();
34
      $this->tags["css"]  = array_unique($this->tags["css"]);
35
      $this->tags["js"]   = array_unique($this->tags["js"]);
36
      $this->tags["base"] = $server->server["RELATIVE"]."/";
37
      $this->stringifyDipendences();
38
      return jBlockFile($this->template, $this->tags);
39
    }
40
    public function getCss() {
41
      return $this->getRequire("getCss",".css");
42
    }
43
    public function getJs() {
44
      return $this->getRequire("getJs",".js");
45
    }
46
    public function getJsVars() {
47
      return $this->jsVars;
48
    }
49
    public function addJsVar( $_name, $_value ) {
50
      if(!is_string($_name))
51
        throw new InvalidArgumentException("Parameter name must be a string.");
52
      if(!is_string($_value))
53
        throw new InvalidArgumentException("Parameter value must be a string.");
54
      $this->jsVars[] = [$_name, $_value];
55
    }
56
    public function addJsVars( $_array ) {
57
      if(!is_array($_array))
58
        throw new InvalidArgumentException("Parameter must be an array.");
59
      foreach ($_array as $value)
60
        $this->addJsVar($value[0], $value[1]);
61
    }
62
    protected function stringifyDipendences() {
63
      $tempStr = "";
64
      $timeParameter = "?t=".time();
65
      $time = ($this->app->cache->css == true) ? "" : $timeParameter;
66
      foreach ($this->tags["css"] as $i)
0 ignored issues
show
Bug introduced by
The expression $this->tags['css'] of type array|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
67
      $tempStr .= "<link rel='stylesheet' href='$i$time'>";
68
      $this->tags["css"] = $tempStr;
69
      $tempStr = "";
70
      $time = ($this->app->cache->js == true) ? "" : $timeParameter;
71
      foreach ($this->tags["js"] as $i)
72
      $tempStr .= "<script src='$i$time'></script>";
73
      $this->tags["js"] = $tempStr;
74
      $tempStr = "";
75
      $tempStr .= "<script type='text/javascript'>";
76
      foreach ($this->tags["jsVar"] as $i)
77
      $tempStr .= " $i[0] = $i[1];\n";
78
      $tempStr .= "</script>";
79
      $this->tags["jsVar"] = $tempStr;
80
    }
81
    protected function addDipendences() {
82
      $this->tags["css"]   = $this->getCss();
83
      $this->tags["js"]    = $this->getJs();
84
      $this->tags["jsVar"] = $this->getJsVars();
85
    }
86
    protected function getRequire( $_function, $_extenction) {
87
      $temp = [];
88
      $filesRequired = $this->getFilesRequired();
89
      $files         = $this->getFiles();
90 View Code Duplication
      foreach ($filesRequired as $i)
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...
91
        if (!is_array($i) && strpos($i, $_extenction) !== FALSE)
92
          $temp[] = $i;
93
      foreach ($this->modules as $i)
94
        $temp = array_merge( $temp, $i->$_function() );
95 View Code Duplication
      foreach ($files as $i)
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...
96
        if (!is_array($i) && strpos($i, $_extenction) !== FALSE)
97
          $temp[] = $i;
98
      return $temp;
99
    }
100
  }
101
?>
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...
102