Completed
Push — develop ( b386e5...33ac12 )
by Damien
12:18 queued 14s
created

VariablesTrait::prepVariables()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 101

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 101
rs 7.6888
c 0
b 0
f 0
cc 5
nc 10
nop 1

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
 */
6
7
namespace flipbox\saml\core\controllers\cp\view\metadata;
8
9
use Craft;
10
use craft\helpers\UrlHelper;
11
use flipbox\keychain\KeyChain;
12
use flipbox\saml\core\records\ProviderInterface;
13
use flipbox\saml\core\traits\EnsureSamlPlugin;
14
15
trait VariablesTrait
16
{
17
    use EnsureSamlPlugin;
18
19
    /**
20
     * @param ProviderInterface $provider
21
     * @return array
22
     */
23
    protected function getActions(ProviderInterface $provider)
24
    {
25
        $actions = [];
26
27
        if ($provider->id) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface flipbox\saml\core\records\ProviderInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
28
            $actions = [
29
                [
30
                    //action list 1
31
                    [
32
                        'action' => $this->getSamlPlugin()->getHandle() . '/metadata/change-status',
33
                        'label'  => $provider->enabled ? 'Disable' : 'Enable',
0 ignored issues
show
Bug introduced by
Accessing enabled on the interface flipbox\saml\core\records\ProviderInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
34
                    ],
35
                    [
36
                        'action' => $this->getSamlPlugin()->getHandle() . '/metadata/delete',
37
                        'label'  => 'Delete',
38
                    ],
39
                ],
40
            ];
41
        }
42
        return $actions;
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    protected function getBaseVariables()
49
    {
50
51
        return array_merge(
52
            parent::getBaseVariables(),
53
            [
54
                'autoCreate' => false,
55
                'myEntityId' => $this->getSamlPlugin()->getSettings()->getEntityId(),
56
                'myType'     => $this->getSamlPlugin()->getSettings(),
57
            ]
58
        );
59
    }
60
61
    /**
62
     * @param string|null|ProviderInterface $provider
63
     * @return array
64
     */
65
    protected function prepVariables($provider = null)
66
    {
67
        $variables = $this->getBaseVariables();
68
69
        $variables['title'] = Craft::t(
70
            $this->getSamlPlugin()->getHandle(),
71
            $this->getSamlPlugin()->name
0 ignored issues
show
Bug introduced by
Accessing name on the interface flipbox\saml\core\SamlPluginInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
72
        );
73
74
        /**
75
         * TYPES
76
         */
77
        $variables['myType'] = $this->getSamlPlugin()->getMyType();
78
        $variables['remoteType'] = $this->getSamlPlugin()->getRemoteType();
79
        $variables['createType'] = $variables['remoteType'];
80
81
        if ($provider) {
82
            /**
83
             * @var ProviderInterface $provider
84
             */
85
            $provider = $variables['provider'] = (
86
                /**
87
                 * Is instance provider
88
                 */
89
            $provider instanceof ProviderInterface ?
90
                $provider :
91
                $provider = $this->getSamlPlugin()->getProviderRecordClass()::find()->where([
92
                    /**
93
                     * Is ID
94
                     */
95
                    'id' => $provider,
96
                ])->one()
97
            );
98
99
            $variables['title'] .= ': Edit';
100
101
            $crumb = [
102
                [
103
104
                    'url'   => UrlHelper::cpUrl(
105
                        implode(
106
                            '/',
107
                            [
108
                                $this->getSamlPlugin()->getHandle(),
109
                                'metadata',
110
                            ]
111
                        )
112
                    ),
113
                    'label' => 'Provider List',
114
                ], [
115
                    'url'   => UrlHelper::cpUrl(
116
                        $this->getSamlPlugin()->getHandle() . '/metadata/' . $provider->id
0 ignored issues
show
Bug introduced by
Accessing id on the interface flipbox\saml\core\records\ProviderInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
117
                    ),
118
                    'label' => $provider->label ?: $provider->entityId,
0 ignored issues
show
Bug introduced by
Accessing label on the interface flipbox\saml\core\records\ProviderInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing entityId on the interface flipbox\saml\core\records\ProviderInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
119
                ]
120
            ];
121
            $variables['keypair'] = $provider->keychain;
0 ignored issues
show
Bug introduced by
Accessing keychain on the interface flipbox\saml\core\records\ProviderInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
122
123
            $variables = array_merge(
124
                $variables,
125
                $this->addUrls($provider)
0 ignored issues
show
Bug introduced by
It seems like addUrls() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
126
            );
127
        } else {
128
            $record = $this->getSamlPlugin()->getProviderRecordClass();
129
130
            $provider = $variables['provider'] = new $record([
0 ignored issues
show
Unused Code introduced by
$provider is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
131
                'providerType' => 'idp',
132
            ]);
133
134
            $variables['title'] .= ': Create';
135
136
            $crumb = [
137
                [
138
                    'url'   => UrlHelper::cpUrl(
139
                        $this->getSamlPlugin()->getHandle() . '/new'
140
                    ),
141
                    'label' => 'New',
142
                ]
143
            ];
144
        }
145
146
        $variables['allkeypairs'] = [];
147
148
        $keypairs = KeyChain::getInstance()->getService()->findByPlugin($this->getSamlPlugin())->all();
149
150
        foreach ($keypairs as $keypair) {
151
            $variables['allkeypairs'][] = [
152
                'label' => $keypair->description,
153
                'value' => $keypair->id,
154
            ];
155
        }
156
157
        $variables['crumbs'] = array_merge([
158
            [
159
                'url'   => UrlHelper::cpUrl($this->getSamlPlugin()->getHandle()),
160
                'label' => Craft::t($this->getSamlPlugin()->getHandle(), $this->getSamlPlugin()->name),
0 ignored issues
show
Bug introduced by
Accessing name on the interface flipbox\saml\core\SamlPluginInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
161
            ],
162
        ], $crumb);
163
164
        return $variables;
165
    }
166
}
167