AbstractExternalIdentity::getInputHtml()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 */
6
7
namespace flipbox\saml\core\fields;
8
9
use craft\base\ElementInterface;
10
use craft\base\Field;
11
use craft\elements\User;
12
use craft\helpers\UrlHelper;
13
use flipbox\saml\core\records\ProviderIdentityInterface;
14
use yii\db\Query;
15
16
abstract class AbstractExternalIdentity extends Field implements \flipbox\saml\core\EnsureSAMLPlugin
17
{
18
19
    public static function displayName(): string
20
    {
21
        return 'SAML External Identity';
22
    }
23
24
    /**
25
     * @inheritdoc
26
     */
27
    public static function hasContentColumn(): bool
28
    {
29
        return false;
30
    }
31
32
    public function normalizeValue($value, ElementInterface $element = null)
33
    {
34
        if (! ($element instanceof User)) {
35
            return null;
36
        }
37
        /** @var User $element */
38
        return $this->getPlugin()->getProviderIdentity()->findByUser($element);
39
    }
40
41
    public function getInputHtml($value, ElementInterface $element = null): string
42
    {
43
        return $this->getStaticHtml($value, $element);
0 ignored issues
show
Bug introduced by
It seems like $element defined by parameter $element on line 41 can be null; however, flipbox\saml\core\fields...entity::getStaticHtml() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
44
    }
45
46
    /**
47
     * @param $value
48
     * @param ElementInterface $element
49
     * @return string
50
     * @throws \Twig_Error_Loader
51
     * @throws \yii\base\Exception
52
     */
53
    public function getStaticHtml($value, ElementInterface $element): string
54
    {
55
        if (! ($value instanceof Query)) {
56
            return '';
57
        }
58
        $handle = $this->getPlugin()->getHandle();
59
60
        return \Craft::$app->getView()->renderTemplate(
61
            $handle . '/_cp/fields/external-id',
62
            [
63
                'identities' => $value,
64
                'element' => $element,
65
                'baseProviderUrl' => UrlHelper::cpUrl(
66
                    $handle . '/metadata'
67
                ),
68
            ]
69
        );
70
    }
71
}
72