handle()   B
last analyzed

Complexity

Conditions 8
Paths 9

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 61
ccs 0
cts 48
cp 0
rs 7.6064
c 0
b 0
f 0
cc 8
nc 9
nop 1
crap 72

How to fix   Long Method   

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
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/patron/license
6
 * @link       https://www.flipboxfactory.com/software/patron/
7
 */
8
9
namespace flipbox\organizations\events\handlers;
10
11
use Craft;
12
use craft\elements\User;
13
use craft\events\SetElementTableAttributeHtmlEvent;
14
use craft\helpers\Html;
15
use flipbox\organizations\behaviors\OrganizationsAssociatedToUserBehavior;
16
use flipbox\organizations\Organizations;
17
use flipbox\organizations\records\UserAssociation;
18
use flipbox\organizations\records\UserType;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 */
24
class SetOrganizationUserElementTableAttributeHtml
25
{
26
    /**
27
     * @param SetElementTableAttributeHtmlEvent $event
28
     * @throws \Twig\Error\LoaderError
29
     * @throws \Twig\Error\RuntimeError
30
     * @throws \Twig\Error\SyntaxError
31
     */
32
    public static function handle(SetElementTableAttributeHtmlEvent $event)
33
    {
34
        if (!in_array($event->attribute, ['state', 'types', 'edit'], true)) {
35
            return;
36
        }
37
38
        if ($event->attribute === 'edit') {
39
            $event->html = '<span class="edit-association icon settings"></span>';
40
            return;
41
        }
42
43
        /** @var User|OrganizationsAssociatedToUserBehavior $element */
44
        $element = $event->sender;
45
46
        /** @var UserAssociation $association */
47
        $association = $element->getOrganizations()->findOne(
0 ignored issues
show
Bug introduced by
The method getOrganizations does only exist in flipbox\organizations\be...ssociatedToUserBehavior, but not in craft\elements\User.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
48
            Craft::$app->getRequest()->getParam('organization')
49
        );
50
51
        switch ($event->attribute) {
52
            case 'state':
53
                $params = [
54
                    'indicator' => '',
55
                    'label' => 'N/A'
56
                ];
57
58
                $state = Organizations::getInstance()->getSettings()->getUserStates();
59
60
                if ($association) {
61
                    $params = [
62
                        'indicator' => $association->state,
63
                        'label' => $state[$association->state] ?? 'N/A'
64
                    ];
65
                }
66
67
                $event->html = Html::encodeParams(
68
                    '<span class="user-state status {indicator}"></span>{label}',
69
                    $params
70
                );
71
                break;
72
73
            case 'types':
74
                $types = $association ? $association->getTypes() : [];
75
76
                $html = $label = [];
77
                foreach ($types->getCollection() as $type) {
78
                    $label[] = $type->name;
79
                    $html[] = self::icon($type);
80
                }
81
82
                $event->html = Html::encodeParams(
83
                    '<span class="user-types-icons" data-label="' . implode(', ', $label) . '">' . implode(
84
                        '',
85
                        $html
86
                    ) . '</span>',
87
                    []
88
                );
89
90
                break;
91
        }
92
    }
93
94
    /**
95
     * @param UserType $type
96
     * @return string
97
     * @throws \Twig\Error\LoaderError
98
     * @throws \Twig\Error\RuntimeError
99
     * @throws \Twig\Error\SyntaxError
100
     */
101
    private static function icon(UserType $type)
102
    {
103
        return '<span class="foo" data-label="' . $type->name . '">' . Craft::$app->getView()->renderTemplate(
104
            "organizations/_includes/icon.svg",
105
            [
106
                'label' => $type->name
107
            ]
108
        ) . '</span>';
109
    }
110
}
111