Completed
Push — master ( 84aa2d...183b20 )
by Nils
20:22
created

HttpsCertificateAuthorityRule   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 23
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A doValidate() 0 10 4
1
<?php
2
3
namespace whm\Smoke\Rules\Http;
4
5
use whm\Smoke\Rules\ValidationFailedException;
6
7
/**
8
 * This rule checks if a given https certificate is cretaed by a special authority
9
 */
10
class HttpsCertificateAuthorityRule extends HttpsRule
11
{
12
    private $authorityName;
13
14
    /**
15
     * @param string $authorityName authority name
16
     */
17
    public function init($authorityName)
18
    {
19
        $this->authorityName = $authorityName;
20
    }
21
22
    protected function doValidate($certInfo)
23
    {
24
        if (array_key_exists('issuer', $certInfo) and array_key_exists('CN', $certInfo['issuer'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
25
            if ($certInfo['issuer']['CN'] != $this->authorityName) {
26
                throw new ValidationFailedException('Expected authority was "' . $this->authorityName . '", "' . $certInfo['issuer']['CN'] . '" found.');
27
            }
28
        } else {
29
            throw new ValidationFailedException('Expected authority was "' . $this->authorityName . '". No authority found.');
30
        }
31
    }
32
}
33