This repository has been archived by the owner on Mar 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathAddition.php
71 lines (62 loc) · 1.9 KB
/
Addition.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
* Humbug
*
* @category Humbug
* @package Humbug
* @copyright Copyright (c) 2015 Pádraic Brady (http://blog.astrumfutura.com)
* @license https://github.com/padraic/humbug/blob/master/LICENSE New BSD License
*/
namespace Humbug\Mutator\Arithmetic;
use Humbug\Mutator\MutatorAbstract;
class Addition extends MutatorAbstract
{
/**
* Replace plus sign (+) with minus sign (-)
*
* @param array $tokens
* @param int $index
* @return array
*/
public static function getMutation(array &$tokens, $index)
{
$tokens[$index] = '-';
}
/**
* Not all additions can be mutated.
*
* The PHP language allows union of arrays : $var = ['foo' => true] + ['bar' => true]
* see http://php.net/manual/en/language.operators.array.php for details.
*
* So for this case, we can't create a mutation.
*
* @param array $tokens
* @param $index
* @return bool
*/
public static function mutates(array &$tokens, $index)
{
$t = $tokens[$index];
if (!is_array($t) && $t == '+') {
$tokenCount = count($tokens);
for ($i = $index + 1; $i < $tokenCount; $i++) {
// check for short array syntax
if (!is_array($tokens[$i]) && $tokens[$i][0] == '[') {
return false;
}
// check for long array syntax
if (is_array($tokens[$i]) && $tokens[$i][0] == T_ARRAY && $tokens[$i][1] == 'array') {
return false;
}
// if we're at the end of the array
// and we didn't see any array, we
// can probably mutate this addition
if (!is_array($tokens[$i]) && $tokens[$i] == ';') {
return true;
}
}
return true;
}
return false;
}
}