Enum::valueOf()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * spindle/types
4
 *
5
 * @license CC0-1.0 (Public Domain) https://creativecommons.org/publicdomain/zero/1.0/
6
 */
7
namespace Spindle\Types;
8
9
abstract class Enum
10
{
11
    private $scalar;
12
    private static $cache = array();
13
14 3
    function __construct($value)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
15
    {
16 3
        $ref = new \ReflectionObject($this);
17 3
        $consts = $ref->getConstants();
18 3
        if (! in_array($value, $consts, true)) {
19 1
            throw new \InvalidArgumentException(
20 1
                'value must be ' . implode(',', $consts) . ". but $value passed."
21 1
            );
22
        }
23 2
        $this->scalar = $value;
24 2
    }
25
26 1
    final static function __callStatic($name, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args 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...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
    {
28 1
        $class = get_called_class();
29 1
        $constantName = "$class::$name";
30 1
        if (isset(self::$cache[$constantName])) {
31 1
            return self::$cache[$constantName];
32
        } else {
33 1
            $const = constant($constantName);
34 1
            return self::$cache[$constantName] = new $class($const);
35
        }
36
    }
37
38 2
    final function valueOf()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
39
    {
40 2
        return $this->scalar;
41
    }
42
43 2
    final function __toString()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
44
    {
45 2
        return (string) $this->scalar;
46
    }
47
}
48