Completed
Push — master ( 2b7a2e...74ad36 )
by Michael
11:15
created

Coordinate::evaluate()   C

Complexity

Conditions 17
Paths 17

Size

Total Lines 51
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 51
rs 5.6079
c 1
b 0
f 1
cc 17
eloc 42
nc 17
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
	/**
3
##DOC-SIGNATURE##
4
5
    This file is part of WideImage.
6
		
7
    WideImage is free software; you can redistribute it and/or modify
8
    it under the terms of the GNU Lesser General Public License as published by
9
    the Free Software Foundation; either version 2.1 of the License, or
10
    (at your option) any later version.
11
		
12
    WideImage is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU Lesser General Public License for more details.
16
		
17
    You should have received a copy of the GNU Lesser General Public License
18
    along with WideImage; if not, write to the Free Software
19
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21
	* @package Internals
22
  **/
23
24
namespace WideImage;
25
26
27
use WideImage\Exception\InvalidCoordinateException;
28
29
/**
30
 * A utility class for smart coordinates
31
 *  
32
 * @package Internals
33
 **/
34
class Coordinate
35
{
36
	protected static $coord_align   = array('left', 'center', 'right', 'top', 'middle', 'bottom');
37
	protected static $coord_numeric = array('[0-9]+', '[0-9]+\.[0-9]+', '[0-9]+%', '[0-9]+\.[0-9]+%');
38
	
39
	/**
40
	 * Parses a numeric or string representation of a corrdinate into a structure
41
	 * 
42
	 * @param string $coord Smart coordinate
0 ignored issues
show
Bug introduced by
There is no parameter named $coord. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
43
	 * @return array Parsed smart coordinate
44
	 */
45
	public static function parse($c)
46
	{
47
		$tokens    = array();
48
		$operators = array('+', '-');
49
		
50
		$flush_operand    = false;
51
		$flush_operator   = false;
52
		$current_operand  = '';
53
		$current_operator = '';
0 ignored issues
show
Unused Code introduced by
$current_operator is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
54
		$coordinate       = strval($c);
55
		$expr_len         = strlen($coordinate);
56
		
57
		for ($i = 0; $i < $expr_len; $i++) {
58
			$char = $coordinate[$i];
59
			
60
			if (in_array($char, $operators)) {
61
				$flush_operand    = true;
62
				$flush_operator   = true;
63
				$current_operator = $char;
0 ignored issues
show
Unused Code introduced by
$current_operator is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64
			} else {
65
				$current_operand .= $char;
66
				if ($i == $expr_len - 1) {
67
					$flush_operand = true;
68
				}
69
			}
70
			
71
			if ($flush_operand) {
72
				if (trim($current_operand) != '') {
73
					$tokens[] = array('type' => 'operand', 'value' => trim($current_operand));
74
				}
75
				
76
				$current_operand = '';
77
				$flush_operand   = false;
78
			}
79
			
80
			if ($flush_operator) {
81
				$tokens[]       = array('type' => 'operator', 'value' => $char);
82
				$flush_operator = false;
83
			}
84
		}
85
		
86
		return $tokens;
87
	}
88
	
89
	/**
90
	 * Evaluates the $coord relatively to $dim
91
	 * 
92
	 * @param string $coord A numeric value or percent string
93
	 * @param int $dim Dimension
94
	 * @param int $sec_dim Secondary dimension (for align)
95
	 * @return int Calculated value
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
96
	 */
97
	public static function evaluate($coord, $dim, $sec_dim = null)
98
	{
99
		$comp_regex = implode('|', self::$coord_align) . '|' . implode('|', self::$coord_numeric);
100
		
101
		if (preg_match("/^([+-])?({$comp_regex})$/", $coord, $matches)) {
102
			$sign = intval($matches[1] . "1");
103
			$val  = $matches[2];
104
			
105
			if (in_array($val, self::$coord_align)) {
106
				if ($sec_dim === null) {
107
					switch ($val) {
108
						case 'left':
109
						case 'top':
110
							return 0;
111
							break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
112
						case 'center':
113
						case 'middle':
114
							return $sign * intval($dim / 2);
115
							break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
116
						case 'right':
117
						case 'bottom':
118
							return $sign * $dim;
119
							break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
120
						default:
121
							return null;
122
					}
123
				} else {
124
					switch ($val) {
125
						case 'left':
126
						case 'top':
127
							return 0;
128
							break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
129
						case 'center':
130
						case 'middle':
131
							return $sign * intval($dim / 2 - $sec_dim / 2);
132
							break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
133
						case 'right':
134
						case 'bottom':
135
							return $sign * ($dim - $sec_dim);
136
							break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
137
						default:
138
							return null;
139
					}
140
				}
141
			} elseif (substr($val, -1) === '%') {
142
				return intval(round($sign * $dim * floatval(str_replace('%', '', $val)) / 100));
143
			} else {
144
				return $sign * intval(round($val));
145
			}
146
		}
147
	}
148
	
149
	/**
150
	 * Calculates and fixes a smart coordinate into a numeric value
151
	 * 
152
	 * @param mixed $value Smart coordinate, relative to $dim
153
	 * @param int $dim Coordinate to which $value is relative
154
	 * @param int $sec_dim Secondary dimension (for align)
155
	 * @return int Calculated value
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
156
	 */
157
	public static function fix($value, $dim, $sec_dim = null)
158
	{
159
		$coord_tokens = self::parse($value);
160
		
161
		if (count($coord_tokens) == 0 || $coord_tokens[count($coord_tokens) - 1]['type'] != 'operand') {
162
			throw new InvalidCoordinateException("Couldn't parse coordinate '$value' properly.");
163
		}
164
		
165
		$value     = 0;
166
		$operation = 1;
167
		
168
		foreach ($coord_tokens as $token) {
169
			if ($token['type'] == 'operand') {
170
				$operand_value = self::evaluate($token['value'], $dim, $sec_dim);
171
				
172
				if ($operation == 1) {
173
					$value = $value + $operand_value;
174
				} elseif ($operation == -1) {
175
					$value = $value - $operand_value;
176
				} else {
177
					throw new InvalidCoordinateException("Invalid coordinate syntax.");
178
				}
179
				
180
				$operation = 0;
181
			} elseif ($token['type'] == 'operator') {
182
				if ($token['value'] == '-') {
183
					if ($operation == 0) {
184
						$operation = -1;
185
					} else {
186
						$operation = $operation * -1;
187
					}
188
				} elseif ($token['value'] == '+') {
189
					if ($operation == 0) {
190
						$operation = '1';
191
					}
192
				}
193
			}
194
		}
195
		
196
		return $value;
197
	}
198
}
199