CalcSet::__construct()   B
last analyzed

Complexity

Conditions 6
Paths 13

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 26
rs 8.439
cc 6
eloc 20
nc 13
nop 1
1
<?php
2
3
namespace DiceCalc;
4
5
/**
6
 * Class CalcSet
7
 *
8
 * @package DiceCalc
9
 * @author  Owen Winkler <[email protected]>
10
 * @license MIT http://opensource.org/licenses/MIT
11
 */
12
class CalcSet {
13
    protected $values = [];
14
    protected $label;
15
16
    public function __construct($set_value) {
17
        if (is_array($set_value)) {
18
            $this->values       = $set_value;
19
            $this->saved_values = $this->values;
0 ignored issues
show
Bug introduced by
The property saved_values does not seem to exist. Did you mean values?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
20
            $this->label        = '[' . implode(', ', $set_value) . ']';
21
        } else {
22
            $this->label = $set_value;
23
            preg_match('%^(?P<multiple>\d*)\[(?P<set>[^\]]+)\]$%i', $set_value, $matches);
24
            $set_value = $matches['set'];
25
            $values    = explode(',', $set_value);
26
            for ($z = 0; $z < max(1, intval($matches['multiple'])); $z ++) {
0 ignored issues
show
Performance Best Practice introduced by
Consider avoiding function calls on each iteration of the for loop.

If you have a function call in the test part of a for loop, this function is executed on each iteration. Often such a function, can be moved to the initialization part and be cached.

// count() is called on each iteration
for ($i=0; $i < count($collection); $i++) { }

// count() is only called once
for ($i=0, $c=count($collection); $i<$c; $i++) { }
Loading history...
27
                foreach ($values as $set_value) {
28
                    $this->values[] = $set_value;
29
                }
30
            }
31
            $this->saved_values = $this->values;
0 ignored issues
show
Bug introduced by
The property saved_values does not seem to exist. Did you mean values?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
32
            foreach ($this->values as $k => $set_value) {
33
                $calc             = new Calc($set_value);
34
                $this->values[$k] = $calc();
35
            }
36
            foreach ($this->values as $k => $value) {
37
                $calc             = new Calc($value);
38
                $this->values[$k] = $calc();
39
            }
40
        }
41
    }
42
43
    public function __toString() {
44
        $out = [];
45
        foreach (array_keys($this->saved_values) as $key) {
0 ignored issues
show
Bug introduced by
The property saved_values does not seem to exist. Did you mean values?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
46
            $vout = $this->values[$key];
47
            if (!isset($this->values[$key])) {
48
                $vout = $this->saved_values[$key];
0 ignored issues
show
Bug introduced by
The property saved_values does not seem to exist. Did you mean values?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
49
            }
50
            if ($vout === true) {
51
                $vout = '<span class="true">true</span>';
52
            }
53
            if ($vout === false) {
54
                $vout = '<span class="false">false</span>';
55
            }
56
            if (isset($this->values[$key])) {
57
                $out[] = $vout;
58
            } else {
59
                $out[] = '<s>' . $vout . '</s>';
60
            }
61
        }
62
        $out = '[' . implode(', ', $out) . ']';
63
64
        return $out;
65
    }
66
67 View Code Duplication
    public function calc($operator, $operand) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
        $out = [];
69
        foreach ($this->values as $value) {
70
            $out[] = CalcOperation::calc($operator, $operand, $value);
71
        }
72
73
        return new CalcSet($out);
74
    }
75
76 View Code Duplication
    public function rcalc($operator, $operand) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
        $out = [];
78
        foreach ($this->values as $value) {
79
            $out[] = CalcOperation::calc($operator, $value, $operand);
80
        }
81
82
        return new CalcSet($out);
83
    }
84
85 View Code Duplication
    public function mcalc($operator, $operand)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87
        $out = [];
88
        foreach ($this->values as $value) {
89
            $out[] = $operand->rcalc($operator, $value);
90
        }
91
92
        return new CalcSet($out);
93
    }
94
95
    public function value()
96
    {
97
        $allnumeric = true;
98
        foreach ($this->values as $v) {
99
            if (is_numeric($v)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
100
            } else {
101
                $allnumeric = false;
102
            }
103
        }
104
105
        if ($allnumeric) {
106
            return array_sum($this->values);
107
        } else {
108
            return $this;
109
        }
110
    }
111
}
112