Pair::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Logikos\Util;
4
5
class Pair {
6
  private $key;
7
  private $value;
8
9 8
  public function __construct($key, $value) {
10 8
    $this->key = $this->getScalar($key);
11 6
    $this->value = $value;
12 6
  }
13
14 4
  public function getKey() {
15 4
    return $this->key;
16
  }
17
18 2
  public function getValue() {
19 2
    return $this->value;
20
  }
21
22 8
  private function getScalar($key) {
23 8
    $this->validateNotNull($key);
24 7
    $this->validateNotObjectOrHasToString($key);
25
26 6
    if (is_numeric($key) || is_string($key))
27 5
      return $key;
28
29 1
    return (string) $key;
30
  }
31
32
  /**
33
   * @param $key
34
   * @throws InvalidTypeException
35
   */
36 8
  private function validateNotNull($key) {
37 8
    if (is_null($key))
38 1
      throw new InvalidTypeException('Can not use null for a key');
39 7
  }
40
41
  /**
42
   * @param $key
43
   * @throws InvalidTypeException
44
   */
45 7
  private function validateNotObjectOrHasToString($key) {
46 7
    if (is_object($key) && !method_exists($key, '__toString'))
47 1
      throw new InvalidTypeException('Object keys must implement __toString');
48
  }
49
}