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 namespace Mascame\Artificer\Fields; |
||
2 | |||
3 | use \Illuminate\Support\Str as Str; |
||
4 | |||
5 | class FieldParser |
||
6 | { |
||
7 | /** |
||
8 | * @var array |
||
9 | */ |
||
10 | protected $types = []; |
||
11 | |||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | public $typeReason = []; |
||
16 | |||
17 | public function __construct(array $types) |
||
18 | { |
||
19 | $this->types = $types; |
||
20 | } |
||
21 | |||
22 | /** |
||
23 | * @return array |
||
24 | */ |
||
25 | public function getTypes() |
||
26 | { |
||
27 | return $this->types; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * @param $field |
||
32 | * @return bool|int|mixed|string |
||
33 | */ |
||
34 | public function parse($field) |
||
35 | { |
||
36 | $type = $this->detectType($field); |
||
37 | |||
38 | if (isset($this->types[$type]['onParse'])) { |
||
39 | $this->types[$type]['onParse']($field, $type); |
||
40 | } |
||
41 | |||
42 | return $type; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param $name |
||
47 | * @param $types |
||
48 | * @return bool |
||
49 | */ |
||
50 | public function isTypeEqual($name, $types) |
||
51 | { |
||
52 | if (in_array(snake_case($name), array_keys($types)) |
||
53 | || in_array(strtolower($name), array_keys($types)) |
||
54 | ) { |
||
55 | $this->setTypeReason($name, 'equal'); |
||
56 | |||
57 | return true; |
||
58 | } |
||
59 | |||
60 | return false; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param $fields |
||
65 | * @param $name |
||
66 | * @param $type |
||
67 | * @return int |
||
68 | */ |
||
69 | public function getSimilarityPoints($fields, $name, $type) |
||
70 | { |
||
71 | $points = 0; |
||
72 | |||
73 | if ($this->isSimilar($name, $type)) { |
||
74 | // Gives more importance to similar TYPE than field |
||
75 | $points = +2; |
||
76 | } |
||
77 | |||
78 | foreach ($fields as $field) { |
||
79 | if ($this->isSimilar($name, $field)) { |
||
80 | $points++; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | return $points; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param $name |
||
89 | * @param $types |
||
90 | * @return bool|mixed |
||
91 | */ |
||
92 | public function isTypeSimilar($name, $types) |
||
93 | { |
||
94 | $points = array(); |
||
95 | |||
96 | View Code Duplication | foreach ($types as $type => $data) { |
|
0 ignored issues
–
show
|
|||
97 | if (!isset($data['autodetect'])) { |
||
98 | continue; |
||
99 | } |
||
100 | |||
101 | $points[$type] = $this->getSimilarityPoints($data['autodetect'], $name, $type); |
||
102 | } |
||
103 | |||
104 | if (max($points) > 0) { |
||
105 | $this->setTypeReason($name, 'similar to one in admin.fields'); |
||
106 | |||
107 | return array_search(max($points), $points); |
||
108 | } |
||
109 | |||
110 | return false; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param $haystack |
||
115 | * @param $needle |
||
116 | * @return bool |
||
117 | */ |
||
118 | public function isSimilar($haystack, $needle) |
||
119 | { |
||
120 | return Str::startsWith($haystack, $needle) |
||
121 | || Str::endsWith($haystack, $needle) |
||
122 | || Str::contains($haystack, $needle) ? true : false; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param $name |
||
127 | * @param $types |
||
128 | * @return bool|int|string |
||
129 | */ |
||
130 | public function isUserType($name, $types) |
||
131 | { |
||
132 | // if (!isset($types['autodetect']) || empty($types['autodetect'])) return false; |
||
133 | |||
134 | View Code Duplication | foreach ($types as $type => $data) { |
|
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. ![]() |
|||
135 | if (!isset($data['autodetect'])) { |
||
136 | continue; |
||
137 | } |
||
138 | |||
139 | if (in_array($name, $data['autodetect'])) { |
||
140 | $this->setTypeReason($name, 'set by user in admin.fields'); |
||
141 | |||
142 | return $type; |
||
143 | } |
||
144 | } |
||
145 | |||
146 | return false; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param $name |
||
151 | * @param $types |
||
152 | * @return bool|int|string |
||
153 | */ |
||
154 | public function matchesRegex($name, $types) |
||
155 | { |
||
156 | foreach ($types as $type => $data) { |
||
157 | if (!isset($data['regex'])) { |
||
158 | continue; |
||
159 | } |
||
160 | |||
161 | if (preg_match($data['regex'], $name, $matches)) { |
||
162 | $this->setTypeReason($name, "matched regex '{$data['regex']}'"); |
||
163 | |||
164 | return $type; |
||
165 | } |
||
166 | } |
||
167 | |||
168 | return false; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param $name |
||
173 | * @param $types |
||
174 | * @return bool|int|mixed|string |
||
175 | */ |
||
176 | public function detectType($name) |
||
177 | { |
||
178 | if ($type = $this->matchesRegex($name, $this->types)) { |
||
179 | return $type; |
||
180 | } |
||
181 | |||
182 | if ($this->isTypeEqual($name, $this->types)) { |
||
183 | return $name; |
||
184 | } |
||
185 | |||
186 | if ($type = $this->isUserType($name, $this->types)) { |
||
187 | return $type; |
||
188 | } |
||
189 | |||
190 | if ($type = $this->isTypeSimilar($name, $this->types)) { |
||
191 | return $type; |
||
192 | } |
||
193 | |||
194 | $this->setTypeReason($name, 'default'); |
||
195 | |||
196 | return $this->types['default']['type']; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @param $name |
||
201 | * @param $value |
||
202 | */ |
||
203 | protected function setTypeReason($name, $value) |
||
204 | { |
||
205 | $this->typeReason[$name] = $value; |
||
206 | } |
||
207 | |||
208 | } |
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.