Completed
Push — master ( 829e7b...9495f2 )
by Nate
01:24
created

handle()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 0
cts 48
cp 0
rs 7.6282
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\UserType;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 1.0.0
22
 */
23
class SetOrganizationUserElementTableAttributeHtml
24
{
25
    /**
26
     * @param SetElementTableAttributeHtmlEvent $event
27
     * @throws \Twig\Error\LoaderError
28
     * @throws \Twig\Error\RuntimeError
29
     * @throws \Twig\Error\SyntaxError
30
     */
31
    public static function handle(SetElementTableAttributeHtmlEvent $event)
32
    {
33
        if (!in_array($event->attribute, ['state', 'types', 'edit'], true)) {
34
            return;
35
        }
36
37
        if ($event->attribute === 'edit') {
38
            $event->html = '<span class="edit-association icon settings"></span>';
39
            return;
40
        }
41
42
        /** @var User|OrganizationsAssociatedToUserBehavior $element */
43
        $element = $event->sender;
44
45
        $association = $element->getOrganizationManager()->findOne(
0 ignored issues
show
Bug introduced by
The method getOrganizationManager 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...
46
            Craft::$app->getRequest()->getParam('organization')
47
        );
48
49
        switch ($event->attribute) {
50
            case 'state':
51
                $params = [
52
                    'indicator' => '',
53
                    'label' => 'N/A'
54
                ];
55
56
                $state = Organizations::getInstance()->getSettings()->getUserStates();
57
58
                if ($association) {
59
                    $params = [
60
                        'indicator' => $association->state,
61
                        'label' => $state[$association->state] ?? 'N/A'
62
                    ];
63
                }
64
65
                $event->html = Html::encodeParams(
66
                    '<span class="user-state status {indicator}"></span>{label}',
67
                    $params
68
                );
69
                break;
70
71
            case 'types':
72
                $types = $association ? $association->types : [];
73
74
                $html = $label = [];
75
                foreach ($types as $type) {
76
                    $label[] = $type->name;
77
                    $html[] = self::icon($type);
78
                }
79
80
                $event->html = Html::encodeParams(
81
                    '<span class="user-types-icons" data-label="' . implode(', ', $label) . '">' . implode(
82
                        '',
83
                        $html
84
                    ) . '</span>',
85
                    []
86
                );
87
88
                break;
89
        }
90
    }
91
92
    /**
93
     * @param UserType $type
94
     * @return string
95
     * @throws \Twig\Error\LoaderError
96
     * @throws \Twig\Error\RuntimeError
97
     * @throws \Twig\Error\SyntaxError
98
     */
99
    private static function icon(UserType $type)
100
    {
101
        return '<span class="foo" data-label="' . $type->name . '">' . Craft::$app->getView()->renderTemplate(
102
            "organizations/_includes/icon.svg",
103
            [
104
                'label' => $type->name
105
            ]
106
        ) . '</span>';
107
    }
108
}
109