GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CommonNameValidator::getNameVariations()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Punkstar\Ssl\Validator;
4
5
use Punkstar\Ssl\Certificate;
6
7
class CommonNameValidator
8
{
9
    /**
10
     * @var Certificate
11
     */
12
    private $certificate;
13
    
14 3
    public function __construct(Certificate $certificate)
15
    {
16 3
        $this->certificate = $certificate;
17 3
    }
18
    
19 3
    public function isValid($domain) : bool
20
    {
21 3
        $hostname = parse_url($domain, PHP_URL_HOST);
22
        
23 3
        if (!$hostname) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $hostname of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
24 2
            $hostname = $domain;
25
        }
26
        
27 3
        foreach ($this->getNameVariations($hostname) as $nameVariation) {
28 3
            if (in_array($nameVariation, $this->getAllowedNames(), true)) {
29 3
                return true;
30
            }
31
        }
32
        
33 3
        return false;
34
    }
35
    
36 3
    private function getAllowedNames() : array
37
    {
38
        // Add any SANS that might be on the certificate.
39 3
        $allowedNames = $this->certificate->sans();
40
    
41 3
        $sslCertSubject = $this->certificate->subject();
42
    
43
        // Add the common name from the certificate.
44 3
        if (isset($sslCertSubject['CN'])) {
45 3
            $allowedNames[] = $sslCertSubject['CN'];
46
        }
47
        
48 3
        return $allowedNames;
49
    }
50
    
51 3
    private function getNameVariations($domain) : array
52
    {
53 3
        $nameVariations = [$domain];
54
    
55
        // If we're looking at a subdomain then check for wildcards.
56 3
        if (substr_count($domain, '.') >= 2) {
57 3
            $nameVariations[] = '*' . substr($domain, strpos($domain, '.'));
58
        }
59
        
60 3
        return $nameVariations;
61
    }
62
}
63