GridButton::replaceCallback()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
/**
3
 * @company MTE Telecom, Ltd.
4
 * @author Ushkov Nikolai <[email protected]>
5
 * Date: 19.04.16
6
 * Time: 14:45
7
 */
8
9
namespace Nnx\DataGrid\View\Helper\JqGrid;
10
11
use Zend\View\Helper\AbstractHelper;
12
use Nnx\DataGrid\Button\ButtonInterface;
13
use Zend\View\Helper\EscapeHtml;
14
use Zend\View\Renderer\PhpRenderer;
15
16
/**
17
 * Class GridButton
18
 * @package Nnx\DataGrid\View\Helper\JqGrid
19
 */
20
class GridButton extends AbstractHelper
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $variables = [];
26
27
    public function __invoke(ButtonInterface $button, $variables)
28
    {
29
        $this->variables = $variables;
30
        /** @var PhpRenderer $view */
31
        $view = $this->getView();
32
        /** @var EscapeHtml $escape */
33
        $escape = $view->plugin('escapeHtml');
34
35
        $attributeString = $this->getAttributesString($button->getAttributes());
36
        $url = $button->getUrl();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
37
        if (is_array($url)) {
38
            $routeName = '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
39
            $routeParams = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
40
            $routeOptions = [];
41
            if (array_key_exists('routeName', $url)) {
42
                $routeName = $url['routeName'];
43
            }
44
            if (array_key_exists('routeParams', $url)) {
45
                $routeParams = $url['routeParams'];
46
            }
47
            if (array_key_exists('routeOptions', $url)) {
48
                $routeOptions = $url['routeOptions'];
49
            }
50
            $url = $view->url($routeName, $routeParams, $routeOptions);
51
        }
52
        if ($button->getLibJs()) {
53
            $view->mteAsseticAppend([
54
                'headScript' => $button->getLibJs()
55
            ]);
56
        }
57
        $url = $this->getUrl($url);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
58
        $title = $escape($button->getTitle());
59
        $html = "<a href='$url' $attributeString>$title</a> ";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
60
        $js = $button->getJs();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $js. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
61
        return ['html' => $html,'js' => $js];
62
    }
63
64
    /**
65
     * @param $attributes
66
     * @return string
67
     */
68
    protected function getAttributesString($attributes)
69
    {
70
        /** @var PhpRenderer $view */
71
        $view = $this->getView();
72
        /** @var EscapeHtml $escape */
73
        $escape = $view->plugin('escapeHtml');
74
        $res = '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
75
        foreach ($attributes as $name=>$value) {
76
            $res .= $escape($name) . '="'.$escape($value).'" ';
77
        }
78
        return $res;
79
    }
80
81
    /**
82
     * @param string $urlTemplate
83
     * @return string
84
     */
85
    public function getUrl($urlTemplate)
86
    {
87
        return preg_replace_callback('/:([a-zA-Z_]+)/', [$this, 'replaceCallback'], $urlTemplate);
88
    }
89
90
    /**
91
     * @param array $matches
92
     * @return mixed|string
93
     */
94
    protected function replaceCallback($matches)
95
    {
96
        $varName = $matches[1];
97
        $value = '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
98
99
        if (array_key_exists($varName, $this->variables)) {
100
            if ($varName !== 'backurl') {
101
                $value = urlencode($this->variables[$varName]);
102
            } else {
103
                $value = $this->variables[$varName];
104
            }
105
        }
106
107
        return $value;
108
    }
109
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
110