Emulate   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 119
rs 10
wmc 17
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A env() 0 7 1
A registerLongArrays() 0 7 2
A registerGlobals() 0 17 4
B magicQuotesGPCR() 0 22 5
A sessionRegister() 0 8 3
A sessionUnregister() 0 3 1
A sessionIsRegistered() 0 3 1
1
<?php
2
3
namespace DBSeller\Legacy\PHP53;
4
5
class Emulate {
6
7
  private static $egpcs = array(
8
    'ENV', 'GET', 'POST', 'COOKIE', 'SERVER', 'SESSION', 'FILES'
9
  );
10
11
  /**
12
   * Emula a variavel de ambiente $_ENV
13
   *
14
   * @return boolean
15
   */
16
  public static function env() {
1 ignored issue
show
Coding Style introduced by
env 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...
17
18
    $GLOBALS['_ENV'] = array();
19
    $GLOBALS['_ENV']['PATH_INFO'] = getenv('PATH_INFO');
20
    $GLOBALS['_ENV']['HTTP_USER_AGENT'] = getenv('HTTP_USER_AGENT');
21
    return true;
22
  }
23
24
  /**
25
   * Emula: register_long_arrays = On
26
   *
27
   * @return boolean
28
   */
29
  public static function registerLongArrays() {
30
31
    foreach (self::$egpcs as $name) {
32
      $GLOBALS["HTTP_{$name}_VARS"] =& $GLOBALS["_$name"];
33
    }
34
    return true;
35
  }
36
37
  /**
38
   * Emula: register_globals = On
39
   *
40
   * @return boolean
41
   */
42
  public static function registerGlobals() {
43
44
    foreach (self::$egpcs as $name) {
45
46
      if (!isset($GLOBALS["_$name"])) {
47
        continue;
48
      }
49
50
      foreach($GLOBALS['_'.$name] as $key => & $value) {
51
        $GLOBALS[$key] = $value;
52
      }
53
54
      reset($GLOBALS["_$name"]);
55
    }
56
57
    return true;
58
  }
59
60
  /**
61
   * Emula: magic_quotes_gpc = On|Off
62
   *
63
   * @return void
64
   */
65
  public static function magicQuotesGPCR($directive = 'On') {
66
67
    $active = true;
68
69
    if (strtolower($directive) == 'off' || (is_bool($directive) && $directive === false))  {
70
      $active = false;
71
    }
72
73
    $handler = function(&$value) use($active) {
74
      $value = $active ? addslashes($value) : stripslashes($value);
75
    };
76
77
    array_walk_recursive($_GET, $handler);
78
    array_walk_recursive($_POST, $handler);
79
    array_walk_recursive($_COOKIE, $handler);
80
    array_walk_recursive($_REQUEST, $handler);
81
82
    reset($_GET);
83
    reset($_POST);
84
    reset($_COOKIE);
85
    reset($_REQUEST);
86
  }
87
88
  /**
89
   * Emula o comportamento do session_register
90
   * @static
91
   * @link http://php.net/manual/pt_BR/function.session-register.php
92
   */
93
  public static function sessionRegister() {
1 ignored issue
show
Coding Style introduced by
sessionRegister 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...
94
95
    $args = func_get_args();
96
    foreach ($args as $key){
97
98
      $_SESSION[$key]= isset($GLOBALS[$key]) ? $GLOBALS[$key] : null;
99
    }
100
  }
101
102
  /**
103
   * Emula o comportamento da função session_is_registered
104
   *
105
   * @link http://php.net/manual/pt_BR/function.session-is-registered.php
106
   * @static
107
   * @param mixed $key
108
   */
109
  public static function sessionIsRegistered($key) {
110
    return array_key_exists($key, $_SESSION);
111
  }
112
113
  /**
114
   * Emula o comportamento da função sesion_unregister
115
   *
116
   * @link http://php.net/manual/pt_BR/function.session-unregister.php
117
   * @static
118
   * @param mixed $key
119
   */
120
  public static function sessionUnregister($key) {
1 ignored issue
show
Coding Style introduced by
sessionUnregister uses the super-global variable $_SESSION 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...
121
    unset($_SESSION[$key]);
122
  }
123
}
124