Passed
Push — master ( 42be68...0f9666 )
by Artem
03:36 queued 11s
created

ValidatesInput   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 52
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supportedNameIdFormats() 0 11 1
A validateNameIdFormat() 0 3 1
A resolveNameIdFormat() 0 11 3
1
<?php
2
3
namespace Slides\Saml2\Commands;
4
5
/**
6
 * Trait ValidatesInput
7
 *
8
 * @package Slides\Saml2\Commands
9
 */
10
trait ValidatesInput
11
{
12
    /**
13
     * Resolve the nameIdFormat.
14
     *
15
     * @param string $option
16
     *
17
     * @return string|null
18
     */
19
    protected function resolveNameIdFormat(string $option = 'nameIdFormat')
20
    {
21
        $value = $this->option($option) ?: 'persistent';
0 ignored issues
show
Bug introduced by
It seems like option() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
        $value = $this->/** @scrutinizer ignore-call */ option($option) ?: 'persistent';
Loading history...
22
23
        if ($this->validateNameIdFormat($value)) {
24
            return $value;
25
        }
26
27
        $this->error('Name ID format is invalid. Supported values: ' . implode(', ', $this->supportedNameIdFormats()));
0 ignored issues
show
Bug introduced by
It seems like error() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
        $this->/** @scrutinizer ignore-call */ 
28
               error('Name ID format is invalid. Supported values: ' . implode(', ', $this->supportedNameIdFormats()));
Loading history...
28
29
        return null;
30
    }
31
32
    /**
33
     * Validate Name ID format.
34
     *
35
     * @param string $format
36
     *
37
     * @return bool
38
     */
39
    protected function validateNameIdFormat(string $format): bool
40
    {
41
        return in_array($format, $this->supportedNameIdFormats());
42
    }
43
44
    /**
45
     * The list of supported Name ID formats.
46
     *
47
     * See https://docs.oracle.com/cd/E19316-01/820-3886/6nfcvtepi/index.html
48
     *
49
     * @return string[]|array
50
     */
51
    protected function supportedNameIdFormats(): array
52
    {
53
        return [
54
            'persistent',
55
            'transient',
56
            'emailAddress',
57
            'unspecified',
58
            'X509SubjectName',
59
            'WindowsDomainQualifiedName',
60
            'kerberos',
61
            'entity'
62
        ];
63
    }
64
}