liutec /
cfgtokenlib
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 | namespace ConfigToken\TreeCompiler; |
||
| 4 | |||
| 5 | |||
| 6 | use ConfigToken\TokenCollection; |
||
| 7 | use ConfigToken\TokenInjector; |
||
| 8 | use ConfigToken\TokenParser; |
||
| 9 | use ConfigToken\TokenResolver\Types\RegisteredTokenResolver; |
||
| 10 | use ConfigToken\TokenResolver\Types\ScopeTokenResolver; |
||
| 11 | use ConfigToken\TreeCompiler; |
||
| 12 | |||
| 13 | class XrefTokenResolverCollection |
||
| 14 | { |
||
| 15 | /** @var XrefTokenResolver[] */ |
||
| 16 | protected $collection = array(); |
||
| 17 | |||
| 18 | const _KEY = 0; |
||
| 19 | const _KEY_TOKENS = 1; |
||
| 20 | const _KEY_REF = 2; |
||
| 21 | const _VALUE = 3; |
||
| 22 | const _VALUE_TOKENS = 4; |
||
| 23 | const _VALUE_REF = 5; |
||
| 24 | const _LEVEL = 6; |
||
| 25 | |||
| 26 | 5 | public function add(XrefTokenResolver $xrefTokenResolver) |
|
| 27 | { |
||
| 28 | 5 | $this->collection[] = $xrefTokenResolver; |
|
| 29 | 5 | } |
|
| 30 | |||
| 31 | 3 | public function addCollection(XrefTokenResolverCollection $xrefTokenResolverCollection) |
|
| 32 | { |
||
| 33 | 3 | $this->collection = array_merge($this->collection, $xrefTokenResolverCollection->collection); |
|
| 34 | 3 | } |
|
| 35 | |||
| 36 | public function prepend(XrefTokenResolver $xrefTokenResolver) |
||
| 37 | { |
||
| 38 | array_unshift($this->collection, $xrefTokenResolver); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function prependCollection(XrefTokenResolverCollection $xrefTokenResolverCollection) |
||
| 42 | { |
||
| 43 | $this->collection = array_merge($xrefTokenResolverCollection->collection, $this->collection); |
||
| 44 | } |
||
| 45 | |||
| 46 | 5 | protected function resolveTokens(TokenCollection $tokens, XrefTokenResolver $xrefTokenResolver) |
|
| 47 | { |
||
| 48 | 5 | if (!$xrefTokenResolver->hasTokenResolver()) { |
|
| 49 | throw new \Exception('No token resolver found for Xref token resolver.'); |
||
| 50 | } |
||
| 51 | 5 | $tokenResolver = $xrefTokenResolver->getTokenResolver(); |
|
| 52 | 5 | $hasValues = False; |
|
| 53 | 5 | if ($tokenResolver instanceof RegisteredTokenResolver) { |
|
| 54 | 5 | $hasValues = $tokenResolver->hasRegisteredTokenValues(); |
|
| 55 | 5 | } |
|
| 56 | 5 | if ($tokenResolver instanceof ScopeTokenResolver) { |
|
| 57 | $hasValues = $tokenResolver->hasScope(); |
||
| 58 | } |
||
| 59 | 5 | if (!$hasValues) { |
|
| 60 | 4 | if ($xrefTokenResolver->hasXref()) { |
|
| 61 | $xref = $xrefTokenResolver->getXref(); |
||
| 62 | if (!$xref->isResolved()) { |
||
| 63 | $treeCompiler = new TreeCompiler(); |
||
| 64 | $compiledValues = $treeCompiler->compileXref($xref); |
||
|
0 ignored issues
–
show
|
|||
| 65 | $xref->setData($compiledValues); |
||
| 66 | } |
||
| 67 | $values = $xref->getData(); |
||
| 68 | } else { |
||
| 69 | 4 | $values = $xrefTokenResolver->getRegisteredTokenValues(); |
|
| 70 | } |
||
| 71 | 4 | if ($tokenResolver instanceof RegisteredTokenResolver) { |
|
| 72 | 4 | $tokenResolver->setRegisteredTokenValues($values); |
|
| 73 | 4 | } |
|
| 74 | 4 | if ($tokenResolver instanceof ScopeTokenResolver) { |
|
| 75 | $tokenResolver->setScope($values); |
||
| 76 | } |
||
| 77 | 4 | } |
|
| 78 | 5 | $xrefTokenResolver->resolve($tokens); |
|
| 79 | 5 | } |
|
| 80 | |||
| 81 | 5 | protected function getTokensFromArray(&$array, TokenParser $parser, &$result, $level=0) |
|
| 82 | { |
||
| 83 | 5 | foreach ($array as $key => &$value) { |
|
| 84 | 5 | $t = array(); |
|
| 85 | 5 | View Code Duplication | if (gettype($key) == 'string') { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 86 | 5 | $tokens = $parser->parseString($key); |
|
| 87 | 5 | if (!$tokens->isEmpty()) { |
|
| 88 | 2 | $t[self::_KEY] = $key; |
|
| 89 | 2 | $t[self::_KEY_TOKENS] = $tokens; |
|
| 90 | 2 | $t[self::_KEY_REF] = &$array; |
|
| 91 | 2 | } |
|
| 92 | 5 | } |
|
| 93 | 5 | $valueType = gettype($value); |
|
| 94 | 5 | View Code Duplication | if ($valueType == 'string') { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 95 | 5 | $tokens = $parser->parseString($value); |
|
| 96 | 5 | if (!$tokens->isEmpty()) { |
|
| 97 | 5 | $t[self::_VALUE] = $value; |
|
| 98 | 5 | $t[self::_VALUE_TOKENS] = $tokens; |
|
| 99 | 5 | $t[self::_VALUE_REF] = &$value; |
|
| 100 | 5 | } |
|
| 101 | 5 | } |
|
| 102 | 5 | if (count($t) > 0) { |
|
| 103 | 5 | $t[self::_LEVEL] = $level; |
|
| 104 | 5 | $result[] = $t; |
|
| 105 | 5 | unset($t); |
|
| 106 | 5 | } |
|
| 107 | 5 | if ($valueType == 'array') { |
|
| 108 | 1 | $this->getTokensFromArray($value, $parser, $result, $level + 1); |
|
| 109 | 1 | } |
|
| 110 | 5 | } |
|
| 111 | 5 | unset($value); |
|
| 112 | 5 | if ($level == 0) { |
|
| 113 | 5 | usort($result, function($a, $b) { |
|
| 114 | 3 | return $b[XrefTokenResolverCollection::_LEVEL] - $a[XrefTokenResolverCollection::_LEVEL]; |
|
| 115 | 5 | }); |
|
| 116 | 5 | } |
|
| 117 | 5 | } |
|
| 118 | |||
| 119 | 3 | public function applyToString($string) |
|
| 120 | { |
||
| 121 | 3 | foreach ($this->collection as $xrefTokenResolver) { |
|
| 122 | 2 | $tokenParser = $xrefTokenResolver->getTokenParser(); |
|
| 123 | 2 | $tokens = $tokenParser->parseString($string); |
|
| 124 | 2 | $this->resolveTokens($tokens, $xrefTokenResolver); |
|
| 125 | 2 | $string = TokenInjector::injectString($string, $tokens); |
|
| 126 | 3 | } |
|
| 127 | 3 | return $string; |
|
| 128 | } |
||
| 129 | |||
| 130 | 5 | public function applyToArray(&$array) |
|
| 131 | { |
||
| 132 | 5 | foreach ($this->collection as $xrefTokenResolver) { |
|
| 133 | 5 | $tokenParser = $xrefTokenResolver->getTokenParser(); |
|
| 134 | 5 | $lookup = array(); |
|
| 135 | 5 | $this->getTokensFromArray($array, $tokenParser, $lookup); |
|
| 136 | 5 | foreach ($lookup as $record) { |
|
| 137 | 5 | if (isset($record[self::_VALUE_TOKENS])) { |
|
| 138 | 5 | $this->resolveTokens($record[self::_VALUE_TOKENS], $xrefTokenResolver); |
|
| 139 | 5 | } |
|
| 140 | 5 | if (isset($record[self::_KEY_TOKENS])) { |
|
| 141 | 2 | $this->resolveTokens($record[self::_KEY_TOKENS], $xrefTokenResolver); |
|
| 142 | 2 | } |
|
| 143 | 5 | } |
|
| 144 | 5 | foreach ($lookup as &$record) { |
|
| 145 | 5 | if (isset($record[self::_VALUE])) { |
|
| 146 | 5 | $newValue = TokenInjector::injectString( |
|
| 147 | 5 | $record[self::_VALUE], |
|
| 148 | 5 | $record[self::_VALUE_TOKENS] |
|
| 149 | 5 | ); |
|
| 150 | 5 | $record[self::_VALUE_REF] = $newValue; |
|
| 151 | 5 | unset($record[self::_VALUE_TOKENS]); |
|
| 152 | 5 | unset($record[self::_VALUE_REF]); |
|
| 153 | 5 | } |
|
| 154 | 5 | if (isset($record[self::_KEY])) { |
|
| 155 | 2 | $oldKey = $record[self::_KEY]; |
|
| 156 | 2 | $newKey = TokenInjector::injectString( |
|
| 157 | 2 | $oldKey, |
|
| 158 | 2 | $record[self::_KEY_TOKENS] |
|
| 159 | 2 | ); |
|
| 160 | 2 | unset($record[self::_KEY_TOKENS]); |
|
| 161 | 2 | if ($oldKey != $newKey) { |
|
| 162 | 2 | $record[self::_KEY_REF][$newKey] = $record[self::_KEY_REF][$oldKey]; |
|
| 163 | 2 | unset($record[self::_KEY_REF][$oldKey]); |
|
| 164 | 2 | unset($record[self::_KEY_REF]); |
|
| 165 | 2 | } |
|
| 166 | 2 | } |
|
| 167 | 5 | } |
|
| 168 | 5 | unset($record); |
|
| 169 | 5 | } |
|
| 170 | } |
||
| 171 | } |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: