Conditions | 21 |
Paths | 6913 |
Total Lines | 111 |
Code Lines | 57 |
Lines | 0 |
Ratio | 0 % |
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 declare(strict_types=1); |
||
36 | public function search(EntityDefinition $definition, Criteria $criteria, Context $context): IdSearchResult |
||
37 | { |
||
38 | if ($criteria->getLimit() === 0) { |
||
39 | return new IdSearchResult(0, [], $criteria, $context); |
||
40 | } |
||
41 | |||
42 | $table = $definition->getEntityName(); |
||
43 | |||
44 | $query = new QueryBuilder($this->connection); |
||
45 | |||
46 | $fields = []; |
||
47 | foreach ($definition->getFields() as $field) { |
||
48 | if (!$field instanceof StorageAware || $field instanceof ReferenceVersionField || $field instanceof VersionField) { |
||
49 | continue; |
||
50 | } |
||
51 | if ($field instanceof NumberRangeField) { |
||
52 | $fields[$field->getStorageName()] = $field; |
||
53 | |||
54 | continue; |
||
55 | } |
||
56 | if ($field instanceof AutoIncrementField) { |
||
57 | $fields[$field->getStorageName()] = $field; |
||
58 | |||
59 | continue; |
||
60 | } |
||
61 | if ($field->is(PrimaryKey::class)) { |
||
62 | $fields[$field->getStorageName()] = $field; |
||
63 | } |
||
64 | } |
||
65 | |||
66 | /** @var StorageAware $field */ |
||
67 | foreach ($fields as $field) { |
||
68 | $query->addSelect( |
||
69 | EntityDefinitionQueryHelper::escape($table) . '.' . EntityDefinitionQueryHelper::escape($field->getStorageName()) |
||
70 | ); |
||
71 | } |
||
72 | |||
73 | $query = $this->criteriaQueryBuilder->build($query, $definition, $criteria, $context); |
||
74 | |||
75 | if (!empty($criteria->getIds())) { |
||
76 | $this->queryHelper->addIdCondition($criteria, $definition, $query); |
||
77 | } |
||
78 | |||
79 | $this->addGroupBy($definition, $criteria, $context, $query, $table); |
||
80 | |||
81 | // add pagination |
||
82 | if ($criteria->getOffset() !== null) { |
||
83 | $query->setFirstResult($criteria->getOffset()); |
||
84 | } |
||
85 | if ($criteria->getLimit() !== null) { |
||
86 | $query->setMaxResults($criteria->getLimit()); |
||
87 | } |
||
88 | |||
89 | $this->addTotalCountMode($criteria, $query); |
||
90 | |||
91 | if ($criteria->getTitle()) { |
||
92 | $query->setTitle($criteria->getTitle() . '::search-ids'); |
||
93 | } |
||
94 | |||
95 | // execute and fetch ids |
||
96 | $rows = $query->executeQuery()->fetchAllAssociative(); |
||
97 | |||
98 | $total = $this->getTotalCount($criteria, $query, $rows); |
||
99 | |||
100 | if ($criteria->getTotalCountMode() === Criteria::TOTAL_COUNT_MODE_NEXT_PAGES) { |
||
101 | $rows = \array_slice($rows, 0, $criteria->getLimit()); |
||
102 | } |
||
103 | |||
104 | $converted = []; |
||
105 | |||
106 | foreach ($rows as $row) { |
||
107 | $pk = []; |
||
108 | $data = []; |
||
109 | |||
110 | foreach ($row as $storageName => $value) { |
||
111 | $field = $fields[$storageName] ?? null; |
||
112 | |||
113 | if (!$field) { |
||
114 | $data[$storageName] = $value; |
||
115 | |||
116 | continue; |
||
117 | } |
||
118 | |||
119 | $value = $field->getSerializer()->decode($field, $value); |
||
120 | |||
121 | $data[$field->getPropertyName()] = $value; |
||
122 | |||
123 | if (!$field->is(PrimaryKey::class)) { |
||
124 | continue; |
||
125 | } |
||
126 | |||
127 | $pk[$field->getPropertyName()] = $value; |
||
128 | } |
||
129 | |||
130 | $arrayKey = implode('-', $pk); |
||
131 | |||
132 | if (\count($pk) === 1) { |
||
133 | $pk = array_shift($pk); |
||
134 | } |
||
135 | |||
136 | $converted[$arrayKey] = [ |
||
137 | 'primaryKey' => $pk, |
||
138 | 'data' => $data, |
||
139 | ]; |
||
140 | } |
||
141 | |||
142 | if ($criteria->useIdSorting()) { |
||
143 | $converted = $this->sortByIdArray($criteria->getIds(), $converted); |
||
144 | } |
||
145 | |||
146 | return new IdSearchResult($total, $converted, $criteria, $context); |
||
147 | } |
||
212 |