Conditions | 14 |
Paths | 10 |
Total Lines | 100 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Tests | 44 |
CRAP Score | 14 |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
28 | 30 | public function tokenize($versionString) |
|
29 | { |
||
30 | /** @var Token[] $tokenList */ |
||
31 | 30 | $tokenList = []; |
|
32 | 30 | $digitAccumulator = ''; |
|
33 | 30 | $stringAccumulator = ''; |
|
34 | |||
35 | // Iterate through string, character-by-character |
||
36 | 30 | for ($i = 0; $i < strlen($versionString); $i++) { |
|
37 | 30 | $chr = $this->getCharacter($i, $versionString); |
|
38 | |||
39 | switch (true) { |
||
1 ignored issue
–
show
|
|||
40 | |||
41 | // Simple token types - separators and range indicators |
||
42 | 30 | case !is_null($this->getSimpleToken($chr)): |
|
43 | |||
44 | // Handle preceding digits or labels |
||
45 | 28 | $this->conditionallyAddToken(Token::DIGITS, $digitAccumulator, $tokenList); |
|
46 | 28 | $this->conditionallyAddToken(Token::LABEL_STRING, $stringAccumulator, $tokenList); |
|
47 | |||
48 | 28 | $tokenList[] = $this->getSimpleToken($chr); |
|
49 | 28 | break; |
|
50 | |||
51 | // Comparator token types |
||
52 | 30 | case !is_null($token = $this->getComparatorToken($i, $versionString)): |
|
53 | |||
54 | // Handle preceding digits or labels |
||
55 | 10 | $this->conditionallyAddToken(Token::DIGITS, $digitAccumulator, $tokenList); |
|
56 | 10 | $this->conditionallyAddToken(Token::LABEL_STRING, $stringAccumulator, $tokenList); |
|
57 | |||
58 | 10 | $this->addImplicitAnd($tokenList); |
|
59 | |||
60 | 10 | $tokenList[] = $token; |
|
61 | 10 | break; |
|
62 | |||
63 | // Skip the 'v' character if immediately preceding a digit |
||
64 | 30 | case $this->isPrefixedV($i, $versionString): |
|
65 | // Do nothing |
||
66 | // TODO: Should we store this for correct reverse transformation? |
||
67 | 1 | break; |
|
68 | |||
69 | // Spaces, pipes, ampersands and commas may contextually be logical AND/OR |
||
70 | 30 | case $this->isPossibleLogicalOperator($chr): |
|
71 | |||
72 | // Handle preceding digits or labels |
||
73 | 6 | $this->conditionallyAddToken(Token::DIGITS, $digitAccumulator, $tokenList); |
|
74 | 6 | $this->conditionallyAddToken(Token::LABEL_STRING, $stringAccumulator, $tokenList); |
|
75 | |||
76 | $operatorTokenList = [ |
||
77 | 6 | Token::GREATER_THAN, |
|
78 | Token::GREATER_THAN_EQUAL, |
||
79 | Token::LESS_THAN, |
||
80 | Token::LESS_THAN_EQUAL, |
||
81 | Token::EQUAL_TO |
||
82 | ]; |
||
83 | |||
84 | // No previous tokens, or previous token was not a comparator |
||
85 | if ( |
||
86 | 6 | !count($tokenList) |
|
87 | 6 | || !in_array($tokenList[count($tokenList)-1]->getType(), $operatorTokenList) |
|
88 | ) { |
||
89 | 5 | $possibleOperator = trim($chr); |
|
90 | |||
91 | 5 | for ($j = $i + 1; $j < strlen($versionString); $j++) { |
|
92 | 5 | $operatorChr = $this->getCharacter($j, $versionString); |
|
93 | |||
94 | 5 | if ($this->isPossibleLogicalOperator($operatorChr)) { |
|
95 | 3 | $possibleOperator .= trim($operatorChr); |
|
96 | } else { |
||
97 | 5 | if (!strlen($possibleOperator) || ',' === $possibleOperator) { |
|
98 | 4 | $tokenList[] = new Token(Token::LOGICAL_AND, $possibleOperator); |
|
99 | } else { |
||
100 | 2 | $tokenList[] = new Token(Token::LOGICAL_OR, $possibleOperator); |
|
101 | } |
||
102 | 5 | $i = $j - 1; |
|
103 | 5 | break; |
|
104 | } |
||
105 | } |
||
106 | } |
||
107 | |||
108 | 6 | break; |
|
109 | |||
110 | // Start accumulating on the first non-digit & continue until we reach a separator |
||
111 | 30 | case !is_numeric($chr) || strlen($stringAccumulator): |
|
112 | 5 | $stringAccumulator .= $chr; |
|
113 | 5 | break; |
|
114 | |||
115 | // Simple digits |
||
116 | default: |
||
117 | 30 | $digitAccumulator .= $chr; |
|
118 | 30 | break; |
|
119 | } |
||
120 | } |
||
121 | |||
122 | // Handle any remaining digits or labels |
||
123 | 30 | $this->conditionallyAddToken(Token::DIGITS, $digitAccumulator, $tokenList); |
|
124 | 30 | $this->conditionallyAddToken(Token::LABEL_STRING, $stringAccumulator, $tokenList); |
|
125 | |||
126 | 30 | return $tokenList; |
|
127 | } |
||
128 | |||
274 |