This repository has been archived by the owner on Mar 30, 2018. It is now read-only.
/
MutatorAbstract.php
85 lines (73 loc) · 2.55 KB
/
MutatorAbstract.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?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;
use Humbug\Utility\Tokenizer;
abstract class MutatorAbstract
{
/**
* Perform a mutation against the given original source code tokens for
* a mutable element
*
* @param array $tokens
* @param int $index
* @return string
*/
public static function mutate(array &$tokens, $index)
{
static::getMutation($tokens, $index);
return Tokenizer::reconstructFromTokens($tokens);
}
private static function shouldSkip($token, array $excludeTokens)
{
if (! is_array($token) && in_array($token, $excludeTokens, true)) {
return true;
}
return in_array($token[0], $excludeTokens, true);
}
/**
* Finds the next token in token array after a given index.
* @param array $tokens Token array to lookup
* @param int $index Position to start lookup at
* @param array $excludeTokens Excluded tokens list
*
* @return int|string|false The next match if found, or false. Token is guaranteed to be a scalar if a match is found.
*/
protected static function getNextToken(array &$tokens, $index, array $excludeTokens = [])
{
$tokenCount = count($tokens);
while ($index < $tokenCount && isset($tokens[$index+1])
&& self::shouldSkip($tokens[$index + 1], $excludeTokens)) {
$index++;
}
if (! isset($tokens[$index+1])) {
return false;
}
return is_array($tokens[$index+1]) ? $tokens[$index+1][0] : $tokens[$index+1];
}
/**
* Finds the next token value in token array before a given index.
* @param array $tokens Token array to lookup
* @param int $index Position to start lookup at
* @param array $excludeTokens Excluded tokens list
*
* @return int|string|false The previous match if found, or false. Token is guaranteed to be a scalar if a match is found.
*/
protected static function getPreviousToken(array &$tokens, $index, array $excludeTokens = [])
{
while ($index > 0 && isset($tokens[$index-1])
&& self::shouldSkip($tokens[$index - 1], $excludeTokens)) {
$index--;
}
if (! isset($tokens[$index-1])) {
return false;
}
return is_array($tokens[$index-1]) ? $tokens[$index-1][0] : $tokens[$index-1];
}
}