Common::bind()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 *
5
 * This file is part of the Apix Project.
6
 *
7
 * (c) Franck Cassedanne <franck at ouarz.net>
8
 *
9
 * @license     http://opensource.org/licenses/BSD-3-Clause  New BSD License
10
 *
11
 */
12
13
namespace Apix\View\ViewModel;
14
15
use Apix\View\ViewModel;
16
use Apix\Service;
17
use Apix\Resource\Help;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Apix\View\ViewModel\Help.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
19
class Common extends ViewModel
20
{
21
22
    /**
23
     * Returns the API resources.
24
     *
25
     * @return array
26
     */
27
    public function getResources()
28
    {
29
        if ($this->items) {
30
            $items = $this->items;
0 ignored issues
show
Bug introduced by
The property items 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...
31
        } else {
32
            $help = new Help();
33
            $server =  Service::get('server');
34
            $items = $help->getResourcesDocs($server);
35
        }
36
37
        $resources = array();
38
        foreach ($items as $resource) {
39
            foreach ($resource['methods'] as $v) {
40
                if(
41
                    !isset($v['apix_man_toc_hidden'])
42
                ) {
43
                    $resources[] = array(
44
                        'method'   => $v['method'],
45
                        'resource' => $resource['path'],
46
                        'querystr' => $v['method'] !== 'GET'
47
                                        ? '?method=' . $v['method']
48
                                        : null
49
                    );
50
                }
51
            }
52
        }
53
54
        return $resources;
55
    }
56
57
    /**
58
     * Deals with the resource groups definitions.
59
     *
60
     * @return array
61
     */
62
    public function getResourceGroups()
63
    {
64
        #$ignore = array('internal', 'id', 'toc', 'todo', 'method');
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
65
        $titles = array(
66
            'example'   => $this->hasMany('example') ? 'Examples' : 'Example',
67
            'see'       => 'See also',
68
            'link'      => $this->hasMany('link') ? 'Links' : 'Link',
69
            'copyright' => 'Copyright',
70
            'license'   => 'Licensing'
71
        );
72
        $groups = array();
73
74
        foreach ($titles as $key => $title) {
75
            if(
76
                isset($this->{$key}) || isset($this->options[$key])
77
                #&& !in_array($key, $ignore)
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
78
            ) {
79
                $groups[] = array(
80
                    'title' => $title,
81
                    'items' => $this->get($key)
82
                );
83
            }
84
        }
85
86
        return $groups;
87
    }
88
89
    /**
90
     * Returns the man index/section string.
91
     *
92
     * @return integer
93
     */
94
    public function getManTocSection()
95
    {
96
        static $str;
97
        if (!$str) {
98
            switch($this->getLayout()):
99
                case 'man_error': $section = 7; break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
100
                case 'man_page': $section = 3; break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
101
                default: $section = 1;
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
102
            endswitch;
103
            $str = sprintf('%s(%s)', $this->config['output_rootNode'], $section);
104
        }
105
106
        return $str;
107
    }
108
109
    /**
110
     * _def - view helper.
111
     *
112
     * @return string
113
     */
114
    public function _def()
115
    {
116
        return function ($t) {
117
            return '<span class="default">' . $t . '</span>';
118
        };
119
    }
120
121
    public function debug($data=null)
122
    {
123
        echo '<pre>';
124
        print_r(  null !== $data ? $data : $this );
125
        echo '</pre>';
126
    }
127
128
    /**
129
     * Assigns a property.
130
     *
131
     *     // This value can be accessed as {{foo}} within the template
132
     *     $view->set('foo', 'my value');
133
     *
134
     * You can also use an array to set several values at once:
135
     *
136
     *     // Create the values {{food}} and {{beverage}} in the template
137
     *     $view->set(array('food' => 'bread', 'beverage' => 'water'));
138
     *
139
     * @param  string|array $blob  A string key or an associative array to set.
140
     * @param  mixed        $value The value to set if the blob is a string.
141
     * @return $this
142
     */
143 View Code Duplication
    public function set($blob, $value = null)
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...
144
    {
145
        if (is_array($blob)) {
146
            foreach ($blob as $key => $value) {
147
                $this->{$key} = $value;
148
            }
149
        } else {
150
            $this->{$blob} = $value;
151
        }
152
153
        return $this;
154
    }
155
156 View Code Duplication
    public static function htmlizer($string)
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...
157
    {
158
        $pattern = array(
159
          '/((?:[\w\d]+\:\/\/)?(?:[\w\-\d]+\.)+[\w\-\d]+(?:\/[\w\-\d]+)*(?:\/|\.[\w\-\d]+)?(?:\?[\w\-\d]+\=[\w\-\d]+\&?)?(?:\#[\w\-\d\.]*)?)/', # URL
160
          '/([\w\-\d]+\@[\w\-\d]+\.[\w\-\d]+)/', # email
161
          // '/\S{2}/', # line break
162
        );
163
        $replace = array(
164
            '<a href="$1">$1</a>',
165
            '<a href="mailto:$1">$1</a>',
166
            // '- $1'
167
        );
168
169
        return preg_replace($pattern, $replace, $string);
170
    }
171
172
    /**
173
     * Assigns a value by reference. The benefit of binding is that values can
174
     * be altered without re-setting them. It is also possible to bind variables
175
     * before they have values. Assigned values will be available as a
176
     * variable within the template file:
177
     *
178
     *     // This reference can be accessed as {{ref}} within the template
179
     *     $view->bind('ref', $bar);
180
     *
181
     * @param   string   variable name
182
     * @param   mixed    referenced variable
183
     * @return $this
184
     */
185
    public function bind($key, & $value)
186
    {
187
        $this->{$key} =& $value;
188
189
        return $this;
190
    }
191
192
    /* ---- generic helpers --- */
193
194 View Code Duplication
    public function hasMany($mix)
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...
195
    {
196
        if (is_string($mix) && isset($this->{$mix})) {
197
            return count($this->{$mix})>1;
198
        } elseif (is_array($mix)) {
199
            return count($mix)>1;
200
        }
201
202
        return false;
203
    }
204
205
    /**
206
     * Returns this view model layout.
207
     *
208
     * @return string
209
     */
210
    public function getLayout()
211
    {
212
        return $this->_layout;
0 ignored issues
show
Bug introduced by
The property _layout 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...
213
    }
214
215
}
216