| Conditions | 35 |
| Paths | > 20000 |
| Total Lines | 233 |
| Code Lines | 166 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 51 | public function testJoinWithAlias(string $aliasMethod): void |
||
| 52 | { |
||
| 53 | $orders = []; |
||
| 54 | $this->checkFixture($this->db, 'order'); |
||
| 55 | |||
| 56 | /** left join and eager loading */ |
||
| 57 | $orderQuery = new ActiveQuery(Order::class, $this->db); |
||
| 58 | $query = $orderQuery->joinWith(['customer c']); |
||
| 59 | |||
| 60 | if ($aliasMethod === 'explicit') { |
||
| 61 | $orders = $query->orderBy('c.id DESC, order.id')->all(); |
||
| 62 | } elseif ($aliasMethod === 'querysyntax') { |
||
| 63 | $orders = $query->orderBy('{{@customer}}.id DESC, {{@order}}.id')->all(); |
||
| 64 | } elseif ($aliasMethod === 'applyAlias') { |
||
| 65 | $orders = $query->orderBy( |
||
| 66 | $query->applyAlias('customer', 'id') . ' DESC,' . $query->applyAlias('order', 'id') |
||
|
|
|||
| 67 | )->all(); |
||
| 68 | } |
||
| 69 | |||
| 70 | $this->assertCount(3, $orders); |
||
| 71 | $this->assertEquals(2, $orders[0]->id); |
||
| 72 | $this->assertEquals(3, $orders[1]->id); |
||
| 73 | $this->assertEquals(1, $orders[2]->id); |
||
| 74 | $this->assertTrue($orders[0]->isRelationPopulated('customer')); |
||
| 75 | $this->assertTrue($orders[1]->isRelationPopulated('customer')); |
||
| 76 | $this->assertTrue($orders[2]->isRelationPopulated('customer')); |
||
| 77 | |||
| 78 | /** inner join filtering and eager loading */ |
||
| 79 | $orderQuery = new ActiveQuery(Order::class, $this->db); |
||
| 80 | $query = $orderQuery->innerJoinWith(['customer c']); |
||
| 81 | |||
| 82 | if ($aliasMethod === 'explicit') { |
||
| 83 | $orders = $query->where('{{c}}.[[id]]=2')->orderBy('order.id')->all(); |
||
| 84 | } elseif ($aliasMethod === 'querysyntax') { |
||
| 85 | $orders = $query->where('{{@customer}}.[[id]]=2')->orderBy('{{@order}}.id')->all(); |
||
| 86 | } elseif ($aliasMethod === 'applyAlias') { |
||
| 87 | $orders = $query->where( |
||
| 88 | [$query->applyAlias('customer', 'id') => 2] |
||
| 89 | )->orderBy($query->applyAlias('order', 'id'))->all(); |
||
| 90 | } |
||
| 91 | |||
| 92 | $this->assertCount(2, $orders); |
||
| 93 | $this->assertEquals(2, $orders[0]->id); |
||
| 94 | $this->assertEquals(3, $orders[1]->id); |
||
| 95 | $this->assertTrue($orders[0]->isRelationPopulated('customer')); |
||
| 96 | $this->assertTrue($orders[1]->isRelationPopulated('customer')); |
||
| 97 | |||
| 98 | /** inner join filtering without eager loading */ |
||
| 99 | $orderQuery = new ActiveQuery(Order::class, $this->db); |
||
| 100 | $query = $orderQuery->innerJoinWith(['customer c'], false); |
||
| 101 | |||
| 102 | if ($aliasMethod === 'explicit') { |
||
| 103 | $orders = $query->where('{{c}}.[[id]]=2')->orderBy('order.id')->all(); |
||
| 104 | } elseif ($aliasMethod === 'querysyntax') { |
||
| 105 | $orders = $query->where('{{@customer}}.[[id]]=2')->orderBy('{{@order}}.id')->all(); |
||
| 106 | } elseif ($aliasMethod === 'applyAlias') { |
||
| 107 | $orders = $query->where( |
||
| 108 | [$query->applyAlias('customer', 'id') => 2] |
||
| 109 | )->orderBy($query->applyAlias('order', 'id'))->all(); |
||
| 110 | } |
||
| 111 | |||
| 112 | $this->assertCount(2, $orders); |
||
| 113 | $this->assertEquals(2, $orders[0]->id); |
||
| 114 | $this->assertEquals(3, $orders[1]->id); |
||
| 115 | $this->assertFalse($orders[0]->isRelationPopulated('customer')); |
||
| 116 | $this->assertFalse($orders[1]->isRelationPopulated('customer')); |
||
| 117 | |||
| 118 | /** join with via-relation */ |
||
| 119 | $orderQuery = new ActiveQuery(Order::class, $this->db); |
||
| 120 | $query = $orderQuery->innerJoinWith(['books b']); |
||
| 121 | |||
| 122 | if ($aliasMethod === 'explicit') { |
||
| 123 | $orders = $query->where( |
||
| 124 | ['b.name' => 'Yii 1.1 Application Development Cookbook'] |
||
| 125 | )->orderBy('order.id')->all(); |
||
| 126 | } elseif ($aliasMethod === 'querysyntax') { |
||
| 127 | $orders = $query->where( |
||
| 128 | ['{{@item}}.name' => 'Yii 1.1 Application Development Cookbook'] |
||
| 129 | )->orderBy('{{@order}}.id')->all(); |
||
| 130 | } elseif ($aliasMethod === 'applyAlias') { |
||
| 131 | $orders = $query->where( |
||
| 132 | [$query->applyAlias('book', 'name') => 'Yii 1.1 Application Development Cookbook'] |
||
| 133 | )->orderBy($query->applyAlias('order', 'id'))->all(); |
||
| 134 | } |
||
| 135 | |||
| 136 | $this->assertCount(2, $orders); |
||
| 137 | $this->assertCount(2, $orders[0]->books); |
||
| 138 | $this->assertCount(1, $orders[1]->books); |
||
| 139 | $this->assertEquals(1, $orders[0]->id); |
||
| 140 | $this->assertEquals(3, $orders[1]->id); |
||
| 141 | $this->assertTrue($orders[0]->isRelationPopulated('books')); |
||
| 142 | $this->assertTrue($orders[1]->isRelationPopulated('books')); |
||
| 143 | |||
| 144 | /** joining sub relations */ |
||
| 145 | $orderQuery = new ActiveQuery(Order::class, $this->db); |
||
| 146 | $query = $orderQuery->innerJoinWith( |
||
| 147 | [ |
||
| 148 | 'items i' => static function ($q) use ($aliasMethod) { |
||
| 149 | /** @var $q ActiveQuery */ |
||
| 150 | if ($aliasMethod === 'explicit') { |
||
| 151 | $q->orderBy('{{i}}.id'); |
||
| 152 | } elseif ($aliasMethod === 'querysyntax') { |
||
| 153 | $q->orderBy('{{@item}}.id'); |
||
| 154 | } elseif ($aliasMethod === 'applyAlias') { |
||
| 155 | $q->orderBy($q->applyAlias('item', 'id')); |
||
| 156 | } |
||
| 157 | }, |
||
| 158 | 'items.category c' => static function ($q) use ($aliasMethod) { |
||
| 159 | /** @var $q ActiveQuery */ |
||
| 160 | if ($aliasMethod === 'explicit') { |
||
| 161 | $q->where('{{c}}.[[id]] = 2'); |
||
| 162 | } elseif ($aliasMethod === 'querysyntax') { |
||
| 163 | $q->where('{{@category}}.[[id]] = 2'); |
||
| 164 | } elseif ($aliasMethod === 'applyAlias') { |
||
| 165 | $q->where([$q->applyAlias('category', 'id') => 2]); |
||
| 166 | } |
||
| 167 | }, |
||
| 168 | ] |
||
| 169 | ); |
||
| 170 | |||
| 171 | if ($aliasMethod === 'explicit') { |
||
| 172 | $orders = $query->orderBy('{{i}}.id')->all(); |
||
| 173 | } elseif ($aliasMethod === 'querysyntax') { |
||
| 174 | $orders = $query->orderBy('{{@item}}.id')->all(); |
||
| 175 | } elseif ($aliasMethod === 'applyAlias') { |
||
| 176 | $orders = $query->orderBy($query->applyAlias('item', 'id'))->all(); |
||
| 177 | } |
||
| 178 | |||
| 179 | $this->assertCount(1, $orders); |
||
| 180 | $this->assertCount(3, $orders[0]->items); |
||
| 181 | $this->assertEquals(2, $orders[0]->id); |
||
| 182 | $this->assertEquals(2, $orders[0]->items[0]->category->id); |
||
| 183 | $this->assertTrue($orders[0]->isRelationPopulated('items')); |
||
| 184 | $this->assertTrue($orders[0]->items[0]->isRelationPopulated('category')); |
||
| 185 | |||
| 186 | /** join with ON condition */ |
||
| 187 | if ($aliasMethod === 'explicit' || $aliasMethod === 'querysyntax') { |
||
| 188 | $relationName = 'books' . ucfirst($aliasMethod); |
||
| 189 | |||
| 190 | $orderQuery = new ActiveQuery(Order::class, $this->db); |
||
| 191 | $orders = $orderQuery->joinWith(["$relationName b"])->orderBy('order.id')->all(); |
||
| 192 | |||
| 193 | $this->assertCount(3, $orders); |
||
| 194 | $this->assertCount(2, $orders[0]->$relationName); |
||
| 195 | $this->assertCount(0, $orders[1]->$relationName); |
||
| 196 | $this->assertCount(1, $orders[2]->$relationName); |
||
| 197 | $this->assertEquals(1, $orders[0]->id); |
||
| 198 | $this->assertEquals(2, $orders[1]->id); |
||
| 199 | $this->assertEquals(3, $orders[2]->id); |
||
| 200 | $this->assertTrue($orders[0]->isRelationPopulated($relationName)); |
||
| 201 | $this->assertTrue($orders[1]->isRelationPopulated($relationName)); |
||
| 202 | $this->assertTrue($orders[2]->isRelationPopulated($relationName)); |
||
| 203 | } |
||
| 204 | |||
| 205 | /** join with ON condition and alias in relation definition */ |
||
| 206 | if ($aliasMethod === 'explicit' || $aliasMethod === 'querysyntax') { |
||
| 207 | $relationName = 'books' . ucfirst($aliasMethod) . 'A'; |
||
| 208 | |||
| 209 | $orderQuery = new ActiveQuery(Order::class, $this->db); |
||
| 210 | $orders = $orderQuery->joinWith([(string)$relationName])->orderBy('order.id')->all(); |
||
| 211 | |||
| 212 | $this->assertCount(3, $orders); |
||
| 213 | $this->assertCount(2, $orders[0]->$relationName); |
||
| 214 | $this->assertCount(0, $orders[1]->$relationName); |
||
| 215 | $this->assertCount(1, $orders[2]->$relationName); |
||
| 216 | $this->assertEquals(1, $orders[0]->id); |
||
| 217 | $this->assertEquals(2, $orders[1]->id); |
||
| 218 | $this->assertEquals(3, $orders[2]->id); |
||
| 219 | $this->assertTrue($orders[0]->isRelationPopulated($relationName)); |
||
| 220 | $this->assertTrue($orders[1]->isRelationPopulated($relationName)); |
||
| 221 | $this->assertTrue($orders[2]->isRelationPopulated($relationName)); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** join with count and query */ |
||
| 225 | $orderQuery = new ActiveQuery(Order::class, $this->db); |
||
| 226 | $query = $orderQuery->joinWith(['customer c']); |
||
| 227 | |||
| 228 | if ($aliasMethod === 'explicit') { |
||
| 229 | $count = $query->count('{{c}}.[[id]]'); |
||
| 230 | } elseif ($aliasMethod === 'querysyntax') { |
||
| 231 | $count = $query->count('{{@customer}}.id'); |
||
| 232 | } elseif ($aliasMethod === 'applyAlias') { |
||
| 233 | $count = $query->count($query->applyAlias('customer', 'id')); |
||
| 234 | } |
||
| 235 | |||
| 236 | $this->assertEquals(3, $count); |
||
| 237 | |||
| 238 | $orders = $query->all(); |
||
| 239 | $this->assertCount(3, $orders); |
||
| 240 | |||
| 241 | /** relational query */ |
||
| 242 | $orderQuery = new ActiveQuery(Order::class, $this->db); |
||
| 243 | $order = $orderQuery->findOne(1); |
||
| 244 | |||
| 245 | $customerQuery = $order->getCustomer()->innerJoinWith(['orders o'], false); |
||
| 246 | |||
| 247 | if ($aliasMethod === 'explicit') { |
||
| 248 | $customer = $customerQuery->where(['{{o}}.[[id]]' => 1])->one(); |
||
| 249 | } elseif ($aliasMethod === 'querysyntax') { |
||
| 250 | $customer = $customerQuery->where(['{{@order}}.id' => 1])->one(); |
||
| 251 | } elseif ($aliasMethod === 'applyAlias') { |
||
| 252 | $customer = $customerQuery->where([$query->applyAlias('order', 'id') => 1])->one(); |
||
| 253 | } |
||
| 254 | |||
| 255 | $this->assertEquals(1, $customer->id); |
||
| 256 | $this->assertNotNull($customer); |
||
| 257 | |||
| 258 | /** join with sub-relation called inside Closure */ |
||
| 259 | $orderQuery = new ActiveQuery(Order::class, $this->db); |
||
| 260 | $orders = $orderQuery->joinWith( |
||
| 261 | [ |
||
| 262 | 'items' => static function ($q) use ($aliasMethod) { |
||
| 263 | /** @var $q ActiveQuery */ |
||
| 264 | $q->orderBy('item.id'); |
||
| 265 | $q->joinWith(['category c']); |
||
| 266 | |||
| 267 | if ($aliasMethod === 'explicit') { |
||
| 268 | $q->where('{{c}}.[[id]] = 2'); |
||
| 269 | } elseif ($aliasMethod === 'querysyntax') { |
||
| 270 | $q->where('{{@category}}.[[id]] = 2'); |
||
| 271 | } elseif ($aliasMethod === 'applyAlias') { |
||
| 272 | $q->where([$q->applyAlias('category', 'id') => 2]); |
||
| 273 | } |
||
| 274 | }, |
||
| 275 | ] |
||
| 276 | )->orderBy('order.id')->all(); |
||
| 277 | |||
| 278 | $this->assertCount(1, $orders); |
||
| 279 | $this->assertCount(3, $orders[0]->items); |
||
| 280 | $this->assertEquals(2, $orders[0]->id); |
||
| 281 | $this->assertEquals(2, $orders[0]->items[0]->category->id); |
||
| 282 | $this->assertTrue($orders[0]->isRelationPopulated('items')); |
||
| 283 | $this->assertTrue($orders[0]->items[0]->isRelationPopulated('category')); |
||
| 284 | } |
||
| 425 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.