Pair::getScalar()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
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
}