Completed
Push — master ( 81b718...72a657 )
by Andrii
03:05
created

AbstractUnit::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * PHP Units of Measure Library
4
 *
5
 * @link      https://github.com/hiqdev/php-units
6
 * @package   php-units
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\units;
12
13
use hiqdev\php\units\tree\TreeConverter;
14
15
/**
16
 * Abstract Unit of measure. E.g.
17
 * Almost final.
18
 * Only findCoverter can be changed.
19
 *
20
 * @see UnitInterface
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
abstract class AbstractUnit implements UnitInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    private $name;
29
30
    /**
31
     * @var ConverterInterface
32
     */
33 3
    private $converter;
34
35 3
    private function __construct($name, ConverterInterface $converter)
36 3
    {
37 3
        $this->name = $name;
38
        $this->converter = $converter;
39 4
    }
40
41 4
    private function getConverter()
42
    {
43
        return $this->converter;
44
    }
45
46
    /**
47 4
     * {@inheritdoc}
48
     */
49 4
    final public function getName()
50
    {
51
        return $this->name;
52
    }
53
54
    /**
55 2
     * {@inheritdoc}
56
     */
57 2
    final public function getMeasure()
58
    {
59
        return $this->getConverter()->getMeasure($this);
60
    }
61
62
    /**
63 1
     * {@inheritdoc}
64
     */
65 1
    final public function equals(UnitInterface $other)
66
    {
67
        return $this->getConverter()->equals($this, $other);
68
    }
69
70
    /**
71 1
     * {@inheritdoc}
72
     */
73 1
    final public function isConvertible(UnitInterface $other)
74
    {
75
        return $this->getConverter()->isConvertible($this, $other);
76
    }
77
78
    /**
79 1
     * {@inheritdoc}
80
     */
81 1
    final public function convert(UnitInterface $other, $quantity)
82
    {
83
        return $this->getConverter()->convert($this, $other, $quantity);
84
    }
85
86
    /**
87
     * @var Unit[]
88
     */
89 5
    protected static $units;
90
91 5
    final public static function __callStatic($name, $args)
92 3
    {
93
        if (!isset(static::$units[$name])) {
94
            static::$units[$name] = new static($name, static::findConverter($name));
95 5
        }
96
97
        return static::$units[$name];
98
    }
99
100
    /**
101
     * Returns converter for given unit name.
102
     * The only function to change in child classes.
103
     * XXX Should be defined as abstract but not supported in PHP5.
104
     * @param string $name
105
     * @return ConverterInterface
106
     */
107
    protected static function findConverter($name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
    {
109
        throw new InvalidConfigException('findConverter method must be redefined');
110
    }
111
}
112