RowAction::__invoke()   C
last analyzed

Complexity

Conditions 11
Paths 37

Size

Total Lines 52
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 5.9999
c 0
b 0
f 0
cc 11
eloc 31
nc 37
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AtDataGrid\View\Helper;
4
5
use AtDataGrid\Row\Action;
6
use Zend\Db\ResultSet\ResultSet;
7
use Zend\View\Helper\AbstractHelper;
8
use Zend\View\View;
9
10
class RowAction extends AbstractHelper
11
{
12
    /**
13
     * @param Action $action
14
     * @param $row
15
     * @return string
16
     */
17
    public function __invoke(Action $action, $row)
18
    {
19
        // Convert data to array
20
        if (! is_array($row)) {
21
            if ($row instanceof ResultSet) {
0 ignored issues
show
Bug introduced by
The class Zend\Db\ResultSet\ResultSet does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
22
                $row = $row->toArray();
23
            } elseif ($row instanceof \ArrayIterator || $row instanceof \ArrayObject) {
24
                $row = $row->getArrayCopy();
25
            } else {
26
                throw new \RuntimeException('Row data couldn\'t be converted to array');
27
            }
28
        }
29
30
        /** @var View $view */
31
        $view = $this->getView();
32
33
        if ($url = $action->getUrl()) {
34
            $p = [];
35
            $v = [];
36
37
            foreach ($row as $key => $val) {
38
                $p[] = '%'. $key .'%';
39
                $v[] = $val;
40
            }
41
42
            $actionUrl = str_replace($p, $v, $url);
43
        } else {
44
            $routeParams = array_merge($action->getRouteParams(), $row);
45
            $actionUrl = $view->url($action->getRouteName(), $routeParams, $action->getRouteOptions());
0 ignored issues
show
Bug introduced by
The method url() does not seem to exist on object<Zend\View\View>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
        }
47
48
        $onClick = '';
49
        if ($action->isConfirm()) {
50
            $onClick = 'onclick="DataGrid.confirmAction(this, \''. $view->translate($action->getConfirmMessage()) . '\')"';
0 ignored issues
show
Bug introduced by
The method translate() does not seem to exist on object<Zend\View\View>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
        }
52
53
        $disabledClass = '';
54
        if ($action->isDisablable() && $callback = $action->getDisableCallback()) {
55
            $result = call_user_func($callback, $row);
56
            if ($result) {
57
                $disabledClass = 'disabled';
58
            }
59
        }
60
61
        $html = '<a class="action-' .$action->getName() .' btn btn-default '.$disabledClass.'" href="'. $actionUrl .'"'. $onClick .'>
62
                     <span title="'. $view->translate($action->getLabel()) .'"
0 ignored issues
show
Bug introduced by
The method translate() does not seem to exist on object<Zend\View\View>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
                           class="'. $action->getClass().'">
64
                     </span>
65
                 </a>';
66
67
        return $html;
68
    }
69
}