IsInstanceOf   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 11
dl 0
loc 31
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 2 1
A validate() 0 2 1
A getFqcn() 0 2 1
A __construct() 0 6 2
A isValidClassName() 0 2 2
1
<?php
2
3
namespace Logikos\Util\Validation\Validator;
4
5
use Logikos\Util\Validation\Validator;
6
7
class IsInstanceOf implements Validator {
8
  private $fqcn;
9
  private $description;
10
11
  /**
12
   * @param string $fqcn
13
   * @param string $description
14
   * @throws Exception
15
   */
16 10
  public function __construct($fqcn, $description = null) {
17 10
    if (!$this->isValidClassName($fqcn))
18 1
      throw new Exception("No such class: {$fqcn}");
19
20 9
    $this->fqcn = $fqcn;
21 9
    $this->description = $description ?? "Must be an instance of {$fqcn}";
22 9
  }
23
24 1
  public function getFqcn() {
25 1
    return $this->fqcn;
26
  }
27
28 3
  public function validate($value) : bool {
29 3
    return is_a($value, $this->fqcn);
30
  }
31
32 10
  protected function isValidClassName($fqcn) {
33 10
    return class_exists($fqcn) || interface_exists($fqcn);
34
  }
35
36 3
  public function getDescription() {
37 3
    return $this->description;
38
  }
39
}