appserver-io /
doppelgaenger
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * \AppserverIo\Doppelgaenger\Utils\Parser |
||
| 5 | * |
||
| 6 | * NOTICE OF LICENSE |
||
| 7 | * |
||
| 8 | * This source file is subject to the Open Software License (OSL 3.0) |
||
| 9 | * that is available through the world-wide-web at this URL: |
||
| 10 | * http://opensource.org/licenses/osl-3.0.php |
||
| 11 | * |
||
| 12 | * PHP version 5 |
||
| 13 | * |
||
| 14 | * @author Bernhard Wick <[email protected]> |
||
| 15 | * @copyright 2015 TechDivision GmbH - <[email protected]> |
||
| 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
||
| 17 | * @link https://github.com/appserver-io/doppelgaenger |
||
| 18 | * @link http://www.appserver.io/ |
||
| 19 | */ |
||
| 20 | |||
| 21 | namespace AppserverIo\Doppelgaenger\Utils; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Will provide basic parsing methods for analyzing and code structures |
||
| 25 | * |
||
| 26 | * @author Bernhard Wick <[email protected]> |
||
| 27 | * @copyright 2015 TechDivision GmbH - <[email protected]> |
||
| 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
||
| 29 | * @link https://github.com/appserver-io/doppelgaenger |
||
| 30 | * @link http://www.appserver.io/ |
||
| 31 | */ |
||
| 32 | class Parser |
||
| 33 | { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Will get the count of certain brackets within a string. |
||
| 37 | * Will return an integer which is calculated as the number of opening brackets against closing ones. |
||
| 38 | * |
||
| 39 | * @param string $string The string to search in |
||
| 40 | * @param string $bracketType A bracket to be taken as general bracket type e.g. '(' |
||
| 41 | * |
||
| 42 | * @return integer |
||
| 43 | */ |
||
| 44 | public function getBracketCount($string, $bracketType) |
||
| 45 | { |
||
| 46 | // prepare opening and closing bracket according to bracket type |
||
| 47 | View Code Duplication | switch ($bracketType) { |
|
| 48 | case '(': |
||
| 49 | case ')': |
||
| 50 | $openingBracket = '('; |
||
| 51 | $closingBracket = ')'; |
||
| 52 | break; |
||
| 53 | |||
| 54 | case '{': |
||
| 55 | case '}': |
||
| 56 | $openingBracket = '{'; |
||
| 57 | $closingBracket = '}'; |
||
| 58 | break; |
||
| 59 | |||
| 60 | case '[': |
||
| 61 | case ']': |
||
| 62 | $openingBracket = '['; |
||
| 63 | $closingBracket = ']'; |
||
| 64 | break; |
||
| 65 | |||
| 66 | default: |
||
| 67 | throw new \Exception(sprintf('Unrecognized bracket type %s', $bracketType)); |
||
| 68 | } |
||
| 69 | |||
| 70 | return substr_count($string, $openingBracket) - substr_count($string, $closingBracket); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Will return an integer value representing the length of the first portion of the given string which is completely |
||
| 75 | * enclosed by a certain bracket type. |
||
| 76 | * Will return 0 if nothing is found. |
||
| 77 | * |
||
| 78 | * @param string $string String to investigate |
||
| 79 | * @param string $bracketType A bracket to be taken as general bracket type e.g. '(' |
||
| 80 | * @param integer $offset Offset at which to start looking, will default to 0 |
||
| 81 | * |
||
| 82 | * @return integer |
||
| 83 | */ |
||
| 84 | public function getBracketSpan($string, $bracketType, $offset = 0) |
||
| 85 | { |
||
| 86 | // prepare opening and closing bracket according to bracket type |
||
| 87 | View Code Duplication | switch ($bracketType) { |
|
| 88 | case '(': |
||
| 89 | case ')': |
||
| 90 | $openingBracket = '('; |
||
| 91 | $closingBracket = ')'; |
||
| 92 | break; |
||
| 93 | |||
| 94 | case '{': |
||
| 95 | case '}': |
||
| 96 | $openingBracket = '{'; |
||
| 97 | $closingBracket = '}'; |
||
| 98 | break; |
||
| 99 | |||
| 100 | case '[': |
||
| 101 | case ']': |
||
| 102 | $openingBracket = '['; |
||
| 103 | $closingBracket = ']'; |
||
| 104 | break; |
||
| 105 | |||
| 106 | default: |
||
| 107 | throw new \Exception(sprintf('Unrecognized bracket type %s', $bracketType)); |
||
| 108 | } |
||
| 109 | |||
| 110 | // split up the string and analyse it character for character |
||
| 111 | $bracketCounter = null; |
||
| 112 | $stringArray = str_split($string); |
||
| 113 | $strlen = strlen($string); |
||
| 114 | $firstBracket = 0; |
||
| 115 | for ($i = $offset; $i < $strlen; $i++) { |
||
| 116 | // count different bracket types by de- and increasing the counter |
||
| 117 | if ($stringArray[$i] === $openingBracket) { |
||
| 118 | if (is_null($bracketCounter)) { |
||
| 119 | $firstBracket = $i; |
||
| 120 | } |
||
| 121 | $bracketCounter = (int) $bracketCounter + 1; |
||
| 122 | |||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 123 | } elseif ($stringArray[$i] === $closingBracket) { |
||
| 124 | $bracketCounter = (int) $bracketCounter - 1; |
||
| 125 | } |
||
| 126 | |||
| 127 | // if we reach 0 again we have a completely enclosed string |
||
| 128 | if ($bracketCounter === 0) { |
||
| 129 | return $i + 1 - $firstBracket; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | return 0; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Will return the length of the string a token array is based on. |
||
| 138 | * |
||
| 139 | * @param array $tokens The token array |
||
| 140 | * |
||
| 141 | * @return integer |
||
| 142 | */ |
||
| 143 | public function getStringLength($tokens) |
||
| 144 | { |
||
| 145 | // Iterator over the tokens and get their length |
||
| 146 | $result = 0; |
||
| 147 | $tokenCount = count($tokens); |
||
| 148 | for ($i = 0; $i < $tokenCount; $i++) { |
||
| 149 | if (is_array($tokens[$i])) { |
||
| 150 | $result += strlen($tokens[$i][1]); |
||
| 151 | |||
|
0 ignored issues
–
show
|
|||
| 152 | } else { |
||
| 153 | $result += strlen($tokens[$i]); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | return $result; |
||
| 158 | } |
||
| 159 | } |
||
| 160 |