Completed
Push — master ( 1857e6...d96bec )
by Joao
03:02
created

Annotations::replaceVars()   C

Complexity

Conditions 7
Paths 32

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 6.7272
cc 7
eloc 16
nc 32
nop 2
1
<?php
2
3
namespace ByJG\AnyDataset\Model;
4
5
use ReflectionClass;
6
use SparQL\Exception;
7
8
abstract class Annotations
9
{
10
    protected $config = "object";
11
    protected $annotations;
12
    
13
14
    public function getAnnotations($key = null, $default = null)
15
    {
16
        if ($key == null) {
17
            return $this->annotations;
18
        }
19
20
        if (!isset($this->annotations["$this->config:$key"])) {
21
            return $default;
22
        }
23
24
        if (is_bool($default)) {
25
            return array_key_exists($key, $this->annotations);
26
        }
27
28
        return $this->annotations["$this->config:$key"];
29
    }
30
31
    protected function replaceVars($name, $text)
0 ignored issues
show
Coding Style introduced by
replaceVars uses the super-global variable $_SERVER 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...
32
    {
33
        # Host
34
        $port = isset($_SERVER["SERVER_PORT"]) ? $_SERVER["SERVER_PORT"] : 80;
35
        $httpHost = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : 'localhost';
36
        $host = ($port == 443 ? "https://" : "http://").$httpHost;
37
        # Replace Part One
38
        $text = preg_replace(array("/\{[hH][oO][sS][tT]\}/", "/\{[cC][lL][aA][sS][sS]\}/"), array($host, $name), $text);
39
        if (preg_match('/(\{(\S+)\})/', $text, $matches)) {
40
            $class = new ReflectionClass(get_class($this->_model));
0 ignored issues
show
Bug introduced by
The property _model does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
            $method = str_replace("()", "", $matches[2]);
42
            $value = spl_object_hash($this->_model);
43
            if ($class->hasMethod($method)) {
44
                try {
45
                    $value = $this->_model->$method();
46
                } catch (Exception $ex) {
47
                    $value = "***$value***";
48
                }
49
            }
50
            $text = preg_replace('/(\{(\S+)\})/', $value, $text);
51
        }
52
        return $text;
53
    }
54
55
    protected function adjustParams($arr)
56
    {
57
        $count = count($arr[0]);
58
        $result = array();
59
60
        for ($i = 0; $i < $count; $i++) {
61
            $key = strtolower($arr["param"][$i]);
62
            $value = $arr["value"][$i];
63
64
            if (!array_key_exists($key, $result)) {
65
                $result[$key] = $value;
66
            } elseif (is_array($result[$key])) {
67
                $result[$key][] = $value;
68
            } else {
69
                $result[$key] = array($result[$key], $value);
70
            }
71
        }
72
73
        return $result;
74
    }
75
76
}
77