| Conditions | 1 |
| Paths | 1 |
| Total Lines | 64 |
| Code Lines | 40 |
| 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 |
||
| 32 | public function testSelectJoin() |
||
| 33 | { |
||
| 34 | /** @var ManyManyThroughListTest\TestObject $parent */ |
||
| 35 | $parent = $this->objFromFixture(ManyManyThroughListTest\TestObject::class, 'parent1'); |
||
| 36 | $this->assertDOSEquals( |
||
| 37 | [ |
||
| 38 | ['Title' => 'item 1'], |
||
| 39 | ['Title' => 'item 2'] |
||
| 40 | ], |
||
| 41 | $parent->Items() |
||
| 42 | ); |
||
| 43 | // Check filters on list work |
||
| 44 | $item1 = $parent->Items()->filter('Title', 'item 1')->first(); |
||
| 45 | $this->assertNotNull($item1); |
||
| 46 | $this->assertNotNull($item1->getJoin()); |
||
| 47 | $this->assertEquals('join 1', $item1->getJoin()->Title); |
||
| 48 | $this->assertInstanceOf( |
||
| 49 | ManyManyThroughListTest\JoinObject::class, |
||
| 50 | $item1->ManyManyThroughListTest_JoinObject |
||
| 51 | ); |
||
| 52 | $this->assertEquals('join 1', $item1->ManyManyThroughListTest_JoinObject->Title); |
||
| 53 | |||
| 54 | // Check filters on list work |
||
| 55 | $item2 = $parent->Items()->filter('Title', 'item 2')->first(); |
||
| 56 | $this->assertNotNull($item2); |
||
| 57 | $this->assertNotNull($item2->getJoin()); |
||
| 58 | $this->assertEquals('join 2', $item2->getJoin()->Title); |
||
| 59 | $this->assertEquals('join 2', $item2->ManyManyThroughListTest_JoinObject->Title); |
||
| 60 | |||
| 61 | // To filter on join table need to use some raw sql |
||
| 62 | $item2 = $parent->Items()->where(['"ManyManyThroughListTest_JoinObject"."Title"' => 'join 2'])->first(); |
||
| 63 | $this->assertNotNull($item2); |
||
| 64 | $this->assertEquals('item 2', $item2->Title); |
||
| 65 | $this->assertNotNull($item2->getJoin()); |
||
| 66 | $this->assertEquals('join 2', $item2->getJoin()->Title); |
||
| 67 | $this->assertEquals('join 2', $item2->ManyManyThroughListTest_JoinObject->Title); |
||
| 68 | |||
| 69 | // Test sorting on join table |
||
| 70 | $items = $parent->Items()->sort('"ManyManyThroughListTest_JoinObject"."Sort"'); |
||
| 71 | $this->assertDOSEquals( |
||
| 72 | [ |
||
| 73 | ['Title' => 'item 2'], |
||
| 74 | ['Title' => 'item 1'], |
||
| 75 | ], |
||
| 76 | $items |
||
| 77 | ); |
||
| 78 | |||
| 79 | $items = $parent->Items()->sort('"ManyManyThroughListTest_JoinObject"."Sort" ASC'); |
||
| 80 | $this->assertDOSEquals( |
||
| 81 | [ |
||
| 82 | ['Title' => 'item 1'], |
||
| 83 | ['Title' => 'item 2'], |
||
| 84 | ], |
||
| 85 | $items |
||
| 86 | ); |
||
| 87 | $items = $parent->Items()->sort('"ManyManyThroughListTest_JoinObject"."Title" DESC'); |
||
| 88 | $this->assertDOSEquals( |
||
| 89 | [ |
||
| 90 | ['Title' => 'item 2'], |
||
| 91 | ['Title' => 'item 1'], |
||
| 92 | ], |
||
| 93 | $items |
||
| 94 | ); |
||
| 95 | } |
||
| 96 | |||
| 187 |
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: