1 | <?php |
||
8 | class SearchController extends JSONController |
||
9 | { |
||
10 | /** |
||
11 | * The maximum number of objects to allow being excluded |
||
12 | * |
||
13 | * @var int |
||
14 | */ |
||
15 | const MAX_EXCLUDED_OBJECTS = 10; |
||
16 | |||
17 | /** |
||
18 | * A list of types that can be returned when searching |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | private static $acceptableTypes = array('player', 'team'); |
||
23 | |||
24 | public function searchAction(Request $request) |
||
25 | { |
||
26 | if (!$request->query->has('types')) { |
||
27 | throw new \BadRequestException(); |
||
28 | } |
||
29 | |||
30 | $types = explode(',', $request->query->get('types')); |
||
31 | $excludeQuery = $request->query->get('exclude'); |
||
32 | |||
33 | $queries = $results = array(); |
||
34 | |||
35 | foreach ($types as &$type) { |
||
36 | if (!in_array($type, self::$acceptableTypes)) { |
||
37 | throw new \BadRequestException("An invalid type was provided"); |
||
38 | } |
||
39 | |||
40 | $type = ucfirst($type); |
||
41 | $queries[$type] = $type::getQueryBuilder(); |
||
42 | } |
||
43 | |||
44 | $excluded = $this->decompose($excludeQuery, $types, false, self::MAX_EXCLUDED_OBJECTS); |
||
45 | |||
46 | foreach ($queries as $type => $query) { |
||
47 | if ($startsWith = $request->query->get('startsWith')) { |
||
48 | $query->where('name')->startsWith($startsWith); |
||
49 | } |
||
50 | |||
51 | $query->active()->sortBy('name'); |
||
52 | |||
53 | foreach ($excluded[$type] as $exclude) { |
||
|
|||
54 | $query->except($exclude); |
||
55 | } |
||
56 | |||
57 | if ($type === 'Player') { |
||
58 | $result = $this->playerQuery($query, $request); |
||
59 | } else { |
||
60 | $result = $query->getArray(array('name')); |
||
61 | } |
||
62 | |||
63 | foreach ($result as $model) { |
||
64 | $model['type'] = $type; |
||
65 | $results[] = $model; |
||
66 | } |
||
67 | } |
||
68 | |||
69 | return new JsonResponse(array( |
||
70 | 'results' => $results |
||
71 | )); |
||
72 | } |
||
73 | |||
74 | private function playerQuery(\QueryBuilder $query, Request $request) |
||
82 | |||
83 | public function playerByBzidAction(Player $me, Request $request, FlashBag $flashBag, $bzid = null) |
||
107 | } |
||
108 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.